1 /* 2 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 27 #include "gc/shenandoah/heuristics/shenandoahGenerationalHeuristics.hpp" 28 #include "gc/shenandoah/shenandoahCollectionSet.hpp" 29 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp" 30 #include "gc/shenandoah/shenandoahGeneration.hpp" 31 #include "gc/shenandoah/shenandoahGenerationalHeap.inline.hpp" 32 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp" 33 #include "gc/shenandoah/shenandoahOldGeneration.hpp" 34 #include "gc/shenandoah/shenandoahEvacInfo.hpp" 35 #include "gc/shenandoah/shenandoahTrace.hpp" 36 37 #include "logging/log.hpp" 38 39 ShenandoahGenerationalHeuristics::ShenandoahGenerationalHeuristics(ShenandoahGeneration* generation) 40 : ShenandoahAdaptiveHeuristics(generation), _generation(generation) { 41 } 42 43 void ShenandoahGenerationalHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set) { 44 assert(collection_set->is_empty(), "Must be empty"); 45 46 auto heap = ShenandoahGenerationalHeap::heap(); 47 size_t region_size_bytes = ShenandoahHeapRegion::region_size_bytes(); 48 49 50 // Check all pinned regions have updated status before choosing the collection set. 51 heap->assert_pinned_region_status(); 52 53 // Step 1. Build up the region candidates we care about, rejecting losers and accepting winners right away. 54 55 size_t num_regions = heap->num_regions(); 56 57 RegionData* candidates = _region_data; 58 59 size_t cand_idx = 0; 60 size_t preselected_candidates = 0; 61 62 size_t total_garbage = 0; 63 64 size_t immediate_garbage = 0; 65 size_t immediate_regions = 0; 66 67 size_t free = 0; 68 size_t free_regions = 0; 69 70 // This counts number of humongous regions that we intend to promote in this cycle. 71 size_t humongous_regions_promoted = 0; 72 // This counts number of regular regions that will be promoted in place. 73 size_t regular_regions_promoted_in_place = 0; 74 // This counts bytes of memory used by regular regions to be promoted in place. 75 size_t regular_regions_promoted_usage = 0; 76 // This counts bytes of memory free in regular regions to be promoted in place. 77 size_t regular_regions_promoted_free = 0; 78 // This counts bytes of garbage memory in regular regions to be promoted in place. 79 size_t regular_regions_promoted_garbage = 0; 80 81 for (size_t i = 0; i < num_regions; i++) { 82 ShenandoahHeapRegion* region = heap->get_region(i); 83 if (!_generation->contains(region)) { 84 continue; 85 } 86 size_t garbage = region->garbage(); 87 total_garbage += garbage; 88 if (region->is_empty()) { 89 free_regions++; 90 free += region_size_bytes; 91 } else if (region->is_regular()) { 92 if (!region->has_live()) { 93 // We can recycle it right away and put it in the free set. 94 immediate_regions++; 95 immediate_garbage += garbage; 96 region->make_trash_immediate(); 97 } else { 98 bool is_candidate; 99 // This is our candidate for later consideration. 100 if (collection_set->is_preselected(i)) { 101 assert(heap->is_tenurable(region), "Preselection filter"); 102 is_candidate = true; 103 preselected_candidates++; 104 // Set garbage value to maximum value to force this into the sorted collection set. 105 garbage = region_size_bytes; 106 } else if (region->is_young() && heap->is_tenurable(region)) { 107 // Note that for GLOBAL GC, region may be OLD, and OLD regions do not qualify for pre-selection 108 109 // This region is old enough to be promoted but it was not preselected, either because its garbage is below 110 // ShenandoahOldGarbageThreshold so it will be promoted in place, or because there is not sufficient room 111 // in old gen to hold the evacuated copies of this region's live data. In both cases, we choose not to 112 // place this region into the collection set. 113 if (region->get_top_before_promote() != nullptr) { 114 // Region was included for promotion-in-place 115 regular_regions_promoted_in_place++; 116 regular_regions_promoted_usage += region->used_before_promote(); 117 regular_regions_promoted_free += region->free(); 118 regular_regions_promoted_garbage += region->garbage(); 119 } 120 is_candidate = false; 121 } else { 122 is_candidate = true; 123 } 124 if (is_candidate) { 125 candidates[cand_idx].set_region_and_garbage(region, garbage); 126 cand_idx++; 127 } 128 } 129 } else if (region->is_humongous_start()) { 130 // Reclaim humongous regions here, and count them as the immediate garbage 131 #ifdef ASSERT 132 bool reg_live = region->has_live(); 133 bool bm_live = heap->active_generation()->complete_marking_context()->is_marked(cast_to_oop(region->bottom())); 134 assert(reg_live == bm_live, 135 "Humongous liveness and marks should agree. Region live: %s; Bitmap live: %s; Region Live Words: " SIZE_FORMAT, 136 BOOL_TO_STR(reg_live), BOOL_TO_STR(bm_live), region->get_live_data_words()); 137 #endif 138 if (!region->has_live()) { 139 heap->trash_humongous_region_at(region); 140 141 // Count only the start. Continuations would be counted on "trash" path 142 immediate_regions++; 143 immediate_garbage += garbage; 144 } else { 145 if (region->is_young() && heap->is_tenurable(region)) { 146 oop obj = cast_to_oop(region->bottom()); 147 size_t humongous_regions = ShenandoahHeapRegion::required_regions(obj->size() * HeapWordSize); 148 humongous_regions_promoted += humongous_regions; 149 } 150 } 151 } else if (region->is_trash()) { 152 // Count in just trashed collection set, during coalesced CM-with-UR 153 immediate_regions++; 154 immediate_garbage += garbage; 155 } 156 } 157 heap->old_generation()->set_expected_humongous_region_promotions(humongous_regions_promoted); 158 heap->old_generation()->set_expected_regular_region_promotions(regular_regions_promoted_in_place); 159 log_info(gc, ergo)("Planning to promote in place " SIZE_FORMAT " humongous regions and " SIZE_FORMAT 160 " regular regions, spanning a total of " SIZE_FORMAT " used bytes", 161 humongous_regions_promoted, regular_regions_promoted_in_place, 162 humongous_regions_promoted * ShenandoahHeapRegion::region_size_bytes() + 163 regular_regions_promoted_usage); 164 165 // Step 2. Look back at garbage statistics, and decide if we want to collect anything, 166 // given the amount of immediately reclaimable garbage. If we do, figure out the collection set. 167 168 assert (immediate_garbage <= total_garbage, 169 "Cannot have more immediate garbage than total garbage: " SIZE_FORMAT "%s vs " SIZE_FORMAT "%s", 170 byte_size_in_proper_unit(immediate_garbage), proper_unit_for_byte_size(immediate_garbage), 171 byte_size_in_proper_unit(total_garbage), proper_unit_for_byte_size(total_garbage)); 172 173 size_t immediate_percent = (total_garbage == 0) ? 0 : (immediate_garbage * 100 / total_garbage); 174 175 bool doing_promote_in_place = (humongous_regions_promoted + regular_regions_promoted_in_place > 0); 176 if (doing_promote_in_place || (preselected_candidates > 0) || (immediate_percent <= ShenandoahImmediateThreshold)) { 177 // Only young collections need to prime the collection set. 178 if (_generation->is_young()) { 179 heap->old_generation()->heuristics()->prime_collection_set(collection_set); 180 } 181 182 // Call the subclasses to add young-gen regions into the collection set. 183 choose_collection_set_from_regiondata(collection_set, candidates, cand_idx, immediate_garbage + free); 184 } 185 186 if (collection_set->has_old_regions()) { 187 heap->shenandoah_policy()->record_mixed_cycle(); 188 } 189 190 size_t cset_percent = (total_garbage == 0) ? 0 : (collection_set->garbage() * 100 / total_garbage); 191 size_t collectable_garbage = collection_set->garbage() + immediate_garbage; 192 size_t collectable_garbage_percent = (total_garbage == 0) ? 0 : (collectable_garbage * 100 / total_garbage); 193 194 log_info(gc, ergo)("Collectable Garbage: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), " 195 "Immediate: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), " SIZE_FORMAT " regions, " 196 "CSet: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), " SIZE_FORMAT " regions", 197 198 byte_size_in_proper_unit(collectable_garbage), 199 proper_unit_for_byte_size(collectable_garbage), 200 collectable_garbage_percent, 201 202 byte_size_in_proper_unit(immediate_garbage), 203 proper_unit_for_byte_size(immediate_garbage), 204 immediate_percent, 205 immediate_regions, 206 207 byte_size_in_proper_unit(collection_set->garbage()), 208 proper_unit_for_byte_size(collection_set->garbage()), 209 cset_percent, 210 collection_set->count()); 211 212 if (collection_set->garbage() > 0) { 213 size_t young_evac_bytes = collection_set->get_young_bytes_reserved_for_evacuation(); 214 size_t promote_evac_bytes = collection_set->get_young_bytes_to_be_promoted(); 215 size_t old_evac_bytes = collection_set->get_old_bytes_reserved_for_evacuation(); 216 size_t total_evac_bytes = young_evac_bytes + promote_evac_bytes + old_evac_bytes; 217 log_info(gc, ergo)("Evacuation Targets: YOUNG: " SIZE_FORMAT "%s, " 218 "PROMOTE: " SIZE_FORMAT "%s, " 219 "OLD: " SIZE_FORMAT "%s, " 220 "TOTAL: " SIZE_FORMAT "%s", 221 byte_size_in_proper_unit(young_evac_bytes), proper_unit_for_byte_size(young_evac_bytes), 222 byte_size_in_proper_unit(promote_evac_bytes), proper_unit_for_byte_size(promote_evac_bytes), 223 byte_size_in_proper_unit(old_evac_bytes), proper_unit_for_byte_size(old_evac_bytes), 224 byte_size_in_proper_unit(total_evac_bytes), proper_unit_for_byte_size(total_evac_bytes)); 225 226 ShenandoahEvacuationInformation evacInfo; 227 evacInfo.set_collection_set_regions(collection_set->count()); 228 evacInfo.set_collection_set_used_before(collection_set->used()); 229 evacInfo.set_collection_set_used_after(collection_set->live()); 230 evacInfo.set_collected_old(old_evac_bytes); 231 evacInfo.set_collected_promoted(promote_evac_bytes); 232 evacInfo.set_collected_young(young_evac_bytes); 233 evacInfo.set_regions_promoted_humongous(humongous_regions_promoted); 234 evacInfo.set_regions_promoted_regular(regular_regions_promoted_in_place); 235 evacInfo.set_regular_promoted_garbage(regular_regions_promoted_garbage); 236 evacInfo.set_regular_promoted_free(regular_regions_promoted_free); 237 evacInfo.set_regions_immediate(immediate_regions); 238 evacInfo.set_immediate_size(immediate_garbage); 239 evacInfo.set_free_regions(free_regions); 240 241 ShenandoahTracer().report_evacuation_info(&evacInfo); 242 } 243 } 244 245 246 size_t ShenandoahGenerationalHeuristics::add_preselected_regions_to_collection_set(ShenandoahCollectionSet* cset, 247 const RegionData* data, 248 size_t size) const { 249 // cur_young_garbage represents the amount of memory to be reclaimed from young-gen. In the case that live objects 250 // are known to be promoted out of young-gen, we count this as cur_young_garbage because this memory is reclaimed 251 // from young-gen and becomes available to serve future young-gen allocation requests. 252 size_t cur_young_garbage = 0; 253 for (size_t idx = 0; idx < size; idx++) { 254 ShenandoahHeapRegion* r = data[idx].get_region(); 255 if (cset->is_preselected(r->index())) { 256 assert(ShenandoahGenerationalHeap::heap()->is_tenurable(r), "Preselected regions must have tenure age"); 257 // Entire region will be promoted, This region does not impact young-gen or old-gen evacuation reserve. 258 // This region has been pre-selected and its impact on promotion reserve is already accounted for. 259 260 // r->used() is r->garbage() + r->get_live_data_bytes() 261 // Since all live data in this region is being evacuated from young-gen, it is as if this memory 262 // is garbage insofar as young-gen is concerned. Counting this as garbage reduces the need to 263 // reclaim highly utilized young-gen regions just for the sake of finding min_garbage to reclaim 264 // within young-gen memory. 265 266 cur_young_garbage += r->garbage(); 267 cset->add_region(r); 268 } 269 } 270 return cur_young_garbage; 271 } 272 273 void ShenandoahGenerationalHeuristics::log_cset_composition(ShenandoahCollectionSet* cset) const { 274 size_t collected_old = cset->get_old_bytes_reserved_for_evacuation(); 275 size_t collected_promoted = cset->get_young_bytes_to_be_promoted(); 276 size_t collected_young = cset->get_young_bytes_reserved_for_evacuation(); 277 278 log_info(gc, ergo)( 279 "Chosen CSet evacuates young: " SIZE_FORMAT "%s (of which at least: " SIZE_FORMAT "%s are to be promoted), " 280 "old: " SIZE_FORMAT "%s", 281 byte_size_in_proper_unit(collected_young), proper_unit_for_byte_size(collected_young), 282 byte_size_in_proper_unit(collected_promoted), proper_unit_for_byte_size(collected_promoted), 283 byte_size_in_proper_unit(collected_old), proper_unit_for_byte_size(collected_old)); 284 }