< prev index next >

src/hotspot/share/gc/shenandoah/heuristics/shenandoahHeuristics.cpp

Print this page

  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(nullptr),
 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   size_t num_regions = ShenandoahHeap::heap()->num_regions();
 57   assert(num_regions > 0, "Sanity");
 58 
 59   _region_data = NEW_C_HEAP_ARRAY(RegionData, num_regions, mtGC);



 60 }
 61 
 62 ShenandoahHeuristics::~ShenandoahHeuristics() {
 63   FREE_C_HEAP_ARRAY(RegionGarbage, _region_data);
 64 }
 65 
 66 void ShenandoahHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set) {
 67   assert(collection_set->count() == 0, "Must be empty");
 68 
 69   ShenandoahHeap* heap = ShenandoahHeap::heap();
 70 
 71   // Check all pinned regions have updated status before choosing the collection set.
 72   heap->assert_pinned_region_status();
 73 
 74   // Step 1. Build up the region candidates we care about, rejecting losers and accepting winners right away.
 75 
 76   size_t num_regions = heap->num_regions();
 77 
 78   RegionData* candidates = _region_data;
 79 
 80   size_t cand_idx = 0;
 81 
 82   size_t total_garbage = 0;
 83 
 84   size_t immediate_garbage = 0;
 85   size_t immediate_regions = 0;
 86 
 87   size_t free = 0;
 88   size_t free_regions = 0;
 89 
 90   ShenandoahMarkingContext* const ctx = heap->complete_marking_context();
 91 
 92   for (size_t i = 0; i < num_regions; i++) {
 93     ShenandoahHeapRegion* region = heap->get_region(i);
 94 
 95     size_t garbage = region->garbage();
 96     total_garbage += garbage;
 97 
 98     if (region->is_empty()) {
 99       free_regions++;
100       free += ShenandoahHeapRegion::region_size_bytes();
101     } else if (region->is_regular()) {
102       if (!region->has_live()) {
103         // We can recycle it right away and put it in the free set.
104         immediate_regions++;
105         immediate_garbage += garbage;
106         region->make_trash_immediate();
107       } else {
108         // This is our candidate for later consideration.
109         candidates[cand_idx]._region = region;
110         candidates[cand_idx]._garbage = garbage;
111         cand_idx++;
112       }
113     } else if (region->is_humongous_start()) {
114       // Reclaim humongous regions here, and count them as the immediate garbage
115 #ifdef ASSERT
116       bool reg_live = region->has_live();
117       bool bm_live = ctx->is_marked(cast_to_oop(region->bottom()));
118       assert(reg_live == bm_live,
119              "Humongous liveness and marks should agree. Region live: %s; Bitmap live: %s; Region Live Words: " SIZE_FORMAT,
120              BOOL_TO_STR(reg_live), BOOL_TO_STR(bm_live), region->get_live_data_words());
121 #endif
122       if (!region->has_live()) {
123         heap->trash_humongous_region_at(region);
124 
125         // Count only the start. Continuations would be counted on "trash" path
126         immediate_regions++;
127         immediate_garbage += garbage;
128       }
129     } else if (region->is_trash()) {
130       // Count in just trashed collection set, during coalesced CM-with-UR
131       immediate_regions++;
132       immediate_garbage += garbage;
133     }
134   }
135 
136   // Step 2. Look back at garbage statistics, and decide if we want to collect anything,
137   // given the amount of immediately reclaimable garbage. If we do, figure out the collection set.
138 
139   assert (immediate_garbage <= total_garbage,
140           "Cannot have more immediate garbage than total garbage: " SIZE_FORMAT "%s vs " SIZE_FORMAT "%s",
141           byte_size_in_proper_unit(immediate_garbage), proper_unit_for_byte_size(immediate_garbage),
142           byte_size_in_proper_unit(total_garbage),     proper_unit_for_byte_size(total_garbage));
143 
144   size_t immediate_percent = (total_garbage == 0) ? 0 : (immediate_garbage * 100 / total_garbage);
145 
146   if (immediate_percent <= ShenandoahImmediateThreshold) {
147     choose_collection_set_from_regiondata(collection_set, candidates, cand_idx, immediate_garbage + free);
148   }
149 
150   size_t cset_percent = (total_garbage == 0) ? 0 : (collection_set->garbage() * 100 / total_garbage);
151 
152   size_t collectable_garbage = collection_set->garbage() + immediate_garbage;
153   size_t collectable_garbage_percent = (total_garbage == 0) ? 0 : (collectable_garbage * 100 / total_garbage);
154 
155   log_info(gc, ergo)("Collectable Garbage: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), "
156                      "Immediate: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), "
157                      "CSet: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%)",
158 
159                      byte_size_in_proper_unit(collectable_garbage),
160                      proper_unit_for_byte_size(collectable_garbage),
161                      collectable_garbage_percent,
162 
163                      byte_size_in_proper_unit(immediate_garbage),
164                      proper_unit_for_byte_size(immediate_garbage),
165                      immediate_percent,

166 
167                      byte_size_in_proper_unit(collection_set->garbage()),
168                      proper_unit_for_byte_size(collection_set->garbage()),
169                      cset_percent);

170 }
171 
172 void ShenandoahHeuristics::record_cycle_start() {
173   _cycle_start = os::elapsedTime();
174 }
175 
176 void ShenandoahHeuristics::record_cycle_end() {
177   _last_cycle_end = os::elapsedTime();
178 }
179 
180 bool ShenandoahHeuristics::should_start_gc() {




181   // Perform GC to cleanup metaspace
182   if (has_metaspace_oom()) {
183     // Some of vmTestbase/metaspace tests depend on following line to count GC cycles
184     log_info(gc)("Trigger: %s", GCCause::to_string(GCCause::_metadata_GC_threshold));

185     return true;
186   }
187 
188   if (ShenandoahGuaranteedGCInterval > 0) {
189     double last_time_ms = (os::elapsedTime() - _last_cycle_end) * 1000;
190     if (last_time_ms > ShenandoahGuaranteedGCInterval) {
191       log_info(gc)("Trigger: Time since last GC (%.0f ms) is larger than guaranteed interval (" UINTX_FORMAT " ms)",
192                    last_time_ms, ShenandoahGuaranteedGCInterval);

193       return true;
194     }
195   }
196 
197   return false;
198 }
199 
200 bool ShenandoahHeuristics::should_degenerate_cycle() {
201   return _degenerated_cycles_in_a_row <= ShenandoahFullGCThreshold;
202 }
203 
204 void ShenandoahHeuristics::adjust_penalty(intx step) {
205   assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,
206           "In range before adjustment: " INTX_FORMAT, _gc_time_penalties);






