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