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.hpp"
  27 #include "gc/shared/workerPolicy.hpp"
  28 #include "gc/shenandoah/shenandoahArguments.hpp"
  29 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  31 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  32 #include "utilities/defaultStream.hpp"
  33 
  34 void ShenandoahArguments::initialize() {
  35 #if !(defined AARCH64 || defined AMD64 || defined IA32)
  36   vm_exit_during_initialization("Shenandoah GC is not supported on this platform.");
  37 #endif
  38 
  39 #if 0 // leave this block as stepping stone for future platforms
  40   log_warning(gc)("Shenandoah GC is not fully supported on this platform:");
  41   log_warning(gc)("  concurrent modes are not supported, only STW cycles are enabled;");
  42   log_warning(gc)("  arch-specific barrier code is not implemented, disabling barriers;");
  43 
  44   FLAG_SET_DEFAULT(ShenandoahGCHeuristics,           "passive");
  45 
  46   FLAG_SET_DEFAULT(ShenandoahSATBBarrier,            false);
  47   FLAG_SET_DEFAULT(ShenandoahLoadRefBarrier,         false);
  48   FLAG_SET_DEFAULT(ShenandoahKeepAliveBarrier,       false);
  49   FLAG_SET_DEFAULT(ShenandoahStoreValEnqueueBarrier, false);
  50   FLAG_SET_DEFAULT(ShenandoahCASBarrier,             false);
  51   FLAG_SET_DEFAULT(ShenandoahCloneBarrier,           false);
  52 
  53   FLAG_SET_DEFAULT(ShenandoahVerifyOptoBarriers,     false);
  54 #endif
  55 
  56   if (UseLargePages && (MaxHeapSize / os::large_page_size()) < ShenandoahHeapRegion::MIN_NUM_REGIONS) {
  57     warning("Large pages size (" SIZE_FORMAT "K) is too large to afford page-sized regions, disabling uncommit",
  58             os::large_page_size() / K);
  59     FLAG_SET_DEFAULT(ShenandoahUncommit, false);
  60   }
  61 
  62   // Enable NUMA by default. While Shenandoah is not NUMA-aware, enabling NUMA makes
  63   // storage allocation code NUMA-aware, and NUMA interleaving makes the storage
  64   // allocated in consistent manner (interleaving) to minimize run-to-run variance.
  65   if (FLAG_IS_DEFAULT(UseNUMA)) {
  66     FLAG_SET_DEFAULT(UseNUMA, true);
  67     FLAG_SET_DEFAULT(UseNUMAInterleaving, true);
  68   }
  69 
  70   // Set up default number of concurrent threads. We want to have cycles complete fast
  71   // enough, but we also do not want to steal too much CPU from the concurrently running
  72   // application. Using 1/4 of available threads for concurrent GC seems a good
  73   // compromise here.
  74   if (FLAG_IS_DEFAULT(ConcGCThreads)) {
  75     FLAG_SET_DEFAULT(ConcGCThreads, MAX2(1, os::processor_count() / 4));
  76   }
  77 
  78   if (ConcGCThreads == 0) {
  79     vm_exit_during_initialization("Shenandoah expects ConcGCThreads > 0, check -XX:ConcGCThreads=#");
  80   }
  81 
  82   // Set up default number of parallel threads. We want to have decent pauses performance
  83   // which would use parallel threads, but we also do not want to do too many threads
  84   // that will overwhelm the OS scheduler. Using 1/2 of available threads seems to be a fair
  85   // compromise here. Due to implementation constraints, it should not be lower than
  86   // the number of concurrent threads.
  87   if (FLAG_IS_DEFAULT(ParallelGCThreads)) {
  88     FLAG_SET_DEFAULT(ParallelGCThreads, MAX2(1, os::processor_count() / 2));
  89   }
  90 
  91   if (ParallelGCThreads == 0) {
  92     vm_exit_during_initialization("Shenandoah expects ParallelGCThreads > 0, check -XX:ParallelGCThreads=#");
  93   }
  94 
  95   if (ParallelGCThreads < ConcGCThreads) {
  96     warning("Shenandoah expects ConcGCThreads <= ParallelGCThreads, adjusting ParallelGCThreads automatically");
  97     FLAG_SET_DEFAULT(ParallelGCThreads, ConcGCThreads);
  98   }
  99 
 100   if (FLAG_IS_DEFAULT(ParallelRefProcEnabled)) {
 101     FLAG_SET_DEFAULT(ParallelRefProcEnabled, true);
 102   }
 103 
 104   if (ShenandoahRegionSampling && FLAG_IS_DEFAULT(PerfDataMemorySize)) {
 105     // When sampling is enabled, max out the PerfData memory to get more
 106     // Shenandoah data in, including Matrix.
 107     FLAG_SET_DEFAULT(PerfDataMemorySize, 2048*K);
 108   }
 109 
 110 #ifdef COMPILER2
 111   // Shenandoah cares more about pause times, rather than raw throughput.
 112   if (FLAG_IS_DEFAULT(UseCountedLoopSafepoints)) {
 113     FLAG_SET_DEFAULT(UseCountedLoopSafepoints, true);
 114     if (FLAG_IS_DEFAULT(LoopStripMiningIter)) {
 115       FLAG_SET_DEFAULT(LoopStripMiningIter, 1000);
 116     }
 117   }
 118 #ifdef ASSERT
 119   // C2 barrier verification is only reliable when all default barriers are enabled
 120   if (ShenandoahVerifyOptoBarriers &&
 121           (!FLAG_IS_DEFAULT(ShenandoahSATBBarrier)            ||
 122            !FLAG_IS_DEFAULT(ShenandoahLoadRefBarrier)         ||
 123            !FLAG_IS_DEFAULT(ShenandoahKeepAliveBarrier)       ||
 124            !FLAG_IS_DEFAULT(ShenandoahStoreValEnqueueBarrier) ||
 125            !FLAG_IS_DEFAULT(ShenandoahCASBarrier)             ||
 126            !FLAG_IS_DEFAULT(ShenandoahCloneBarrier)
 127           )) {
 128     warning("Unusual barrier configuration, disabling C2 barrier verification");
 129     FLAG_SET_DEFAULT(ShenandoahVerifyOptoBarriers, false);
 130   }
 131 #else
 132   guarantee(!ShenandoahVerifyOptoBarriers, "Should be disabled");
 133 #endif // ASSERT
 134 #endif // COMPILER2
 135 
 136   if (AlwaysPreTouch) {
 137     // Shenandoah handles pre-touch on its own. It does not let the
 138     // generic storage code to do the pre-touch before Shenandoah has
 139     // a chance to do it on its own.
 140     FLAG_SET_DEFAULT(AlwaysPreTouch, false);
 141     FLAG_SET_DEFAULT(ShenandoahAlwaysPreTouch, true);
 142   }
 143 
 144   // Record more information about previous cycles for improved debugging pleasure
 145   if (FLAG_IS_DEFAULT(LogEventsBufferEntries)) {
 146     FLAG_SET_DEFAULT(LogEventsBufferEntries, 250);
 147   }
 148 
 149   if (ShenandoahAlwaysPreTouch) {
 150     if (!FLAG_IS_DEFAULT(ShenandoahUncommit)) {
 151       warning("AlwaysPreTouch is enabled, disabling ShenandoahUncommit");
 152     }
 153     FLAG_SET_DEFAULT(ShenandoahUncommit, false);
 154   }
 155 
 156   if ((InitialHeapSize == MaxHeapSize) && ShenandoahUncommit) {
 157     log_info(gc)("Min heap equals to max heap, disabling ShenandoahUncommit");
 158     FLAG_SET_DEFAULT(ShenandoahUncommit, false);
 159   }
 160 
 161   // If class unloading is disabled, no unloading for concurrent cycles as well.
 162   // If class unloading is enabled, users should opt-in for unloading during
 163   // concurrent cycles.
 164   if (!ClassUnloading || !FLAG_IS_CMDLINE(ClassUnloadingWithConcurrentMark)) {
 165     log_info(gc)("Consider -XX:+ClassUnloadingWithConcurrentMark if large pause times "
 166                  "are observed on class-unloading sensitive workloads");
 167     FLAG_SET_DEFAULT(ClassUnloadingWithConcurrentMark, false);
 168   }
 169 
 170   // AOT is not supported yet
 171   if (UseAOT) {
 172     if (!FLAG_IS_DEFAULT(UseAOT)) {
 173       warning("Shenandoah does not support AOT at this moment, disabling UseAOT");
 174     }
 175     FLAG_SET_DEFAULT(UseAOT, false);
 176   }
 177 
 178   // TLAB sizing policy makes resizing decisions before each GC cycle. It averages
 179   // historical data, assigning more recent data the weight according to TLABAllocationWeight.
 180   // Current default is good for generational collectors that run frequent young GCs.
 181   // With Shenandoah, GC cycles are much less frequent, so we need we need sizing policy
 182   // to converge faster over smaller number of resizing decisions.
 183   if (FLAG_IS_DEFAULT(TLABAllocationWeight)) {
 184     FLAG_SET_DEFAULT(TLABAllocationWeight, 90);
 185   }
 186 
 187   // Shenandoah needs more C2 nodes to compile some methods with lots of barriers.
 188   // NodeLimitFudgeFactor needs to stay the same relative to MaxNodeLimit.
 189 #ifdef COMPILER2
 190   if (FLAG_IS_DEFAULT(MaxNodeLimit)) {
 191     FLAG_SET_DEFAULT(MaxNodeLimit, MaxNodeLimit * 3);
 192     FLAG_SET_DEFAULT(NodeLimitFudgeFactor, NodeLimitFudgeFactor * 3);
 193   }
 194 #endif
 195 
 196   // Make sure safepoint deadlocks are failing predictably. This sets up VM to report
 197   // fatal error after 10 seconds of wait for safepoint syncronization (not the VM
 198   // operation itself). There is no good reason why Shenandoah would spend that
 199   // much time synchronizing.
 200 #ifdef ASSERT
 201   FLAG_SET_DEFAULT(SafepointTimeout, true);
 202   FLAG_SET_DEFAULT(SafepointTimeoutDelay, 10000);
 203   FLAG_SET_DEFAULT(AbortVMOnSafepointTimeout, true);
 204 #endif
 205 }
 206 
 207 size_t ShenandoahArguments::conservative_max_heap_alignment() {
 208   size_t align = ShenandoahMaxRegionSize;
 209   if (UseLargePages) {
 210     align = MAX2(align, os::large_page_size());
 211   }
 212   return align;
 213 }
 214 
 215 void ShenandoahArguments::initialize_alignments() {
 216   // Need to setup sizes early to get correct alignments.
 217   ShenandoahHeapRegion::setup_sizes(MaxHeapSize);
 218 
 219   // This is expected by our algorithm for ShenandoahHeap::heap_region_containing().
 220   size_t align = ShenandoahHeapRegion::region_size_bytes();
 221   if (UseLargePages) {
 222     align = MAX2(align, os::large_page_size());
 223   }
 224   SpaceAlignment = align;
 225   HeapAlignment = align;
 226 }
 227 
 228 CollectedHeap* ShenandoahArguments::create_heap() {
 229   return new ShenandoahHeap(new ShenandoahCollectorPolicy());
 230 }