Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-aarch32-jdk8u
Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/lang/invoke/LFCaching/LambdaFormTestCase.java
48803 views
1
/*
2
* Copyright (c) 2014, 2015, 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 com.oracle.testlibrary.jsr292.Helper;
25
import com.oracle.testlibrary.jsr292.CodeCacheOverflowProcessor;
26
import java.lang.invoke.MethodHandle;
27
import java.lang.management.GarbageCollectorMXBean;
28
import java.lang.management.ManagementFactory;
29
import java.lang.ref.Reference;
30
import java.lang.reflect.Field;
31
import java.lang.reflect.Method;
32
import java.util.Collection;
33
import java.util.List;
34
import java.util.function.Function;
35
import jdk.testlibrary.Utils;
36
import jdk.testlibrary.TimeLimitedRunner;
37
38
/**
39
* Lambda forms caching test case class. Contains all necessary test routines to
40
* test lambda forms caching in method handles returned by methods of
41
* MethodHandles class.
42
*
43
* @author kshefov
44
*/
45
public abstract class LambdaFormTestCase {
46
47
private static final long TIMEOUT = Helper.IS_THOROUGH ? 0L : (long) (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) * 0.9);
48
49
/**
50
* Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is
51
* used to get a lambda form from a method handle.
52
*/
53
protected final static Method INTERNAL_FORM;
54
protected final static Field DEBUG_NAME;
55
protected final static Field REF_FIELD;
56
private static final List<GarbageCollectorMXBean> gcInfo;
57
58
private static long gcCount() {
59
return gcInfo.stream().mapToLong(GarbageCollectorMXBean::getCollectionCount).sum();
60
}
61
62
static {
63
try {
64
INTERNAL_FORM = MethodHandle.class.getDeclaredMethod("internalForm");
65
INTERNAL_FORM.setAccessible(true);
66
67
DEBUG_NAME = Class.forName("java.lang.invoke.LambdaForm").getDeclaredField("debugName");
68
DEBUG_NAME.setAccessible(true);
69
70
REF_FIELD = Reference.class.getDeclaredField("referent");
71
REF_FIELD.setAccessible(true);
72
} catch (Exception ex) {
73
throw new Error("Unexpected exception", ex);
74
}
75
76
gcInfo = ManagementFactory.getGarbageCollectorMXBeans();
77
if (gcInfo.size() == 0) {
78
throw new Error("No GarbageCollectorMXBeans found.");
79
}
80
}
81
82
private final TestMethods testMethod;
83
private long gcCountAtStart;
84
85
private static class TestRun {
86
87
final Function<TestMethods, LambdaFormTestCase> ctor;
88
final Collection<TestMethods> testMethods;
89
final long totalIterations;
90
long doneIterations;
91
long testCounter;
92
long failCounter;
93
boolean passed;
94
95
TestRun(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
96
this.ctor = ctor;
97
this.testMethods = testMethods;
98
long testCaseNum = testMethods.size();
99
long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
100
System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
101
iterations, iterations * testCaseNum);
102
System.out.printf("Number of iterations is set to %d (%d cases)%n",
103
iterations, iterations * testCaseNum);
104
System.out.flush();
105
totalIterations = iterations;
106
doneIterations = 0L;
107
testCounter = 0L;
108
failCounter = 0L;
109
passed = true;
110
}
111
112
Boolean doIteration() {
113
if (doneIterations >= totalIterations) {
114
return false;
115
}
116
System.err.println(String.format("Iteration %d:", doneIterations));
117
for (TestMethods testMethod : testMethods) {
118
LambdaFormTestCase testCase = ctor.apply(testMethod);
119
try {
120
System.err.printf("Tested LF caching feature"
121
+ " with MethodHandles.%s method.%n",
122
testCase.getTestMethod().name);
123
Throwable t = CodeCacheOverflowProcessor
124
.runMHTest(testCase::doTest);
125
if (t != null) {
126
return false;
127
}
128
System.err.println("PASSED");
129
} catch (OutOfMemoryError oome) {
130
// Don't swallow OOME so a heap dump can be created.
131
System.err.println("FAILED");
132
throw oome;
133
} catch (Throwable t) {
134
t.printStackTrace();
135
System.err.printf("FAILED. Caused by %s%n", t.getMessage());
136
passed = false;
137
failCounter++;
138
}
139
testCounter++;
140
}
141
doneIterations++;
142
return true;
143
}
144
145
void checkPassed() {
146
if (!passed) {
147
throw new Error(String.format("%d of %d test cases FAILED! %n"
148
+ "Rerun the test with the same \"-Dseed=\" option as in the log file!",
149
failCounter, testCounter));
150
} else {
151
System.err.printf("All %d test cases PASSED!%n", testCounter);
152
}
153
}
154
}
155
156
/**
157
* Test case constructor. Generates test cases with random method types for
158
* given methods form {@code j.l.i.MethodHandles} class.
159
*
160
* @param testMethod A method from {@code j.l.i.MethodHandles} class which
161
* returns a {@code j.l.i.MethodHandle}.
162
*/
163
protected LambdaFormTestCase(TestMethods testMethod) {
164
this.testMethod = testMethod;
165
this.gcCountAtStart = gcCount();
166
}
167
168
public TestMethods getTestMethod() {
169
return testMethod;
170
}
171
172
protected boolean noGCHappened() {
173
return gcCount() == gcCountAtStart;
174
}
175
176
/**
177
* Routine that executes a test case.
178
*/
179
public abstract void doTest();
180
181
/**
182
* Runs a number of test cases defined by the size of testCases list.
183
*
184
* @param ctor constructor of LambdaFormCachingTest or its child classes
185
* object.
186
* @param testMethods list of test methods
187
*/
188
public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
189
LambdaFormTestCase.TestRun run
190
= new LambdaFormTestCase.TestRun(ctor, testMethods);
191
TimeLimitedRunner runner = new TimeLimitedRunner(TIMEOUT, 4.0d, run::doIteration);
192
try {
193
runner.call();
194
} catch (Exception ex) {
195
System.err.println("FAILED");
196
throw new Error("Unexpected error!", ex);
197
}
198
run.checkPassed();
199
}
200
}
201
202