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.hpp" 32 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp" 33 #include "gc/shenandoah/shenandoahOldGeneration.hpp" 34 35 #include "logging/log.hpp" 36 37 ShenandoahGenerationalHeuristics::ShenandoahGenerationalHeuristics(ShenandoahGeneration* generation) 38 : ShenandoahAdaptiveHeuristics(generation), _generation(generation) { 39 } 40 41 void ShenandoahGenerationalHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set) { 42 assert(collection_set->is_empty(), "Must be empty"); 43 44 auto heap = ShenandoahGenerationalHeap::heap(); 45 size_t region_size_bytes = ShenandoahHeapRegion::region_size_bytes(); 46 47 48 // Check all pinned regions have updated status before choosing the collection set. 49 heap->assert_pinned_region_status(); 50 51 // Step 1. Build up the region candidates we care about, rejecting losers and accepting winners right away. 52 53 size_t num_regions = heap->num_regions(); 54 55 RegionData* candidates = _region_data; 56 57 size_t cand_idx = 0; 58 size_t preselected_candidates = 0; 59 60 size_t total_garbage = 0; 61 62 size_t immediate_garbage = 0; 63 size_t immediate_regions = 0; 64 65 size_t free = 0; 66 size_t free_regions = 0; 67 68 const uint tenuring_threshold = heap->age_census()->tenuring_threshold(); 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 77 for (size_t i = 0; i < num_regions; i++) { 78 ShenandoahHeapRegion* region = heap->get_region(i); 79 if (!_generation->contains(region)) { 80 continue; 81 } 82 size_t garbage = region->garbage(); 83 total_garbage += garbage; 84 if (region->is_empty()) { 85 free_regions++; 86 free += region_size_bytes; 87 } else if (region->is_regular()) { 88 if (!region->has_live()) { 89 // We can recycle it right away and put it in the free set. 90 immediate_regions++; 91 immediate_garbage += garbage; 92 region->make_trash_immediate(); 93 } else { 94 bool is_candidate; 95 // This is our candidate for later consideration. 96 if (collection_set->is_preselected(i)) { 97 assert(region->age() >= tenuring_threshold, "Preselection filter"); 98 is_candidate = true; 99 preselected_candidates++; 100 // Set garbage value to maximum value to force this into the sorted collection set. 101 garbage = region_size_bytes; 102 } else if (region->is_young() && (region->age() >= tenuring_threshold)) { 103 // Note that for GLOBAL GC, region may be OLD, and OLD regions do not qualify for pre-selection 104 105 // This region is old enough to be promoted but it was not preselected, either because its garbage is below 106 // ShenandoahOldGarbageThreshold so it will be promoted in place, or because there is not sufficient room 107 // in old gen to hold the evacuated copies of this region's live data. In both cases, we choose not to 108 // place this region into the collection set. 109 if (region->get_top_before_promote() != nullptr) { 110 // Region was included for promotion-in-place 111 regular_regions_promoted_in_place++; 112 regular_regions_promoted_usage += region->used_before_promote(); 113 } 114 is_candidate = false; 115 } else { 116 is_candidate = true; 117 } 118 if (is_candidate) { 119 candidates[cand_idx]._region = region; 120 candidates[cand_idx]._u._garbage = garbage; 121 cand_idx++; 122 } 123 } 124 } else if (region->is_humongous_start()) { 125 // Reclaim humongous regions here, and count them as the immediate garbage 126 #ifdef ASSERT 127 bool reg_live = region->has_live(); 128 bool bm_live = heap->complete_marking_context()->is_marked(cast_to_oop(region->bottom())); 129 assert(reg_live == bm_live, 130 "Humongous liveness and marks should agree. Region live: %s; Bitmap live: %s; Region Live Words: " SIZE_FORMAT, 131 BOOL_TO_STR(reg_live), BOOL_TO_STR(bm_live), region->get_live_data_words()); 132 #endif 133 if (!region->has_live()) { 134 heap->trash_humongous_region_at(region); 135 136 // Count only the start. Continuations would be counted on "trash" path 137 immediate_regions++; 138 immediate_garbage += garbage; 139 } else { 140 if (region->is_young() && region->age() >= tenuring_threshold) { 141 oop obj = cast_to_oop(region->bottom()); 142 size_t humongous_regions = ShenandoahHeapRegion::required_regions(obj->size() * HeapWordSize); 143 humongous_regions_promoted += humongous_regions; 144 } 145 } 146 } else if (region->is_trash()) { 147 // Count in just trashed collection set, during coalesced CM-with-UR 148 immediate_regions++; 149 immediate_garbage += garbage; 150 } 151 } 152 heap->old_generation()->set_expected_humongous_region_promotions(humongous_regions_promoted); 153 heap->old_generation()->set_expected_regular_region_promotions(regular_regions_promoted_in_place); 154 log_info(gc, ergo)("Planning to promote in place " SIZE_FORMAT " humongous regions and " SIZE_FORMAT 155 " regular regions, spanning a total of " SIZE_FORMAT " used bytes", 156 humongous_regions_promoted, regular_regions_promoted_in_place, 157 humongous_regions_promoted * ShenandoahHeapRegion::region_size_bytes() + 158 regular_regions_promoted_usage); 159 160 // Step 2. Look back at garbage statistics, and decide if we want to collect anything, 161 // given the amount of immediately reclaimable garbage. If we do, figure out the collection set. 162 163 assert (immediate_garbage <= total_garbage, 164 "Cannot have more immediate garbage than total garbage: " SIZE_FORMAT "%s vs " SIZE_FORMAT "%s", 165 byte_size_in_proper_unit(immediate_garbage), proper_unit_for_byte_size(immediate_garbage), 166 byte_size_in_proper_unit(total_garbage), proper_unit_for_byte_size(total_garbage)); 167 168 size_t immediate_percent = (total_garbage == 0) ? 0 : (immediate_garbage * 100 / total_garbage); 169 170 bool doing_promote_in_place = (humongous_regions_promoted + regular_regions_promoted_in_place > 0); 171 if (doing_promote_in_place || (preselected_candidates > 0) || (immediate_percent <= ShenandoahImmediateThreshold)) { 172 // Only young collections need to prime the collection set. 173 if (_generation->is_young()) { 174 heap->old_generation()->heuristics()->prime_collection_set(collection_set); 175 } 176 177 // Call the subclasses to add young-gen regions into the collection set. 178 choose_collection_set_from_regiondata(collection_set, candidates, cand_idx, immediate_garbage + free); 179 } 180 181 if (collection_set->has_old_regions()) { 182 heap->shenandoah_policy()->record_mixed_cycle(); 183 } 184 185 size_t cset_percent = (total_garbage == 0) ? 0 : (collection_set->garbage() * 100 / total_garbage); 186 size_t collectable_garbage = collection_set->garbage() + immediate_garbage; 187 size_t collectable_garbage_percent = (total_garbage == 0) ? 0 : (collectable_garbage * 100 / total_garbage); 188 189 log_info(gc, ergo)("Collectable Garbage: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), " 190 "Immediate: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), " SIZE_FORMAT " regions, " 191 "CSet: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), " SIZE_FORMAT " regions", 192 193 byte_size_in_proper_unit(collectable_garbage), 194 proper_unit_for_byte_size(collectable_garbage), 195 collectable_garbage_percent, 196 197 byte_size_in_proper_unit(immediate_garbage), 198 proper_unit_for_byte_size(immediate_garbage), 199 immediate_percent, 200 immediate_regions, 201 202 byte_size_in_proper_unit(collection_set->garbage()), 203 proper_unit_for_byte_size(collection_set->garbage()), 204 cset_percent, 205 collection_set->count()); 206 207 if (collection_set->garbage() > 0) { 208 size_t young_evac_bytes = collection_set->get_young_bytes_reserved_for_evacuation(); 209 size_t promote_evac_bytes = collection_set->get_young_bytes_to_be_promoted(); 210 size_t old_evac_bytes = collection_set->get_old_bytes_reserved_for_evacuation(); 211 size_t total_evac_bytes = young_evac_bytes + promote_evac_bytes + old_evac_bytes; 212 log_info(gc, ergo)("Evacuation Targets: YOUNG: " SIZE_FORMAT "%s, " 213 "PROMOTE: " SIZE_FORMAT "%s, " 214 "OLD: " SIZE_FORMAT "%s, " 215 "TOTAL: " SIZE_FORMAT "%s", 216 byte_size_in_proper_unit(young_evac_bytes), proper_unit_for_byte_size(young_evac_bytes), 217 byte_size_in_proper_unit(promote_evac_bytes), proper_unit_for_byte_size(promote_evac_bytes), 218 byte_size_in_proper_unit(old_evac_bytes), proper_unit_for_byte_size(old_evac_bytes), 219 byte_size_in_proper_unit(total_evac_bytes), proper_unit_for_byte_size(total_evac_bytes)); 220 } 221 } 222 223 224 size_t ShenandoahGenerationalHeuristics::add_preselected_regions_to_collection_set(ShenandoahCollectionSet* cset, 225 const RegionData* data, 226 size_t size) const { 227 #ifdef ASSERT 228 const uint tenuring_threshold = ShenandoahHeap::heap()->age_census()->tenuring_threshold(); 229 #endif 230 231 // cur_young_garbage represents the amount of memory to be reclaimed from young-gen. In the case that live objects 232 // are known to be promoted out of young-gen, we count this as cur_young_garbage because this memory is reclaimed 233 // from young-gen and becomes available to serve future young-gen allocation requests. 234 size_t cur_young_garbage = 0; 235 for (size_t idx = 0; idx < size; idx++) { 236 ShenandoahHeapRegion* r = data[idx]._region; 237 if (cset->is_preselected(r->index())) { 238 assert(r->age() >= tenuring_threshold, "Preselected regions must have tenure age"); 239 // Entire region will be promoted, This region does not impact young-gen or old-gen evacuation reserve. 240 // This region has been pre-selected and its impact on promotion reserve is already accounted for. 241 242 // r->used() is r->garbage() + r->get_live_data_bytes() 243 // Since all live data in this region is being evacuated from young-gen, it is as if this memory 244 // is garbage insofar as young-gen is concerned. Counting this as garbage reduces the need to 245 // reclaim highly utilized young-gen regions just for the sake of finding min_garbage to reclaim 246 // within young-gen memory. 247 248 cur_young_garbage += r->garbage(); 249 cset->add_region(r); 250 } 251 } 252 return cur_young_garbage; 253 } 254 255 void ShenandoahGenerationalHeuristics::log_cset_composition(ShenandoahCollectionSet* cset) const { 256 size_t collected_old = cset->get_old_bytes_reserved_for_evacuation(); 257 size_t collected_promoted = cset->get_young_bytes_to_be_promoted(); 258 size_t collected_young = cset->get_young_bytes_reserved_for_evacuation(); 259 260 log_info(gc, ergo)( 261 "Chosen CSet evacuates young: " SIZE_FORMAT "%s (of which at least: " SIZE_FORMAT "%s are to be promoted), " 262 "old: " SIZE_FORMAT "%s", 263 byte_size_in_proper_unit(collected_young), proper_unit_for_byte_size(collected_young), 264 byte_size_in_proper_unit(collected_promoted), proper_unit_for_byte_size(collected_promoted), 265 byte_size_in_proper_unit(collected_old), proper_unit_for_byte_size(collected_old)); 266 }