1 /*
  2  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "gc/shenandoah/heuristics/shenandoahGlobalHeuristics.hpp"
 26 #include "gc/shenandoah/shenandoahAgeCensus.hpp"
 27 #include "gc/shenandoah/shenandoahFreeSet.hpp"
 28 #include "gc/shenandoah/shenandoahGlobalGeneration.hpp"
 29 #include "gc/shenandoah/shenandoahHeap.hpp"
 30 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 31 #include "gc/shenandoah/shenandoahUtils.hpp"
 32 #include "gc/shenandoah/shenandoahVerifier.hpp"
 33 
 34 
 35 const char* ShenandoahGlobalGeneration::name() const {
 36   return type() == NON_GEN ? "" : "Global";
 37 }
 38 
 39 size_t ShenandoahGlobalGeneration::max_capacity() const {
 40   return ShenandoahHeap::heap()->max_capacity();
 41 }
 42 
 43 size_t ShenandoahGlobalGeneration::used_regions() const {
 44   ShenandoahGenerationalHeap* heap = ShenandoahGenerationalHeap::heap();
 45   assert(heap->mode()->is_generational(), "Region usage accounting is only for generational mode");
 46   return heap->old_generation()->used_regions() + heap->young_generation()->used_regions();
 47 }
 48 
 49 size_t ShenandoahGlobalGeneration::used_regions_size() const {
 50   return ShenandoahHeap::heap()->capacity();
 51 }
 52 
 53 size_t ShenandoahGlobalGeneration::available() const {
 54   // The collector reserve may eat into what the mutator is allowed to use. Make sure we are looking
 55   // at what is available to the mutator when reporting how much memory is available.
 56   size_t available = this->ShenandoahGeneration::available();
 57   return MIN2(available, ShenandoahHeap::heap()->free_set()->available());
 58 }
 59 
 60 size_t ShenandoahGlobalGeneration::soft_available() const {
 61   size_t available = this->available();
 62 
 63   // Make sure the code below treats available without the soft tail.
 64   assert(max_capacity() >= ShenandoahHeap::heap()->soft_max_capacity(), "Max capacity must be greater than soft max capacity.");
 65   size_t soft_tail = max_capacity() - ShenandoahHeap::heap()->soft_max_capacity();
 66   return (available > soft_tail) ? (available - soft_tail) : 0;
 67 }
 68 
 69 void ShenandoahGlobalGeneration::set_concurrent_mark_in_progress(bool in_progress) {
 70   ShenandoahHeap* heap = ShenandoahHeap::heap();
 71   if (in_progress && heap->mode()->is_generational()) {
 72     // Global collection has preempted an old generation mark. This is fine
 73     // because the global generation includes the old generation, but we
 74     // want the global collect to start from a clean slate and we don't want
 75     // any stale state in the old generation.
 76     assert(!heap->is_concurrent_old_mark_in_progress(), "Old cycle should not be running.");
 77   }
 78 
 79   heap->set_concurrent_young_mark_in_progress(in_progress);
 80 }
 81 
 82 bool ShenandoahGlobalGeneration::contains(ShenandoahAffiliation affiliation) const {
 83   return true;
 84 }
 85 
 86 bool ShenandoahGlobalGeneration::contains(ShenandoahHeapRegion* region) const {
 87   return true;
 88 }
 89 
 90 void ShenandoahGlobalGeneration::parallel_heap_region_iterate(ShenandoahHeapRegionClosure* cl) {
 91   ShenandoahHeap::heap()->parallel_heap_region_iterate(cl);
 92 }
 93 
 94 void ShenandoahGlobalGeneration::heap_region_iterate(ShenandoahHeapRegionClosure* cl) {
 95   ShenandoahHeap::heap()->heap_region_iterate(cl);
 96 }
 97 
 98 bool ShenandoahGlobalGeneration::is_concurrent_mark_in_progress() {
 99   ShenandoahHeap* heap = ShenandoahHeap::heap();
100   return heap->is_concurrent_mark_in_progress();
101 }
102 
103 ShenandoahHeuristics* ShenandoahGlobalGeneration::initialize_heuristics(ShenandoahMode* gc_mode) {
104   if (gc_mode->is_generational()) {
105     _heuristics = new ShenandoahGlobalHeuristics(this);
106   } else {
107     _heuristics = gc_mode->initialize_heuristics(this);
108   }
109 
110   _heuristics->set_guaranteed_gc_interval(ShenandoahGuaranteedGCInterval);
111   confirm_heuristics_mode();
112   return _heuristics;
113 }
114 
115 void ShenandoahGlobalGeneration::set_mark_complete() {
116   ShenandoahGeneration::set_mark_complete();
117   if (ShenandoahHeap::heap()->mode()->is_generational()) {
118     ShenandoahGenerationalHeap* heap = ShenandoahGenerationalHeap::heap();
119     heap->young_generation()->set_mark_complete();
120     heap->old_generation()->set_mark_complete();
121   }
122 }
123 
124 void ShenandoahGlobalGeneration::set_mark_incomplete() {
125   ShenandoahGeneration::set_mark_incomplete();
126   if (ShenandoahHeap::heap()->mode()->is_generational()) {
127     ShenandoahGenerationalHeap* heap = ShenandoahGenerationalHeap::heap();
128     heap->young_generation()->set_mark_incomplete();
129     heap->old_generation()->set_mark_incomplete();
130   }
131 }
132 
133 void ShenandoahGlobalGeneration::prepare_gc() {
134   ShenandoahGeneration::prepare_gc();
135 
136   if (ShenandoahHeap::heap()->mode()->is_generational()) {
137     assert(type() == GLOBAL, "Unexpected generation type");
138     // Clear any stale/partial local census data before the start of a
139     // new marking cycle
140     ShenandoahGenerationalHeap::heap()->age_census()->reset_local();
141   } else {
142     assert(type() == NON_GEN, "Unexpected generation type");
143   }
144 }