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 31 int ShenandoahHeuristics::compare_by_garbage(RegionData a, RegionData b) { 32 if (a._garbage > b._garbage) 33 return -1; 34 else if (a._garbage < b._garbage) 35 return 1; 36 else return 0; 37 } 38 39 int ShenandoahHeuristics::compare_by_alloc_seq_ascending(RegionData a, RegionData b) { 40 if (a._seqnum_last_alloc == b._seqnum_last_alloc) 41 return 0; 42 else if (a._seqnum_last_alloc < b._seqnum_last_alloc) 43 return -1; 44 else return 1; 45 } 46 47 int ShenandoahHeuristics::compare_by_alloc_seq_descending(RegionData a, RegionData b) { 48 return -compare_by_alloc_seq_ascending(a, b); 49 } 50 51 int ShenandoahHeuristics::compare_by_connects(RegionConnections a, RegionConnections b) { 52 if (a._connections == b._connections) 53 return 0; 54 else if (a._connections < b._connections) 55 return -1; 56 else return 1; 57 } 58 59 ShenandoahHeuristics::ShenandoahHeuristics() : 60 _bytes_in_cset(0), 61 _degenerated_cycles_in_a_row(0), 62 _successful_cycles_in_a_row(0), 63 _region_data(NULL), 64 _region_data_size(0), 65 _region_connects(NULL), 66 _region_connects_size(0), 67 _update_refs_early(false), 68 _update_refs_adaptive(false), 69 _last_cycle_end(0) 70 { 71 if (strcmp(ShenandoahUpdateRefsEarly, "on") == 0 || 72 strcmp(ShenandoahUpdateRefsEarly, "true") == 0 ) { 73 _update_refs_early = true; 74 } else if (strcmp(ShenandoahUpdateRefsEarly, "off") == 0 || 75 strcmp(ShenandoahUpdateRefsEarly, "false") == 0 ) { 76 _update_refs_early = false; 77 } else if (strcmp(ShenandoahUpdateRefsEarly, "adaptive") == 0) { 78 _update_refs_adaptive = true; 79 _update_refs_early = true; 80 } else { 81 vm_exit_during_initialization("Unknown -XX:ShenandoahUpdateRefsEarly option: %s", ShenandoahUpdateRefsEarly); 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 ShenandoahHeuristics::RegionConnections* ShenandoahHeuristics::get_region_connects_cache(size_t num) { 106 RegionConnections* res = _region_connects; 107 if (res == NULL) { 108 res = NEW_C_HEAP_ARRAY(RegionConnections, num, mtGC); 109 _region_connects = res; 110 _region_connects_size = num; 111 } else if (_region_connects_size < num) { 112 res = REALLOC_C_HEAP_ARRAY(RegionConnections, _region_connects, num, mtGC); 113 _region_connects = res; 114 _region_connects_size = num; 115 } 116 return res; 117 } 118 119 void ShenandoahHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set) { 120 assert(collection_set->count() == 0, "Must be empty"); 121 start_choose_collection_set(); 122 123 ShenandoahHeap* heap = ShenandoahHeap::heap(); 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 for (size_t i = 0; i < num_regions; i++) { 142 ShenandoahHeapRegion* region = heap->get_region(i); 143 144 size_t garbage = region->garbage(); 145 total_garbage += garbage; 146 147 if (region->is_empty()) { 148 free_regions++; 149 free += ShenandoahHeapRegion::region_size_bytes(); 150 } else if (region->is_regular()) { 151 if (!region->has_live()) { 152 // We can recycle it right away and put it in the free set. 153 immediate_regions++; 154 immediate_garbage += garbage; 155 region->make_trash(); 156 } else { 157 // This is our candidate for later consideration. 158 candidates[cand_idx]._region = region; 159 candidates[cand_idx]._garbage = garbage; 160 cand_idx++; 161 } 162 } else if (region->is_humongous_start()) { 163 // Reclaim humongous regions here, and count them as the immediate garbage 164 #ifdef ASSERT 165 bool reg_live = region->has_live(); 166 bool bm_live = heap->is_marked_complete(oop(region->bottom() + BrooksPointer::word_size())); 167 assert(reg_live == bm_live, 168 err_msg("Humongous liveness and marks should agree. Region live: %s; Bitmap live: %s; Region Live Words: " SIZE_FORMAT, 169 BOOL_TO_STR(reg_live), BOOL_TO_STR(bm_live), region->get_live_data_words())); 170 #endif 171 if (!region->has_live()) { 172 heap->trash_humongous_region_at(region); 173 174 // Count only the start. Continuations would be counted on "trash" path 175 immediate_regions++; 176 immediate_garbage += garbage; 177 } 178 } else if (region->is_trash()) { 179 // Count in just trashed collection set, during coalesced CM-with-UR 180 immediate_regions++; 181 immediate_garbage += garbage; 182 } 183 } 184 185 // Step 2. Look back at garbage statistics, and decide if we want to collect anything, 186 // given the amount of immediately reclaimable garbage. If we do, figure out the collection set. 187 188 assert (immediate_garbage <= total_garbage, 189 err_msg("Cannot have more immediate garbage than total garbage: " SIZE_FORMAT "M vs " SIZE_FORMAT "M", 190 immediate_garbage / M, total_garbage / M)); 191 192 size_t immediate_percent = total_garbage == 0 ? 0 : (immediate_garbage * 100 / total_garbage); 193 194 if (immediate_percent <= ShenandoahImmediateThreshold) { 195 choose_collection_set_from_regiondata(collection_set, candidates, cand_idx, immediate_garbage + free); 196 collection_set->update_region_status(); 197 198 size_t cset_percent = total_garbage == 0 ? 0 : (collection_set->garbage() * 100 / total_garbage); 199 log_info(gc, ergo)("Collectable Garbage: "SIZE_FORMAT"M ("SIZE_FORMAT"%% of total), "SIZE_FORMAT"M CSet, "SIZE_FORMAT" CSet regions", 200 collection_set->garbage() / M, cset_percent, collection_set->live_data() / M, collection_set->count()); 201 } 202 end_choose_collection_set(); 203 204 log_info(gc, ergo)("Immediate Garbage: "SIZE_FORMAT"M ("SIZE_FORMAT"%% of total), "SIZE_FORMAT" regions", 205 immediate_garbage / M, immediate_percent, immediate_regions); 206 } 207 208 void ShenandoahHeuristics::record_gc_start() { 209 // Do nothing 210 } 211 212 void ShenandoahHeuristics::record_gc_end() { 213 // Do nothing 214 } 215 216 void ShenandoahHeuristics::record_cycle_start() { 217 // Do nothing 218 } 219 220 void ShenandoahHeuristics::record_cycle_end() { 221 _last_cycle_end = os::elapsedTime(); 222 } 223 224 void ShenandoahHeuristics::record_phase_time(ShenandoahPhaseTimings::Phase phase, double secs) { 225 // Do nothing 226 } 227 228 void ShenandoahHeuristics::print_thresholds() { 229 // Do nothing 230 } 231 232 bool ShenandoahHeuristics::should_start_update_refs() { 233 return _update_refs_early; 234 } 235 236 bool ShenandoahHeuristics::update_refs() const { 237 return _update_refs_early; 238 } 239 240 bool ShenandoahHeuristics::should_degenerate_cycle() { 241 return _degenerated_cycles_in_a_row <= ShenandoahFullGCThreshold; 242 } 243 244 void ShenandoahHeuristics::record_success_concurrent() { 245 _degenerated_cycles_in_a_row = 0; 246 _successful_cycles_in_a_row++; 247 } 248 249 void ShenandoahHeuristics::record_success_degenerated() { 250 _degenerated_cycles_in_a_row++; 251 _successful_cycles_in_a_row = 0; 252 } 253 254 void ShenandoahHeuristics::record_success_full() { 255 _degenerated_cycles_in_a_row = 0; 256 _successful_cycles_in_a_row++; 257 } 258 259 void ShenandoahHeuristics::record_allocation_failure_gc() { 260 _bytes_in_cset = 0; 261 } 262 263 void ShenandoahHeuristics::record_explicit_gc() { 264 _bytes_in_cset = 0; 265 } 266 267 void ShenandoahHeuristics::record_peak_occupancy() { 268 // Nothing to do by default. 269 } 270 271 bool ShenandoahHeuristics::should_process_references() { 272 if (ShenandoahRefProcFrequency == 0) return false; 273 size_t cycle = ShenandoahHeap::heap()->shenandoahPolicy()->cycle_counter(); 274 // Process references every Nth GC cycle. 275 return cycle % ShenandoahRefProcFrequency == 0; 276 } 277 278 bool ShenandoahHeuristics::should_unload_classes() { 279 if (ShenandoahUnloadClassesFrequency == 0) return false; 280 size_t cycle = ShenandoahHeap::heap()->shenandoahPolicy()->cycle_counter(); 281 // Unload classes every Nth GC cycle. 282 // This should not happen in the same cycle as process_references to amortize costs. 283 // Offsetting by one is enough to break the rendezvous when periods are equal. 284 // When periods are not equal, offsetting by one is just as good as any other guess. 285 return (cycle + 1) % ShenandoahUnloadClassesFrequency == 0; 286 } 287 288 void ShenandoahHeuristics::initialize() { 289 // Nothing to do by default. 290 }