1 /*
  2  * Copyright (c) 2018, 2020, Red Hat, Inc. 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 "precompiled.hpp"
 26 #include "gc/shared/gcCause.hpp"
 27 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
 28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
 29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 30 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 31 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
 32 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
 33 #include "logging/log.hpp"
 34 #include "logging/logTag.hpp"
 35 #include "runtime/globals_extension.hpp"
 36 
 37 int ShenandoahHeuristics::compare_by_garbage(RegionData a, RegionData b) {
 38   if (a._garbage > b._garbage)
 39     return -1;
 40   else if (a._garbage < b._garbage)
 41     return 1;
 42   else return 0;
 43 }
 44 
 45 ShenandoahHeuristics::ShenandoahHeuristics() :
 46   _region_data(NULL),
 47   _degenerated_cycles_in_a_row(0),
 48   _successful_cycles_in_a_row(0),
 49   _cycle_start(os::elapsedTime()),
 50   _last_cycle_end(0),
 51   _gc_times_learned(0),
 52   _gc_time_penalties(0),
 53   _gc_time_history(new TruncatedSeq(10, ShenandoahAdaptiveDecayFactor)),
 54   _metaspace_oom()
 55 {
 56   // No unloading during concurrent mark? Communicate that to heuristics
 57   if (!ClassUnloadingWithConcurrentMark) {
 58     FLAG_SET_DEFAULT(ShenandoahUnloadClassesFrequency, 0);
 59   }
 60 
 61   size_t num_regions = ShenandoahHeap::heap()->num_regions();
 62   assert(num_regions > 0, "Sanity");
 63 
 64   _region_data = NEW_C_HEAP_ARRAY(RegionData, num_regions, mtGC);
 65 }
 66 
 67 ShenandoahHeuristics::~ShenandoahHeuristics() {
 68   FREE_C_HEAP_ARRAY(RegionGarbage, _region_data);
 69 }
 70 
 71 void ShenandoahHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set) {
 72   assert(collection_set->count() == 0, "Must be empty");
 73 
 74   ShenandoahHeap* heap = ShenandoahHeap::heap();
 75 
 76   // Check all pinned regions have updated status before choosing the collection set.
 77   heap->assert_pinned_region_status();
 78 
 79   // Step 1. Build up the region candidates we care about, rejecting losers and accepting winners right away.
 80 
 81   size_t num_regions = heap->num_regions();
 82 
 83   RegionData* candidates = _region_data;
 84 
 85   size_t cand_idx = 0;
 86 
 87   size_t total_garbage = 0;
 88 
 89   size_t immediate_garbage = 0;
 90   size_t immediate_regions = 0;
 91 
 92   size_t free = 0;
 93   size_t free_regions = 0;
 94 
 95   ShenandoahMarkingContext* const ctx = heap->complete_marking_context();
 96 
 97   for (size_t i = 0; i < num_regions; i++) {
 98     ShenandoahHeapRegion* region = heap->get_region(i);
 99 
100     size_t garbage = region->garbage();
101     total_garbage += garbage;
102 
103     if (region->is_empty()) {
104       free_regions++;
105       free += ShenandoahHeapRegion::region_size_bytes();
106     } else if (region->is_regular()) {
107       if (!region->has_live()) {
108         // We can recycle it right away and put it in the free set.
109         immediate_regions++;
110         immediate_garbage += garbage;
111         region->make_trash_immediate();
112       } else {
113         // This is our candidate for later consideration.
114         candidates[cand_idx]._region = region;
115         candidates[cand_idx]._garbage = garbage;
116         cand_idx++;
117       }
118     } else if (region->is_humongous_start()) {
119       // Reclaim humongous regions here, and count them as the immediate garbage
120 #ifdef ASSERT
121       bool reg_live = region->has_live();
122       bool bm_live = ctx->is_marked(cast_to_oop(region->bottom()));
123       assert(reg_live == bm_live,
124              "Humongous liveness and marks should agree. Region live: %s; Bitmap live: %s; Region Live Words: " SIZE_FORMAT,
125              BOOL_TO_STR(reg_live), BOOL_TO_STR(bm_live), region->get_live_data_words());
126 #endif
127       if (!region->has_live()) {
128         heap->trash_humongous_region_at(region);
129 
130         // Count only the start. Continuations would be counted on "trash" path
131         immediate_regions++;
132         immediate_garbage += garbage;
133       }
134     } else if (region->is_trash()) {
135       // Count in just trashed collection set, during coalesced CM-with-UR
136       immediate_regions++;
137       immediate_garbage += garbage;
138     }
139   }
140 
141   // Step 2. Look back at garbage statistics, and decide if we want to collect anything,
142   // given the amount of immediately reclaimable garbage. If we do, figure out the collection set.
143 
144   assert (immediate_garbage <= total_garbage,
145           "Cannot have more immediate garbage than total garbage: " SIZE_FORMAT "%s vs " SIZE_FORMAT "%s",
146           byte_size_in_proper_unit(immediate_garbage), proper_unit_for_byte_size(immediate_garbage),
147           byte_size_in_proper_unit(total_garbage),     proper_unit_for_byte_size(total_garbage));
148 
149   size_t immediate_percent = (total_garbage == 0) ? 0 : (immediate_garbage * 100 / total_garbage);
150 
151   if (immediate_percent <= ShenandoahImmediateThreshold) {
152     choose_collection_set_from_regiondata(collection_set, candidates, cand_idx, immediate_garbage + free);
153   }
154 
155   size_t cset_percent = (total_garbage == 0) ? 0 : (collection_set->garbage() * 100 / total_garbage);
156 
157   size_t collectable_garbage = collection_set->garbage() + immediate_garbage;
158   size_t collectable_garbage_percent = (total_garbage == 0) ? 0 : (collectable_garbage * 100 / total_garbage);
159 
160   log_info(gc, ergo)("Collectable Garbage: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), "
161                      "Immediate: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), "
162                      "CSet: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%)",
163 
164                      byte_size_in_proper_unit(collectable_garbage),
165                      proper_unit_for_byte_size(collectable_garbage),
166                      collectable_garbage_percent,
167 
168                      byte_size_in_proper_unit(immediate_garbage),
169                      proper_unit_for_byte_size(immediate_garbage),
170                      immediate_percent,
171 
172                      byte_size_in_proper_unit(collection_set->garbage()),
173                      proper_unit_for_byte_size(collection_set->garbage()),
174                      cset_percent);
175 }
176 
177 void ShenandoahHeuristics::record_cycle_start() {
178   _cycle_start = os::elapsedTime();
179 }
180 
181 void ShenandoahHeuristics::record_cycle_end() {
182   _last_cycle_end = os::elapsedTime();
183 }
184 
185 bool ShenandoahHeuristics::should_start_gc() {
186   // Perform GC to cleanup metaspace
187   if (has_metaspace_oom()) {
188     // Some of vmTestbase/metaspace tests depend on following line to count GC cycles
189     log_info(gc)("Trigger: %s", GCCause::to_string(GCCause::_metadata_GC_threshold));
190     return true;
191   }
192 
193   if (ShenandoahGuaranteedGCInterval > 0) {
194     double last_time_ms = (os::elapsedTime() - _last_cycle_end) * 1000;
195     if (last_time_ms > ShenandoahGuaranteedGCInterval) {
196       log_info(gc)("Trigger: Time since last GC (%.0f ms) is larger than guaranteed interval (" UINTX_FORMAT " ms)",
197                    last_time_ms, ShenandoahGuaranteedGCInterval);
198       return true;
199     }
200   }
201 
202   return false;
203 }
204 
205 bool ShenandoahHeuristics::should_degenerate_cycle() {
206   return _degenerated_cycles_in_a_row <= ShenandoahFullGCThreshold;
207 }
208 
209 void ShenandoahHeuristics::adjust_penalty(intx step) {
210   assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,
211           "In range before adjustment: " INTX_FORMAT, _gc_time_penalties);
212 
213   intx new_val = _gc_time_penalties + step;
214   if (new_val < 0) {
215     new_val = 0;
216   }
217   if (new_val > 100) {
218     new_val = 100;
219   }
220   _gc_time_penalties = new_val;
221 
222   assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,
223           "In range after adjustment: " INTX_FORMAT, _gc_time_penalties);
224 }
225 
226 void ShenandoahHeuristics::record_success_concurrent() {
227   _degenerated_cycles_in_a_row = 0;
228   _successful_cycles_in_a_row++;
229 
230   _gc_time_history->add(time_since_last_gc());
231   _gc_times_learned++;
232 
233   adjust_penalty(Concurrent_Adjust);
234 }
235 
236 void ShenandoahHeuristics::record_success_degenerated() {
237   _degenerated_cycles_in_a_row++;
238   _successful_cycles_in_a_row = 0;
239 
240   adjust_penalty(Degenerated_Penalty);
241 }
242 
243 void ShenandoahHeuristics::record_success_full() {
244   _degenerated_cycles_in_a_row = 0;
245   _successful_cycles_in_a_row++;
246 
247   adjust_penalty(Full_Penalty);
248 }
249 
250 void ShenandoahHeuristics::record_allocation_failure_gc() {
251   // Do nothing.
252 }
253 
254 void ShenandoahHeuristics::record_requested_gc() {
255   // Assume users call System.gc() when external state changes significantly,
256   // which forces us to re-learn the GC timings and allocation rates.
257   _gc_times_learned = 0;
258 }
259 
260 bool ShenandoahHeuristics::can_unload_classes() {
261   if (!ClassUnloading) return false;
262   return true;
263 }
264 
265 bool ShenandoahHeuristics::can_unload_classes_normal() {
266   if (!can_unload_classes()) return false;
267   if (has_metaspace_oom()) return true;
268   if (!ClassUnloadingWithConcurrentMark) return false;
269   if (ShenandoahUnloadClassesFrequency == 0) return false;
270   return true;
271 }
272 
273 bool ShenandoahHeuristics::should_unload_classes() {
274   if (!can_unload_classes_normal()) return false;
275   if (has_metaspace_oom()) return true;
276   size_t cycle = ShenandoahHeap::heap()->shenandoah_policy()->cycle_counter();
277   // Unload classes every Nth GC cycle.
278   // This should not happen in the same cycle as process_references to amortize costs.
279   // Offsetting by one is enough to break the rendezvous when periods are equal.
280   // When periods are not equal, offsetting by one is just as good as any other guess.
281   return (cycle + 1) % ShenandoahUnloadClassesFrequency == 0;
282 }
283 
284 void ShenandoahHeuristics::initialize() {
285   // Nothing to do by default.
286 }
287 
288 double ShenandoahHeuristics::time_since_last_gc() const {
289   return os::elapsedTime() - _cycle_start;
290 }