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.inline.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
60 size_t young_evac_reserve = heap->young_generation()->get_evacuation_reserve();
61 size_t old_evac_reserve = heap->old_generation()->get_evacuation_reserve();
62 size_t max_young_cset = (size_t) (young_evac_reserve / ShenandoahEvacWaste);
63 size_t young_cur_cset = 0;
64 size_t max_old_cset = (size_t) (old_evac_reserve / ShenandoahOldEvacWaste);
65 size_t old_cur_cset = 0;
66
67 // Figure out how many unaffiliated young regions are dedicated to mutator and to evacuator. Allow the young
68 // collector's unaffiliated regions to be transferred to old-gen if old-gen has more easily reclaimed garbage
69 // than young-gen. At the end of this cycle, any excess regions remaining in old-gen will be transferred back
70 // to young. Do not transfer the mutator's unaffiliated regions to old-gen. Those must remain available
71 // to the mutator as it needs to be able to consume this memory during concurrent GC.
72
73 size_t unaffiliated_young_regions = heap->young_generation()->free_unaffiliated_regions();
74 size_t unaffiliated_young_memory = unaffiliated_young_regions * region_size_bytes;
75
76 if (unaffiliated_young_memory > max_young_cset) {
77 size_t unaffiliated_mutator_memory = unaffiliated_young_memory - max_young_cset;
78 unaffiliated_young_memory -= unaffiliated_mutator_memory;
79 unaffiliated_young_regions = unaffiliated_young_memory / region_size_bytes; // round down
80 unaffiliated_young_memory = unaffiliated_young_regions * region_size_bytes;
81 }
82
83 // We'll affiliate these unaffiliated regions with either old or young, depending on need.
84 max_young_cset -= unaffiliated_young_memory;
85
86 // Keep track of how many regions we plan to transfer from young to old.
87 size_t regions_transferred_to_old = 0;
88
89 size_t free_target = (capacity * ShenandoahMinFreeThreshold) / 100 + max_young_cset;
90 size_t min_garbage = (free_target > actual_free) ? (free_target - actual_free) : 0;
91
92 log_info(gc, ergo)("Adaptive CSet Selection for GLOBAL. Max Young Evacuation: %zu"
93 "%s, Max Old Evacuation: %zu%s, Actual Free: %zu%s.",
94 byte_size_in_proper_unit(max_young_cset), proper_unit_for_byte_size(max_young_cset),
95 byte_size_in_proper_unit(max_old_cset), proper_unit_for_byte_size(max_old_cset),
96 byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free));
97
98 for (size_t idx = 0; idx < size; idx++) {
99 ShenandoahHeapRegion* r = data[idx].get_region();
100 assert(!cset->is_preselected(r->index()), "There should be no preselected regions during GLOBAL GC");
101 bool add_region = false;
102 if (r->is_old() || heap->is_tenurable(r)) {
103 size_t new_cset = old_cur_cset + r->get_live_data_bytes();
104 if ((r->garbage() > garbage_threshold)) {
105 while ((new_cset > max_old_cset) && (unaffiliated_young_regions > 0)) {
106 unaffiliated_young_regions--;
107 regions_transferred_to_old++;
108 max_old_cset += region_size_bytes / ShenandoahOldEvacWaste;
109 }
110 }
111 if ((new_cset <= max_old_cset) && (r->garbage() > garbage_threshold)) {
112 add_region = true;
113 old_cur_cset = new_cset;
114 }
115 } else {
116 assert(r->is_young() && !heap->is_tenurable(r), "DeMorgan's law (assuming r->is_affiliated)");
117 size_t new_cset = young_cur_cset + r->get_live_data_bytes();
118 size_t region_garbage = r->garbage();
119 size_t new_garbage = cur_young_garbage + region_garbage;
120 bool add_regardless = (region_garbage > ignore_threshold) && (new_garbage < min_garbage);
121
122 if (add_regardless || (r->garbage() > garbage_threshold)) {
123 while ((new_cset > max_young_cset) && (unaffiliated_young_regions > 0)) {
124 unaffiliated_young_regions--;
125 max_young_cset += region_size_bytes / ShenandoahEvacWaste;
126 }
127 }
128 if ((new_cset <= max_young_cset) && (add_regardless || (region_garbage > garbage_threshold))) {
129 add_region = true;
130 young_cur_cset = new_cset;
131 cur_young_garbage = new_garbage;
132 }
133 }
134 if (add_region) {
135 cset->add_region(r);
136 }
137 }
138
139 if (regions_transferred_to_old > 0) {
140 heap->generation_sizer()->force_transfer_to_old(regions_transferred_to_old);
141 heap->young_generation()->set_evacuation_reserve(young_evac_reserve - regions_transferred_to_old * region_size_bytes);
142 heap->old_generation()->set_evacuation_reserve(old_evac_reserve + regions_transferred_to_old * region_size_bytes);
143 }
144 }