207 
208   intx new_val = _gc_time_penalties + step;
209   if (new_val < 0) {
210     new_val = 0;
211   }
212   if (new_val > 100) {
213     new_val = 100;
214   }
215   _gc_time_penalties = new_val;
216 
217   assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,
218           "In range after adjustment: " INTX_FORMAT, _gc_time_penalties);
219 }
220 
221 void ShenandoahHeuristics::record_success_concurrent() {
222   _degenerated_cycles_in_a_row = 0;
223   _successful_cycles_in_a_row++;














224 
225   _gc_time_history->add(time_since_last_gc());

226   _gc_times_learned++;
227 
228   adjust_penalty(Concurrent_Adjust);
229 }
230 
231 void ShenandoahHeuristics::record_success_degenerated() {
232   _degenerated_cycles_in_a_row++;
233   _successful_cycles_in_a_row = 0;
234 
235   adjust_penalty(Degenerated_Penalty);
236 }
237 
238 void ShenandoahHeuristics::record_success_full() {
239   _degenerated_cycles_in_a_row = 0;
240   _successful_cycles_in_a_row++;
241 
242   adjust_penalty(Full_Penalty);
243 }
244 
245 void ShenandoahHeuristics::record_allocation_failure_gc() {
246   // Do nothing.
247 }
248 
249 void ShenandoahHeuristics::record_requested_gc() {
250   // Assume users call System.gc() when external state changes significantly,
251   // which forces us to re-learn the GC timings and allocation rates.
252   _gc_times_learned = 0;
253 }
254 
255 bool ShenandoahHeuristics::can_unload_classes() {
256   if (!ClassUnloading) return false;
257   return true;
258 }
259 
260 bool ShenandoahHeuristics::should_unload_classes() {
261   if (!can_unload_classes()) return false;
262   if (has_metaspace_oom()) return true;
263   return ClassUnloadingWithConcurrentMark;
264 }
265 
266 void ShenandoahHeuristics::initialize() {
267   // Nothing to do by default.
268 }
269 
270 double ShenandoahHeuristics::time_since_last_gc() const {
271   return os::elapsedTime() - _cycle_start;
272 }

  1 /*
  2  * Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "gc/shared/gcCause.hpp"

 28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"

 29 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 30 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
 31 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
 32 #include "logging/log.hpp"
 33 #include "logging/logTag.hpp"
 34 #include "runtime/globals_extension.hpp"
 35 #include "utilities/quickSort.hpp"
 36 
 37 // sort by decreasing garbage (so most garbage comes first)
 38 int ShenandoahHeuristics::compare_by_garbage(RegionData a, RegionData b) {
 39   if (a.get_garbage() > b.get_garbage()) {
 40     return -1;
 41   } else if (a.get_garbage() < b.get_garbage()) {
 42     return 1;
 43   } else {
 44     return 0;
 45   }
 46 }
 47 
 48 ShenandoahHeuristics::ShenandoahHeuristics(ShenandoahSpaceInfo* space_info) :
 49   _start_gc_is_pending(false),
 50   _declined_trigger_count(0),
 51   _most_recent_declined_trigger_count(0),
 52   _space_info(space_info),
 53   _region_data(nullptr),
 54   _guaranteed_gc_interval(0),

 55   _cycle_start(os::elapsedTime()),
 56   _last_cycle_end(0),
 57   _gc_times_learned(0),
 58   _gc_time_penalties(0),
 59   _gc_cycle_time_history(new TruncatedSeq(Moving_Average_Samples, ShenandoahAdaptiveDecayFactor)),
 60   _metaspace_oom()
 61 {
 62   size_t num_regions = ShenandoahHeap::heap()->num_regions();
 63   assert(num_regions > 0, "Sanity");
 64 
 65   _region_data = NEW_C_HEAP_ARRAY(RegionData, num_regions, mtGC);
 66   for (size_t i = 0; i < num_regions; i++) {
 67     _region_data[i].clear();
 68   }
 69 }
 70 
 71 ShenandoahHeuristics::~ShenandoahHeuristics() {
 72   FREE_C_HEAP_ARRAY(RegionGarbage, _region_data);
 73 }
 74 
 75 void ShenandoahHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set) {
 76   assert(collection_set->is_empty(), "Must be empty");
 77 
 78   ShenandoahHeap* heap = ShenandoahHeap::heap();
 79 
 80   // Check all pinned regions have updated status before choosing the collection set.
 81   heap->assert_pinned_region_status();
 82 
 83   // Step 1. Build up the region candidates we care about, rejecting losers and accepting winners right away.
 84 
 85   size_t num_regions = heap->num_regions();
 86 
 87   RegionData* candidates = _region_data;
 88 
 89   size_t cand_idx = 0;
 90 
 91   size_t total_garbage = 0;
 92 
 93   size_t immediate_garbage = 0;
 94   size_t immediate_regions = 0;
 95 
 96   size_t free = 0;
 97   size_t free_regions = 0;
 98 


 99   for (size_t i = 0; i < num_regions; i++) {
100     ShenandoahHeapRegion* region = heap->get_region(i);
101 
102     size_t garbage = region->garbage();
103     total_garbage += garbage;
104 
105     if (region->is_empty()) {
106       free_regions++;
107       free += ShenandoahHeapRegion::region_size_bytes();
108     } else if (region->is_regular()) {
109       if (!region->has_live()) {
110         // We can recycle it right away and put it in the free set.
111         immediate_regions++;
112         immediate_garbage += garbage;
113         region->make_trash_immediate();
114       } else {
115         // This is our candidate for later consideration.
116         candidates[cand_idx].set_region_and_garbage(region, garbage);

117         cand_idx++;
118       }
119     } else if (region->is_humongous_start()) {
120       // Reclaim humongous regions here, and count them as the immediate garbage
121 #ifdef ASSERT
122       bool reg_live = region->has_live();
123       bool bm_live = heap->gc_generation()->complete_marking_context()->is_marked(cast_to_oop(region->bottom()));
124       assert(reg_live == bm_live,
125              "Humongous liveness and marks should agree. Region live: %s; Bitmap live: %s; Region Live Words: " SIZE_FORMAT,
126              BOOL_TO_STR(reg_live), BOOL_TO_STR(bm_live), region->get_live_data_words());
127 #endif
128       if (!region->has_live()) {
129         heap->trash_humongous_region_at(region);
130 
131         // Count only the start. Continuations would be counted on "trash" path
132         immediate_regions++;
133         immediate_garbage += garbage;
134       }
135     } else if (region->is_trash()) {
136       // Count in just trashed collection set, during coalesced CM-with-UR
137       immediate_regions++;
138       immediate_garbage += garbage;
139     }
140   }
141 
142   // Step 2. Look back at garbage statistics, and decide if we want to collect anything,
143   // given the amount of immediately reclaimable garbage. If we do, figure out the collection set.
144 
145   assert (immediate_garbage <= total_garbage,
146           "Cannot have more immediate garbage than total garbage: " SIZE_FORMAT "%s vs " SIZE_FORMAT "%s",
147           byte_size_in_proper_unit(immediate_garbage), proper_unit_for_byte_size(immediate_garbage),
148           byte_size_in_proper_unit(total_garbage),     proper_unit_for_byte_size(total_garbage));
149 
150   size_t immediate_percent = (total_garbage == 0) ? 0 : (immediate_garbage * 100 / total_garbage);
151 
152   if (immediate_percent <= ShenandoahImmediateThreshold) {
153     choose_collection_set_from_regiondata(collection_set, candidates, cand_idx, immediate_garbage + free);
154   }
155 
156   size_t cset_percent = (total_garbage == 0) ? 0 : (collection_set->garbage() * 100 / total_garbage);

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 "%%), " SIZE_FORMAT " regions, "
162                      "CSet: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), " SIZE_FORMAT " regions",
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                      immediate_regions,
172 
173                      byte_size_in_proper_unit(collection_set->garbage()),
174                      proper_unit_for_byte_size(collection_set->garbage()),
175                      cset_percent,
176                      collection_set->count());
177 }
178 
179 void ShenandoahHeuristics::record_cycle_start() {
180   _cycle_start = os::elapsedTime();
181 }
182 
183 void ShenandoahHeuristics::record_cycle_end() {
184   _last_cycle_end = os::elapsedTime();
185 }
186 
187 bool ShenandoahHeuristics::should_start_gc() {
188   if (_start_gc_is_pending) {
189     log_trigger("GC start is already pending");
190     return true;
191   }
192   // Perform GC to cleanup metaspace
193   if (has_metaspace_oom()) {
194     // Some of vmTestbase/metaspace tests depend on following line to count GC cycles
195     log_trigger("%s", GCCause::to_string(GCCause::_metadata_GC_threshold));
196     accept_trigger();
197     return true;
198   }
199 
200   if (_guaranteed_gc_interval > 0) {
201     double last_time_ms = (os::elapsedTime() - _last_cycle_end) * 1000;
202     if (last_time_ms > _guaranteed_gc_interval) {
203       log_trigger("Time since last GC (%.0f ms) is larger than guaranteed interval (" UINTX_FORMAT " ms)",
204                    last_time_ms, _guaranteed_gc_interval);
205       accept_trigger();
206       return true;
207     }
208   }
209   decline_trigger();
210   return false;
211 }
212 
213 bool ShenandoahHeuristics::should_degenerate_cycle() {
214   return ShenandoahHeap::heap()->shenandoah_policy()->consecutive_degenerated_gc_count() <= ShenandoahFullGCThreshold;
215 }
216 
217 void ShenandoahHeuristics::adjust_penalty(intx step) {
218   assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,
219          "In range before adjustment: " INTX_FORMAT, _gc_time_penalties);
220 
221   if ((_most_recent_declined_trigger_count <= Penalty_Free_Declinations) && (step > 0)) {
222     // Don't penalize if heuristics are not responsible for a negative outcome.  Allow Penalty_Free_Declinations following
223     // previous GC for self calibration without penalty.
224     step = 0;
225   }
226 
227   intx new_val = _gc_time_penalties + step;
228   if (new_val < 0) {
229     new_val = 0;
230   }
231   if (new_val > 100) {
232     new_val = 100;
233   }
234   _gc_time_penalties = new_val;
235 
236   assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,
237          "In range after adjustment: " INTX_FORMAT, _gc_time_penalties);
238 }
239 
240 void ShenandoahHeuristics::log_trigger(const char* fmt, ...) {
241   LogTarget(Info, gc) lt;
242   if (lt.is_enabled()) {
243     ResourceMark rm;
244     LogStream ls(lt);
245     ls.print_raw("Trigger", 7);
246     if (ShenandoahHeap::heap()->mode()->is_generational()) {
247       ls.print(" (%s)", _space_info->name());
248     }
249     ls.print_raw(": ", 2);
250     va_list va;
251     va_start(va, fmt);
252     ls.vprint(fmt, va);
253     va_end(va);
254     ls.cr();
255   }
256 }
257 
258 void ShenandoahHeuristics::record_success_concurrent() {
259   _gc_cycle_time_history->add(elapsed_cycle_time());
260   _gc_times_learned++;
261 
262   adjust_penalty(Concurrent_Adjust);
263 }
264 
265 void ShenandoahHeuristics::record_success_degenerated() {



266   adjust_penalty(Degenerated_Penalty);
267 }
268 
269 void ShenandoahHeuristics::record_success_full() {



270   adjust_penalty(Full_Penalty);
271 }
272 
273 void ShenandoahHeuristics::record_allocation_failure_gc() {
274   // Do nothing.
275 }
276 
277 void ShenandoahHeuristics::record_requested_gc() {
278   // Assume users call System.gc() when external state changes significantly,
279   // which forces us to re-learn the GC timings and allocation rates.
280   _gc_times_learned = 0;
281 }
282 
283 bool ShenandoahHeuristics::can_unload_classes() {
284   return ClassUnloading;

285 }
286 
287 bool ShenandoahHeuristics::should_unload_classes() {
288   if (!can_unload_classes()) return false;
289   if (has_metaspace_oom()) return true;
290   return ClassUnloadingWithConcurrentMark;
291 }
292 
293 void ShenandoahHeuristics::initialize() {
294   // Nothing to do by default.
295 }
296 
297 double ShenandoahHeuristics::elapsed_cycle_time() const {
298   return os::elapsedTime() - _cycle_start;
299 }
< prev index next >