1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "gc/g1/g1Arguments.hpp"
  28 #include "gc/g1/g1CollectedHeap.inline.hpp"
  29 #include "gc/g1/g1HeapVerifier.hpp"
  30 #include "gc/g1/heapRegion.hpp"
  31 #include "gc/g1/heapRegionRemSet.hpp"
  32 #include "gc/shared/cardTableRS.hpp"
  33 #include "gc/shared/gcArguments.hpp"
  34 #include "gc/shared/workerPolicy.hpp"
  35 #include "runtime/globals.hpp"
  36 #include "runtime/globals_extension.hpp"
  37 
  38 static const double MaxRamFractionForYoung = 0.8;
  39 size_t G1Arguments::MaxMemoryForYoung;
  40 
  41 static size_t calculate_heap_alignment(size_t space_alignment) {
  42   size_t card_table_alignment = CardTableRS::ct_max_alignment_constraint();
  43   size_t page_size = UseLargePages ? os::large_page_size() : os::vm_page_size();
  44   return MAX3(card_table_alignment, space_alignment, page_size);
  45 }
  46 
  47 void G1Arguments::initialize_alignments() {
  48   // Set up the region size and associated fields.
  49   //
  50   // There is a circular dependency here. We base the region size on the heap
  51   // size, but the heap size should be aligned with the region size. To get
  52   // around this we use the unaligned values for the heap.
  53   HeapRegion::setup_heap_region_size(InitialHeapSize, MaxHeapSize);
  54   HeapRegionRemSet::setup_remset_size();
  55 
  56   SpaceAlignment = HeapRegion::GrainBytes;
  57   HeapAlignment = calculate_heap_alignment(SpaceAlignment);
  58 }
  59 
  60 size_t G1Arguments::conservative_max_heap_alignment() {
  61   return HeapRegion::max_region_size();
  62 }
  63 
  64 void G1Arguments::initialize_verification_types() {
  65   if (strlen(VerifyGCType) > 0) {
  66     const char delimiter[] = " ,\n";
  67     size_t length = strlen(VerifyGCType);
  68     char* type_list = NEW_C_HEAP_ARRAY(char, length + 1, mtInternal);
  69     strncpy(type_list, VerifyGCType, length + 1);
  70     char* save_ptr;
  71 
  72     char* token = strtok_r(type_list, delimiter, &save_ptr);
  73     while (token != NULL) {
  74       parse_verification_type(token);
  75       token = strtok_r(NULL, delimiter, &save_ptr);
  76     }
  77     FREE_C_HEAP_ARRAY(char, type_list);
  78   }
  79 }
  80 
  81 void G1Arguments::parse_verification_type(const char* type) {
  82   if (strcmp(type, "young-normal") == 0) {
  83     G1HeapVerifier::enable_verification_type(G1HeapVerifier::G1VerifyYoungNormal);
  84   } else if (strcmp(type, "concurrent-start") == 0) {
  85     G1HeapVerifier::enable_verification_type(G1HeapVerifier::G1VerifyConcurrentStart);
  86   } else if (strcmp(type, "mixed") == 0) {
  87     G1HeapVerifier::enable_verification_type(G1HeapVerifier::G1VerifyMixed);
  88   } else if (strcmp(type, "remark") == 0) {
  89     G1HeapVerifier::enable_verification_type(G1HeapVerifier::G1VerifyRemark);
  90   } else if (strcmp(type, "cleanup") == 0) {
  91     G1HeapVerifier::enable_verification_type(G1HeapVerifier::G1VerifyCleanup);
  92   } else if (strcmp(type, "full") == 0) {
  93     G1HeapVerifier::enable_verification_type(G1HeapVerifier::G1VerifyFull);
  94   } else {
  95     log_warning(gc, verify)("VerifyGCType: '%s' is unknown. Available types are: "
  96                             "young-normal, concurrent-start, mixed, remark, cleanup and full", type);
  97   }
  98 }
  99 
 100 void G1Arguments::initialize() {
 101   GCArguments::initialize();
 102   assert(UseG1GC, "Error");
 103   FLAG_SET_DEFAULT(ParallelGCThreads, WorkerPolicy::parallel_worker_threads());
 104   if (ParallelGCThreads == 0) {
 105     assert(!FLAG_IS_DEFAULT(ParallelGCThreads), "The default value for ParallelGCThreads should not be 0.");
 106     vm_exit_during_initialization("The flag -XX:+UseG1GC can not be combined with -XX:ParallelGCThreads=0", NULL);
 107   }
 108 
 109   // When dumping the CDS archive we want to reduce fragmentation by
 110   // triggering a full collection. To get as low fragmentation as
 111   // possible we only use one worker thread.
 112   if (DumpSharedSpaces) {
 113     FLAG_SET_ERGO(ParallelGCThreads, 1);
 114   }
 115 
 116   if (FLAG_IS_DEFAULT(G1ConcRefinementThreads)) {
 117     FLAG_SET_ERGO(G1ConcRefinementThreads, ParallelGCThreads);
 118   }
 119 
 120   // MarkStackSize will be set (if it hasn't been set by the user)
 121   // when concurrent marking is initialized.
 122   // Its value will be based upon the number of parallel marking threads.
 123   // But we do set the maximum mark stack size here.
 124   if (FLAG_IS_DEFAULT(MarkStackSizeMax)) {
 125     FLAG_SET_DEFAULT(MarkStackSizeMax, 128 * TASKQUEUE_SIZE);
 126   }
 127 
 128   if (FLAG_IS_DEFAULT(GCTimeRatio) || GCTimeRatio == 0) {
 129     // In G1, we want the default GC overhead goal to be higher than
 130     // it is for PS, or the heap might be expanded too aggressively.
 131     // We set it here to ~8%.
 132     FLAG_SET_DEFAULT(GCTimeRatio, 12);
 133   }
 134 
 135   // Below, we might need to calculate the pause time interval based on
 136   // the pause target. When we do so we are going to give G1 maximum
 137   // flexibility and allow it to do pauses when it needs to. So, we'll
 138   // arrange that the pause interval to be pause time target + 1 to
 139   // ensure that a) the pause time target is maximized with respect to
 140   // the pause interval and b) we maintain the invariant that pause
 141   // time target < pause interval. If the user does not want this
 142   // maximum flexibility, they will have to set the pause interval
 143   // explicitly.
 144 
 145   if (FLAG_IS_DEFAULT(MaxGCPauseMillis)) {
 146     // The default pause time target in G1 is 200ms
 147     FLAG_SET_DEFAULT(MaxGCPauseMillis, 200);
 148   }
 149 
 150   // Then, if the interval parameter was not set, set it according to
 151   // the pause time target (this will also deal with the case when the
 152   // pause time target is the default value).
 153   if (FLAG_IS_DEFAULT(GCPauseIntervalMillis)) {
 154     FLAG_SET_DEFAULT(GCPauseIntervalMillis, MaxGCPauseMillis + 1);
 155   }
 156 
 157   if (FLAG_IS_DEFAULT(ParallelRefProcEnabled) && ParallelGCThreads > 1) {
 158     FLAG_SET_DEFAULT(ParallelRefProcEnabled, true);
 159   }
 160 
 161   log_trace(gc)("MarkStackSize: %uk  MarkStackSizeMax: %uk", (unsigned int) (MarkStackSize / K), (uint) (MarkStackSizeMax / K));
 162 
 163   // By default do not let the target stack size to be more than 1/4 of the entries
 164   if (FLAG_IS_DEFAULT(GCDrainStackTargetSize)) {
 165     FLAG_SET_ERGO(GCDrainStackTargetSize, MIN2(GCDrainStackTargetSize, (uintx)TASKQUEUE_SIZE / 4));
 166   }
 167 
 168 #ifdef COMPILER2
 169   // Enable loop strip mining to offer better pause time guarantees
 170   if (FLAG_IS_DEFAULT(UseCountedLoopSafepoints)) {
 171     FLAG_SET_DEFAULT(UseCountedLoopSafepoints, true);
 172     if (FLAG_IS_DEFAULT(LoopStripMiningIter)) {
 173       FLAG_SET_DEFAULT(LoopStripMiningIter, 1000);
 174     }
 175   }
 176 #endif
 177 
 178   initialize_verification_types();
 179 }
 180 
 181 static size_t calculate_reasonable_max_memory_for_young(FormatBuffer<100> &calc_str, double max_ram_fraction_for_young) {
 182   julong phys_mem;
 183   // If MaxRam is specified, we use that as maximum physical memory available.
 184   if (FLAG_IS_DEFAULT(MaxRAM)) {
 185     phys_mem = os::physical_memory();
 186     calc_str.append("Physical_Memory");
 187   } else {
 188     phys_mem = (julong)MaxRAM;
 189     calc_str.append("MaxRAM");
 190   }
 191 
 192   julong reasonable_max = phys_mem;
 193 
 194   // If either MaxRAMFraction or MaxRAMPercentage is specified, we use them to calculate
 195   // reasonable max size of young generation.
 196   if (!FLAG_IS_DEFAULT(MaxRAMFraction)) {
 197     reasonable_max = (julong)(phys_mem / MaxRAMFraction);
 198     calc_str.append(" / MaxRAMFraction");
 199   }  else if (!FLAG_IS_DEFAULT(MaxRAMPercentage)) {
 200     reasonable_max = (julong)((phys_mem * MaxRAMPercentage) / 100);
 201     calc_str.append(" * MaxRAMPercentage / 100");
 202   }  else {
 203     // We use our own fraction to calculate max size of young generation.
 204     reasonable_max = phys_mem * max_ram_fraction_for_young;
 205     calc_str.append(" * %0.2f", max_ram_fraction_for_young);
 206   }
 207 
 208   return (size_t)reasonable_max;
 209 }
 210 
 211 void G1Arguments::initialize_heap_flags_and_sizes() {
 212   if (AllocateOldGenAt != NULL) {
 213     initialize_heterogeneous();
 214   }
 215 
 216   GCArguments::initialize_heap_flags_and_sizes();
 217 }
 218 
 219 void G1Arguments::initialize_heterogeneous() {
 220   FormatBuffer<100> calc_str("");
 221 
 222   MaxMemoryForYoung = calculate_reasonable_max_memory_for_young(calc_str, MaxRamFractionForYoung);
 223 
 224   if (MaxNewSize > MaxMemoryForYoung) {
 225     if (FLAG_IS_CMDLINE(MaxNewSize)) {
 226       log_warning(gc, ergo)("Setting MaxNewSize to " SIZE_FORMAT " based on dram available (calculation = align(%s))",
 227                             MaxMemoryForYoung, calc_str.buffer());
 228     } else {
 229       log_info(gc, ergo)("Setting MaxNewSize to " SIZE_FORMAT " based on dram available (calculation = align(%s)). "
 230                          "Dram usage can be lowered by setting MaxNewSize to a lower value", MaxMemoryForYoung, calc_str.buffer());
 231     }
 232     MaxNewSize = MaxMemoryForYoung;
 233   }
 234   if (NewSize > MaxMemoryForYoung) {
 235     if (FLAG_IS_CMDLINE(NewSize)) {
 236       log_warning(gc, ergo)("Setting NewSize to " SIZE_FORMAT " based on dram available (calculation = align(%s))",
 237                             MaxMemoryForYoung, calc_str.buffer());
 238     }
 239     NewSize = MaxMemoryForYoung;
 240   }
 241 
 242 }
 243 
 244 CollectedHeap* G1Arguments::create_heap() {
 245   return new G1CollectedHeap();
 246 }
 247 
 248 bool G1Arguments::is_heterogeneous_heap() {
 249   return AllocateOldGenAt != NULL;
 250 }
 251 
 252 size_t G1Arguments::reasonable_max_memory_for_young() {
 253   return MaxMemoryForYoung;
 254 }
 255 
 256 size_t G1Arguments::heap_reserved_size_bytes() {
 257   return (is_heterogeneous_heap() ? 2 : 1) * MaxHeapSize;
 258 }