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