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/shenandoahGlobalHeuristics.hpp" 27 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp" 28 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp" 29 #include "gc/shenandoah/shenandoahGlobalGeneration.hpp" 30 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp" 31 #include "utilities/quickSort.hpp" 32 33 ShenandoahGlobalHeuristics::ShenandoahGlobalHeuristics(ShenandoahGlobalGeneration* generation) 34 : ShenandoahGenerationalHeuristics(generation) { 35 } 36 37 38 void ShenandoahGlobalHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset, 39 RegionData* data, size_t size, 40 size_t actual_free) { 41 // Better select garbage-first regions 42 QuickSort::sort<RegionData>(data, (int) size, compare_by_garbage); 43 44 choose_global_collection_set(cset, data, size, actual_free, 0 /* cur_young_garbage */); 45 46 log_cset_composition(cset); 47 } 48 49 50 void ShenandoahGlobalHeuristics::choose_global_collection_set(ShenandoahCollectionSet* cset, 51 const ShenandoahHeuristics::RegionData* data, 52 size_t size, size_t actual_free, 53 size_t cur_young_garbage) const { 54 auto heap = ShenandoahGenerationalHeap::heap(); 55 size_t region_size_bytes = ShenandoahHeapRegion::region_size_bytes(); 56 size_t capacity = heap->young_generation()->max_capacity(); 57 size_t garbage_threshold = region_size_bytes * ShenandoahGarbageThreshold / 100; 58 size_t ignore_threshold = region_size_bytes * ShenandoahIgnoreGarbageThreshold / 100; 59 const uint tenuring_threshold = heap->age_census()->tenuring_threshold(); 60 61 size_t young_evac_reserve = heap->young_generation()->get_evacuation_reserve(); 62 size_t old_evac_reserve = heap->old_generation()->get_evacuation_reserve(); 63 size_t max_young_cset = (size_t) (young_evac_reserve / ShenandoahEvacWaste); 64 size_t young_cur_cset = 0; 65 size_t max_old_cset = (size_t) (old_evac_reserve / ShenandoahOldEvacWaste); 66 size_t old_cur_cset = 0; 67 68 // Figure out how many unaffiliated young regions are dedicated to mutator and to evacuator. Allow the young 69 // collector's unaffiliated regions to be transferred to old-gen if old-gen has more easily reclaimed garbage 70 // than young-gen. At the end of this cycle, any excess regions remaining in old-gen will be transferred back 71 // to young. Do not transfer the mutator's unaffiliated regions to old-gen. Those must remain available 72 // to the mutator as it needs to be able to consume this memory during concurrent GC. 73 74 size_t unaffiliated_young_regions = heap->young_generation()->free_unaffiliated_regions(); 75 size_t unaffiliated_young_memory = unaffiliated_young_regions * region_size_bytes; 76 77 if (unaffiliated_young_memory > max_young_cset) { 78 size_t unaffiliated_mutator_memory = unaffiliated_young_memory - max_young_cset; 79 unaffiliated_young_memory -= unaffiliated_mutator_memory; 80 unaffiliated_young_regions = unaffiliated_young_memory / region_size_bytes; // round down 81 unaffiliated_young_memory = unaffiliated_young_regions * region_size_bytes; 82 } 83 84 // We'll affiliate these unaffiliated regions with either old or young, depending on need. 85 max_young_cset -= unaffiliated_young_memory; 86 87 // Keep track of how many regions we plan to transfer from young to old. 88 size_t regions_transferred_to_old = 0; 89 90 size_t free_target = (capacity * ShenandoahMinFreeThreshold) / 100 + max_young_cset; 91 size_t min_garbage = (free_target > actual_free) ? (free_target - actual_free) : 0; 92 93 log_info(gc, ergo)("Adaptive CSet Selection for GLOBAL. Max Young Evacuation: %zu" 94 "%s, Max Old Evacuation: %zu%s, Actual Free: %zu%s.", 95 byte_size_in_proper_unit(max_young_cset), proper_unit_for_byte_size(max_young_cset), 96 byte_size_in_proper_unit(max_old_cset), proper_unit_for_byte_size(max_old_cset), 97 byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free)); 98 99 for (size_t idx = 0; idx < size; idx++) { 100 ShenandoahHeapRegion* r = data[idx].get_region(); 101 assert(!cset->is_preselected(r->index()), "There should be no preselected regions during GLOBAL GC"); 102 bool add_region = false; 103 if (r->is_old() || (r->age() >= tenuring_threshold)) { 104 size_t new_cset = old_cur_cset + r->get_live_data_bytes(); 105 if ((r->garbage() > garbage_threshold)) { 106 while ((new_cset > max_old_cset) && (unaffiliated_young_regions > 0)) { 107 unaffiliated_young_regions--; 108 regions_transferred_to_old++; 109 max_old_cset += region_size_bytes / ShenandoahOldEvacWaste; 110 } 111 } 112 if ((new_cset <= max_old_cset) && (r->garbage() > garbage_threshold)) { 113 add_region = true; 114 old_cur_cset = new_cset; 115 } 116 } else { 117 assert(r->is_young() && (r->age() < tenuring_threshold), "DeMorgan's law (assuming r->is_affiliated)"); 118 size_t new_cset = young_cur_cset + r->get_live_data_bytes(); 119 size_t region_garbage = r->garbage(); 120 size_t new_garbage = cur_young_garbage + region_garbage; 121 bool add_regardless = (region_garbage > ignore_threshold) && (new_garbage < min_garbage); 122 123 if (add_regardless || (r->garbage() > garbage_threshold)) { 124 while ((new_cset > max_young_cset) && (unaffiliated_young_regions > 0)) { 125 unaffiliated_young_regions--; 126 max_young_cset += region_size_bytes / ShenandoahEvacWaste; 127 } 128 } 129 if ((new_cset <= max_young_cset) && (add_regardless || (region_garbage > garbage_threshold))) { 130 add_region = true; 131 young_cur_cset = new_cset; 132 cur_young_garbage = new_garbage; 133 } 134 } 135 if (add_region) { 136 cset->add_region(r); 137 } 138 } 139 140 if (regions_transferred_to_old > 0) { 141 heap->generation_sizer()->force_transfer_to_old(regions_transferred_to_old); 142 heap->young_generation()->set_evacuation_reserve(young_evac_reserve - regions_transferred_to_old * region_size_bytes); 143 heap->old_generation()->set_evacuation_reserve(old_evac_reserve + regions_transferred_to_old * region_size_bytes); 144 } 145 }