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/cms/cmsArguments.hpp"
28#include "gc/cms/cmsHeap.hpp"
29#include "gc/cms/compactibleFreeListSpace.hpp"
30#include "gc/shared/cardTableRS.hpp"
31#include "gc/shared/gcArguments.hpp"
32#include "gc/shared/genCollectedHeap.hpp"
33#include "gc/shared/workerPolicy.hpp"
34#include "runtime/arguments.hpp"
35#include "runtime/globals.hpp"
36#include "runtime/globals_extension.hpp"
37#include "utilities/defaultStream.hpp"
38
39void CMSArguments::set_parnew_gc_flags() {
40 assert(!UseSerialGC && !UseParallelOldGC && !UseParallelGC && !UseG1GC,
41 "control point invariant");
42 assert(UseConcMarkSweepGC, "CMS is expected to be on here");
43
44 if (FLAG_IS_DEFAULT(ParallelGCThreads)) {
45 FLAG_SET_DEFAULT(ParallelGCThreads, WorkerPolicy::parallel_worker_threads());
46 assert(ParallelGCThreads > 0, "We should always have at least one thread by default");
47 } else if (ParallelGCThreads == 0) {
48 jio_fprintf(defaultStream::error_stream(),
49 "The ParNew GC can not be combined with -XX:ParallelGCThreads=0\n");
50 vm_exit(1);
51 }
52
53 // By default YoungPLABSize and OldPLABSize are set to 4096 and 1024 respectively,
54 // these settings are default for Parallel Scavenger. For ParNew+Tenured configuration
55 // we set them to 1024 and 1024.
56 // See CR 6362902.
57 if (FLAG_IS_DEFAULT(YoungPLABSize)) {
58 FLAG_SET_DEFAULT(YoungPLABSize, (intx)1024);
59 }
60 if (FLAG_IS_DEFAULT(OldPLABSize)) {
61 FLAG_SET_DEFAULT(OldPLABSize, (intx)1024);
62 }
63
64 // When using compressed oops, we use local overflow stacks,
65 // rather than using a global overflow list chained through
66 // the klass word of the object's pre-image.
67 if (UseCompressedOops && !ParGCUseLocalOverflow) {
68 if (!FLAG_IS_DEFAULT(ParGCUseLocalOverflow)) {
69 warning("Forcing +ParGCUseLocalOverflow: needed if using compressed references");
70 }
71 FLAG_SET_DEFAULT(ParGCUseLocalOverflow, true);
72 }
73 assert(ParGCUseLocalOverflow || !UseCompressedOops, "Error");
74}
75
76// Adjust some sizes to suit CMS and/or ParNew needs; these work well on
77// sparc/solaris for certain applications, but would gain from
78// further optimization and tuning efforts, and would almost
79// certainly gain from analysis of platform and environment.
80void CMSArguments::initialize() {
81 GCArguments::initialize();
82
83 assert(!UseSerialGC && !UseParallelOldGC && !UseParallelGC, "Error");
84 assert(UseConcMarkSweepGC, "CMS is expected to be on here");
85
86 // CMS space iteration, which FLSVerifyAllHeapreferences entails,
87 // insists that we hold the requisite locks so that the iteration is
88 // MT-safe. For the verification at start-up and shut-down, we don't
89 // yet have a good way of acquiring and releasing these locks,
90 // which are not visible at the CollectedHeap level. We want to
91 // be able to acquire these locks and then do the iteration rather
92 // than just disable the lock verification. This will be fixed under
93 // bug 4788986.
94 if (UseConcMarkSweepGC && FLSVerifyAllHeapReferences) {
95 if (VerifyDuringStartup) {
96 warning("Heap verification at start-up disabled "
97 "(due to current incompatibility with FLSVerifyAllHeapReferences)");
98 VerifyDuringStartup = false; // Disable verification at start-up
99 }
100
101 if (VerifyBeforeExit) {
102 warning("Heap verification at shutdown disabled "
103 "(due to current incompatibility with FLSVerifyAllHeapReferences)");
104 VerifyBeforeExit = false; // Disable verification at shutdown
105 }
106 }
107
108 if (!ClassUnloading) {
109 FLAG_SET_CMDLINE(CMSClassUnloadingEnabled, false);
110 }
111
112 // Set CMS global values
113 CompactibleFreeListSpace::set_cms_values();
114
115 // Turn off AdaptiveSizePolicy by default for cms until it is complete.
116 disable_adaptive_size_policy("UseConcMarkSweepGC");
117
118 set_parnew_gc_flags();
119
120 size_t max_heap = align_down(MaxHeapSize,
121 CardTableRS::ct_max_alignment_constraint());
122
123 // Now make adjustments for CMS
124 intx tenuring_default = (intx)6;
125 size_t young_gen_per_worker = CMSYoungGenPerWorker;
126
127 // Preferred young gen size for "short" pauses:
128 // upper bound depends on # of threads and NewRatio.
129 const size_t preferred_max_new_size_unaligned =
130 MIN2(max_heap/(NewRatio+1), ScaleForWordSize(young_gen_per_worker * ParallelGCThreads));
131 size_t preferred_max_new_size =
132 align_up(preferred_max_new_size_unaligned, os::vm_page_size());
133
134 // Unless explicitly requested otherwise, size young gen
135 // for "short" pauses ~ CMSYoungGenPerWorker*ParallelGCThreads
136
137 // If either MaxNewSize or NewRatio is set on the command line,
138 // assume the user is trying to set the size of the young gen.
139 if (FLAG_IS_DEFAULT(MaxNewSize) && FLAG_IS_DEFAULT(NewRatio)) {
140
141 // Set MaxNewSize to our calculated preferred_max_new_size unless
142 // NewSize was set on the command line and it is larger than
143 // preferred_max_new_size.
144 if (!FLAG_IS_DEFAULT(NewSize)) { // NewSize explicitly set at command-line
145 FLAG_SET_ERGO(MaxNewSize, MAX2(NewSize, preferred_max_new_size));
146 } else {
147 FLAG_SET_ERGO(MaxNewSize, preferred_max_new_size);
148 }
149 log_trace(gc, heap)("CMS ergo set MaxNewSize: " SIZE_FORMAT, MaxNewSize);
150
151 // Code along this path potentially sets NewSize and OldSize
152 log_trace(gc, heap)("CMS set min_heap_size: " SIZE_FORMAT " initial_heap_size: " SIZE_FORMAT " max_heap: " SIZE_FORMAT,
153 MinHeapSize, InitialHeapSize, max_heap);
154 size_t min_new = preferred_max_new_size;
155 if (FLAG_IS_CMDLINE(NewSize)) {
156 min_new = NewSize;
157 }
158 if (max_heap > min_new && MinHeapSize > min_new) {
159 // Unless explicitly requested otherwise, make young gen
160 // at least min_new, and at most preferred_max_new_size.
161 if (FLAG_IS_DEFAULT(NewSize)) {
162 FLAG_SET_ERGO(NewSize, MAX2(NewSize, min_new));
163 FLAG_SET_ERGO(NewSize, MIN2(preferred_max_new_size, NewSize));
164 log_trace(gc, heap)("CMS ergo set NewSize: " SIZE_FORMAT, NewSize);
165 }
166 // Unless explicitly requested otherwise, size old gen
167 // so it's NewRatio x of NewSize.
168 if (FLAG_IS_DEFAULT(OldSize)) {
169 if (max_heap > NewSize) {
170 FLAG_SET_ERGO(OldSize, MIN2(NewRatio*NewSize, max_heap - NewSize));
171 log_trace(gc, heap)("CMS ergo set OldSize: " SIZE_FORMAT, OldSize);
172 }
173 }
174 }
175 }
176 // Unless explicitly requested otherwise, definitely
177 // promote all objects surviving "tenuring_default" scavenges.
178 if (FLAG_IS_DEFAULT(MaxTenuringThreshold) &&
179 FLAG_IS_DEFAULT(SurvivorRatio)) {
180 FLAG_SET_ERGO(MaxTenuringThreshold, tenuring_default);
181 }
182 // If we decided above (or user explicitly requested)
183 // `promote all' (via MaxTenuringThreshold := 0),
184 // prefer minuscule survivor spaces so as not to waste
185 // space for (non-existent) survivors
186 if (FLAG_IS_DEFAULT(SurvivorRatio) && MaxTenuringThreshold == 0) {
187 FLAG_SET_ERGO(SurvivorRatio, MAX2((uintx)1024, SurvivorRatio));
188 }
189
190 // OldPLABSize is interpreted in CMS as not the size of the PLAB in words,
191 // but rather the number of free blocks of a given size that are used when
192 // replenishing the local per-worker free list caches.
193 if (FLAG_IS_DEFAULT(OldPLABSize)) {
194 if (!FLAG_IS_DEFAULT(ResizeOldPLAB) && !ResizeOldPLAB) {
195 // OldPLAB sizing manually turned off: Use a larger default setting,
196 // unless it was manually specified. This is because a too-low value
197 // will slow down scavenges.
198 FLAG_SET_ERGO(OldPLABSize, CompactibleFreeListSpaceLAB::_default_static_old_plab_size); // default value before 6631166
199 } else {
200 FLAG_SET_DEFAULT(OldPLABSize, CompactibleFreeListSpaceLAB::_default_dynamic_old_plab_size); // old CMSParPromoteBlocksToClaim default
201 }
202 }
203
204 // If either of the static initialization defaults have changed, note this
205 // modification.
206 if (!FLAG_IS_DEFAULT(OldPLABSize) || !FLAG_IS_DEFAULT(OldPLABWeight)) {
207 CompactibleFreeListSpaceLAB::modify_initialization(OldPLABSize, OldPLABWeight);
208 }
209
210 log_trace(gc)("MarkStackSize: %uk MarkStackSizeMax: %uk", (unsigned int) (MarkStackSize / K), (uint) (MarkStackSizeMax / K));
211}
212
213void CMSArguments::disable_adaptive_size_policy(const char* collector_name) {
214 if (UseAdaptiveSizePolicy) {
215 if (FLAG_IS_CMDLINE(UseAdaptiveSizePolicy)) {
216 warning("Disabling UseAdaptiveSizePolicy; it is incompatible with %s.",
217 collector_name);
218 }
219 FLAG_SET_DEFAULT(UseAdaptiveSizePolicy, false);
220 }
221}
222
223CollectedHeap* CMSArguments::create_heap() {
224 return new CMSHeap();
225}
226