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