Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/except/except001.java
41155 views
1
/*
2
* Copyright (c) 1999, 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
24
25
/*
26
* @test
27
* @key stress
28
*
29
* @summary converted from VM testbase nsk/stress/except/except001.
30
* VM testbase keywords: [stress, diehard, slow, nonconcurrent, quick]
31
* VM testbase readme:
32
* DESCRIPTION
33
* This checks if OutOfMemoryError exception is correctly enwrapped into
34
* InvocationTargetException when thrown inside a method invoked via
35
* reflection.
36
* The test tries to occupy all of memory available in the heap by
37
* allocating lots of new Object() instances. Instances of the "empty"
38
* type Object are the smallest objects, so they apparently should occupy
39
* most fine-grained fragments in the heap. Thus, there apparently should
40
* not remain any free space to incarnate new Throwable instance, and VM
41
* possibly could crash while trying to throw new OutOfMemoryError and
42
* enwrap it into new InvocationTargetException instance.
43
* By the way, the test checks time elapsed to allocate memory. Both
44
* classic VM and HotSpot seem to fall into poor performance of memory
45
* allocation when heap is almost over. E.g.: HotSpot 1.3-betaH may spend
46
* more than 1 minute to allocate next Object in this case (tested on
47
* Pentium-II, 350MHz, 128Mb RAM). To avoid this problem, the test enforce
48
* OutOfMemoryError if more then 5 minutes is spent to allocate "last bytes"
49
* of memory.
50
* COMMENTS
51
* HotSpot releases 1.0-fcsE (both Win32 and Sparc), and 1.3-betaH (Win32)
52
* fail on this test due to poor performance of memory allocation.
53
* #4248801 (P3/S5) slow memory allocation when heap is almost exhausted
54
* Despite this bug is treated fixed in HotSpot 1.0.1, it still does suffer
55
* slow memory allocation when running on PC having 64Mb or less of RAM.
56
* There is also a workaround involved to avoid the following bugs known
57
* for HotSpot and for classic VM:
58
* #4239841 (P1/S5) 1.1: poor garbage collector performance (HotSpot bug)
59
* #4245060 (P4/S5) poor garbage collector performance (Classic VM bug)
60
* However, printing of the test's error messages, warnings, and of execution
61
* trace may fail under JDK 1.2 for Win32 even so.
62
* HotSpot 2.0-devA (Win32) crashes due to the known HotSpot bug:
63
* #4239828 (P1/S4) 1.3c1: VM crashes when heap is exhausted
64
*
65
* @run main/othervm -Xms50M -Xmx200M nsk.stress.except.except001
66
*/
67
68
package nsk.stress.except;
69
70
import java.io.PrintStream;
71
import java.lang.reflect.InvocationTargetException;
72
import java.lang.reflect.Method;
73
74
/**
75
* This checks if <code>OutOfMemoryError</code> exception is correctly
76
* enwrapped into <code>InvocationTargetException</code> when thrown inside
77
* a method invoked via reflection.
78
* <p>
79
* <p>The test tries to occupy all of memory available in the heap by
80
* allocating lots of new <code>Object()</code> instances. Instances of the
81
* ``empty'' type <code>Object</code> are the smallest objects, so they
82
* apparently should occupy most fine-grained fragments in the heap.
83
* Thus, there apparently should not remain any free space to incarnate new
84
* <code>Throwable</code> instance, and VM possibly could crash while trying
85
* to throw new <code>OutOfMemoryError</code> and enwrap it into new
86
* <code>InvocationTargetException</code> instance.
87
* <p>
88
* <p>By the way, the test checks time elapsed to allocate memory.
89
* Both classic VM and HotSpot seem to fall into poor performance of memory
90
* allocation when heap is almost over. E.g.: HotSpot 1.3-betaH may spend
91
* more than 1 minute to allocate next <code>Object</code> in this case
92
* (tested on Pentium-II, 350MHz, 128Mb RAM). To workaround this problem,
93
* the test enforces <code>OutOfMemoryError</code> if more then 5 minutes
94
* is spent to allocate ``last bytes'' of the memory.
95
*/
96
public class except001 {
97
/**
98
* This field allows or supresses printing with <code>display()</code>
99
* method.
100
*
101
* @see #display(Object)
102
* @see #complain(Object)
103
* @see #out
104
*/
105
private static boolean MODE_VERBOSE = true;
106
/*
107
* Storage for a lot of tiny objects
108
* "static volatile" keywords are for preventing heap optimization
109
*/
110
private static volatile Object pool[] = null;
111
112
/**
113
* Print execution trace if <code>MODE_VERBOSE</code> is <code>true</code>
114
* (optional).
115
*
116
* @see #MODE_VERBOSE
117
* @see #complain(Object)
118
* @see #out
119
*/
120
private static void display(Object message) {
121
if (MODE_VERBOSE)
122
out.println(message.toString());
123
out.flush();
124
}
125
126
/**
127
* Print error <code>message</code>.
128
*
129
* @see #display(Object)
130
* @see #out
131
*/
132
private static void complain(Object message) {
133
out.println("# " + message);
134
out.flush();
135
}
136
137
/**
138
* The log-stream assigned at runtime by the method
139
* <code>run(args,out)</code>.
140
*
141
* @see #display(Object)
142
* @see #complain(Object)
143
* @see #run(String[], PrintStream)
144
*/
145
private static PrintStream out;
146
147
/**
148
* Try to allocate lots of instances of the type <code>Object</code>.
149
* Such instances are most fine-grained, and thus they should occupy
150
* smallest fragments of free memory in the heap.
151
* <p>
152
* <p>By the way, break the test, if JVM has spent more than
153
* 5 minutes to allocate latest portions of memory.
154
*/
155
public static void raiseOutOfMemory() throws OutOfMemoryError {
156
try {
157
// Repository for objects, which should be allocated:
158
int index = 0;
159
for (int size = 1 << 30; size > 0 && pool == null; size >>= 1)
160
try {
161
pool = new Object[size];
162
} catch (OutOfMemoryError oome) {
163
}
164
if (pool == null)
165
throw new Error("HS bug: cannot allocate new Object[1]");
166
167
// Sum up time spent, when it was hard to JVM to allocate next object
168
// (i.e.: when JVM has spent more than 1 second to allocate new object):
169
double totalDelay = 0;
170
long timeMark = System.currentTimeMillis();
171
172
for (; index < pool.length; index++) {
173
//-------------------------
174
pool[index] = new Object();
175
long nextTimeMark = System.currentTimeMillis();
176
long elapsed = nextTimeMark - timeMark;
177
timeMark = nextTimeMark;
178
//----------------------
179
if (elapsed > 1000) {
180
double seconds = elapsed / 1000.0;
181
display(
182
"pool[" + index +
183
"]=new Object(); // elapsed " + seconds + "s");
184
totalDelay += seconds;
185
if (totalDelay > 300) {
186
complain(
187
"Memory allocation became slow: so heap seems exhausted.");
188
throw new OutOfMemoryError();
189
}
190
}
191
}
192
193
// This method should never return:
194
throw new Error("TEST_BUG: failed to provoke OutOfMemoryError");
195
} finally {
196
// Make sure there will be enough memory for next object allocation
197
pool = null;
198
}
199
}
200
201
/**
202
* Invoke the method <code>raiseOutOfMemory()</code> with reflection,
203
* and check if the exception it throws is just
204
* <code>OutOfMemoryError</code> enwrapped into
205
* <code>InvocationTargetException</code> instance.
206
* <p>
207
* <p>Before the test begins, <code>this.out</code> filed is assigned
208
* to the parameter <code>out</code>. Parameter <code>args[]</code>
209
* is ignored.
210
*
211
* @see #raiseOutOfMemory()
212
*/
213
public static int run(String args[], PrintStream out) {
214
out.println("# While printing this message, JVM seems to initiate the output");
215
out.println("# stream, so that it will not need more memory to print later,");
216
out.println("# when the heap would fail to provide more memory.");
217
out.println("# ");
218
out.println("# That problem is caused by the known JDK/HotSpot bugs:");
219
out.println("# 4239841 (P1/S5) 1.1: poor garbage collector performance");
220
out.println("# 4245060 (P4/S5) poor garbage collector performance");
221
out.println("# ");
222
out.println("# This message is just intended to work-around that problem.");
223
out.println("# If printing should fail even so.");
224
225
if (args.length > 0) {
226
if (args[0].toLowerCase().startsWith("-v"))
227
MODE_VERBOSE = true;
228
}
229
230
except001.out = out;
231
Class testClass = except001.class;
232
try {
233
Method testMethod = testClass.getMethod("raiseOutOfMemory", new Class [0]);
234
Object junk = testMethod.invoke(null, new Object [0]);
235
236
} catch (InvocationTargetException ite) {
237
Throwable targetException = ite.getTargetException();
238
if (targetException instanceof OutOfMemoryError) {
239
display("OutOfMemoryError thrown as expected.");
240
display("Test passed.");
241
return 0;
242
}
243
complain("Unexpected InvocationTargetException: " + targetException);
244
complain("Test failed.");
245
return 2;
246
247
} catch (Exception exception) {
248
complain("Unexpected exception: " + exception);
249
complain("Test failed.");
250
return 2;
251
}
252
//
253
complain("The test has finished unexpectedly.");
254
complain("Test failed.");
255
return 2;
256
}
257
258
/**
259
* Re-call to <code>run(args,out)</code>, and return JCK-like exit status.
260
* (The stream <code>out</code> is assigned to <code>System.out</code> here.)
261
*
262
* @see #run(String[], PrintStream)
263
*/
264
public static void main(String args[]) {
265
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
266
// Last try. If there is some exception outside the code, test should end correctly
267
@Override
268
public void uncaughtException(Thread t, Throwable e) {
269
try {
270
pool = null;
271
System.gc();
272
if (e instanceof OutOfMemoryError) {
273
try {
274
System.out.println("OOME : Test Skipped");
275
System.exit(95);
276
} catch (Throwable ignore) {
277
} // No code in the handler can provoke correct exceptions.
278
} else {
279
e.printStackTrace();
280
throw (RuntimeException) e;
281
}
282
} catch (OutOfMemoryError oome) {
283
}
284
}
285
});
286
int exitCode = run(args, System.out);
287
System.exit(exitCode + 95);
288
// JCK-like exit status.
289
}
290
291
}
292
293