Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/metaspace/gc/HighWaterMarkTest.java
41155 views
1
/*
2
* Copyright (c) 2013, 2019, 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 metaspace.gc;
25
26
import java.util.Arrays;
27
import vm.share.VMRuntimeEnvUtils;
28
29
/**
30
* Test metaspace ergonomic.
31
*
32
* <ul>
33
* <li>MetaspaceSize
34
* <li>MaxMetaspaceSize
35
* <li>MinMetaspaceFreeRatio
36
* <li>MaxMetaspaceFreeRatio
37
* </ul>
38
*
39
* The test loads classes until the committed metaspace achieves the certain
40
* level between MetaspaceSize and MaxMetaspaceSize.
41
* Then it counts how many times GC has been induced.
42
* Test verifies that MinMetaspaceFreeRatio/MaxMetaspaceFreeRatio settings
43
* affect the frequency of GC. (High-water mark)
44
*
45
* Quoting: Java SE 8 HotSpot[tm] Virtual Machine Garbage Collection Tuning
46
* <pre>
47
* Class metadata is deallocated when the corresponding Java class is unloaded.
48
* Java classes are unloaded as a results of garbage collection and garbage
49
* collections may be induced in order to unload classes and deallocate class
50
* metadata. When the space used for class metadata reaches a certain level
51
* (call it a high-water mark), a garbage collection is induced.
52
* After the garbage collection the high-water mark may be raised or lowered
53
* depending on the amount of space freed from class metadata. The high-water
54
* mark would be raised so as not to induce another garbage collection too soon.
55
* The high-water mark is initially set to the value of the command-line
56
* flag MetaspaceSize . It is raised or lowered based on the flags
57
* MaxMetaspaceFreeRatio and MinMetaspaceFreeRatio.
58
* If the committed space available for class metadata as a percentage of
59
* the total committed space for class metadata is greater than
60
* MaxMetaspaceFreeRatio, the high-water mark will be lowered.
61
* If it is less than MinMetaspaceFreeRatio, the high-water mark will be raised.
62
* </pre>
63
*/
64
public class HighWaterMarkTest extends FirstGCTest {
65
66
public static void main(String... args) {
67
new HighWaterMarkTest().run(args);
68
}
69
70
// value given in -XX:MetaspaceSize=<value>
71
private long metaspaceSize = -1;
72
73
// value given in -XX:MaxMetaspaceSize=<value>
74
private long maxMetaspaceSize = -1;
75
76
// value given in -XX:MinMetaspaceFreeRatio=<value>
77
private long minMetaspaceFreeRatio = -1;
78
79
// value given in -XX:MaxMetaspaceFreeRatio=<value>
80
private long maxMetaspaceFreeRatio = -1;
81
82
/**
83
* Parses arguments and vm options.
84
* Throws Fault in cases of wrong values or missed parameters.
85
*
86
* @param args command line options
87
*/
88
@Override
89
protected void parseArgs(String[] args) {
90
if (args.length > 0) {
91
printUsage();
92
throw new Fault("Illegal arguments: " + Arrays.asList(args));
93
}
94
95
if (gclogFileName == null) {
96
printUsage();
97
throw new Fault("Log file name is not given");
98
}
99
100
final String metaSize = "-XX:MetaspaceSize=";
101
final String maxMetaSize = "-XX:MaxMetaspaceSize=";
102
final String minRatio = "-XX:MinMetaspaceFreeRatio=";
103
final String maxRatio = "-XX:MaxMetaspaceFreeRatio=";
104
105
for (String va: vmArgs) {
106
if (va.startsWith(metaSize)) {
107
metaspaceSize = parseValue(va.substring(metaSize.length()));
108
} else if (va.startsWith(maxMetaSize)) {
109
maxMetaspaceSize = parseValue(va.substring(maxMetaSize.length()));
110
} else if (va.startsWith(minRatio)) {
111
minMetaspaceFreeRatio = parseValue(va.substring(minRatio.length()));
112
} else if (va.startsWith(maxRatio)) {
113
maxMetaspaceFreeRatio = parseValue(va.substring(maxRatio.length()));
114
}
115
}
116
117
if (metaspaceSize < 0) {
118
printUsage();
119
throw new Fault("-XX:MetaspaceSize is not specified");
120
} else if (maxMetaspaceSize < 0) {
121
printUsage();
122
throw new Fault("-XX:MaxMetaspaceSize is not specified");
123
} else if (minMetaspaceFreeRatio < 0) {
124
printUsage();
125
throw new Fault("-XX:MinMetaspaceFreeRatio is not specified");
126
} else if (maxMetaspaceFreeRatio < 0) {
127
printUsage();
128
throw new Fault("-XX:MaxMetaspaceFreeRatio is not specified");
129
}
130
131
}
132
133
private void printUsage() {
134
System.err.println("Usage: ");
135
System.err.println("java [-Xlog:gc:<filename>] [-XX:MetaspaceSize=..] [-XX:MaxMetaspaceSize=..] [-XX:MinMetaspaceFreeRatio=..] [-XX:MaxMetaspaceFreeRatio=..] \\");
136
System.err.println(" " + HighWaterMarkTest.class.getCanonicalName());
137
}
138
139
/**
140
* Check that MinMetaspaceFreeRatio/MaxMetaspaceFreeRatio settings
141
* affects the moment of the next GC.
142
*
143
* Eats memory until amount of committed metaspace achieves a certain level
144
* (between MetaspaceSize and MaxMetaspaceSize).
145
* Then checks how many times GC has been invoked.
146
*
147
*/
148
@Override
149
public void doCheck() {
150
151
// to avoid timeouts we limit the number of attempts
152
int attempts = 0;
153
int maxAttempts = 10_000;
154
155
// in between metaspaceSize and maxMetaspaceSize
156
// no OOM is exepcted.
157
long committedLevel = (metaspaceSize + maxMetaspaceSize) / 2;
158
159
while (getCommitted() < committedLevel && attempts < maxAttempts) {
160
attempts++;
161
loadNewClasses(9, true); // load classes and keep references
162
loadNewClasses(1, false); // load classes without keeping references
163
}
164
165
166
System.out.println("% Classes loaded: " + attempts*10);
167
System.out.println("% Used metaspace : " + bytes2k(getUsed()));
168
System.out.println("% Committed metaspce: " + bytes2k(getCommitted()));
169
170
cleanLoadedClasses();
171
172
if (attempts == maxAttempts) {
173
throw new Fault("Committed amount hasn't achieved " + bytes2k(committedLevel));
174
}
175
176
int gcCount = getMetaspaceGCCount();
177
if (gcCount < 0) {
178
// perhpas, it's better to silently pass here... Let's see.
179
throw new Fault ("Unable to count full collections, could be an env issue");
180
}
181
System.out.println("% GC has been invoked: " + gcCount + " times");
182
183
if (maxMetaspaceFreeRatio <= 1) {
184
// min/max = 0/1 boundary value
185
// GC should happen very often
186
checkGCCount(gcCount, 20, -1);
187
} else if (minMetaspaceFreeRatio >= 99) {
188
// min/max = 99/100 boundary value
189
// GC should happen very rare
190
checkGCCount(gcCount, -1, 2);
191
} else if (minMetaspaceFreeRatio >= 10 && maxMetaspaceFreeRatio <= 20) {
192
// GC should happen quite often
193
checkGCCount(gcCount, 3, 30);
194
} else if (minMetaspaceFreeRatio >= 70 && maxMetaspaceFreeRatio <= 80) {
195
// GC should happen quite often
196
checkGCCount(gcCount, 1, 3);
197
} else {
198
// hard to estimate
199
}
200
201
}
202
/**
203
* Checks that count of GC fits the expected range.
204
* Throws Fault if count is unexpected.
205
*
206
* @param count how many times GC has happened
207
* @param min expected minimum, if under zero - undefined
208
* @param max expected maximum, if under zero - undefined
209
*/
210
void checkGCCount(int count, int min, int max) {
211
if (min < 0) {
212
if(count > max) {
213
throw new Fault("GC has happened too often: " + count + " times, " +
214
"expected count: less than " + max);
215
}
216
} else if (max < 0) {
217
if(count < min) {
218
throw new Fault("GC has happened too rare: " + count + " times, " +
219
"expected count greater than " + min);
220
}
221
} else if (count < min || count > max ) {
222
throw new Fault ("GC has happened " + count + " times, " +
223
"approximate count is " + min + " to " + max);
224
}
225
}
226
227
}
228
229