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