Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/gc/arguments/TestSurvivorRatioFlag.java
40942 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 TestSurvivorRatioFlag
28
* @summary Verify that actual survivor ratio is equal to specified SurvivorRatio value
29
* @requires vm.gc != "Z" & vm.gc != "Shenandoah"
30
* @library /test/lib
31
* @library /
32
* @modules java.base/jdk.internal.misc
33
* java.management
34
* @build sun.hotspot.WhiteBox
35
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
36
* @run driver gc.arguments.TestSurvivorRatioFlag
37
*/
38
39
import java.lang.management.MemoryUsage;
40
import java.util.Arrays;
41
import java.util.Collections;
42
import java.util.LinkedList;
43
import jdk.test.lib.process.OutputAnalyzer;
44
import jdk.test.lib.process.ProcessTools;
45
import jdk.test.lib.Utils;
46
import sun.hotspot.WhiteBox;
47
48
public class TestSurvivorRatioFlag {
49
50
public static final long M = 1024 * 1024;
51
public static final long HEAP_SIZE = 200 * M;
52
public static final long NEW_SIZE = 100 * M;
53
54
public static void main(String args[]) throws Exception {
55
LinkedList<String> options = new LinkedList<>(
56
Arrays.asList(Utils.getFilteredTestJavaOpts("-XX:[^ ]*SurvivorRatio=[^ ]+"))
57
);
58
59
testSurvivorRatio(3, options);
60
testSurvivorRatio(6, options);
61
testSurvivorRatio(10, options);
62
testSurvivorRatio(15, options);
63
testSurvivorRatio(20, options);
64
}
65
66
/**
67
* Verify that actual survivor ratio equal to specified.
68
*
69
* @param ratio survivor ratio that be verified
70
* @param options additional options to JVM
71
*/
72
public static void testSurvivorRatio(int ratio, LinkedList<String> options) throws Exception {
73
74
LinkedList<String> vmOptions = new LinkedList<>(options);
75
76
Collections.addAll(vmOptions,
77
"-Xbootclasspath/a:.",
78
"--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
79
"-XX:+UnlockDiagnosticVMOptions",
80
"-XX:+WhiteBoxAPI",
81
"-XX:GCLockerEdenExpansionPercent=0",
82
"-XX:MaxNewSize=" + NEW_SIZE,
83
"-XX:NewSize=" + NEW_SIZE,
84
"-Xmx" + HEAP_SIZE,
85
"-Xms" + HEAP_SIZE,
86
"-XX:SurvivorRatio=" + ratio,
87
SurvivorRatioVerifier.class.getName(),
88
Integer.toString(ratio)
89
);
90
91
ProcessBuilder procBuilder = GCArguments.createJavaProcessBuilder(vmOptions);
92
OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
93
analyzer.shouldHaveExitValue(0);
94
}
95
96
/**
97
* Class that verifies survivor ratio.
98
*/
99
public static class SurvivorRatioVerifier {
100
101
static WhiteBox wb = WhiteBox.getWhiteBox();
102
103
public static final int MAX_ITERATIONS = 10;
104
public static final int ARRAY_LENGTH = 10000;
105
public static final int CHUNK_SIZE = 10000;
106
107
public static void main(String args[]) throws Exception {
108
if (args.length != 1) {
109
throw new IllegalArgumentException("Expected 1 arg: <ratio>");
110
}
111
final int ratio = Integer.valueOf(args[0]);
112
113
AllocationHelper allocator = new AllocationHelper(MAX_ITERATIONS, ARRAY_LENGTH, CHUNK_SIZE, () -> (verifySurvivorRatio(ratio)));
114
allocator.allocateMemoryAndVerify();
115
}
116
117
/**
118
* Verify that actual survivor ratio is equal to expected.
119
* Depending on selected young GC we verify that:
120
* - for DefNew and ParNew: eden_size / survivor_size is close to expectedRatio;
121
* - for PSNew: survivor_size equal to young_gen_size / expectedRatio;
122
* - for G1: survivor_regions <= young_list_length / expectedRatio.
123
*/
124
public static Void verifySurvivorRatio(int expectedRatio) {
125
GCTypes.YoungGCType type = GCTypes.YoungGCType.getYoungGCType();
126
switch (type) {
127
case DefNew:
128
verifyDefNewSurvivorRatio(expectedRatio);
129
break;
130
case PSNew:
131
verifyPSSurvivorRatio(expectedRatio);
132
break;
133
case G1:
134
verifyG1SurvivorRatio(expectedRatio);
135
break;
136
default:
137
throw new RuntimeException("Unexpected young GC type");
138
}
139
return null;
140
}
141
142
private static void verifyDefNewSurvivorRatio(int expectedRatio) {
143
MemoryUsage edenUsage = HeapRegionUsageTool.getEdenUsage();
144
MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage();
145
146
int actualRatio = (int) (edenUsage.getCommitted() / survivorUsage.getCommitted());
147
if (Math.abs(actualRatio - expectedRatio) > 1) {
148
throw new RuntimeException("Expected survivor ratio is: " + expectedRatio
149
+ ", but observed ratio is: " + actualRatio);
150
}
151
}
152
153
private static void verifyPSSurvivorRatio(int expectedRatio) {
154
MemoryUsage edenUsage = HeapRegionUsageTool.getEdenUsage();
155
MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage();
156
157
long youngGenSize = edenUsage.getMax() + 2 * survivorUsage.getMax();
158
// for Paralle GC Min/InitialSurvivorRatio = SurvivorRatio + 2
159
long expectedSize = HeapRegionUsageTool.alignDown(youngGenSize / (expectedRatio + 2),
160
wb.psHeapGenerationAlignment());
161
162
if (expectedSize != survivorUsage.getCommitted()) {
163
throw new RuntimeException("Expected survivor size is: " + expectedSize
164
+ ", but observed size is: " + survivorUsage.getCommitted());
165
}
166
}
167
168
private static void verifyG1SurvivorRatio(int expectedRatio) {
169
MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage();
170
171
int regionSize = wb.g1RegionSize();
172
int youngListLength = (int) Math.max(NEW_SIZE / regionSize, 1);
173
int expectedSurvivorRegions = (int) Math.ceil(youngListLength / (double) expectedRatio);
174
int observedSurvivorRegions = (int) (survivorUsage.getCommitted() / regionSize);
175
176
if (expectedSurvivorRegions < observedSurvivorRegions) {
177
throw new RuntimeException("Expected amount of G1 survivor regions is "
178
+ expectedSurvivorRegions + ", but observed "
179
+ observedSurvivorRegions);
180
}
181
}
182
}
183
}
184
185