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/fullGCForwarding.hpp"
 28 #include "gc/shared/gcArguments.hpp"
 29 #include "gc/shared/tlab_globals.hpp"
 30 #include "gc/shared/workerPolicy.hpp"
 31 #include "gc/shenandoah/shenandoahArguments.hpp"
 32 #include "gc/shenandoah/shenandoahCardTable.hpp"
 33 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
 34 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp"
 35 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 36 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
 37 #include "runtime/globals_extension.hpp"
 38 #include "runtime/java.hpp"
 39 #include "utilities/defaultStream.hpp"
 40 #include "utilities/powerOfTwo.hpp"
 41 
 42 void ShenandoahArguments::initialize() {
 43 #if !(defined AARCH64 || defined AMD64 || defined PPC64 || defined RISCV64)
 44   vm_exit_during_initialization("Shenandoah GC is not supported on this platform.");
 45 #endif
 46 
 47 #if 0 // leave this block as stepping stone for future platforms
 48   log_warning(gc)("Shenandoah GC is not fully supported on this platform:");
 49   log_warning(gc)("  concurrent modes are not supported, only STW cycles are enabled;");
 50   log_warning(gc)("  arch-specific barrier code is not implemented, disabling barriers;");
 51 
 52   FLAG_SET_DEFAULT(ShenandoahGCHeuristics,           "passive");
 53 
 54   FLAG_SET_DEFAULT(ShenandoahSATBBarrier,            false);
 55   FLAG_SET_DEFAULT(ShenandoahLoadRefBarrier,         false);
 56   FLAG_SET_DEFAULT(ShenandoahCASBarrier,             false);
 57   FLAG_SET_DEFAULT(ShenandoahCardBarrier,            false);
 58   FLAG_SET_DEFAULT(ShenandoahCloneBarrier,           false);
 59 
 60   FLAG_SET_DEFAULT(ShenandoahVerifyOptoBarriers,     false);
 61 #endif
 62   if (UseLargePages) {
 63     size_t large_page_size = os::large_page_size();
 64     if ((align_up(MaxHeapSize, large_page_size) / large_page_size) < ShenandoahHeapRegion::MIN_NUM_REGIONS) {
 65       warning("Large pages size (%zuK) is too large to afford page-sized regions, disabling uncommit",
 66               os::large_page_size() / K);
 67       FLAG_SET_DEFAULT(ShenandoahUncommit, false);
 68     }
 69   }
 70 
 71   // Enable NUMA by default. While Shenandoah is not NUMA-aware, enabling NUMA makes
 72   // storage allocation code NUMA-aware.
 73   if (FLAG_IS_DEFAULT(UseNUMA)) {
 74     FLAG_SET_DEFAULT(UseNUMA, true);
 75   }
 76 
 77   // We use this as the time period for tracking minimum mutator utilization (MMU).
 78   // In generational mode, the MMU is used as a signal to adjust the size of the
 79   // young generation.
 80   if (FLAG_IS_DEFAULT(GCPauseIntervalMillis)) {
 81     FLAG_SET_DEFAULT(GCPauseIntervalMillis, 5000);
 82   }
 83 
 84   // Set up default number of concurrent threads. We want to have cycles complete fast
 85   // enough, but we also do not want to steal too much CPU from the concurrently running
 86   // application. Using 1/4 of available threads for concurrent GC seems a good
 87   // compromise here.
 88   bool ergo_conc = FLAG_IS_DEFAULT(ConcGCThreads);
 89   if (ergo_conc) {
 90     FLAG_SET_DEFAULT(ConcGCThreads, MAX2(1, os::initial_active_processor_count() / 4));
 91   }
 92 
 93   if (ConcGCThreads == 0) {
 94     vm_exit_during_initialization("Shenandoah expects ConcGCThreads > 0, check -XX:ConcGCThreads=#");
 95   }
 96 
 97   // Set up default number of parallel threads. We want to have decent pauses performance
 98   // which would use parallel threads, but we also do not want to do too many threads
 99   // that will overwhelm the OS scheduler. Using 1/2 of available threads seems to be a fair
100   // compromise here. Due to implementation constraints, it should not be lower than
101   // the number of concurrent threads.
102   bool ergo_parallel = FLAG_IS_DEFAULT(ParallelGCThreads);
103   if (ergo_parallel) {
104     FLAG_SET_DEFAULT(ParallelGCThreads, MAX2(1, os::initial_active_processor_count() / 2));
105   }
106 
107   if (ParallelGCThreads == 0) {
108     vm_exit_during_initialization("Shenandoah expects ParallelGCThreads > 0, check -XX:ParallelGCThreads=#");
109   }
110 
111   // Make sure ergonomic decisions do not break the thread count invariants.
112   // This may happen when user overrides one of the flags, but not the other.
113   // When that happens, we want to adjust the setting that was set ergonomically.
114   if (ParallelGCThreads < ConcGCThreads) {
115     if (ergo_conc && !ergo_parallel) {
116       FLAG_SET_DEFAULT(ConcGCThreads, ParallelGCThreads);
117     } else if (!ergo_conc && ergo_parallel) {
118       FLAG_SET_DEFAULT(ParallelGCThreads, ConcGCThreads);
119     } else if (ergo_conc && ergo_parallel) {
120       // Should not happen, check the ergonomic computation above. Fail with relevant error.
121       vm_exit_during_initialization("Shenandoah thread count ergonomic error");
122     } else {
123       // User settings error, report and ask user to rectify.
124       vm_exit_during_initialization("Shenandoah expects ConcGCThreads <= ParallelGCThreads, check -XX:ParallelGCThreads, -XX:ConcGCThreads");
125     }
126   }
127 
128   // Disable support for dynamic number of GC threads. We do not let the runtime
129   // heuristics to misjudge how many threads we need during the heavy concurrent phase
130   // or a GC pause.
131   if (UseDynamicNumberOfGCThreads) {
132     if (FLAG_IS_CMDLINE(UseDynamicNumberOfGCThreads)) {
133       warning("Shenandoah does not support UseDynamicNumberOfGCThreads, disabling");
134     }
135     FLAG_SET_DEFAULT(UseDynamicNumberOfGCThreads, false);
136   }
137 
138   if (ShenandoahRegionSampling && FLAG_IS_DEFAULT(PerfDataMemorySize)) {
139     // When sampling is enabled, max out the PerfData memory to get more
140     // Shenandoah data in, including Matrix.
141     FLAG_SET_DEFAULT(PerfDataMemorySize, 2048*K);
142   }
143 
144 #ifdef COMPILER2
145   // Shenandoah cares more about pause times, rather than raw throughput.
146   if (FLAG_IS_DEFAULT(UseCountedLoopSafepoints)) {
147     FLAG_SET_DEFAULT(UseCountedLoopSafepoints, true);
148     if (FLAG_IS_DEFAULT(LoopStripMiningIter)) {
149       FLAG_SET_DEFAULT(LoopStripMiningIter, 1000);
150     }
151   }
152 #ifdef ASSERT
153   // C2 barrier verification is only reliable when all default barriers are enabled
154   if (ShenandoahVerifyOptoBarriers &&
155           (!FLAG_IS_DEFAULT(ShenandoahSATBBarrier)            ||
156            !FLAG_IS_DEFAULT(ShenandoahLoadRefBarrier)         ||
157            !FLAG_IS_DEFAULT(ShenandoahCASBarrier)             ||
158            !FLAG_IS_DEFAULT(ShenandoahCloneBarrier)
159           )) {
160     warning("Unusual barrier configuration, disabling C2 barrier verification");
161     FLAG_SET_DEFAULT(ShenandoahVerifyOptoBarriers, false);
162   }
163 #else
164   guarantee(!ShenandoahVerifyOptoBarriers, "Should be disabled");
165 #endif // ASSERT
166 #endif // COMPILER2
167 
168   // Record more information about previous cycles for improved debugging pleasure
169   if (FLAG_IS_DEFAULT(LogEventsBufferEntries)) {
170     FLAG_SET_DEFAULT(LogEventsBufferEntries, 250);
171   }
172 
173   if ((InitialHeapSize == MaxHeapSize) && ShenandoahUncommit) {
174     log_info(gc)("Min heap equals to max heap, disabling ShenandoahUncommit");
175     FLAG_SET_DEFAULT(ShenandoahUncommit, false);
176   }
177 
178   // If class unloading is disabled, no unloading for concurrent cycles as well.
179   if (!ClassUnloading) {
180     FLAG_SET_DEFAULT(ClassUnloadingWithConcurrentMark, false);
181   }
182 
183   // TLAB sizing policy makes resizing decisions before each GC cycle. It averages
184   // historical data, assigning more recent data the weight according to TLABAllocationWeight.
185   // Current default is good for generational collectors that run frequent young GCs.
186   // With Shenandoah, GC cycles are much less frequent, so we need we need sizing policy
187   // to converge faster over smaller number of resizing decisions.
188   if (strcmp(ShenandoahGCMode, "generational") && FLAG_IS_DEFAULT(TLABAllocationWeight)) {
189     FLAG_SET_DEFAULT(TLABAllocationWeight, 90);
190   }
191   // In generational mode, let TLABAllocationWeight keeps its default value of 35.
192 
193   if (GCCardSizeInBytes < ShenandoahMinCardSizeInBytes) {
194     vm_exit_during_initialization(
195       err_msg("GCCardSizeInBytes ( %u ) must be >= %u\n", GCCardSizeInBytes, (unsigned int) ShenandoahMinCardSizeInBytes));
196   }
197 
198   // Gen shen does not support any ShenandoahGCHeuristics value except for the default "adaptive"
199   if ((strcmp(ShenandoahGCMode, "generational") == 0)
200       && strcmp(ShenandoahGCHeuristics, "adaptive") != 0) {
201     log_warning(gc)("Ignoring -XX:ShenandoahGCHeuristics input: %s, because generational shenandoah only"
202       " supports adaptive heuristics", ShenandoahGCHeuristics);
203   }
204 
205   FullGCForwarding::initialize_flags(MaxHeapSize);
206 }
207 
208 size_t ShenandoahArguments::conservative_max_heap_alignment() {
209   size_t align = next_power_of_2(ShenandoahMaxRegionSize);
210   if (UseLargePages) {
211     align = MAX2(align, os::large_page_size());
212   }
213   return align;
214 }
215 
216 void ShenandoahArguments::initialize_alignments() {
217   CardTable::initialize_card_size();
218 
219   // Need to setup sizes early to get correct alignments.
220   MaxHeapSize = ShenandoahHeapRegion::setup_sizes(MaxHeapSize);
221 
222   // This is expected by our algorithm for ShenandoahHeap::heap_region_containing().
223   size_t align = ShenandoahHeapRegion::region_size_bytes();
224   if (UseLargePages) {
225     align = MAX2(align, os::large_page_size());
226   }
227   SpaceAlignment = align;
228   HeapAlignment = align;
229 
230   if (FLAG_IS_DEFAULT(TLABSize)) {
231     TLABSize = MAX2(ShenandoahHeapRegion::region_size_bytes() / 256, (size_t) 32 * 1024);
232   }
233 }
234 
235 CollectedHeap* ShenandoahArguments::create_heap() {
236   if (strcmp(ShenandoahGCMode, "generational") != 0) {
237     // Not generational
238     return new ShenandoahHeap(new ShenandoahCollectorPolicy());
239   } else {
240     return new ShenandoahGenerationalHeap(new ShenandoahCollectorPolicy());
241   }
242 }