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 #include "precompiled.hpp"
 25 
 26 #include "gc/shenandoah/shenandoahAgeCensus.hpp"
 27 #include "gc/shenandoah/heuristics/shenandoahGlobalHeuristics.hpp"
 28 #include "gc/shenandoah/shenandoahFreeSet.hpp"
 29 #include "gc/shenandoah/shenandoahGlobalGeneration.hpp"
 30 #include "gc/shenandoah/shenandoahHeap.hpp"
 31 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 32 #include "gc/shenandoah/shenandoahUtils.hpp"
 33 #include "gc/shenandoah/shenandoahVerifier.hpp"
 34 
 35 
 36 const char* ShenandoahGlobalGeneration::name() const {
 37   return type() == NON_GEN ? "" : "Global";
 38 }
 39 
 40 size_t ShenandoahGlobalGeneration::max_capacity() const {
 41   return ShenandoahHeap::heap()->max_capacity();
 42 }
 43 
 44 size_t ShenandoahGlobalGeneration::used_regions() const {
 45   ShenandoahGenerationalHeap* heap = ShenandoahGenerationalHeap::heap();
 46   assert(heap->mode()->is_generational(), "Region usage accounting is only for generational mode");
 47   return heap->old_generation()->used_regions() + heap->young_generation()->used_regions();
 48 }
 49 
 50 size_t ShenandoahGlobalGeneration::used_regions_size() const {
 51   return ShenandoahHeap::heap()->capacity();
 52 }
 53 
 54 size_t ShenandoahGlobalGeneration::soft_max_capacity() const {
 55   return ShenandoahHeap::heap()->soft_max_capacity();
 56 }
 57 
 58 size_t ShenandoahGlobalGeneration::available() const {
 59   return ShenandoahHeap::heap()->free_set()->available();
 60 }
 61 
 62 size_t ShenandoahGlobalGeneration::soft_available() const {
 63   size_t available = this->available();
 64 
 65   // Make sure the code below treats available without the soft tail.
 66   assert(max_capacity() >= soft_max_capacity(), "Max capacity must be greater than soft max capacity.");
 67   size_t soft_tail = max_capacity() - soft_max_capacity();
 68   return (available > soft_tail) ? (available - soft_tail) : 0;
 69 }
 70 
 71 void ShenandoahGlobalGeneration::set_concurrent_mark_in_progress(bool in_progress) {
 72   ShenandoahHeap* heap = ShenandoahHeap::heap();
 73   if (in_progress && heap->mode()->is_generational()) {
 74     // Global collection has preempted an old generation mark. This is fine
 75     // because the global generation includes the old generation, but we
 76     // want the global collect to start from a clean slate and we don't want
 77     // any stale state in the old generation.
 78     assert(!heap->is_concurrent_old_mark_in_progress(), "Old cycle should not be running.");
 79   }
 80 
 81   heap->set_concurrent_young_mark_in_progress(in_progress);
 82 }
 83 
 84 bool ShenandoahGlobalGeneration::contains(ShenandoahAffiliation affiliation) const {
 85   return true;
 86 }
 87 
 88 bool ShenandoahGlobalGeneration::contains(ShenandoahHeapRegion* region) const {
 89   return true;
 90 }
 91 
 92 void ShenandoahGlobalGeneration::parallel_heap_region_iterate(ShenandoahHeapRegionClosure* cl) {
 93   ShenandoahHeap::heap()->parallel_heap_region_iterate(cl);
 94 }
 95 
 96 void ShenandoahGlobalGeneration::heap_region_iterate(ShenandoahHeapRegionClosure* cl) {
 97   ShenandoahHeap::heap()->heap_region_iterate(cl);
 98 }
 99 
100 bool ShenandoahGlobalGeneration::is_concurrent_mark_in_progress() {
101   ShenandoahHeap* heap = ShenandoahHeap::heap();
102   return heap->is_concurrent_mark_in_progress();
103 }
104 
105 ShenandoahHeuristics* ShenandoahGlobalGeneration::initialize_heuristics(ShenandoahMode* gc_mode) {
106   if (gc_mode->is_generational()) {
107     _heuristics = new ShenandoahGlobalHeuristics(this);
108   } else {
109     _heuristics = gc_mode->initialize_heuristics(this);
110   }
111 
112   _heuristics->set_guaranteed_gc_interval(ShenandoahGuaranteedGCInterval);
113   confirm_heuristics_mode();
114   return _heuristics;
115 }
116 
117 void ShenandoahGlobalGeneration::set_mark_complete() {
118   ShenandoahGeneration::set_mark_complete();
119   if (ShenandoahHeap::heap()->mode()->is_generational()) {
120     ShenandoahGenerationalHeap* heap = ShenandoahGenerationalHeap::heap();
121     heap->young_generation()->set_mark_complete();
122     heap->old_generation()->set_mark_complete();
123   }
124 }
125 
126 void ShenandoahGlobalGeneration::set_mark_incomplete() {
127   ShenandoahGeneration::set_mark_incomplete();
128   if (ShenandoahHeap::heap()->mode()->is_generational()) {
129     ShenandoahGenerationalHeap* heap = ShenandoahGenerationalHeap::heap();
130     heap->young_generation()->set_mark_incomplete();
131     heap->old_generation()->set_mark_incomplete();
132   }
133 }
134 
135 void ShenandoahGlobalGeneration::prepare_gc() {
136   ShenandoahGeneration::prepare_gc();
137 
138   if (ShenandoahHeap::heap()->mode()->is_generational()) {
139     assert(type() == GLOBAL, "Unexpected generation type");
140     // Clear any stale/partial local census data before the start of a
141     // new marking cycle
142     ShenandoahGenerationalHeap::heap()->age_census()->reset_local();
143   } else {
144     assert(type() == NON_GEN, "Unexpected generation type");
145   }
146 }