1 /*
   2  * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
   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/cms/cmsArguments.hpp"
  27 #include "gc/cms/cmsCollectorPolicy.hpp"
  28 #include "gc/cms/cmsHeap.hpp"
  29 #include "gc/cms/compactibleFreeListSpace.hpp"
  30 #include "gc/shared/gcArguments.inline.hpp"
  31 #include "gc/shared/genCollectedHeap.hpp"
  32 #include "runtime/arguments.hpp"
  33 #include "runtime/globals.hpp"
  34 #include "runtime/globals_extension.hpp"
  35 #include "runtime/vm_version.hpp"
  36 #include "utilities/defaultStream.hpp"
  37 
  38 size_t CMSArguments::conservative_max_heap_alignment() {
  39   return GenCollectedHeap::conservative_max_heap_alignment();
  40 }
  41 
  42 void CMSArguments::set_parnew_gc_flags() {
  43   assert(!UseSerialGC && !UseParallelOldGC && !UseParallelGC && !UseG1GC,
  44          "control point invariant");
  45   assert(UseConcMarkSweepGC, "CMS is expected to be on here");
  46 
  47   if (FLAG_IS_DEFAULT(ParallelGCThreads)) {
  48     FLAG_SET_DEFAULT(ParallelGCThreads, Abstract_VM_Version::parallel_worker_threads());
  49     assert(ParallelGCThreads > 0, "We should always have at least one thread by default");
  50   } else if (ParallelGCThreads == 0) {
  51     jio_fprintf(defaultStream::error_stream(),
  52         "The ParNew GC can not be combined with -XX:ParallelGCThreads=0\n");
  53     vm_exit(1);
  54   }
  55 
  56   // By default YoungPLABSize and OldPLABSize are set to 4096 and 1024 respectively,
  57   // these settings are default for Parallel Scavenger. For ParNew+Tenured configuration
  58   // we set them to 1024 and 1024.
  59   // See CR 6362902.
  60   if (FLAG_IS_DEFAULT(YoungPLABSize)) {
  61     FLAG_SET_DEFAULT(YoungPLABSize, (intx)1024);
  62   }
  63   if (FLAG_IS_DEFAULT(OldPLABSize)) {
  64     FLAG_SET_DEFAULT(OldPLABSize, (intx)1024);
  65   }
  66 
  67   // When using compressed oops, we use local overflow stacks,
  68   // rather than using a global overflow list chained through
  69   // the klass word of the object's pre-image.
  70   if (UseCompressedOops && !ParGCUseLocalOverflow) {
  71     if (!FLAG_IS_DEFAULT(ParGCUseLocalOverflow)) {
  72       warning("Forcing +ParGCUseLocalOverflow: needed if using compressed references");
  73     }
  74     FLAG_SET_DEFAULT(ParGCUseLocalOverflow, true);
  75   }
  76   assert(ParGCUseLocalOverflow || !UseCompressedOops, "Error");
  77 }
  78 
  79 // Adjust some sizes to suit CMS and/or ParNew needs; these work well on
  80 // sparc/solaris for certain applications, but would gain from
  81 // further optimization and tuning efforts, and would almost
  82 // certainly gain from analysis of platform and environment.
  83 void CMSArguments::initialize() {
  84   GCArguments::initialize();
  85 
  86   assert(!UseSerialGC && !UseParallelOldGC && !UseParallelGC, "Error");
  87   assert(UseConcMarkSweepGC, "CMS is expected to be on here");
  88 
  89   if (AllocateOldGenAt != NULL) {
  90     vm_exit_during_initialization("The flag -XX:AllocateOldGenAt can not be used with CMS. Only ParallelOldGC and G1GC are supported.", NULL);
  91   }
  92 
  93   // CMS space iteration, which FLSVerifyAllHeapreferences entails,
  94   // insists that we hold the requisite locks so that the iteration is
  95   // MT-safe. For the verification at start-up and shut-down, we don't
  96   // yet have a good way of acquiring and releasing these locks,
  97   // which are not visible at the CollectedHeap level. We want to
  98   // be able to acquire these locks and then do the iteration rather
  99   // than just disable the lock verification. This will be fixed under
 100   // bug 4788986.
 101   if (UseConcMarkSweepGC && FLSVerifyAllHeapReferences) {
 102     if (VerifyDuringStartup) {
 103       warning("Heap verification at start-up disabled "
 104               "(due to current incompatibility with FLSVerifyAllHeapReferences)");
 105       VerifyDuringStartup = false; // Disable verification at start-up
 106     }
 107 
 108     if (VerifyBeforeExit) {
 109       warning("Heap verification at shutdown disabled "
 110               "(due to current incompatibility with FLSVerifyAllHeapReferences)");
 111       VerifyBeforeExit = false; // Disable verification at shutdown
 112     }
 113   }
 114 
 115   if (!ClassUnloading) {
 116     FLAG_SET_CMDLINE(bool, CMSClassUnloadingEnabled, false);
 117   }
 118 
 119   // Set CMS global values
 120   CompactibleFreeListSpace::set_cms_values();
 121 
 122   // Turn off AdaptiveSizePolicy by default for cms until it is complete.
 123   disable_adaptive_size_policy("UseConcMarkSweepGC");
 124 
 125   set_parnew_gc_flags();
 126 
 127   size_t max_heap = align_down(MaxHeapSize,
 128                                CardTableRS::ct_max_alignment_constraint());
 129 
 130   // Now make adjustments for CMS
 131   intx   tenuring_default = (intx)6;
 132   size_t young_gen_per_worker = CMSYoungGenPerWorker;
 133 
 134   // Preferred young gen size for "short" pauses:
 135   // upper bound depends on # of threads and NewRatio.
 136   const size_t preferred_max_new_size_unaligned =
 137     MIN2(max_heap/(NewRatio+1), ScaleForWordSize(young_gen_per_worker * ParallelGCThreads));
 138   size_t preferred_max_new_size =
 139     align_up(preferred_max_new_size_unaligned, os::vm_page_size());
 140 
 141   // Unless explicitly requested otherwise, size young gen
 142   // for "short" pauses ~ CMSYoungGenPerWorker*ParallelGCThreads
 143 
 144   // If either MaxNewSize or NewRatio is set on the command line,
 145   // assume the user is trying to set the size of the young gen.
 146   if (FLAG_IS_DEFAULT(MaxNewSize) && FLAG_IS_DEFAULT(NewRatio)) {
 147 
 148     // Set MaxNewSize to our calculated preferred_max_new_size unless
 149     // NewSize was set on the command line and it is larger than
 150     // preferred_max_new_size.
 151     if (!FLAG_IS_DEFAULT(NewSize)) {   // NewSize explicitly set at command-line
 152       FLAG_SET_ERGO(size_t, MaxNewSize, MAX2(NewSize, preferred_max_new_size));
 153     } else {
 154       FLAG_SET_ERGO(size_t, MaxNewSize, preferred_max_new_size);
 155     }
 156     log_trace(gc, heap)("CMS ergo set MaxNewSize: " SIZE_FORMAT, MaxNewSize);
 157 
 158     // Code along this path potentially sets NewSize and OldSize
 159     log_trace(gc, heap)("CMS set min_heap_size: " SIZE_FORMAT " initial_heap_size:  " SIZE_FORMAT " max_heap: " SIZE_FORMAT,
 160                         Arguments::min_heap_size(), InitialHeapSize, max_heap);
 161     size_t min_new = preferred_max_new_size;
 162     if (FLAG_IS_CMDLINE(NewSize)) {
 163       min_new = NewSize;
 164     }
 165     if (max_heap > min_new && Arguments::min_heap_size() > min_new) {
 166       // Unless explicitly requested otherwise, make young gen
 167       // at least min_new, and at most preferred_max_new_size.
 168       if (FLAG_IS_DEFAULT(NewSize)) {
 169         FLAG_SET_ERGO(size_t, NewSize, MAX2(NewSize, min_new));
 170         FLAG_SET_ERGO(size_t, NewSize, MIN2(preferred_max_new_size, NewSize));
 171         log_trace(gc, heap)("CMS ergo set NewSize: " SIZE_FORMAT, NewSize);
 172       }
 173       // Unless explicitly requested otherwise, size old gen
 174       // so it's NewRatio x of NewSize.
 175       if (FLAG_IS_DEFAULT(OldSize)) {
 176         if (max_heap > NewSize) {
 177           FLAG_SET_ERGO(size_t, OldSize, MIN2(NewRatio*NewSize, max_heap - NewSize));
 178           log_trace(gc, heap)("CMS ergo set OldSize: " SIZE_FORMAT, OldSize);
 179         }
 180       }
 181     }
 182   }
 183   // Unless explicitly requested otherwise, definitely
 184   // promote all objects surviving "tenuring_default" scavenges.
 185   if (FLAG_IS_DEFAULT(MaxTenuringThreshold) &&
 186       FLAG_IS_DEFAULT(SurvivorRatio)) {
 187     FLAG_SET_ERGO(uintx, MaxTenuringThreshold, tenuring_default);
 188   }
 189   // If we decided above (or user explicitly requested)
 190   // `promote all' (via MaxTenuringThreshold := 0),
 191   // prefer minuscule survivor spaces so as not to waste
 192   // space for (non-existent) survivors
 193   if (FLAG_IS_DEFAULT(SurvivorRatio) && MaxTenuringThreshold == 0) {
 194     FLAG_SET_ERGO(uintx, SurvivorRatio, MAX2((uintx)1024, SurvivorRatio));
 195   }
 196 
 197   // OldPLABSize is interpreted in CMS as not the size of the PLAB in words,
 198   // but rather the number of free blocks of a given size that are used when
 199   // replenishing the local per-worker free list caches.
 200   if (FLAG_IS_DEFAULT(OldPLABSize)) {
 201     if (!FLAG_IS_DEFAULT(ResizeOldPLAB) && !ResizeOldPLAB) {
 202       // OldPLAB sizing manually turned off: Use a larger default setting,
 203       // unless it was manually specified. This is because a too-low value
 204       // will slow down scavenges.
 205       FLAG_SET_ERGO(size_t, OldPLABSize, CompactibleFreeListSpaceLAB::_default_static_old_plab_size); // default value before 6631166
 206     } else {
 207       FLAG_SET_DEFAULT(OldPLABSize, CompactibleFreeListSpaceLAB::_default_dynamic_old_plab_size); // old CMSParPromoteBlocksToClaim default
 208     }
 209   }
 210 
 211   // If either of the static initialization defaults have changed, note this
 212   // modification.
 213   if (!FLAG_IS_DEFAULT(OldPLABSize) || !FLAG_IS_DEFAULT(OldPLABWeight)) {
 214     CompactibleFreeListSpaceLAB::modify_initialization(OldPLABSize, OldPLABWeight);
 215   }
 216 
 217   log_trace(gc)("MarkStackSize: %uk  MarkStackSizeMax: %uk", (unsigned int) (MarkStackSize / K), (uint) (MarkStackSizeMax / K));
 218 }
 219 
 220 void CMSArguments::disable_adaptive_size_policy(const char* collector_name) {
 221   if (UseAdaptiveSizePolicy) {
 222     if (FLAG_IS_CMDLINE(UseAdaptiveSizePolicy)) {
 223       warning("Disabling UseAdaptiveSizePolicy; it is incompatible with %s.",
 224               collector_name);
 225     }
 226     FLAG_SET_DEFAULT(UseAdaptiveSizePolicy, false);
 227   }
 228 }
 229 
 230 CollectedHeap* CMSArguments::create_heap() {
 231   return create_heap_with_policy<CMSHeap, ConcurrentMarkSweepPolicy>();
 232 }