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::soft_max_capacity() const {
 54   return ShenandoahHeap::heap()->soft_max_capacity();
 55 }
 56 
 57 size_t ShenandoahGlobalGeneration::available() const {
 58   // The collector reserve may eat into what the mutator is allowed to use. Make sure we are looking
 59   // at what is available to the mutator when reporting how much memory is available.
 60   size_t available = this->ShenandoahGeneration::available();
 61   return MIN2(available, ShenandoahHeap::heap()->free_set()->available());
 62 }
 63 
 64 size_t ShenandoahGlobalGeneration::soft_available() const {
 65   size_t available = this->available();
 66 
 67   // Make sure the code below treats available without the soft tail.
 68   assert(max_capacity() >= soft_max_capacity(), "Max capacity must be greater than soft max capacity.");
 69   size_t soft_tail = max_capacity() - soft_max_capacity();
 70   return (available > soft_tail) ? (available - soft_tail) : 0;
 71 }
 72 
 73 void ShenandoahGlobalGeneration::set_concurrent_mark_in_progress(bool in_progress) {
 74   ShenandoahHeap* heap = ShenandoahHeap::heap();
 75   if (in_progress && heap->mode()->is_generational()) {
 76     // Global collection has preempted an old generation mark. This is fine
 77     // because the global generation includes the old generation, but we
 78     // want the global collect to start from a clean slate and we don't want
 79     // any stale state in the old generation.
 80     assert(!heap->is_concurrent_old_mark_in_progress(), "Old cycle should not be running.");
 81   }
 82 
 83   heap->set_concurrent_young_mark_in_progress(in_progress);
 84 }
 85 
 86 bool ShenandoahGlobalGeneration::contains(ShenandoahAffiliation affiliation) const {
 87   return true;
 88 }
 89 
 90 bool ShenandoahGlobalGeneration::contains(ShenandoahHeapRegion* region) const {
 91   return true;
 92 }
 93 
 94 void ShenandoahGlobalGeneration::parallel_heap_region_iterate(ShenandoahHeapRegionClosure* cl) {
 95   ShenandoahHeap::heap()->parallel_heap_region_iterate(cl);
 96 }
 97 
 98 void ShenandoahGlobalGeneration::heap_region_iterate(ShenandoahHeapRegionClosure* cl) {
 99   ShenandoahHeap::heap()->heap_region_iterate(cl);
100 }
101 
102 bool ShenandoahGlobalGeneration::is_concurrent_mark_in_progress() {
103   ShenandoahHeap* heap = ShenandoahHeap::heap();
104   return heap->is_concurrent_mark_in_progress();
105 }
106 
107 ShenandoahHeuristics* ShenandoahGlobalGeneration::initialize_heuristics(ShenandoahMode* gc_mode) {
108   if (gc_mode->is_generational()) {
109     _heuristics = new ShenandoahGlobalHeuristics(this);
110   } else {
111     _heuristics = gc_mode->initialize_heuristics(this);
112   }
113 
114   _heuristics->set_guaranteed_gc_interval(ShenandoahGuaranteedGCInterval);
115   confirm_heuristics_mode();
116   return _heuristics;
117 }
118 
119 void ShenandoahGlobalGeneration::set_mark_complete() {
120   ShenandoahGeneration::set_mark_complete();
121   if (ShenandoahHeap::heap()->mode()->is_generational()) {
122     ShenandoahGenerationalHeap* heap = ShenandoahGenerationalHeap::heap();
123     heap->young_generation()->set_mark_complete();
124     heap->old_generation()->set_mark_complete();
125   }
126 }
127 
128 void ShenandoahGlobalGeneration::set_mark_incomplete() {
129   ShenandoahGeneration::set_mark_incomplete();
130   if (ShenandoahHeap::heap()->mode()->is_generational()) {
131     ShenandoahGenerationalHeap* heap = ShenandoahGenerationalHeap::heap();
132     heap->young_generation()->set_mark_incomplete();
133     heap->old_generation()->set_mark_incomplete();
134   }
135 }
136 
137 void ShenandoahGlobalGeneration::prepare_gc() {
138   ShenandoahGeneration::prepare_gc();
139 
140   if (ShenandoahHeap::heap()->mode()->is_generational()) {
141     assert(type() == GLOBAL, "Unexpected generation type");
142     // Clear any stale/partial local census data before the start of a
143     // new marking cycle
144     ShenandoahGenerationalHeap::heap()->age_census()->reset_local();
145   } else {
146     assert(type() == NON_GEN, "Unexpected generation type");
147   }
148 }