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