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