1 /*
   2  * Copyright (c) 2018, 2019, Red Hat, Inc. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/shared/gcArguments.inline.hpp"
  27 #include "gc/shenandoah/shenandoahArguments.hpp"
  28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  30 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  31 #include "utilities/defaultStream.hpp"
  32 
  33 void ShenandoahArguments::initialize() {
  34 #if !(defined AARCH64 || defined AMD64 || defined IA32)
  35   vm_exit_during_initialization("Shenandoah GC is not supported on this platform.");
  36 #endif
  37 
  38 #if 0 // leave this block as stepping stone for future platforms
  39   log_warning(gc)("Shenandoah GC is not fully supported on this platform:");
  40   log_warning(gc)("  concurrent modes are not supported, only STW cycles are enabled;");
  41   log_warning(gc)("  arch-specific barrier code is not implemented, disabling barriers;");
  42 
  43   FLAG_SET_DEFAULT(ShenandoahGCHeuristics,           "passive");
  44 
  45   FLAG_SET_DEFAULT(ShenandoahSATBBarrier,            false);
  46   FLAG_SET_DEFAULT(ShenandoahLoadRefBarrier,         false);
  47   FLAG_SET_DEFAULT(ShenandoahKeepAliveBarrier,       false);
  48   FLAG_SET_DEFAULT(ShenandoahStoreValEnqueueBarrier, false);
  49   FLAG_SET_DEFAULT(ShenandoahCASBarrier,             false);
  50   FLAG_SET_DEFAULT(ShenandoahCloneBarrier,           false);
  51 
  52   FLAG_SET_DEFAULT(ShenandoahVerifyOptoBarriers,     false);
  53 #endif
  54 
  55   if (UseLargePages && (MaxHeapSize / os::large_page_size()) < ShenandoahHeapRegion::MIN_NUM_REGIONS) {
  56     warning("Large pages size (" SIZE_FORMAT "K) is too large to afford page-sized regions, disabling uncommit",
  57             os::large_page_size() / K);
  58     FLAG_SET_DEFAULT(ShenandoahUncommit, false);
  59   }
  60 
  61   // Enable NUMA by default. While Shenandoah is not NUMA-aware, enabling NUMA makes
  62   // storage allocation code NUMA-aware.
  63   if (FLAG_IS_DEFAULT(UseNUMA)) {
  64     FLAG_SET_DEFAULT(UseNUMA, true);
  65   }
  66 
  67   // Set up default number of concurrent threads. We want to have cycles complete fast
  68   // enough, but we also do not want to steal too much CPU from the concurrently running
  69   // application. Using 1/4 of available threads for concurrent GC seems a good
  70   // compromise here.
  71   if (FLAG_IS_DEFAULT(ConcGCThreads)) {
  72     FLAG_SET_DEFAULT(ConcGCThreads, MAX2(1, os::processor_count() / 4));
  73   }
  74 
  75   if (ConcGCThreads == 0) {
  76     vm_exit_during_initialization("Shenandoah expects ConcGCThreads > 0, check -XX:ConcGCThreads=#");
  77   }
  78 
  79   // Set up default number of parallel threads. We want to have decent pauses performance
  80   // which would use parallel threads, but we also do not want to do too many threads
  81   // that will overwhelm the OS scheduler. Using 1/2 of available threads seems to be a fair
  82   // compromise here. Due to implementation constraints, it should not be lower than
  83   // the number of concurrent threads.
  84   if (FLAG_IS_DEFAULT(ParallelGCThreads)) {
  85     FLAG_SET_DEFAULT(ParallelGCThreads, MAX2(1, os::processor_count() / 2));
  86   }
  87 
  88   if (ParallelGCThreads == 0) {
  89     vm_exit_during_initialization("Shenandoah expects ParallelGCThreads > 0, check -XX:ParallelGCThreads=#");
  90   }
  91 
  92   if (ParallelGCThreads < ConcGCThreads) {
  93     warning("Shenandoah expects ConcGCThreads <= ParallelGCThreads, adjusting ParallelGCThreads automatically");
  94     FLAG_SET_DEFAULT(ParallelGCThreads, ConcGCThreads);
  95   }
  96 
  97   if (FLAG_IS_DEFAULT(ParallelRefProcEnabled)) {
  98     FLAG_SET_DEFAULT(ParallelRefProcEnabled, true);
  99   }
 100 
 101   if (ShenandoahRegionSampling && FLAG_IS_DEFAULT(PerfDataMemorySize)) {
 102     // When sampling is enabled, max out the PerfData memory to get more
 103     // Shenandoah data in, including Matrix.
 104     FLAG_SET_DEFAULT(PerfDataMemorySize, 2048*K);
 105   }
 106 
 107 #ifdef COMPILER2
 108   // Shenandoah cares more about pause times, rather than raw throughput.
 109   if (FLAG_IS_DEFAULT(UseCountedLoopSafepoints)) {
 110     FLAG_SET_DEFAULT(UseCountedLoopSafepoints, true);
 111     if (FLAG_IS_DEFAULT(LoopStripMiningIter)) {
 112       FLAG_SET_DEFAULT(LoopStripMiningIter, 1000);
 113     }
 114   }
 115 #ifdef ASSERT
 116   // C2 barrier verification is only reliable when all default barriers are enabled
 117   if (ShenandoahVerifyOptoBarriers &&
 118           (!FLAG_IS_DEFAULT(ShenandoahSATBBarrier)            ||
 119            !FLAG_IS_DEFAULT(ShenandoahLoadRefBarrier)         ||
 120            !FLAG_IS_DEFAULT(ShenandoahKeepAliveBarrier)       ||
 121            !FLAG_IS_DEFAULT(ShenandoahStoreValEnqueueBarrier) ||
 122            !FLAG_IS_DEFAULT(ShenandoahCASBarrier)             ||
 123            !FLAG_IS_DEFAULT(ShenandoahCloneBarrier)
 124           )) {
 125     warning("Unusual barrier configuration, disabling C2 barrier verification");
 126     FLAG_SET_DEFAULT(ShenandoahVerifyOptoBarriers, false);
 127   }
 128 #else
 129   guarantee(!ShenandoahVerifyOptoBarriers, "Should be disabled");
 130 #endif // ASSERT
 131 #endif // COMPILER2
 132 
 133   if (AlwaysPreTouch) {
 134     // Shenandoah handles pre-touch on its own. It does not let the
 135     // generic storage code to do the pre-touch before Shenandoah has
 136     // a chance to do it on its own.
 137     FLAG_SET_DEFAULT(AlwaysPreTouch, false);
 138     FLAG_SET_DEFAULT(ShenandoahAlwaysPreTouch, true);
 139   }
 140 
 141   // Shenandoah C2 optimizations apparently dislike the shape of thread-local handshakes.
 142   // Disable it by default, unless we enable it specifically for debugging.
 143   if (FLAG_IS_DEFAULT(ThreadLocalHandshakes)) {
 144     if (ThreadLocalHandshakes) {
 145       FLAG_SET_DEFAULT(ThreadLocalHandshakes, false);
 146     }
 147   } else {
 148     if (ThreadLocalHandshakes) {
 149       warning("Thread-local handshakes are not working correctly with Shenandoah at the moment. Enable at your own risk.");
 150     }
 151   }
 152 
 153   // Record more information about previous cycles for improved debugging pleasure
 154   if (FLAG_IS_DEFAULT(LogEventsBufferEntries)) {
 155     FLAG_SET_DEFAULT(LogEventsBufferEntries, 250);
 156   }
 157 
 158   if (ShenandoahAlwaysPreTouch) {
 159     if (!FLAG_IS_DEFAULT(ShenandoahUncommit)) {
 160       warning("AlwaysPreTouch is enabled, disabling ShenandoahUncommit");
 161     }
 162     FLAG_SET_DEFAULT(ShenandoahUncommit, false);
 163   }
 164 
 165   if ((InitialHeapSize == MaxHeapSize) && ShenandoahUncommit) {
 166     log_info(gc)("Min heap equals to max heap, disabling ShenandoahUncommit");
 167     FLAG_SET_DEFAULT(ShenandoahUncommit, false);
 168   }
 169 
 170   // If class unloading is disabled, no unloading for concurrent cycles as well.
 171   // If class unloading is enabled, users should opt-in for unloading during
 172   // concurrent cycles.
 173   if (!ClassUnloading || !FLAG_IS_CMDLINE(ClassUnloadingWithConcurrentMark)) {
 174     log_info(gc)("Consider -XX:+ClassUnloadingWithConcurrentMark if large pause times "
 175                  "are observed on class-unloading sensitive workloads");
 176     FLAG_SET_DEFAULT(ClassUnloadingWithConcurrentMark, false);
 177   }
 178 
 179   // AOT is not supported yet
 180   if (UseAOT) {
 181     if (!FLAG_IS_DEFAULT(UseAOT)) {
 182       warning("Shenandoah does not support AOT at this moment, disabling UseAOT");
 183     }
 184     FLAG_SET_DEFAULT(UseAOT, false);
 185   }
 186 
 187   // TLAB sizing policy makes resizing decisions before each GC cycle. It averages
 188   // historical data, assigning more recent data the weight according to TLABAllocationWeight.
 189   // Current default is good for generational collectors that run frequent young GCs.
 190   // With Shenandoah, GC cycles are much less frequent, so we need we need sizing policy
 191   // to converge faster over smaller number of resizing decisions.
 192   if (FLAG_IS_DEFAULT(TLABAllocationWeight)) {
 193     FLAG_SET_DEFAULT(TLABAllocationWeight, 90);
 194   }
 195 
 196   // Shenandoah needs more C2 nodes to compile some methods with lots of barriers.
 197   // NodeLimitFudgeFactor needs to stay the same relative to MaxNodeLimit.
 198 #ifdef COMPILER2
 199   if (FLAG_IS_DEFAULT(MaxNodeLimit)) {
 200     FLAG_SET_DEFAULT(MaxNodeLimit, MaxNodeLimit * 3);
 201     FLAG_SET_DEFAULT(NodeLimitFudgeFactor, NodeLimitFudgeFactor * 3);
 202   }
 203 #endif
 204 
 205   // Make sure safepoint deadlocks are failing predictably. This sets up VM to report
 206   // fatal error after 10 seconds of wait for safepoint syncronization (not the VM
 207   // operation itself). There is no good reason why Shenandoah would spend that
 208   // much time synchronizing.
 209 #ifdef ASSERT
 210   FLAG_SET_DEFAULT(SafepointTimeout, true);
 211   FLAG_SET_DEFAULT(SafepointTimeoutDelay, 10000);
 212   FLAG_SET_DEFAULT(AbortVMOnSafepointTimeout, true);
 213 #endif
 214 }
 215 
 216 size_t ShenandoahArguments::conservative_max_heap_alignment() {
 217   size_t align = ShenandoahMaxRegionSize;
 218   if (UseLargePages) {
 219     align = MAX2(align, os::large_page_size());
 220   }
 221   return align;
 222 }
 223 
 224 CollectedHeap* ShenandoahArguments::create_heap() {
 225   return create_heap_with_policy<ShenandoahHeap, ShenandoahCollectorPolicy>();
 226 }