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