1 /*
   2  * Copyright (c) 2018, 2020, 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(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 // Returns the maximum number of workers to be used in a concurrent
 101 // phase based on the number of GC workers being used in a STW
 102 // phase.
 103 static uint scale_concurrent_worker_threads(uint num_gc_workers) {
 104   return MAX2((num_gc_workers + 2) / 4, 1U);
 105 }
 106 
 107 void G1Arguments::initialize_mark_stack_size() {
 108   if (FLAG_IS_DEFAULT(MarkStackSize)) {
 109     size_t mark_stack_size = MIN2(MarkStackSizeMax,
 110                                   MAX2(MarkStackSize, (size_t)ConcGCThreads * TASKQUEUE_SIZE));
 111     FLAG_SET_ERGO(MarkStackSize, mark_stack_size);
 112   }
 113 
 114   log_trace(gc)("MarkStackSize: %uk  MarkStackSizeMax: %uk", (uint)(MarkStackSize / K), (uint)(MarkStackSizeMax / K));
 115 }
 116 
 117 void G1Arguments::initialize() {
 118   GCArguments::initialize();
 119   assert(UseG1GC, "Error");
 120   FLAG_SET_DEFAULT(ParallelGCThreads, WorkerPolicy::parallel_worker_threads());
 121   if (ParallelGCThreads == 0) {
 122     assert(!FLAG_IS_DEFAULT(ParallelGCThreads), "The default value for ParallelGCThreads should not be 0.");
 123     vm_exit_during_initialization("The flag -XX:+UseG1GC can not be combined with -XX:ParallelGCThreads=0", NULL);
 124   }
 125 
 126   // When dumping the CDS archive we want to reduce fragmentation by
 127   // triggering a full collection. To get as low fragmentation as
 128   // possible we only use one worker thread.
 129   if (DumpSharedSpaces) {
 130     FLAG_SET_ERGO(ParallelGCThreads, 1);
 131   }
 132 
 133   if (FLAG_IS_DEFAULT(G1ConcRefinementThreads)) {
 134     FLAG_SET_ERGO(G1ConcRefinementThreads, ParallelGCThreads);
 135   }
 136 
 137   if (FLAG_IS_DEFAULT(ConcGCThreads) || ConcGCThreads == 0) {
 138     // Calculate the number of concurrent worker threads by scaling
 139     // the number of parallel GC threads.
 140     uint marking_thread_num = scale_concurrent_worker_threads(ParallelGCThreads);
 141     FLAG_SET_ERGO(ConcGCThreads, marking_thread_num);
 142   }
 143 
 144   if (FLAG_IS_DEFAULT(GCTimeRatio) || GCTimeRatio == 0) {
 145     // In G1, we want the default GC overhead goal to be higher than
 146     // it is for PS, or the heap might be expanded too aggressively.
 147     // We set it here to ~8%.
 148     FLAG_SET_DEFAULT(GCTimeRatio, 12);
 149   }
 150 
 151   // Below, we might need to calculate the pause time interval based on
 152   // the pause target. When we do so we are going to give G1 maximum
 153   // flexibility and allow it to do pauses when it needs to. So, we'll
 154   // arrange that the pause interval to be pause time target + 1 to
 155   // ensure that a) the pause time target is maximized with respect to
 156   // the pause interval and b) we maintain the invariant that pause
 157   // time target < pause interval. If the user does not want this
 158   // maximum flexibility, they will have to set the pause interval
 159   // explicitly.
 160 
 161   if (FLAG_IS_DEFAULT(MaxGCPauseMillis)) {
 162     // The default pause time target in G1 is 200ms
 163     FLAG_SET_DEFAULT(MaxGCPauseMillis, 200);
 164   }
 165 
 166   // Then, if the interval parameter was not set, set it according to
 167   // the pause time target (this will also deal with the case when the
 168   // pause time target is the default value).
 169   if (FLAG_IS_DEFAULT(GCPauseIntervalMillis)) {
 170     FLAG_SET_DEFAULT(GCPauseIntervalMillis, MaxGCPauseMillis + 1);
 171   }
 172 
 173   if (FLAG_IS_DEFAULT(ParallelRefProcEnabled) && ParallelGCThreads > 1) {
 174     FLAG_SET_DEFAULT(ParallelRefProcEnabled, true);
 175   }
 176 
 177   // By default do not let the target stack size to be more than 1/4 of the entries
 178   if (FLAG_IS_DEFAULT(GCDrainStackTargetSize)) {
 179     FLAG_SET_ERGO(GCDrainStackTargetSize, MIN2(GCDrainStackTargetSize, (uintx)TASKQUEUE_SIZE / 4));
 180   }
 181 
 182 #ifdef COMPILER2
 183   // Enable loop strip mining to offer better pause time guarantees
 184   if (FLAG_IS_DEFAULT(UseCountedLoopSafepoints)) {
 185     FLAG_SET_DEFAULT(UseCountedLoopSafepoints, true);
 186     if (FLAG_IS_DEFAULT(LoopStripMiningIter)) {
 187       FLAG_SET_DEFAULT(LoopStripMiningIter, 1000);
 188     }
 189   }
 190 #endif
 191 
 192   initialize_mark_stack_size();
 193   initialize_verification_types();
 194 }
 195 
 196 static size_t calculate_reasonable_max_memory_for_young(FormatBuffer<100> &calc_str, double max_ram_fraction_for_young) {
 197   julong phys_mem;
 198   // If MaxRam is specified, we use that as maximum physical memory available.
 199   if (FLAG_IS_DEFAULT(MaxRAM)) {
 200     phys_mem = os::physical_memory();
 201     calc_str.append("Physical_Memory");
 202   } else {
 203     phys_mem = (julong)MaxRAM;
 204     calc_str.append("MaxRAM");
 205   }
 206 
 207   julong reasonable_max = phys_mem;
 208 
 209   // If either MaxRAMFraction or MaxRAMPercentage is specified, we use them to calculate
 210   // reasonable max size of young generation.
 211   if (!FLAG_IS_DEFAULT(MaxRAMFraction)) {
 212     reasonable_max = (julong)(phys_mem / MaxRAMFraction);
 213     calc_str.append(" / MaxRAMFraction");
 214   }  else if (!FLAG_IS_DEFAULT(MaxRAMPercentage)) {
 215     reasonable_max = (julong)((phys_mem * MaxRAMPercentage) / 100);
 216     calc_str.append(" * MaxRAMPercentage / 100");
 217   }  else {
 218     // We use our own fraction to calculate max size of young generation.
 219     reasonable_max = phys_mem * max_ram_fraction_for_young;
 220     calc_str.append(" * %0.2f", max_ram_fraction_for_young);
 221   }
 222 
 223   return (size_t)reasonable_max;
 224 }
 225 
 226 void G1Arguments::initialize_heap_flags_and_sizes() {
 227   if (AllocateOldGenAt != NULL) {
 228     initialize_heterogeneous();
 229   }
 230 
 231   GCArguments::initialize_heap_flags_and_sizes();
 232 }
 233 
 234 void G1Arguments::initialize_heterogeneous() {
 235   FormatBuffer<100> calc_str("");
 236 
 237   MaxMemoryForYoung = calculate_reasonable_max_memory_for_young(calc_str, MaxRamFractionForYoung);
 238 
 239   if (MaxNewSize > MaxMemoryForYoung) {
 240     if (FLAG_IS_CMDLINE(MaxNewSize)) {
 241       log_warning(gc, ergo)("Setting MaxNewSize to " SIZE_FORMAT " based on dram available (calculation = align(%s))",
 242                             MaxMemoryForYoung, calc_str.buffer());
 243     } else {
 244       log_info(gc, ergo)("Setting MaxNewSize to " SIZE_FORMAT " based on dram available (calculation = align(%s)). "
 245                          "Dram usage can be lowered by setting MaxNewSize to a lower value", MaxMemoryForYoung, calc_str.buffer());
 246     }
 247     MaxNewSize = MaxMemoryForYoung;
 248   }
 249   if (NewSize > MaxMemoryForYoung) {
 250     if (FLAG_IS_CMDLINE(NewSize)) {
 251       log_warning(gc, ergo)("Setting NewSize to " SIZE_FORMAT " based on dram available (calculation = align(%s))",
 252                             MaxMemoryForYoung, calc_str.buffer());
 253     }
 254     NewSize = MaxMemoryForYoung;
 255   }
 256 
 257 }
 258 
 259 CollectedHeap* G1Arguments::create_heap() {
 260   return new G1CollectedHeap();
 261 }
 262 
 263 bool G1Arguments::is_heterogeneous_heap() {
 264   return AllocateOldGenAt != NULL;
 265 }
 266 
 267 size_t G1Arguments::reasonable_max_memory_for_young() {
 268   return MaxMemoryForYoung;
 269 }
 270 
 271 size_t G1Arguments::heap_reserved_size_bytes() {
 272   return (is_heterogeneous_heap() ? 2 : 1) * MaxHeapSize;
 273 }
 274 
 275 size_t G1Arguments::heap_max_size_bytes() {
 276   return MaxHeapSize;
 277 }