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