Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/gc/arguments/TestTargetSurvivorRatioFlag.java
40948 views
1
/*
2
* Copyright (c) 2015, 2021, 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
/*
27
* @test TestTargetSurvivorRatioFlag
28
* @summary Verify that option TargetSurvivorRatio affects survivor space occupancy after minor GC.
29
* @requires vm.opt.ExplicitGCInvokesConcurrent != true
30
* @requires vm.opt.UseJVMCICompiler != true
31
* @requires vm.gc != "Z" & vm.gc != "Shenandoah"
32
* @library /test/lib
33
* @library /
34
* @modules java.base/jdk.internal.misc
35
* java.management
36
* @build sun.hotspot.WhiteBox
37
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
38
* @run driver gc.arguments.TestTargetSurvivorRatioFlag
39
*/
40
41
import java.lang.management.GarbageCollectorMXBean;
42
import java.util.Arrays;
43
import java.util.Collections;
44
import java.util.LinkedList;
45
import java.util.List;
46
import java.util.regex.Matcher;
47
import java.util.regex.Pattern;
48
import jdk.internal.misc.Unsafe;
49
import jdk.test.lib.process.OutputAnalyzer;
50
import jdk.test.lib.Utils;
51
import sun.hotspot.WhiteBox;
52
import static gc.testlibrary.Allocation.blackHole;
53
54
/* In order to test that TargetSurvivorRatio affects survivor space occupancy
55
* we setup fixed MaxTenuringThreshold and then verifying that if size of allocated
56
* objects is lower than (survivor_size * TargetSurvivorRatio / 100) then objects
57
* will stay in survivor space until MaxTenuringThreshold minor GC cycles.
58
* If more than (survivor_size * TargetSurvivorRatio / 100) objects were allocated,
59
* then we verify that after MaxTenuringThreshold minor GC cycles survivor space
60
* is almost empty.
61
*/
62
public class TestTargetSurvivorRatioFlag {
63
64
public static final long M = 1024 * 1024;
65
66
// VM option values
67
public static final long MAX_NEW_SIZE = 40 * M;
68
public static final int SURVIVOR_RATIO = 8;
69
public static final int MAX_TENURING_THRESHOLD = 15;
70
71
// Value used to estimate amount of memory that should be allocated
72
// and placed in survivor space.
73
public static final double DELTA = 0.25;
74
75
// Max variance of observed ratio
76
public static double VARIANCE = 1;
77
78
// Messages used by debuggee
79
public static final String UNSUPPORTED_GC = "Unsupported GC";
80
public static final String START_TEST = "Start test";
81
public static final String END_TEST = "End test";
82
83
// Patterns used during log parsing
84
public static final String TENURING_DISTRIBUTION = "Desired survivor size";
85
public static final String AGE_TABLE_ENTRY = ".*-[\\s]+age[\\s]+([0-9]+):[\\s]+([0-9]+)[\\s]+bytes,[\\s]+([0-9]+)[\\s]+total";
86
public static final String MAX_SURVIVOR_SIZE = "Max survivor size: ([0-9]+)";
87
88
public static void main(String args[]) throws Exception {
89
90
LinkedList<String> options = new LinkedList<>(Arrays.asList(Utils.getTestJavaOpts()));
91
92
// Need to consider the effect of TargetPLABWastePct=1 for G1 GC
93
if (options.contains("-XX:+UseG1GC")) {
94
VARIANCE = 2;
95
} else {
96
VARIANCE = 1;
97
}
98
99
negativeTest(-1, options);
100
negativeTest(101, options);
101
102
positiveTest(20, options);
103
positiveTest(30, options);
104
positiveTest(55, options);
105
positiveTest(70, options);
106
}
107
108
/**
109
* Verify that VM will fail to start with specified TargetSurvivorRatio
110
*
111
* @param ratio value of TargetSurvivorRatio
112
* @param options additional VM options
113
*/
114
public static void negativeTest(int ratio, LinkedList<String> options) throws Exception {
115
LinkedList<String> vmOptions = new LinkedList<>(options);
116
vmOptions.add("-XX:TargetSurvivorRatio=" + ratio);
117
vmOptions.add("-version");
118
119
ProcessBuilder procBuilder = GCArguments.createJavaProcessBuilder(vmOptions);
120
OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
121
122
analyzer.shouldHaveExitValue(1);
123
analyzer.shouldContain("Error: Could not create the Java Virtual Machine.");
124
}
125
126
/**
127
* Verify that actual survivor space usage ratio conforms specified TargetSurvivorRatio
128
*
129
* @param ratio value of TargetSurvivorRatio
130
* @param options additional VM options
131
*/
132
public static void positiveTest(int ratio, LinkedList<String> options) throws Exception {
133
LinkedList<String> vmOptions = new LinkedList<>(options);
134
Collections.addAll(vmOptions,
135
"-Xbootclasspath/a:.",
136
"--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
137
"-XX:+UnlockDiagnosticVMOptions",
138
"-XX:+WhiteBoxAPI",
139
"-XX:+UseAdaptiveSizePolicy",
140
"-Xlog:gc+age=trace",
141
"-XX:MaxTenuringThreshold=" + MAX_TENURING_THRESHOLD,
142
"-XX:NewSize=" + MAX_NEW_SIZE,
143
"-XX:MaxNewSize=" + MAX_NEW_SIZE,
144
"-XX:InitialHeapSize=" + 2 * MAX_NEW_SIZE,
145
"-XX:MaxHeapSize=" + 2 * MAX_NEW_SIZE,
146
"-XX:SurvivorRatio=" + SURVIVOR_RATIO,
147
"-XX:TargetSurvivorRatio=" + ratio,
148
// For reducing variance of survivor size.
149
"-XX:TargetPLABWastePct=" + 1,
150
TargetSurvivorRatioVerifier.class.getName(),
151
Integer.toString(ratio)
152
);
153
154
ProcessBuilder procBuilder = GCArguments.createJavaProcessBuilder(vmOptions);
155
OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
156
157
analyzer.shouldHaveExitValue(0);
158
159
String output = analyzer.getOutput();
160
161
// Test avoids verification for parallel GC
162
if (!output.contains(UNSUPPORTED_GC)) {
163
// Two tests should be done - when actual ratio is lower than TargetSurvivorRatio
164
// and when it is higher. We chech that output contains results for exactly two tests.
165
List<Double> ratios = parseTestOutput(output);
166
167
if (ratios.size() != 2) {
168
System.out.println(output);
169
throw new RuntimeException("Expected number of ratios extraced for output is 2,"
170
+ " but " + ratios.size() + " ratios were extracted");
171
}
172
173
// At the end of the first test survivor space usage ratio should lies between
174
// TargetSurvivorRatio and TargetSurvivorRatio - 2*DELTA
175
if (ratio < ratios.get(0) || ratio - ratios.get(0) > VARIANCE) {
176
System.out.println(output);
177
throw new RuntimeException("Survivor space usage ratio expected to be close to "
178
+ ratio + ", but observed ratio is: " + ratios.get(0));
179
}
180
181
// After second test survivor space should be almost empty.
182
if (ratios.get(1) > VARIANCE) {
183
System.out.println(output);
184
throw new RuntimeException("Survivor space expected to be empty due to "
185
+ "TargetSurvivorRatio overlimit, however observed "
186
+ "survivor space usage ratio is: " + ratios.get(1));
187
}
188
} else {
189
System.out.println("Selected GC does not support TargetSurvivorRatio option.");
190
}
191
}
192
193
/**
194
* Parse output produced by TargetSurvivorRatioVerifier.
195
*
196
* @param output output obtained from TargetSurvivorRatioVerifier
197
* @return list of parsed test results, where each result is an actual
198
* survivor ratio after MaxTenuringThreshold minor GC cycles.
199
*/
200
public static List<Double> parseTestOutput(String output) {
201
List<Double> ratios = new LinkedList<Double>();
202
String lines[] = output.split("[\n\r]");
203
boolean testStarted = false;
204
long survivorSize = 0;
205
long survivorOccupancy = 0;
206
int gcCount = 0;
207
Pattern ageTableEntry = Pattern.compile(AGE_TABLE_ENTRY);
208
Pattern maxSurvivorSize = Pattern.compile(MAX_SURVIVOR_SIZE);
209
for (String line : lines) {
210
if (Pattern.matches(MAX_SURVIVOR_SIZE, line)) {
211
// We found estimated survivor space size
212
Matcher m = maxSurvivorSize.matcher(line);
213
m.find();
214
survivorSize = Long.valueOf(m.group(1));
215
} else if (line.contains(START_TEST) && !testStarted) {
216
// Start collecting test results
217
testStarted = true;
218
gcCount = 0;
219
} else if (testStarted) {
220
if (line.contains(TENURING_DISTRIBUTION)) {
221
// We found start of output emitted by -XX:+PrintTenuringDistribution
222
// If it is associated with "MaxTenuringThreshold" GC cycle, then it's
223
// time to report observed survivor usage ratio
224
gcCount++;
225
double survivorRatio = survivorOccupancy / (double) survivorSize;
226
if (gcCount == MAX_TENURING_THRESHOLD || gcCount == MAX_TENURING_THRESHOLD * 2) {
227
ratios.add(survivorRatio * 100.0);
228
testStarted = false;
229
}
230
survivorOccupancy = 0;
231
} else if (Pattern.matches(AGE_TABLE_ENTRY, line)) {
232
// Obtain survivor space usage from "total" age table log entry
233
Matcher m = ageTableEntry.matcher(line);
234
m.find();
235
survivorOccupancy = Long.valueOf(m.group(3));
236
} else if (line.contains(END_TEST)) {
237
// It is expected to find at least MaxTenuringThreshold GC events
238
// until test end
239
if (gcCount < MAX_TENURING_THRESHOLD) {
240
throw new RuntimeException("Observed " + gcCount + " GC events, "
241
+ "while it is expected to see at least "
242
+ MAX_TENURING_THRESHOLD);
243
}
244
testStarted = false;
245
}
246
}
247
}
248
return ratios;
249
}
250
251
public static class TargetSurvivorRatioVerifier {
252
253
static final WhiteBox wb = WhiteBox.getWhiteBox();
254
static final Unsafe unsafe = Unsafe.getUnsafe();
255
256
// Desired size of memory allocated at once
257
public static final int CHUNK_SIZE = 1024;
258
// Length of byte[] array that will have occupy CHUNK_SIZE bytes in heap
259
public static final int ARRAY_LENGTH = CHUNK_SIZE - Unsafe.ARRAY_BYTE_BASE_OFFSET;
260
261
public static void main(String args[]) throws Exception {
262
if (args.length != 1) {
263
throw new IllegalArgumentException("Expected 1 arg: <ratio>");
264
}
265
if (GCTypes.YoungGCType.getYoungGCType() == GCTypes.YoungGCType.PSNew) {
266
System.out.println(UNSUPPORTED_GC);
267
return;
268
}
269
270
int ratio = Integer.valueOf(args[0]);
271
long maxSurvivorSize = getMaxSurvivorSize();
272
System.out.println("Max survivor size: " + maxSurvivorSize);
273
274
allocateMemory(ratio - DELTA, maxSurvivorSize);
275
allocateMemory(ratio + DELTA, maxSurvivorSize);
276
}
277
278
/**
279
* Allocate (<b>ratio</b> * <b>maxSize</b> / 100) bytes of objects
280
* and force at least "MaxTenuringThreshold" minor GCs.
281
*
282
* @param ratio ratio used to calculate how many objects should be allocated
283
* @param maxSize estimated max survivor space size
284
*/
285
public static void allocateMemory(double ratio, long maxSize) throws Exception {
286
GarbageCollectorMXBean youngGCBean = GCTypes.YoungGCType.getYoungGCBean();
287
long garbageSize = (long) (maxSize * (ratio / 100.0));
288
int arrayLength = (int) (garbageSize / CHUNK_SIZE);
289
AllocationHelper allocator = new AllocationHelper(1, arrayLength, ARRAY_LENGTH, null);
290
291
System.out.println(START_TEST);
292
System.gc();
293
final long initialGcId = youngGCBean.getCollectionCount();
294
// allocate memory
295
allocator.allocateMemoryAndVerify();
296
297
// force minor GC
298
while (youngGCBean.getCollectionCount() <= initialGcId + MAX_TENURING_THRESHOLD * 2) {
299
blackHole(new byte[ARRAY_LENGTH]);
300
}
301
302
allocator.release();
303
System.out.println(END_TEST);
304
}
305
306
/**
307
* Estimate max survivor space size.
308
*
309
* For non-G1 GC returns value reported by MemoryPoolMXBean
310
* associated with survivor space.
311
* For G1 GC return max number of survivor regions * region size.
312
* Number if survivor regions estimated from MaxNewSize and SurvivorRatio.
313
*/
314
public static long getMaxSurvivorSize() {
315
if (GCTypes.YoungGCType.getYoungGCType() == GCTypes.YoungGCType.G1) {
316
int youngLength = (int) Math.max(MAX_NEW_SIZE / wb.g1RegionSize(), 1);
317
return (long) Math.ceil(youngLength / (double) SURVIVOR_RATIO) * wb.g1RegionSize();
318
} else {
319
return HeapRegionUsageTool.getSurvivorUsage().getMax();
320
}
321
}
322
}
323
}
324
325