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