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 if (UseNUMA) { 162 if (FLAG_IS_DEFAULT(AlwaysPreTouch)) { 163 FLAG_SET_DEFAULT(AlwaysPreTouch, true); 164 } 165 if (!AlwaysPreTouch && FLAG_IS_CMDLINE(AlwaysPreTouch)) { 166 warning("Disabling AlwaysPreTouch is incompatible with UseNUMA. Disabling UseNUMA."); 167 FLAG_SET_ERGO(UseNUMA, false); 168 } 169 } 170 171 log_trace(gc)("MarkStackSize: %uk MarkStackSizeMax: %uk", (unsigned int) (MarkStackSize / K), (uint) (MarkStackSizeMax / K)); 172 173 // By default do not let the target stack size to be more than 1/4 of the entries 174 if (FLAG_IS_DEFAULT(GCDrainStackTargetSize)) { 175 FLAG_SET_ERGO(GCDrainStackTargetSize, MIN2(GCDrainStackTargetSize, (uintx)TASKQUEUE_SIZE / 4)); 176 } 177 178 #ifdef COMPILER2 179 // Enable loop strip mining to offer better pause time guarantees 180 if (FLAG_IS_DEFAULT(UseCountedLoopSafepoints)) { 181 FLAG_SET_DEFAULT(UseCountedLoopSafepoints, true); 182 if (FLAG_IS_DEFAULT(LoopStripMiningIter)) { 183 FLAG_SET_DEFAULT(LoopStripMiningIter, 1000); 184 } 185 } 186 #endif 187 188 initialize_verification_types(); 189 } 190 191 static size_t calculate_reasonable_max_memory_for_young(FormatBuffer<100> &calc_str, double max_ram_fraction_for_young) { 192 julong phys_mem; 193 // If MaxRam is specified, we use that as maximum physical memory available. 194 if (FLAG_IS_DEFAULT(MaxRAM)) { 195 phys_mem = os::physical_memory(); 196 calc_str.append("Physical_Memory"); 197 } else { 198 phys_mem = (julong)MaxRAM; 199 calc_str.append("MaxRAM"); 200 } 201 202 julong reasonable_max = phys_mem; 203 204 // If either MaxRAMFraction or MaxRAMPercentage is specified, we use them to calculate 205 // reasonable max size of young generation. 206 if (!FLAG_IS_DEFAULT(MaxRAMFraction)) { 207 reasonable_max = (julong)(phys_mem / MaxRAMFraction); 208 calc_str.append(" / MaxRAMFraction"); 209 } else if (!FLAG_IS_DEFAULT(MaxRAMPercentage)) { 210 reasonable_max = (julong)((phys_mem * MaxRAMPercentage) / 100); 211 calc_str.append(" * MaxRAMPercentage / 100"); 212 } else { 213 // We use our own fraction to calculate max size of young generation. 214 reasonable_max = phys_mem * max_ram_fraction_for_young; 215 calc_str.append(" * %0.2f", max_ram_fraction_for_young); 216 } 217 218 return (size_t)reasonable_max; 219 } 220 221 void G1Arguments::initialize_heap_flags_and_sizes() { 222 if (AllocateOldGenAt != NULL) { 223 initialize_heterogeneous(); 224 } 225 226 GCArguments::initialize_heap_flags_and_sizes(); 227 } 228 229 void G1Arguments::initialize_heterogeneous() { 230 FormatBuffer<100> calc_str(""); 231 232 MaxMemoryForYoung = calculate_reasonable_max_memory_for_young(calc_str, MaxRamFractionForYoung); 233 234 if (MaxNewSize > MaxMemoryForYoung) { 235 if (FLAG_IS_CMDLINE(MaxNewSize)) { 236 log_warning(gc, ergo)("Setting MaxNewSize to " SIZE_FORMAT " based on dram available (calculation = align(%s))", 237 MaxMemoryForYoung, calc_str.buffer()); 238 } else { 239 log_info(gc, ergo)("Setting MaxNewSize to " SIZE_FORMAT " based on dram available (calculation = align(%s)). " 240 "Dram usage can be lowered by setting MaxNewSize to a lower value", MaxMemoryForYoung, calc_str.buffer()); 241 } 242 MaxNewSize = MaxMemoryForYoung; 243 } 244 if (NewSize > MaxMemoryForYoung) { 245 if (FLAG_IS_CMDLINE(NewSize)) { 246 log_warning(gc, ergo)("Setting NewSize to " SIZE_FORMAT " based on dram available (calculation = align(%s))", 247 MaxMemoryForYoung, calc_str.buffer()); 248 } 249 NewSize = MaxMemoryForYoung; 250 } 251 252 } 253 254 CollectedHeap* G1Arguments::create_heap() { 255 return new G1CollectedHeap(); 256 } 257 258 bool G1Arguments::is_heterogeneous_heap() { 259 return AllocateOldGenAt != NULL; 260 } 261 262 size_t G1Arguments::reasonable_max_memory_for_young() { 263 return MaxMemoryForYoung; 264 } 265 266 size_t G1Arguments::heap_reserved_size_bytes() { 267 return (is_heterogeneous_heap() ? 2 : 1) * MaxHeapSize; 268 } 269 270 size_t G1Arguments::heap_max_size_bytes() { 271 return MaxHeapSize; 272 }