This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
/
Test.java
236 lines (200 loc) · 9.27 KB
/
Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
*
* @summary converted from VM Testbase vm/mlvm/indy/stress/gc/lotsOfCallSites.
* VM Testbase keywords: [feature_mlvm, nonconcurrent]
*
* @library /vmTestbase
* /test/lib
* @build sun.hotspot.WhiteBox
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
*
* @comment build test class and indify classes
* @build vm.mlvm.indy.stress.gc.lotsOfCallSites.Test
* vm.mlvm.indy.stress.gc.lotsOfCallSites.INDIFY_Testee
* @run driver vm.mlvm.share.IndifiedClassesBuilder
*
* @run main/othervm -Xbootclasspath/a:.
* -XX:+UnlockDiagnosticVMOptions
* -XX:+WhiteBoxAPI
* vm.mlvm.indy.stress.gc.lotsOfCallSites.Test
*/
package vm.mlvm.indy.stress.gc.lotsOfCallSites;
import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.util.HashSet;
import sun.hotspot.WhiteBox;
import nsk.share.test.Stresser;
import vm.mlvm.share.CustomClassLoaders;
import vm.mlvm.share.Env;
import vm.mlvm.share.MlvmTest;
import vm.share.FileUtils;
import vm.share.options.Option;
/**
* The test creates a lot of CallSites by loading a class with a bootstrap method and invokedynamic
* via a custom classloader in a loop.
*
* The test verifies that all CallSites are "delivered to heaven" by creating a PhantomReference per
* a CallSite and checking the number of references put into a queue.
*
*/
public class Test extends MlvmTest {
// TODO (separate bug should be filed): move this option to MlvmTest level
@Option(name = "heapdump", default_value = "false", description = "Dump heap after test has finished")
private boolean heapDumpOpt = false;
@Option(name = "iterations", default_value = "100000", description = "Iterations: each iteration loads one new class")
private int iterations = 100_000;
private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
private static final int GC_COUNT = 6;
private static final boolean TERMINATE_ON_FULL_METASPACE = false;
private static final ReferenceQueue<CallSite> objQueue = new ReferenceQueue<CallSite>();
private static final HashSet<PhantomReference<CallSite>> references = new HashSet<PhantomReference<CallSite>>();
private static long loadedClassCount = 0;
// We avoid direct references to the testee class to avoid loading it by application class loader
// Otherwise the testee class is loaded both by the custom and the application class loaders,
// and when java.lang.invoke.MH.COMPILE_THRESHOLD={0,1} is defined, the test fails with
// "java.lang.IncompatibleClassChangeError: disagree on InnerClasses attribute"
private static final String TESTEE_CLASS_NAME = Test.class.getPackage().getName() + "." + "INDIFY_Testee";
private static final String TESTEE_REFERENCES_FIELD = "references";
private static final String TESTEE_OBJQUEUE_FIELD = "objQueue";
private static final String TESTEE_BOOTSTRAP_CALLED_FIELD = "bootstrapCalled";
private static final String TESTEE_TARGET_CALLED_FIELD = "targetCalled";
private static final String TESTEE_INDY_METHOD = "indyWrapper";
private static int removeQueuedReferences() {
int count = 0;
Reference<? extends CallSite> r;
while ((r = objQueue.poll()) != null) {
if (!references.remove(r)) {
Env.traceNormal("Reference " + r + " was not registered!");
}
++count;
}
if (count > 0) {
Env.traceVerbose("Removed " + count + " phantom references");
} else {
Env.traceDebug("Removed " + count + " phantom references");
}
return count;
}
private MemoryPoolMXBean getClassMetadataMemoryPoolMXBean() {
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
for (MemoryPoolMXBean memPool : ManagementFactory.getMemoryPoolMXBeans()) {
String name = memPool.getName();
if ((name.contains("Compressed class space") || name.contains("Metaspace")) && memPool.getUsage() != null) {
return memPool;
}
}
return null;
}
@Override
public boolean run() throws Throwable {
setHeapDumpAfter(heapDumpOpt);
final byte[] classBytes = FileUtils.readClass(TESTEE_CLASS_NAME);
final MemoryPoolMXBean classMetadataPoolMXB = getClassMetadataMemoryPoolMXBean();
final String memPoolName = classMetadataPoolMXB == null ? "" : classMetadataPoolMXB.getName();
int removedEntries = 0;
Stresser stresser = createStresser();
stresser.start(iterations);
try {
while (stresser.continueExecution()) {
stresser.iteration();
iteration(classBytes, TESTEE_CLASS_NAME);
removedEntries += removeQueuedReferences();
if (stresser.getIteration() % 1000 == 0) {
Env.traceNormal("Iterations: " + stresser.getIteration() + " removed entries: " + removedEntries);
if (TERMINATE_ON_FULL_METASPACE && classMetadataPoolMXB != null) {
MemoryUsage mu = classMetadataPoolMXB.getUsage();
Env.traceNormal(memPoolName + " usage: " + mu);
if (mu.getUsed() >= mu.getMax() * 9 / 10) {
Env.traceNormal(memPoolName + " is nearly out of space: " + mu + ". Terminating.");
break;
}
}
}
}
} catch (OutOfMemoryError e) {
Env.traceNormal(e, "Out of memory. This is OK");
} finally {
stresser.finish();
}
for (int i = 0; i < GC_COUNT; ++i) {
WHITE_BOX.fullGC();
Thread.sleep(500);
removedEntries += removeQueuedReferences();
}
removedEntries += removeQueuedReferences();
Env.traceNormal("Loaded classes: " + loadedClassCount
+ "; References left in set: " + references.size()
+ "; References removed from queue: " + removedEntries);
if (references.size() != 0 || removedEntries != loadedClassCount) {
Env.complain("Not all of the created CallSites were GC-ed");
return false;
}
return true;
}
private void iteration(byte[] classBytes, String indyClassName) throws Throwable {
ClassLoader cl = CustomClassLoaders.makeClassBytesLoader(classBytes, indyClassName);
Class<?> c = cl.loadClass(indyClassName);
++loadedClassCount;
if (c.getClassLoader() != cl) {
throw new RuntimeException("Invalid class loader: " + c.getClassLoader() + "; required=" + cl);
}
Field vr = c.getDeclaredField(TESTEE_REFERENCES_FIELD);
vr.set(null, references);
Field voq = c.getDeclaredField(TESTEE_OBJQUEUE_FIELD);
voq.set(null, objQueue);
Field vbc = c.getDeclaredField(TESTEE_BOOTSTRAP_CALLED_FIELD);
if (vbc.getBoolean(null)) {
throw new RuntimeException(TESTEE_BOOTSTRAP_CALLED_FIELD + " flag should not be set. Not a fresh copy of the testee class?");
}
Field vt = c.getDeclaredField(TESTEE_TARGET_CALLED_FIELD);
if (vt.getBoolean(null)) {
throw new RuntimeException(TESTEE_TARGET_CALLED_FIELD + " flag should not be set. Not a fresh copy of the testee class?");
}
Method m = c.getDeclaredMethod(TESTEE_INDY_METHOD);
m.invoke(null);
if (!vbc.getBoolean(null) ) {
throw new RuntimeException("Bootstrap method of the testee class was not called");
}
if (!vt.getBoolean(null) ) {
throw new RuntimeException("Target method of the testee class was not called");
}
}
public static void main(String[] args) {
MlvmTest.launch(args);
}
}