Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/lib-test/sun/hotspot/whitebox/vm_flags/VmFlagTest.java
41155 views
1
/*
2
* Copyright (c) 2014, 2016, 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
import java.util.function.BiConsumer;
25
import java.util.function.Function;
26
import sun.hotspot.WhiteBox;
27
import sun.management.*;
28
import com.sun.management.*;
29
import jdk.test.lib.Asserts;
30
import java.lang.management.ManagementFactory;
31
32
public final class VmFlagTest<T> {
33
public static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
34
35
private static final String NONEXISTENT_FLAG = "NonexistentFlag";
36
private final String flagName;
37
private final BiConsumer<T, T> test;
38
private final BiConsumer<String, T> set;
39
private final Function<String, T> get;
40
private final boolean isPositive;
41
42
protected VmFlagTest(String flagName, BiConsumer<String, T> set,
43
Function<String, T> get, boolean isPositive) {
44
this.flagName = flagName;
45
this.set = set;
46
this.get = get;
47
this.isPositive = isPositive;
48
if (isPositive) {
49
test = this::testWritePositive;
50
} else {
51
test = this::testWriteNegative;
52
}
53
}
54
55
private void setNewValue(T value) {
56
set.accept(flagName, value);
57
}
58
59
private T getValue() {
60
return get.apply(flagName);
61
}
62
63
protected static <T> void runTest(String existentFlag, T[] tests,
64
BiConsumer<String, T> set, Function<String, T> get) {
65
runTest(existentFlag, tests, tests, set, get);
66
}
67
68
protected static <T> void runTest(String existentFlag, Function<String, T> get) {
69
runTest(existentFlag, null, null, null, get);
70
}
71
72
protected static <T> void runTest(String existentFlag, T[] tests,
73
T[] results, BiConsumer<String, T> set, Function<String, T> get) {
74
if (existentFlag != null) {
75
new VmFlagTest(existentFlag, set, get, true).test(tests, results);
76
}
77
new VmFlagTest(NONEXISTENT_FLAG, set, get, false).test(tests, results);
78
}
79
80
public final void test(T[] tests, T[] results) {
81
if (isPositive) {
82
testRead();
83
}
84
if (tests != null) {
85
Asserts.assertEQ(tests.length, results.length, "[TESTBUG] tests.length != results.length");
86
for (int i = 0, n = tests.length ; i < n; ++i) {
87
test.accept(tests[i], results[i]);
88
}
89
}
90
}
91
92
protected String getVMOptionAsString() {
93
if (WHITE_BOX.isConstantVMFlag(flagName) || WHITE_BOX.isLockedVMFlag(flagName)) {
94
// JMM cannot access debug flags in product builds or locked flags,
95
// use whitebox methods to get such flags value.
96
return asString(getValue());
97
}
98
HotSpotDiagnosticMXBean diagnostic
99
= ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
100
VMOption tmp;
101
try {
102
tmp = diagnostic.getVMOption(flagName);
103
} catch (IllegalArgumentException e) {
104
tmp = null;
105
}
106
return tmp == null ? null : tmp.getValue();
107
}
108
109
private String testRead() {
110
String value = getVMOptionAsString();
111
Asserts.assertNotNull(value);
112
Asserts.assertEQ(value, asString(getValue()));
113
Asserts.assertEQ(value, asString(WHITE_BOX.getVMFlag(flagName)));
114
return value;
115
}
116
117
private void testWritePositive(T value, T expected) {
118
setNewValue(value);
119
String newValue = testRead();
120
Asserts.assertEQ(newValue, asString(expected));
121
}
122
123
private void testWriteNegative(T value, T expected) {
124
// Should always return false for non-existing flags
125
Asserts.assertFalse(WHITE_BOX.isConstantVMFlag(flagName));
126
Asserts.assertFalse(WHITE_BOX.isLockedVMFlag(flagName));
127
String oldValue = getVMOptionAsString();
128
Asserts.assertEQ(oldValue, asString(getValue()));
129
Asserts.assertEQ(oldValue, asString(WHITE_BOX.getVMFlag(flagName)));
130
setNewValue(value);
131
String newValue = getVMOptionAsString();
132
Asserts.assertEQ(oldValue, newValue);
133
}
134
135
private String asString(Object value) {
136
return value == null ? null : "" + value;
137
}
138
}
139
140