Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/metaspace/stressHierarchy/common/PerformChecksHelper.java
41161 views
1
/*
2
* Copyright (c) 2013, 2018, 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
package metaspace.stressHierarchy.common;
24
25
import java.lang.reflect.InvocationHandler;
26
import java.lang.reflect.InvocationTargetException;
27
import java.lang.reflect.Method;
28
import java.lang.reflect.Proxy;
29
import java.util.List;
30
31
import metaspace.stressHierarchy.common.classloader.tree.Node;
32
import metaspace.stressHierarchy.common.classloader.tree.Tree;
33
import metaspace.stressHierarchy.common.exceptions.ClassNotUnloadedException;
34
import metaspace.stressHierarchy.common.exceptions.TimeIsOverException;
35
import nsk.share.test.ExecutionController;
36
import sun.hotspot.WhiteBox;
37
import vm.share.gc.TriggerUnloadingHelper;
38
39
public class PerformChecksHelper {
40
41
private static final int NUMBER_OF_HOT_METHOD_CALLS = 100;
42
43
private static WhiteBox wb = WhiteBox.getWhiteBox();
44
45
// This is the number of failed attempts required to deem class unloading failed
46
private int attemptsLimit = 50;
47
48
// This is the pause between unloading attempts in milliseconds
49
private long unloadingPause = 1000;
50
51
// This is the number of failed attempts after that pauses will be involved
52
private int pausesLimit = 5;
53
54
private TriggerUnloadingHelper triggerUnloadingHelper = null;
55
56
private ExecutionController stresser;
57
58
public PerformChecksHelper(TriggerUnloadingHelper triggerUnloadingHelper, int attemptsLimit, long unloadingPause, int pausesLimit) {
59
this.triggerUnloadingHelper = triggerUnloadingHelper;
60
if (attemptsLimit != -1) {
61
this.attemptsLimit = attemptsLimit;
62
}
63
if (unloadingPause != -1) {
64
this.unloadingPause = unloadingPause;
65
}
66
if (pausesLimit != -1) {
67
this.pausesLimit = pausesLimit;
68
}
69
System.out.println("attemptsLimit = " + this.attemptsLimit);
70
System.out.println("unloadingPause = " + this.unloadingPause);
71
System.out.println("pausesLimit = " + this.pausesLimit);
72
}
73
74
public void checkLevelReclaimed(Tree tree, int level)
75
throws IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotUnloadedException, TimeIsOverException {
76
long attempsCounter = 0;
77
boolean checkPassed = false;
78
ClassNotUnloadedException classNotUnloadedException = null;
79
while (!checkPassed && attempsCounter++ < attemptsLimit) {
80
if (attempsCounter > pausesLimit && unloadingPause > 0) {
81
try {
82
Thread.sleep(unloadingPause);
83
} catch (InterruptedException e) {
84
throw new RuntimeException("Somebody dared to interrupt thread while we were waiting after gc provoke");
85
}
86
}
87
try {
88
checkLevel(tree, level, false);
89
checkPassed = true;
90
} catch (ClassNotUnloadedException exception) {
91
checkPassed = false;
92
classNotUnloadedException = exception;
93
triggerUnloadingHelper.triggerUnloading(stresser);
94
}
95
}
96
if (!checkPassed) {
97
System.out.println("Going to throw classNotUnloadedException. attempsCounter = " + attempsCounter);
98
throw classNotUnloadedException;
99
}
100
101
}
102
103
public void checkLevelAlive(Tree tree, int level) throws IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotUnloadedException, TimeIsOverException {
104
checkLevel(tree, level, true);
105
}
106
107
private void checkLevel(Tree tree, int level, boolean shouldBeAlive)
108
throws IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotUnloadedException, TimeIsOverException {
109
for (Node node : tree.getNodesInLevel(level)) {
110
for (String className : node.getLoadedClassesNames()) {
111
checkStresser();
112
boolean isClassAlive = wb.isClassAlive(className);
113
if (isClassAlive != shouldBeAlive) {
114
throw new ClassNotUnloadedException("Failing test! Class: " + className + " shouldBeAlive: " + shouldBeAlive
115
+ " isClassAlive: " + isClassAlive);
116
}
117
}
118
}
119
if (shouldBeAlive) {
120
checkAncestorsAlive(tree, level);
121
}
122
}
123
124
private void callMethods(Class<?> clazz)
125
throws IllegalAccessException, IllegalArgumentException,
126
InvocationTargetException, InstantiationException {
127
try {
128
for (Method m : clazz.getMethods()) {
129
for (int j = 0; j < NUMBER_OF_HOT_METHOD_CALLS; j++) {
130
if (m.getName().equals("composeString")) {
131
m.invoke(clazz.newInstance());
132
} else if (m.getName().equals("calculate")) {
133
m.invoke(clazz.newInstance());
134
} else if (m.getName().equals("calculate2")) {
135
m.invoke(clazz.newInstance());
136
}
137
}
138
}
139
} catch (OutOfMemoryError e) {
140
if (e.getMessage().trim().toLowerCase().contains("metaspace")) {
141
// avoid string concatenation, which may create more classes.
142
System.out.println("Got OOME in metaspace in PerformChecksHelper.callMethods(Class clazz). ");
143
System.out.println("This is possible with -triggerUnloadingByFillingMetaspace");
144
} else {
145
throw e;
146
}
147
}
148
}
149
150
private void checkAncestors(Class<?> clazz) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, TimeIsOverException {
151
for (; clazz != null; clazz = clazz.getSuperclass()) {
152
checkStresser();
153
if (!clazz.isInterface()) {
154
//check class
155
callMethods(clazz);
156
} else {
157
//check interface by implementing it
158
InvocationHandler handler = new InvocationHandler() {
159
@Override
160
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
161
return Integer.MIN_VALUE;
162
}
163
};
164
Object instance = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] {clazz}, handler);
165
instance.hashCode();
166
}
167
if (!wb.isClassAlive(clazz.getName())) {
168
throw new RuntimeException("Test failed in method checkAncestors: class "
169
+ clazz.getName() + " should be alive");
170
}
171
}
172
}
173
174
private void checkAncestorsAlive(Tree tree, int level)
175
throws IllegalAccessException, InvocationTargetException,
176
InstantiationException, IllegalArgumentException, TimeIsOverException {
177
List<Node> bottomLevel = tree.getNodesInLevel(level);
178
if (bottomLevel.isEmpty()) {
179
throw new RuntimeException("Failing test because of test bug: no nodes in bottom level");
180
}
181
for (Node node : bottomLevel) {
182
if (node.getLoadedClasses() == null || node.getLoadedClasses().isEmpty()) {
183
throw new RuntimeException("Failing test because of test bug: no classes loaded by node " + node);
184
}
185
for (Class<?> clazz : node.getLoadedClasses()) {
186
checkAncestors(clazz);
187
}
188
}
189
}
190
191
public void setStresser(ExecutionController stresser) {
192
this.stresser = stresser;
193
}
194
195
private void checkStresser() throws TimeIsOverException {
196
if (stresser == null) {
197
throw new RuntimeException("Test bug. Wrong usage of PerformChecksHelper. Stresser was not set.");
198
}
199
if (!stresser.continueExecution()) {
200
throw new TimeIsOverException();
201
}
202
}
203
204
205
}
206
207