Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/LargeObjects/large001/large001.java
41161 views
1
/*
2
* Copyright (c) 2004, 2020, 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
/*
25
* @test
26
* @key stress randomness
27
*
28
* @summary converted from VM Testbase gc/gctests/LargeObjects/large001.
29
* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, quick]
30
* VM Testbase readme:
31
* DESCRIPTION
32
* The test checks that Garbage Collector correctly does not throw any
33
* unexpected exceptions/errors while allocating large objects (classes
34
* that have more than 65535 fields and classes that have less than 65535
35
* fields). 65535 of fields is a limitation for JVM (see JVM specification
36
* Second edition 4.10).
37
* Since it is impossible to create one class with about 65535 of fields
38
* (javac cannot compile it), a child class extends a parent class, so the
39
* fields are devided into two subsets. However, the child class still has
40
* about 65535 of fields.
41
* The test starts a number of threads. This number is either set in *.cfg
42
* file or is calculated by the test itself based on the machine (see
43
* nsk.share.gc.Algorithms.getThreadsCount() method). As soon as all threads
44
* are started, each thread begins its checking.
45
* There are 13 classes to be loaded by each thread. These classes are
46
* generated by nsk.share.gc.Generator (see its javadoc for more details).
47
* Each class has a huge number of fields, but this number is less than the JVM
48
* limitation.
49
* The test loads the classes with nsk.share.gc.GCClassUnloader class that
50
* extends nsk.share.ClassUnloader and has a bit different algorith of eating
51
* heap. As soon as a class is loaded, the test creates an instance of
52
* it - allocates an object of that type. Then it drops references to the
53
* class and to the instance and tries to unload the class. The test does not
54
* expect any exceptions to be thrown.
55
*
56
* @library /vmTestbase
57
* /test/lib
58
*
59
* @comment generate and compile nsk.share.gc.newclass.* classes
60
* @run driver nsk.share.gc.GenClassesBuilder
61
*
62
* @run main/othervm
63
* -XX:-UseGCOverheadLimit
64
* -Xlog:gc*
65
* gc.gctests.LargeObjects.large001.large001
66
* -largeClassesPath classes
67
* -isOverLimitFields false
68
* -aggregationDepth 0
69
* -t 1
70
*/
71
72
package gc.gctests.LargeObjects.large001;
73
74
import java.lang.reflect.*;
75
import java.lang.ref.WeakReference;
76
import java.util.*;
77
import nsk.share.TestFailure;
78
79
80
import nsk.share.gc.*;
81
import nsk.share.*;
82
83
public class large001 extends ThreadedGCTest {
84
85
// Package of the classes to be loaded
86
final static String PREFIX = "nsk.share.gc.newclass.";
87
// A bunch of classes that have number of fields more than JVM limitation
88
final static String[] LCLASSES = {PREFIX + "private_int_lchild",
89
PREFIX + "protected_short_lchild",
90
PREFIX + "public_long_lchild",
91
PREFIX + "public_Object_lchild",
92
PREFIX + "static_byte_lchild",
93
PREFIX + "static_float_lchild",
94
PREFIX + "transient_boolean_lchild",
95
PREFIX + "volatile_double_lchild",
96
PREFIX + "protected_combination_lchild",
97
PREFIX + "public_combination_lchild",
98
PREFIX + "static_combination_lchild",
99
PREFIX + "transient_combination_lchild",
100
PREFIX + "volatile_combination_lchild"
101
};
102
// A bunch of classes that have number of fields less than JVM limitation
103
final static String[] SCLASSES = {PREFIX + "private_int_schild",
104
PREFIX + "protected_short_schild",
105
PREFIX + "public_long_schild",
106
PREFIX + "public_Object_schild",
107
PREFIX + "static_byte_schild",
108
PREFIX + "static_float_schild",
109
PREFIX + "transient_boolean_schild",
110
PREFIX + "volatile_double_schild",
111
PREFIX + "protected_combination_schild",
112
PREFIX + "public_combination_schild",
113
PREFIX + "static_combination_schild",
114
PREFIX + "transient_combination_schild",
115
PREFIX + "volatile_combination_schild"
116
};
117
boolean isOverLimitFields = true;
118
int aggregationDepth = 0;
119
String largeClassesPath;
120
121
private class Worker implements Runnable {
122
123
int id;
124
125
public Worker(int id) {
126
this.id = id;
127
}
128
129
public void run() {
130
try {
131
// Use special ClassUnloader to load/unload classes
132
ClassUnloader unloader = new ClassUnloader();
133
String[] classes = isOverLimitFields ? LCLASSES : SCLASSES;
134
135
for (String name : classes) {
136
// Load the class
137
log.debug(id + ": Loading class: " + name);
138
unloader.loadClass(name, largeClassesPath);
139
log.debug(id + ": Class loaded: " + name);
140
141
Class loadedClass = unloader.getLoadedClass();
142
Object loadedClassInstance = loadedClass.newInstance();
143
144
log.debug(id + ": Instance of the class: " + loadedClassInstance);
145
int depth = aggregationDepth;
146
List<WeakReference> refs = new ArrayList<WeakReference>(depth);
147
addObjRef(loadedClassInstance, loadedClass, depth, refs);
148
149
// Drop all references to the class and try to unload it
150
Algorithms.eatMemory(getExecutionController());
151
log.debug(id + ": Testing non-null after GC force for: " + name);
152
if (loadedClass == null || loadedClassInstance == null) {
153
throw new Exception("Null class");
154
}
155
verifyObjRef(loadedClassInstance, depth);
156
for (WeakReference ref : refs) {
157
if (ref.get() == null) {
158
throw new Exception("Unexpected null reference");
159
}
160
}
161
refs = null;
162
loadedClass = null;
163
loadedClassInstance = null;
164
165
log.debug(id + ": Unloading class: "
166
+ name);
167
boolean result = unloader.unloadClass(getExecutionController());
168
log.debug(id + ": Result of uloading "
169
+ "class " + name + ": " + result);
170
}
171
} catch (OutOfMemoryError oome) {
172
// just skip if we eat memory in several threads...
173
// rethrow in the case of one thread
174
if (runParams.getNumberOfThreads() == 1) {
175
throw oome;
176
}
177
} catch (Throwable t) {
178
throw new TestFailure("Unexpected exception: ", t);
179
}
180
}
181
182
// This method recursively create chain of aggregated objects for given object
183
public void addObjRef(Object object, Class clazz, int count, List<WeakReference> list) throws Throwable {
184
if (count == 0) {
185
return;
186
}
187
188
Field[] fields = object.getClass().getFields();
189
for (Field field : fields) {
190
if (field.getName().startsWith("obj")) {
191
Object addedObject = clazz.newInstance();
192
field.set(object, addedObject);
193
System.out.println("Added field " + field.getName() + " .... " + count);
194
addObjRef(addedObject, clazz, count - 1, list);
195
list.add(new WeakReference<Object>(addedObject));
196
}
197
}
198
}
199
200
// This method recursively verfiy chain of aggregated objects for given object.
201
// Throws null pointer exception of objP/C field is null
202
public void verifyObjRef(Object object, int count) throws Throwable {
203
if (count == 0) {
204
return;
205
}
206
207
Field[] fields = object.getClass().getFields();
208
for (Field field : fields) {
209
if (field.getName().startsWith("obj")) {
210
Object obj = field.get(object);
211
verifyObjRef(obj, count - 1);
212
}
213
}
214
}
215
}
216
217
public large001(String[] args) {
218
for (int i = 0; i < args.length; i++) {
219
if (args[i].equals("-largeClassesPath")) {
220
largeClassesPath = args[++i];
221
} else if (args[i].equals("-isOverLimitFields")) {
222
isOverLimitFields = Boolean.getBoolean(args[++i]);
223
} else if (args[i].equals("-aggregationDepth")) {
224
aggregationDepth = Integer.parseInt(args[++i]);
225
}
226
}
227
if (largeClassesPath == null || largeClassesPath.length() == 0) {
228
throw new TestFailure("No classpath for large classes is given");
229
}
230
}
231
232
@Override
233
protected Runnable createRunnable(int i) {
234
return new Worker(i);
235
}
236
237
@Override
238
public void run() {
239
if (isOverLimitFields) {
240
log.debug("Loading classes that have number "
241
+ "of fields over limitation (more "
242
+ "than 65535)");
243
} else {
244
log.debug("Loading classes that have number "
245
+ "of fields under limitation (less "
246
+ "than 65535)");
247
}
248
super.run();
249
}
250
251
public static void main(String args[]) {
252
GC.runTest(new large001(args), args);
253
}
254
}
255
256