1 /*
2 * Copyright (c) 2018, 2022, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 *
7 * This code is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 only, as
9 * published by the Free Software Foundation.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 */
26
27 #include "gc/shared/gcArguments.hpp"
28 #include "gc/shared/tlab_globals.hpp"
29 #include "gc/shared/workerPolicy.hpp"
30 #include "gc/shenandoah/shenandoahArguments.hpp"
31 #include "gc/shenandoah/shenandoahCardTable.hpp"
32 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
33 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp"
34 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
35 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
36 #include "runtime/globals_extension.hpp"
37 #include "runtime/java.hpp"
38 #include "utilities/defaultStream.hpp"
39 #include "utilities/powerOfTwo.hpp"
40
41 void ShenandoahArguments::initialize() {
42 #if !(defined AARCH64 || defined AMD64 || defined PPC64 || defined RISCV64)
43 vm_exit_during_initialization("Shenandoah GC is not supported on this platform.");
44 #endif
45
46 // Shenandoah relies on the object header bits (including the self-forwarded bit
47 // at markWord::self_fwd_mask_in_place) being preserved across monitor inflation,
48 // which only holds with UseObjectMonitorTable.
49 if (!UseObjectMonitorTable) {
50 if (FLAG_IS_CMDLINE(UseObjectMonitorTable)) {
51 vm_exit_during_initialization("Shenandoah requires UseObjectMonitorTable");
52 }
53 FLAG_SET_DEFAULT(UseObjectMonitorTable, true);
54 }
55
56 #if 0 // leave this block as stepping stone for future platforms
57 log_warning(gc)("Shenandoah GC is not fully supported on this platform:");
58 log_warning(gc)(" concurrent modes are not supported, only STW cycles are enabled;");
59 log_warning(gc)(" arch-specific barrier code is not implemented, disabling barriers;");
60
61 FLAG_SET_DEFAULT(ShenandoahGCHeuristics, "passive");
62
63 FLAG_SET_DEFAULT(ShenandoahSATBBarrier, false);
64 FLAG_SET_DEFAULT(ShenandoahLoadRefBarrier, false);
65 FLAG_SET_DEFAULT(ShenandoahCASBarrier, false);
66 FLAG_SET_DEFAULT(ShenandoahCardBarrier, false);
67 FLAG_SET_DEFAULT(ShenandoahCloneBarrier, false);
68
69 FLAG_SET_DEFAULT(ShenandoahVerifyOptoBarriers, false);
70 #endif
71 if (UseLargePages) {
72 size_t large_page_size = os::large_page_size();
73 if ((align_up(MaxHeapSize, large_page_size) / large_page_size) < ShenandoahHeapRegion::MIN_NUM_REGIONS) {
74 warning("Large pages size (%zuK) is too large to afford page-sized regions, disabling uncommit",
75 os::large_page_size() / K);
76 FLAG_SET_DEFAULT(ShenandoahUncommit, false);
77 }
78 }
79
80 // Enable NUMA by default. While Shenandoah is not NUMA-aware, enabling NUMA makes
81 // storage allocation code NUMA-aware.
82 if (FLAG_IS_DEFAULT(UseNUMA)) {
83 FLAG_SET_DEFAULT(UseNUMA, true);
84 }
85
86 // We use this as the time period for tracking minimum mutator utilization (MMU).
87 // In generational mode, the MMU is used as a signal to adjust the size of the
88 // young generation.
89 if (FLAG_IS_DEFAULT(GCPauseIntervalMillis)) {
90 FLAG_SET_DEFAULT(GCPauseIntervalMillis, 5000);
91 }
92
93 // Set up default number of concurrent threads. We want to have cycles complete fast
94 // enough, but we also do not want to steal too much CPU from the concurrently running
95 // application. Using 1/4 of available threads for concurrent GC seems a good
96 // compromise here.
97 bool ergo_conc = FLAG_IS_DEFAULT(ConcGCThreads);
98 if (ergo_conc) {
99 FLAG_SET_DEFAULT(ConcGCThreads, MAX2(1, os::initial_active_processor_count() / 4));
100 }
101
102 if (ConcGCThreads == 0) {
103 vm_exit_during_initialization("Shenandoah expects ConcGCThreads > 0, check -XX:ConcGCThreads=#");
104 }
105
106 // Set up default number of parallel threads. We want to have decent pauses performance
107 // which would use parallel threads, but we also do not want to do too many threads
108 // that will overwhelm the OS scheduler. Using 1/2 of available threads seems to be a fair
109 // compromise here. Due to implementation constraints, it should not be lower than
110 // the number of concurrent threads.
111 bool ergo_parallel = FLAG_IS_DEFAULT(ParallelGCThreads);
112 if (ergo_parallel) {
113 FLAG_SET_DEFAULT(ParallelGCThreads, MAX2(1, os::initial_active_processor_count() / 2));
114 }
115
116 if (ParallelGCThreads == 0) {
117 vm_exit_during_initialization("Shenandoah expects ParallelGCThreads > 0, check -XX:ParallelGCThreads=#");
118 }
119
120 // Make sure ergonomic decisions do not break the thread count invariants.
121 // This may happen when user overrides one of the flags, but not the other.
122 // When that happens, we want to adjust the setting that was set ergonomically.
123 if (ParallelGCThreads < ConcGCThreads) {
124 if (ergo_conc && !ergo_parallel) {
125 FLAG_SET_DEFAULT(ConcGCThreads, ParallelGCThreads);
126 } else if (!ergo_conc && ergo_parallel) {
127 FLAG_SET_DEFAULT(ParallelGCThreads, ConcGCThreads);
128 } else if (ergo_conc && ergo_parallel) {
129 // Should not happen, check the ergonomic computation above. Fail with relevant error.
130 vm_exit_during_initialization("Shenandoah thread count ergonomic error");
131 } else {
132 // User settings error, report and ask user to rectify.
133 vm_exit_during_initialization("Shenandoah expects ConcGCThreads <= ParallelGCThreads, check -XX:ParallelGCThreads, -XX:ConcGCThreads");
134 }
135 }
136
137 // Disable support for dynamic number of GC threads. We do not let the runtime
138 // heuristics to misjudge how many threads we need during the heavy concurrent phase
139 // or a GC pause.
140 if (UseDynamicNumberOfGCThreads) {
141 if (FLAG_IS_CMDLINE(UseDynamicNumberOfGCThreads)) {
142 warning("Shenandoah does not support UseDynamicNumberOfGCThreads, disabling");
143 }
144 FLAG_SET_DEFAULT(UseDynamicNumberOfGCThreads, false);
145 }
146
147 if (ShenandoahRegionSampling && FLAG_IS_DEFAULT(PerfDataMemorySize)) {
148 // When sampling is enabled, max out the PerfData memory to get more
149 // Shenandoah data in, including Matrix.
150 FLAG_SET_DEFAULT(PerfDataMemorySize, 2048*K);
151 }
152
153 #ifdef COMPILER2
154 // Shenandoah cares more about pause times, rather than raw throughput.
155 if (FLAG_IS_DEFAULT(UseCountedLoopSafepoints)) {
156 FLAG_SET_DEFAULT(UseCountedLoopSafepoints, true);
157 if (FLAG_IS_DEFAULT(LoopStripMiningIter)) {
158 FLAG_SET_DEFAULT(LoopStripMiningIter, 1000);
159 }
160 }
161 #ifdef ASSERT
162 // C2 barrier verification is only reliable when all default barriers are enabled
163 if (ShenandoahVerifyOptoBarriers &&
164 (!FLAG_IS_DEFAULT(ShenandoahSATBBarrier) ||
165 !FLAG_IS_DEFAULT(ShenandoahLoadRefBarrier) ||
166 !FLAG_IS_DEFAULT(ShenandoahCASBarrier) ||
167 !FLAG_IS_DEFAULT(ShenandoahCloneBarrier)
168 )) {
169 warning("Unusual barrier configuration, disabling C2 barrier verification");
170 FLAG_SET_DEFAULT(ShenandoahVerifyOptoBarriers, false);
171 }
172 #else
173 guarantee(!ShenandoahVerifyOptoBarriers, "Should be disabled");
174 #endif // ASSERT
175 #endif // COMPILER2
176
177 // Record more information about previous cycles for improved debugging pleasure
178 if (FLAG_IS_DEFAULT(LogEventsBufferEntries)) {
179 FLAG_SET_DEFAULT(LogEventsBufferEntries, 250);
180 }
181
182 if ((InitialHeapSize == MaxHeapSize) && ShenandoahUncommit) {
183 log_info(gc)("Min heap equals to max heap, disabling ShenandoahUncommit");
184 FLAG_SET_DEFAULT(ShenandoahUncommit, false);
185 }
186
187 // If class unloading is disabled, no unloading for concurrent cycles as well.
188 if (!ClassUnloading) {
189 FLAG_SET_DEFAULT(ClassUnloadingWithConcurrentMark, false);
190 }
191
192 // TLAB sizing policy makes resizing decisions before each GC cycle. It averages
193 // historical data, assigning more recent data the weight according to TLABAllocationWeight.
194 // Current default is good for generational collectors that run frequent young GCs.
195 // With Shenandoah, GC cycles are much less frequent, so we need we need sizing policy
196 // to converge faster over smaller number of resizing decisions.
197 if (strcmp(ShenandoahGCMode, "generational") && FLAG_IS_DEFAULT(TLABAllocationWeight)) {
198 FLAG_SET_DEFAULT(TLABAllocationWeight, 90);
199 }
200 // In generational mode, let TLABAllocationWeight keeps its default value of 35.
201
202 if (GCCardSizeInBytes < ShenandoahMinCardSizeInBytes) {
203 vm_exit_during_initialization(
204 err_msg("GCCardSizeInBytes ( %u ) must be >= %u\n", GCCardSizeInBytes, (unsigned int) ShenandoahMinCardSizeInBytes));
205 }
206
207 // Gen shen does not support any ShenandoahGCHeuristics value except for the default "adaptive"
208 if ((strcmp(ShenandoahGCMode, "generational") == 0)
209 && strcmp(ShenandoahGCHeuristics, "adaptive") != 0) {
210 log_warning(gc)("Ignoring -XX:ShenandoahGCHeuristics input: %s, because generational shenandoah only"
211 " supports adaptive heuristics", ShenandoahGCHeuristics);
212 FLAG_SET_ERGO(ShenandoahGCHeuristics, "adaptive");
213 }
214 }
215
216 size_t ShenandoahArguments::conservative_max_heap_alignment() {
217 static_assert(is_power_of_2(ShenandoahHeapRegion::MAX_REGION_SIZE), "Max region size must be a power of 2.");
218 size_t align = ShenandoahHeapRegion::MAX_REGION_SIZE;
219 if (UseLargePages) {
220 align = MAX2(align, os::large_page_size());
221 }
222 return align;
223 }
224
225 void ShenandoahArguments::initialize_alignments() {
226 CardTable::initialize_card_size();
227
228 // Need to setup sizes early to get correct alignments.
229 MaxHeapSize = ShenandoahHeapRegion::setup_sizes(MaxHeapSize);
230
231 // This is expected by our algorithm for ShenandoahHeap::heap_region_containing().
232 size_t align = ShenandoahHeapRegion::region_size_bytes();
233 if (UseLargePages) {
234 align = MAX2(align, os::large_page_size());
235 }
236 SpaceAlignment = align;
237 HeapAlignment = align;
238
239 if (FLAG_IS_DEFAULT(TLABSize)) {
240 TLABSize = MAX2(ShenandoahHeapRegion::region_size_bytes() / 256, (size_t) 32 * 1024);
241 }
242 }
243
244 CollectedHeap* ShenandoahArguments::create_heap() {
245 if (strcmp(ShenandoahGCMode, "generational") != 0) {
246 // Not generational
247 return new ShenandoahHeap(new ShenandoahCollectorPolicy());
248 } else {
249 return new ShenandoahGenerationalHeap(new ShenandoahCollectorPolicy());
250 }
251 }