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