1 /*
  2  * Copyright (c) 2018, 2025, 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 "gc/parallel/parallelArguments.hpp"
 27 #include "gc/parallel/parallelScavengeHeap.hpp"
 28 #include "gc/shared/adaptiveSizePolicy.hpp"
 29 #include "gc/shared/gcArguments.hpp"
 30 #include "gc/shared/genArguments.hpp"
 31 #include "gc/shared/workerPolicy.hpp"
 32 #include "logging/log.hpp"
 33 #include "runtime/globals.hpp"
 34 #include "runtime/globals_extension.hpp"
 35 #include "runtime/java.hpp"
 36 #include "utilities/defaultStream.hpp"
 37 #include "utilities/powerOfTwo.hpp"
 38 
 39 size_t ParallelArguments::conservative_max_heap_alignment() {
 40   return compute_heap_alignment();
 41 }
 42 
 43 void ParallelArguments::initialize() {
 44   GCArguments::initialize();
 45   assert(UseParallelGC, "Error");
 46 
 47   // If no heap maximum was requested explicitly, use some reasonable fraction
 48   // of the physical memory, up to a maximum of 1GB.
 49   FLAG_SET_DEFAULT(ParallelGCThreads,
 50                    WorkerPolicy::parallel_worker_threads());
 51   if (ParallelGCThreads == 0) {
 52     jio_fprintf(defaultStream::error_stream(),
 53         "The Parallel GC can not be combined with -XX:ParallelGCThreads=0\n");
 54     vm_exit(1);
 55   }
 56 
 57   if (UseAdaptiveSizePolicy) {
 58     // We don't want to limit adaptive heap sizing's freedom to adjust the heap
 59     // unless the user actually sets these flags.
 60     if (FLAG_IS_DEFAULT(MinHeapFreeRatio)) {
 61       FLAG_SET_DEFAULT(MinHeapFreeRatio, 0);
 62     }
 63     if (FLAG_IS_DEFAULT(MaxHeapFreeRatio)) {
 64       FLAG_SET_DEFAULT(MaxHeapFreeRatio, 100);
 65     }
 66   }
 67 
 68   if (InitialSurvivorRatio < MinSurvivorRatio) {
 69     if (FLAG_IS_CMDLINE(InitialSurvivorRatio)) {
 70       if (FLAG_IS_CMDLINE(MinSurvivorRatio)) {
 71         jio_fprintf(defaultStream::error_stream(),
 72           "Inconsistent MinSurvivorRatio vs InitialSurvivorRatio: %zu vs %zu\n", MinSurvivorRatio, InitialSurvivorRatio);
 73       }
 74       FLAG_SET_DEFAULT(MinSurvivorRatio, InitialSurvivorRatio);
 75     } else {
 76       FLAG_SET_DEFAULT(InitialSurvivorRatio, MinSurvivorRatio);
 77     }
 78   }
 79 
 80   // If InitialSurvivorRatio or MinSurvivorRatio were not specified, but the
 81   // SurvivorRatio has been set, reset their default values to SurvivorRatio +
 82   // 2.  By doing this we make SurvivorRatio also work for Parallel Scavenger.
 83   // See CR 6362902 for details.
 84   if (!FLAG_IS_DEFAULT(SurvivorRatio)) {
 85     if (FLAG_IS_DEFAULT(InitialSurvivorRatio)) {
 86        FLAG_SET_DEFAULT(InitialSurvivorRatio, SurvivorRatio + 2);
 87     }
 88     if (FLAG_IS_DEFAULT(MinSurvivorRatio)) {
 89       FLAG_SET_DEFAULT(MinSurvivorRatio, SurvivorRatio + 2);
 90     }
 91   }
 92 
 93   if (FLAG_IS_DEFAULT(ParallelRefProcEnabled) && ParallelGCThreads > 1) {
 94     FLAG_SET_DEFAULT(ParallelRefProcEnabled, true);
 95   }
 96 }
 97 
 98 void ParallelArguments::initialize_alignments() {
 99   // Initialize card size before initializing alignments
100   CardTable::initialize_card_size();
101   SpaceAlignment = ParallelScavengeHeap::default_space_alignment();
102   HeapAlignment = compute_heap_alignment();
103 }
104 
105 void ParallelArguments::initialize_heap_flags_and_sizes_one_pass() {
106   // Do basic sizing work
107   GenArguments::initialize_heap_flags_and_sizes();
108 }
109 
110 void ParallelArguments::initialize_heap_flags_and_sizes() {
111   initialize_heap_flags_and_sizes_one_pass();
112 
113   if (!UseLargePages) {
114     ParallelScavengeHeap::set_desired_page_size(os::vm_page_size());
115     return;
116   }
117 
118   // If using large-page, need to update SpaceAlignment so that spaces are page-size aligned.
119   const size_t min_pages = 4; // 1 for eden + 1 for each survivor + 1 for old
120   const size_t page_sz = os::page_size_for_region_aligned(MinHeapSize, min_pages);
121   ParallelScavengeHeap::set_desired_page_size(page_sz);
122 
123   if (page_sz == os::vm_page_size()) {
124     log_warning(gc, heap)("MinHeapSize (%zu) must be large enough for 4 * page-size; Disabling UseLargePages for heap", MinHeapSize);
125     return;
126   }
127 
128   // Space is largepage-aligned.
129   size_t new_alignment = page_sz;
130   if (new_alignment != SpaceAlignment) {
131     SpaceAlignment = new_alignment;
132     // Redo everything from the start
133     initialize_heap_flags_and_sizes_one_pass();
134   }
135 }
136 
137 size_t ParallelArguments::heap_reserved_size_bytes() {
138   return MaxHeapSize;
139 }
140 
141 CollectedHeap* ParallelArguments::create_heap() {
142   return new ParallelScavengeHeap();
143 }