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/fullGCForwarding.hpp"
 30 #include "gc/shared/gcArguments.hpp"
 31 #include "gc/shared/genArguments.hpp"
 32 #include "gc/shared/workerPolicy.hpp"
 33 #include "logging/log.hpp"
 34 #include "runtime/globals.hpp"
 35 #include "runtime/globals_extension.hpp"
 36 #include "runtime/java.hpp"
 37 #include "utilities/defaultStream.hpp"
 38 #include "utilities/powerOfTwo.hpp"
 39 
 40 static size_t num_young_spaces() {
 41   // When using NUMA, we create one MutableNUMASpace for each NUMA node
 42   const size_t num_eden_spaces = UseNUMA ? os::numa_get_groups_num() : 1;
 43 
 44   // The young generation must have room for eden + two survivors
 45   return num_eden_spaces + 2;
 46 }
 47 
 48 static size_t num_old_spaces() {
 49   return 1;
 50 }
 51 
 52 void ParallelArguments::initialize_alignments() {
 53   // Initialize card size before initializing alignments
 54   CardTable::initialize_card_size();
 55   const size_t card_table_alignment = CardTable::ct_max_alignment_constraint();
 56   SpaceAlignment = ParallelScavengeHeap::default_space_alignment();
 57 
 58   if (UseLargePages) {
 59     const size_t total_spaces = num_young_spaces() + num_old_spaces();
 60     const size_t page_size =  os::page_size_for_region_unaligned(MaxHeapSize, total_spaces);
 61     ParallelScavengeHeap::set_desired_page_size(page_size);
 62 
 63     if (page_size == os::vm_page_size()) {
 64       log_warning(gc, heap)("MaxHeapSize (%zu) must be large enough for %zu * page-size; Disabling UseLargePages for heap",
 65                             MaxHeapSize, total_spaces);
 66     }
 67 
 68     if (page_size > SpaceAlignment) {
 69       SpaceAlignment = page_size;
 70     }
 71 
 72     HeapAlignment = lcm(page_size, card_table_alignment);
 73 
 74   } else {
 75     assert(is_aligned(SpaceAlignment, os::vm_page_size()), "");
 76     ParallelScavengeHeap::set_desired_page_size(os::vm_page_size());
 77     HeapAlignment = card_table_alignment;
 78   }
 79 }
 80 
 81 void ParallelArguments::initialize() {
 82   GCArguments::initialize();
 83   assert(UseParallelGC, "Error");
 84 
 85   // If no heap maximum was requested explicitly, use some reasonable fraction
 86   // of the physical memory, up to a maximum of 1GB.
 87   FLAG_SET_DEFAULT(ParallelGCThreads,
 88                    WorkerPolicy::parallel_worker_threads());
 89   if (ParallelGCThreads == 0) {
 90     jio_fprintf(defaultStream::error_stream(),
 91         "The Parallel GC can not be combined with -XX:ParallelGCThreads=0\n");
 92     vm_exit(1);
 93   }
 94 
 95   if (UseAdaptiveSizePolicy) {
 96     // We don't want to limit adaptive heap sizing's freedom to adjust the heap
 97     // unless the user actually sets these flags.
 98     if (FLAG_IS_DEFAULT(MinHeapFreeRatio)) {
 99       FLAG_SET_DEFAULT(MinHeapFreeRatio, 0);
100     }
101     if (FLAG_IS_DEFAULT(MaxHeapFreeRatio)) {
102       FLAG_SET_DEFAULT(MaxHeapFreeRatio, 100);
103     }
104   }
105 
106   if (InitialSurvivorRatio < MinSurvivorRatio) {
107     if (FLAG_IS_CMDLINE(InitialSurvivorRatio)) {
108       if (FLAG_IS_CMDLINE(MinSurvivorRatio)) {
109         jio_fprintf(defaultStream::error_stream(),
110           "Inconsistent MinSurvivorRatio vs InitialSurvivorRatio: %zu vs %zu\n", MinSurvivorRatio, InitialSurvivorRatio);
111       }
112       FLAG_SET_DEFAULT(MinSurvivorRatio, InitialSurvivorRatio);
113     } else {
114       FLAG_SET_DEFAULT(InitialSurvivorRatio, MinSurvivorRatio);
115     }
116   }
117 
118   // If InitialSurvivorRatio or MinSurvivorRatio were not specified, but the
119   // SurvivorRatio has been set, reset their default values to SurvivorRatio +
120   // 2.  By doing this we make SurvivorRatio also work for Parallel Scavenger.
121   // See CR 6362902 for details.
122   if (!FLAG_IS_DEFAULT(SurvivorRatio)) {
123     if (FLAG_IS_DEFAULT(InitialSurvivorRatio)) {
124        FLAG_SET_DEFAULT(InitialSurvivorRatio, SurvivorRatio + 2);
125     }
126     if (FLAG_IS_DEFAULT(MinSurvivorRatio)) {
127       FLAG_SET_DEFAULT(MinSurvivorRatio, SurvivorRatio + 2);
128     }
129   }
130 
131   FullGCForwarding::initialize_flags(heap_reserved_size_bytes());
132 }
133 
134 size_t ParallelArguments::conservative_max_heap_alignment() {
135   // The card marking array and the offset arrays for old generations are
136   // committed in os pages as well. Make sure they are entirely full (to
137   // avoid partial page problems), e.g. if 512 bytes heap corresponds to 1
138   // byte entry and the os page size is 4096, the maximum heap size should
139   // be 512*4096 = 2MB aligned.
140 
141   size_t alignment = CardTable::ct_max_alignment_constraint();
142 
143   if (UseLargePages) {
144       // In presence of large pages we have to make sure that our
145       // alignment is large page aware.
146       alignment = lcm(os::large_page_size(), alignment);
147   }
148 
149   return alignment;
150 }
151 
152 CollectedHeap* ParallelArguments::create_heap() {
153   return new ParallelScavengeHeap();
154 }
155 
156 size_t ParallelArguments::young_gen_size_lower_bound() {
157   return num_young_spaces() * SpaceAlignment;
158 }
159 
160 size_t ParallelArguments::old_gen_size_lower_bound() {
161   return num_old_spaces() * SpaceAlignment;
162 }
163 
164 size_t ParallelArguments::heap_reserved_size_bytes() {
165   return MaxHeapSize;
166 }