Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/gc/arguments/TestMaxHeapSizeTools.java
40943 views
1
/*
2
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package gc.arguments;
25
26
import java.util.regex.Matcher;
27
import java.util.regex.Pattern;
28
import java.util.ArrayList;
29
import java.util.Arrays;
30
31
import jdk.test.lib.process.ProcessTools;
32
import jdk.test.lib.process.OutputAnalyzer;
33
import sun.hotspot.WhiteBox;
34
35
class ErgoArgsPrinter {
36
public static void main(String[] args) throws Exception {
37
WhiteBox wb = WhiteBox.getWhiteBox();
38
wb.printHeapSizes();
39
}
40
}
41
42
final class MinInitialMaxValues {
43
public long minHeapSize;
44
public long initialHeapSize;
45
public long maxHeapSize;
46
47
public long spaceAlignment;
48
public long heapAlignment;
49
}
50
51
class TestMaxHeapSizeTools {
52
53
public static void checkMinInitialMaxHeapFlags(String gcflag) throws Exception {
54
checkInvalidMinInitialHeapCombinations(gcflag);
55
checkValidMinInitialHeapCombinations(gcflag);
56
checkInvalidInitialMaxHeapCombinations(gcflag);
57
checkValidInitialMaxHeapCombinations(gcflag);
58
checkInvalidMinMaxHeapCombinations(gcflag);
59
checkValidMinMaxHeapCombinations(gcflag);
60
}
61
62
public static void checkMinInitialErgonomics(String gcflag) throws Exception {
63
// heap sizing ergonomics use the value NewSize + OldSize as default values
64
// for ergonomics calculation. Retrieve these values.
65
long[] values = new long[2];
66
getNewOldSize(gcflag, values);
67
68
// we check cases with values smaller and larger than this default value.
69
long newPlusOldSize = values[0] + values[1];
70
long smallValue = newPlusOldSize / 2;
71
long largeValue = newPlusOldSize * 2;
72
long maxHeapSize = largeValue + (2 * 1024 * 1024);
73
74
// -Xms is not set
75
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize }, values, -1, -1);
76
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:MinHeapSize=" + smallValue }, values, smallValue, -1);
77
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:MinHeapSize=" + largeValue }, values, largeValue, -1);
78
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:MinHeapSize=0" }, values, -1, -1);
79
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=" + smallValue }, values, -1, smallValue);
80
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue);
81
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=0" }, values, -1, -1);
82
// Some extra checks when both are set.
83
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:MinHeapSize=" + smallValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue);
84
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:MinHeapSize=" + smallValue, "-XX:InitialHeapSize=" + largeValue }, values, smallValue, largeValue);
85
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:MinHeapSize=" + largeValue, "-XX:InitialHeapSize=" + largeValue }, values, largeValue, largeValue);
86
87
// -Xms is set to zero
88
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0" }, values, -1, -1);
89
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:MinHeapSize=" + smallValue }, values, smallValue, -1);
90
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:MinHeapSize=" + largeValue }, values, largeValue, -1);
91
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:MinHeapSize=0" }, values, -1, -1);
92
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=" + smallValue }, values, -1, smallValue);
93
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue);
94
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=0" }, values, -1, -1);
95
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:MinHeapSize=" + smallValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue);
96
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:MinHeapSize=" + smallValue, "-XX:InitialHeapSize=" + largeValue }, values, smallValue, largeValue);
97
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:MinHeapSize=" + largeValue, "-XX:InitialHeapSize=" + largeValue }, values, largeValue, largeValue);
98
99
// -Xms is set to small value
100
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue }, values, -1, -1);
101
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:MinHeapSize=" + smallValue }, values, smallValue, smallValue);
102
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:MinHeapSize=0" }, values, -1, smallValue);
103
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue);
104
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=" + largeValue }, values, smallValue, largeValue);
105
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=0" }, values, smallValue, -1);
106
107
// -Xms is set to large value
108
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue }, values, largeValue, largeValue);
109
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue, "-XX:InitialHeapSize=0" }, values, largeValue, -1);
110
checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue, "-XX:MinHeapSize=0" }, values, -1, largeValue);
111
}
112
113
private static long align_up(long value, long alignment) {
114
long alignmentMinusOne = alignment - 1;
115
return (value + alignmentMinusOne) & ~alignmentMinusOne;
116
}
117
118
private static void getNewOldSize(String gcflag, long[] values) throws Exception {
119
ProcessBuilder pb = GCArguments.createJavaProcessBuilder(gcflag,
120
"-XX:+PrintFlagsFinal", "-version");
121
OutputAnalyzer output = new OutputAnalyzer(pb.start());
122
output.shouldHaveExitValue(0);
123
124
String stdout = output.getStdout();
125
values[0] = getFlagValue(" NewSize", stdout);
126
values[1] = getFlagValue(" OldSize", stdout);
127
}
128
129
public static void checkGenMaxHeapErgo(String gcflag) throws Exception {
130
TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 4);
131
TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 5);
132
}
133
134
private static void checkInvalidMinInitialHeapCombinations(String gcflag) throws Exception {
135
expectError(new String[] { gcflag, "-XX:InitialHeapSize=1023K", "-version" });
136
expectError(new String[] { gcflag, "-Xms64M", "-XX:InitialHeapSize=32M", "-version" });
137
expectError(new String[] { gcflag, "-XX:MinHeapSize=1023K", "-version" });
138
// Note: MinHeapSize values get aligned up by HeapAlignment which is 32M with 64k pages.
139
expectError(new String[] { gcflag, "-Xms4M", "-XX:MinHeapSize=64M", "-version" });
140
expectError(new String[] { gcflag, "-XX:MinHeapSize=8M -XX:InitialHeapSize=4m" });
141
}
142
143
private static void checkValidMinInitialHeapCombinations(String gcflag) throws Exception {
144
expectValid(new String[] { gcflag, "-XX:InitialHeapSize=1024K", "-version" });
145
expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms4M", "-version" });
146
expectValid(new String[] { gcflag, "-Xms4M", "-XX:InitialHeapSize=8M", "-version" });
147
expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms8M", "-version" });
148
expectValid(new String[] { gcflag, "-XX:MinHeapSize=1024K", "-version" });
149
expectValid(new String[] { gcflag, "-XX:MinHeapSize=8M", "-Xms4M", "-version" });
150
expectValid(new String[] { gcflag, "-XX:MinHeapSize=8M", "-Xms8M", "-version" });
151
expectValid(new String[] { gcflag, "-Xms8M", "-XX:MinHeapSize=4M", "-version" });
152
// the following is not an error as -Xms sets both minimal and initial heap size
153
expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-Xms8M", "-version" });
154
expectValid(new String[] { gcflag, "-XX:MinHeapSize=4M", "-Xms8M", "-version" });
155
}
156
157
private static void checkInvalidInitialMaxHeapCombinations(String gcflag) throws Exception {
158
expectError(new String[] { gcflag, "-XX:MaxHeapSize=2047K", "-version" });
159
expectError(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=8M", "-version" });
160
expectError(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-XX:MaxHeapSize=4M", "-version" });
161
}
162
163
private static void checkInvalidMinMaxHeapCombinations(String gcflag) throws Exception {
164
expectError(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:MinHeapSize=8M", "-version" });
165
expectError(new String[] { gcflag, "-XX:MinHeapSize=8M", "-XX:MaxHeapSize=4M", "-version" });
166
}
167
168
169
private static void checkValidInitialMaxHeapCombinations(String gcflag) throws Exception {
170
expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-XX:MaxHeapSize=8M", "-version" });
171
expectValid(new String[] { gcflag, "-XX:MaxHeapSize=8M", "-XX:InitialHeapSize=4M", "-version" });
172
expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=4M", "-version" });
173
// a value of "0" for initial heap size means auto-detect
174
expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=0M", "-version" });
175
}
176
177
private static void checkValidMinMaxHeapCombinations(String gcflag) throws Exception {
178
expectValid(new String[] { gcflag, "-XX:MinHeapSize=4M", "-XX:MaxHeapSize=8M", "-version" });
179
expectValid(new String[] { gcflag, "-XX:MaxHeapSize=8M", "-XX:MinHeapSize=4M", "-version" });
180
expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:MinHeapSize=4M", "-version" });
181
// a value of "0" for min heap size means auto-detect
182
expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:MinHeapSize=0M", "-version" });
183
}
184
185
private static long valueAfter(String source, String match) {
186
int start = source.indexOf(match) + match.length();
187
String tail = source.substring(start).split(" ")[0];
188
return Long.parseLong(tail);
189
}
190
191
/**
192
* Executes a new VM process with the given class and parameters.
193
* @param vmargs Arguments to the VM to run
194
* @param classname Name of the class to run
195
* @param arguments Arguments to the class
196
* @return The OutputAnalyzer with the results for the invocation.
197
*/
198
public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments) throws Exception {
199
ArrayList<String> finalargs = new ArrayList<String>();
200
201
String[] whiteboxOpts = new String[] {
202
"-Xbootclasspath/a:.",
203
"-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
204
"-cp", System.getProperty("java.class.path"),
205
};
206
207
finalargs.addAll(Arrays.asList(vmargs));
208
finalargs.addAll(Arrays.asList(whiteboxOpts));
209
finalargs.add(classname);
210
finalargs.addAll(Arrays.asList(arguments));
211
212
ProcessBuilder pb = GCArguments.createJavaProcessBuilder(finalargs.toArray(String[]::new));
213
OutputAnalyzer output = new OutputAnalyzer(pb.start());
214
output.shouldHaveExitValue(0);
215
216
return output;
217
}
218
219
private static void getMinInitialMaxHeap(String[] args, MinInitialMaxValues val) throws Exception {
220
OutputAnalyzer output = runWhiteBoxTest(args, ErgoArgsPrinter.class.getName(), new String[] {});
221
222
// the output we watch for has the following format:
223
//
224
// "Minimum heap X Initial heap Y Maximum heap Z Min alignment A Max Alignment B"
225
//
226
// where A, B, X, Y and Z are sizes in bytes.
227
// Unfortunately there is no other way to retrieve the minimum heap size and
228
// the alignments.
229
230
Matcher m = Pattern.compile("Minimum heap \\d+ Initial heap \\d+ Maximum heap \\d+ Space alignment \\d+ Heap alignment \\d+").
231
matcher(output.getStdout());
232
if (!m.find()) {
233
throw new RuntimeException("Could not find heap size string.");
234
}
235
236
String match = m.group();
237
238
// actual values
239
val.minHeapSize = valueAfter(match, "Minimum heap ");
240
val.initialHeapSize = valueAfter(match, "Initial heap ");
241
val.maxHeapSize = valueAfter(match, "Maximum heap ");
242
val.spaceAlignment = valueAfter(match, "Space alignment ");
243
val.heapAlignment = valueAfter(match, "Heap alignment ");
244
}
245
246
/**
247
* Verify whether the VM automatically synchronizes minimum and initial heap size if only
248
* one is given for the GC specified.
249
*/
250
public static void checkErgonomics(String[] args, long[] newoldsize,
251
long expectedMin, long expectedInitial) throws Exception {
252
253
MinInitialMaxValues v = new MinInitialMaxValues();
254
getMinInitialMaxHeap(args, v);
255
256
if ((expectedMin != -1) && (align_up(expectedMin, v.heapAlignment) != v.minHeapSize)) {
257
throw new RuntimeException("Actual minimum heap size of " + v.minHeapSize +
258
" differs from expected minimum heap size of " + expectedMin);
259
}
260
261
if ((expectedInitial != -1) && (align_up(expectedInitial, v.heapAlignment) != v.initialHeapSize)) {
262
throw new RuntimeException("Actual initial heap size of " + v.initialHeapSize +
263
" differs from expected initial heap size of " + expectedInitial);
264
}
265
266
// always check the invariant min <= initial <= max heap size
267
if (!(v.minHeapSize <= v.initialHeapSize && v.initialHeapSize <= v.maxHeapSize)) {
268
throw new RuntimeException("Inconsistent min/initial/max heap sizes, they are " +
269
v.minHeapSize + "/" + v.initialHeapSize + "/" + v.maxHeapSize);
270
}
271
}
272
273
/**
274
* Verify whether the VM respects the given maximum heap size in MB for the
275
* GC specified.
276
* @param gcflag The garbage collector to test as command line flag. E.g. -XX:+UseG1GC
277
* @param maxHeapSize the maximum heap size to verify, in MB.
278
*/
279
public static void checkGenMaxHeapSize(String gcflag, long maxHeapsize) throws Exception {
280
final long K = 1024;
281
282
MinInitialMaxValues v = new MinInitialMaxValues();
283
getMinInitialMaxHeap(new String[] { gcflag, "-XX:MaxHeapSize=" + maxHeapsize + "M" }, v);
284
285
long expectedHeapSize = align_up(maxHeapsize * K * K, v.heapAlignment);
286
long actualHeapSize = v.maxHeapSize;
287
288
if (actualHeapSize > expectedHeapSize) {
289
throw new RuntimeException("Heap has " + actualHeapSize +
290
" bytes, expected to be less than " + expectedHeapSize);
291
}
292
}
293
294
private static long getFlagValue(String flag, String where) {
295
Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);
296
if (!m.find()) {
297
throw new RuntimeException("Could not find value for flag " + flag + " in output string");
298
}
299
String match = m.group();
300
return Long.parseLong(match.substring(match.lastIndexOf(" ") + 1, match.length()));
301
}
302
303
private static void shouldContainOrNot(OutputAnalyzer output, boolean contains, String message) throws Exception {
304
if (contains) {
305
output.shouldContain(message);
306
} else {
307
output.shouldNotContain(message);
308
}
309
}
310
311
private static void expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception {
312
ProcessBuilder pb = GCArguments.createJavaProcessBuilder(flags);
313
OutputAnalyzer output = new OutputAnalyzer(pb.start());
314
shouldContainOrNot(output, hasWarning, "Warning");
315
shouldContainOrNot(output, hasError, "Error");
316
output.shouldHaveExitValue(errorcode);
317
}
318
319
private static void expectError(String[] flags) throws Exception {
320
expect(flags, false, true, 1);
321
}
322
323
private static void expectValid(String[] flags) throws Exception {
324
expect(flags, false, false, 0);
325
}
326
}
327
328