1 /* 2 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. 3 * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "gc/shenandoah/heuristics/shenandoahOldHeuristics.hpp" 27 #include "gc/shenandoah/heuristics/shenandoahYoungHeuristics.hpp" 28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp" 29 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp" 30 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp" 31 #include "gc/shenandoah/shenandoahOldGeneration.hpp" 32 #include "gc/shenandoah/shenandoahYoungGeneration.hpp" 33 #include "utilities/quickSort.hpp" 34 35 ShenandoahYoungHeuristics::ShenandoahYoungHeuristics(ShenandoahYoungGeneration* generation) 36 : ShenandoahGenerationalHeuristics(generation) { 37 } 38 39 40 void ShenandoahYoungHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset, 41 RegionData* data, size_t size, 42 size_t actual_free) { 43 // See comments in ShenandoahAdaptiveHeuristics::choose_collection_set_from_regiondata(): 44 // we do the same here, but with the following adjustments for generational mode: 45 // 46 // In generational mode, the sort order within the data array is not strictly descending amounts 47 // of garbage. In particular, regions that have reached tenure age will be sorted into this 48 // array before younger regions that typically contain more garbage. This is one reason why, 49 // for example, we continue examining regions even after rejecting a region that has 50 // more live data than we can evacuate. 51 52 // Better select garbage-first regions 53 QuickSort::sort<RegionData>(data, (int) size, compare_by_garbage); 54 55 size_t cur_young_garbage = add_preselected_regions_to_collection_set(cset, data, size); 56 57 choose_young_collection_set(cset, data, size, actual_free, cur_young_garbage); 58 59 log_cset_composition(cset); 60 } 61 62 void ShenandoahYoungHeuristics::choose_young_collection_set(ShenandoahCollectionSet* cset, 63 const RegionData* data, 64 size_t size, size_t actual_free, 65 size_t cur_young_garbage) const { 66 67 auto heap = ShenandoahGenerationalHeap::heap(); 68 69 size_t capacity = heap->young_generation()->max_capacity(); 70 size_t garbage_threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahGarbageThreshold / 100; 71 size_t ignore_threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahIgnoreGarbageThreshold / 100; 72 const uint tenuring_threshold = heap->age_census()->tenuring_threshold(); 73 74 // This is young-gen collection or a mixed evacuation. 75 // If this is mixed evacuation, the old-gen candidate regions have already been added. 76 size_t max_cset = (size_t) (heap->young_generation()->get_evacuation_reserve() / ShenandoahEvacWaste); 77 size_t cur_cset = 0; 78 size_t free_target = (capacity * ShenandoahMinFreeThreshold) / 100 + max_cset; 79 size_t min_garbage = (free_target > actual_free) ? (free_target - actual_free) : 0; 80 81 82 log_info(gc, ergo)( 83 "Adaptive CSet Selection for YOUNG. Max Evacuation: %zu%s, Actual Free: %zu%s.", 84 byte_size_in_proper_unit(max_cset), proper_unit_for_byte_size(max_cset), 85 byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free)); 86 87 for (size_t idx = 0; idx < size; idx++) { 88 ShenandoahHeapRegion* r = data[idx].get_region(); 89 if (cset->is_preselected(r->index())) { 90 continue; 91 } 92 if (r->age() < tenuring_threshold) { 93 size_t new_cset = cur_cset + r->get_live_data_bytes(); 94 size_t region_garbage = r->garbage(); 95 size_t new_garbage = cur_young_garbage + region_garbage; 96 bool add_regardless = (region_garbage > ignore_threshold) && (new_garbage < min_garbage); 97 assert(r->is_young(), "Only young candidates expected in the data array"); 98 if ((new_cset <= max_cset) && (add_regardless || (region_garbage > garbage_threshold))) { 99 cur_cset = new_cset; 100 cur_young_garbage = new_garbage; 101 cset->add_region(r); 102 } 103 } 104 // Note that we do not add aged regions if they were not pre-selected. The reason they were not preselected 105 // is because there is not sufficient room in old-gen to hold their to-be-promoted live objects or because 106 // they are to be promoted in place. 107 } 108 } 109 110 111 bool ShenandoahYoungHeuristics::should_start_gc() { 112 auto heap = ShenandoahGenerationalHeap::heap(); 113 ShenandoahOldGeneration* old_generation = heap->old_generation(); 114 ShenandoahOldHeuristics* old_heuristics = old_generation->heuristics(); 115 116 // Checks that an old cycle has run for at least ShenandoahMinimumOldTimeMs before allowing a young cycle. 117 if (ShenandoahMinimumOldTimeMs > 0) { 118 if (old_generation->is_preparing_for_mark() || old_generation->is_concurrent_mark_in_progress()) { 119 size_t old_time_elapsed = size_t(old_heuristics->elapsed_cycle_time() * 1000); 120 if (old_time_elapsed < ShenandoahMinimumOldTimeMs) { 121 // Do not decline_trigger() when waiting for minimum quantum of Old-gen marking. It is not at our discretion 122 // to trigger at this time. 123 log_debug(gc)("Young heuristics declines to trigger because old_time_elapsed < ShenandoahMinimumOldTimeMs"); 124 return false; 125 } 126 } 127 } 128 129 // inherited triggers have already decided to start a cycle, so no further evaluation is required 130 if (ShenandoahAdaptiveHeuristics::should_start_gc()) { 131 return true; 132 } 133 134 // Get through promotions and mixed evacuations as quickly as possible. These cycles sometimes require significantly 135 // more time than traditional young-generation cycles so start them up as soon as possible. This is a "mitigation" 136 // for the reality that old-gen and young-gen activities are not truly "concurrent". If there is old-gen work to 137 // be done, we start up the young-gen GC threads so they can do some of this old-gen work. As implemented, promotion 138 // gets priority over old-gen marking. 139 size_t promo_expedite_threshold = percent_of(heap->young_generation()->max_capacity(), ShenandoahExpeditePromotionsThreshold); 140 size_t promo_potential = old_generation->get_promotion_potential(); 141 if (promo_potential > promo_expedite_threshold) { 142 // Detect unsigned arithmetic underflow 143 assert(promo_potential < heap->capacity(), "Sanity"); 144 log_trigger("Expedite promotion of " PROPERFMT, PROPERFMTARGS(promo_potential)); 145 accept_trigger(); 146 return true; 147 } 148 149 size_t mixed_candidates = old_heuristics->unprocessed_old_collection_candidates(); 150 if (mixed_candidates > ShenandoahExpediteMixedThreshold && !heap->is_concurrent_weak_root_in_progress()) { 151 // We need to run young GC in order to open up some free heap regions so we can finish mixed evacuations. 152 // If concurrent weak root processing is in progress, it means the old cycle has chosen mixed collection 153 // candidates, but has not completed. There is no point in trying to start the young cycle before the old 154 // cycle completes. 155 log_trigger("Expedite mixed evacuation of %zu regions", mixed_candidates); 156 accept_trigger(); 157 return true; 158 } 159 160 // Don't decline_trigger() here That was done in ShenandoahAdaptiveHeuristics::should_start_gc() 161 return false; 162 } 163 164 // Return a conservative estimate of how much memory can be allocated before we need to start GC. The estimate is based 165 // on memory that is currently available within young generation plus all of the memory that will be added to the young 166 // generation at the end of the current cycle (as represented by young_regions_to_be_reclaimed) and on the anticipated 167 // amount of time required to perform a GC. 168 size_t ShenandoahYoungHeuristics::bytes_of_allocation_runway_before_gc_trigger(size_t young_regions_to_be_reclaimed) { 169 size_t capacity = _space_info->max_capacity(); 170 size_t usage = _space_info->used(); 171 size_t available = (capacity > usage)? capacity - usage: 0; 172 size_t allocated = _space_info->bytes_allocated_since_gc_start(); 173 174 size_t available_young_collected = ShenandoahHeap::heap()->collection_set()->get_young_available_bytes_collected(); 175 size_t anticipated_available = 176 available + young_regions_to_be_reclaimed * ShenandoahHeapRegion::region_size_bytes() - available_young_collected; 177 size_t spike_headroom = capacity * ShenandoahAllocSpikeFactor / 100; 178 size_t penalties = capacity * _gc_time_penalties / 100; 179 180 double rate = _allocation_rate.sample(allocated); 181 182 // At what value of available, would avg and spike triggers occur? 183 // if allocation_headroom < avg_cycle_time * avg_alloc_rate, then we experience avg trigger 184 // if allocation_headroom < avg_cycle_time * rate, then we experience spike trigger if is_spiking 185 // 186 // allocation_headroom = 187 // 0, if penalties > available or if penalties + spike_headroom > available 188 // available - penalties - spike_headroom, otherwise 189 // 190 // so we trigger if available - penalties - spike_headroom < avg_cycle_time * avg_alloc_rate, which is to say 191 // available < avg_cycle_time * avg_alloc_rate + penalties + spike_headroom 192 // or if available < penalties + spike_headroom 193 // 194 // since avg_cycle_time * avg_alloc_rate > 0, the first test is sufficient to test both conditions 195 // 196 // thus, evac_slack_avg is MIN2(0, available - avg_cycle_time * avg_alloc_rate + penalties + spike_headroom) 197 // 198 // similarly, evac_slack_spiking is MIN2(0, available - avg_cycle_time * rate + penalties + spike_headroom) 199 // but evac_slack_spiking is only relevant if is_spiking, as defined below. 200 201 double avg_cycle_time = _gc_cycle_time_history->davg() + (_margin_of_error_sd * _gc_cycle_time_history->dsd()); 202 double avg_alloc_rate = _allocation_rate.upper_bound(_margin_of_error_sd); 203 size_t evac_slack_avg; 204 if (anticipated_available > avg_cycle_time * avg_alloc_rate + penalties + spike_headroom) { 205 evac_slack_avg = anticipated_available - (avg_cycle_time * avg_alloc_rate + penalties + spike_headroom); 206 } else { 207 // we have no slack because it's already time to trigger 208 evac_slack_avg = 0; 209 } 210 211 bool is_spiking = _allocation_rate.is_spiking(rate, _spike_threshold_sd); 212 size_t evac_slack_spiking; 213 if (is_spiking) { 214 if (anticipated_available > avg_cycle_time * rate + penalties + spike_headroom) { 215 evac_slack_spiking = anticipated_available - (avg_cycle_time * rate + penalties + spike_headroom); 216 } else { 217 // we have no slack because it's already time to trigger 218 evac_slack_spiking = 0; 219 } 220 } else { 221 evac_slack_spiking = evac_slack_avg; 222 } 223 224 size_t threshold = min_free_threshold(); 225 size_t evac_min_threshold = (anticipated_available > threshold)? anticipated_available - threshold: 0; 226 return MIN3(evac_slack_spiking, evac_slack_avg, evac_min_threshold); 227 }