Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/except/except002.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/except002.
30
* VM testbase keywords: [stress, diehard, slow, nonconcurrent, quick]
31
* VM testbase readme:
32
* DESCRIPTION
33
* This checks if various exceptions are thrown (and caught) correctly
34
* when there apparently are no free space in the heap to allocate new
35
* Throwable instance.
36
* The test tries to occupy all of memory available in the heap by allocating
37
* lots of new Object() instances. Instances of the type Object are the smallest
38
* objects, so they apparently should occupy most fine-grained fragments in the
39
* heap and leave no free space for new Throwable instance. After that, the test
40
* provokes various exceptions (e.g.: by executing integer division by 0 and so
41
* on), and checks if appropriate exceptions are thrown.
42
* COMMENTS
43
* The test needs a lot of memory to start up, so it should not run under older
44
* JDK 1.1.x release due to its poorer heap utilization. Also, some checks are
45
* skipped when testing classic VM, because OutOfMemoryError is correctly thrown
46
* instead of target exception.
47
* When the test is being self-initiating (i.e.: eating heap), memory occupation
48
* is terminated if memory allocation slows down crucially. This is a workaround
49
* intended to avoid the HotSpot bug:
50
* #4248801 (P1/S5) slow memory allocation when heap is almost exhausted
51
* There is also a workaround involved to avoid the following bugs known
52
* for HotSpot and for classic VM:
53
* #4239841 (P1/S5) 1.1: poor garbage collector performance (HotSpot bug)
54
* #4245060 (P4/S5) poor garbage collector performance (Classic VM bug)
55
* However, printing of the test's error messages, warnings, and of execution
56
* trace fails under JDK 1.2 for Win32 even so. If the test fails due to this
57
* problem, exit status 96 is returned instead of 97.
58
* JDK 1.3 classic VM for Sparc may crash (core dump) due to the known bug:
59
* #4245057 (P2/S3) VM crashes when heap is exhausted
60
*
61
* @run main/othervm -Xms50M -Xmx200M nsk.stress.except.except002
62
*/
63
64
package nsk.stress.except;
65
66
import java.io.PrintStream;
67
68
/**
69
* This checks if various exceptions are thrown (and caught) correctly
70
* when there apparently are no free space in the heap to allocate new
71
* <code>Throwable</code> instance.
72
* <p>
73
* <p>The test tries to occupy all of memory available in the heap by
74
* allocating lots of new <code>Object()</code> instances. Instances of the
75
* type <code>Object</code> are the smallest objects, so they apparently should
76
* occupy most fine-grained fragments in the heap and leave no free space for
77
* new <code>Throwable</code> instance. After that, the test provokes various
78
* exceptions (e.g.: by executing integer division by 0 and so on), and checks
79
* if appropriate exceptions are thrown.
80
* <p>
81
* <p>Note, that memory occupation is terminated if memory allocation slows
82
* down crucially. This is a workaround intended to avoid the HotSpot bug:
83
* <br>&nbsp;&nbsp;
84
* #4248801 (P1/S5) slow memory allocation when heap is almost exhausted
85
* <p>
86
* <p>There is also a workaround involved to avoid the following bugs known
87
* for HotSpot and for classic VM:
88
* <br>&nbsp;&nbsp;
89
* #4239841 (P1/S5) 1.1: poor garbage collector performance
90
* <br>&nbsp;&nbsp;
91
* #4245060 (P4/S5) poor garbage collector performance
92
* <br>However, printing of the test's error messages, warnings, and of
93
* execution trace may fail even so. If the test fails due to poor GC
94
* performance, exit status 96 is returned instead of 97.
95
* <p>
96
* <p>Also note, that the test needs a lot of memory to start up, so it should
97
* not run under older JDK 1.1.x release due to its poor heap utilization.
98
*/
99
public class except002 {
100
/**
101
* Either allow or supress printing of execution trace.
102
*/
103
private static boolean TRACE_ON = false;
104
/**
105
* Either allow or supress printing of warning messages.
106
*/
107
private static final boolean WARN_ON = true;
108
/*
109
* Storage for a lot of tiny objects
110
* "static volatile" keywords are for preventing heap optimization
111
*/
112
public static volatile Object pool[] = null;
113
/**
114
* Temporary <code>log</code> for error messages, warnings and/or execution trace.
115
*
116
* @see #messages
117
*/
118
private static String log[] = new String[1000]; // up to 1000 messages
119
/**
120
* How many <code>messages</code> were submitted to the <code>log</code>.
121
*
122
* @see #log
123
*/
124
private static int messages = 0;
125
126
/**
127
* Re-call to the method <code>run(out)</code> (ignore <code>args[]</code>),
128
* and print the test summary - either test passed of failed.
129
*/
130
public static int run(String args[], PrintStream out) {
131
if (args.length > 0) {
132
if (args[0].toLowerCase().startsWith("-v"))
133
TRACE_ON = true;
134
}
135
136
int exitCode = run(out);
137
pool = null;
138
System.gc();
139
// Print the log[] and the test summary:
140
try {
141
for (int i = 0; i < messages; i++)
142
out.println(log[i]);
143
if (exitCode == 0) {
144
if (TRACE_ON)
145
out.println("Test passed.");
146
} else
147
out.println("Test failed.");
148
} catch (OutOfMemoryError oome) {
149
// Poor performance of garbage collector:
150
exitCode = 1;
151
}
152
153
return exitCode;
154
}
155
156
/**
157
* Allocate as much <code>Object</code> instances as possible to bring JVM
158
* into stress, and then check if exceptions are correctly thrown accordingly
159
* to various situations like integer division by 0, etc.
160
*/
161
private static int run(PrintStream out) {
162
out.println("# While printing this message, JVM seems to initiate the output");
163
out.println("# stream, so that it will not need more memory to print later,");
164
out.println("# when the heap would fail to provide more memory.");
165
out.println("# ");
166
out.println("# Note, that the test maintains especial static log[] field in");
167
out.println("# order to avoid printing when the heap seems exhausted.");
168
out.println("# Nevertheless, printing could arise OutOfMemoryError even");
169
out.println("# after all the memory allocated by the test is released.");
170
out.println("# ");
171
out.println("# That problem is caused by the known JDK/HotSpot bugs:");
172
out.println("# 4239841 (P1/S5) 1.1: poor garbage collector performance");
173
out.println("# 4245060 (P4/S5) poor garbage collector performance");
174
out.println("# ");
175
out.println("# This message is just intended to work-around that problem.");
176
out.println("# If printing should fail even so, the test will return the");
177
out.println("# exit status 96 instead of 97 to indicate the problem.");
178
179
// Prepare some items, which will be used by the test:
180
Object trash = null;
181
182
// Sum up exit code:
183
int exitCode = 0; // apparently PASSED
184
int skipped = 0; // some checks may correctly suffer OutOfMemoryError
185
// Allocate repository for a lots of tiny objects:
186
for (int size = 1 << 30; size > 0 && pool == null; size >>= 1)
187
try {
188
pool = new Object[size];
189
} catch (OutOfMemoryError oome) {
190
}
191
if (pool == null)
192
throw new Error("HS bug: cannot allocate new Object[1]");
193
int poolSize = pool.length;
194
int index = 0;
195
196
// Sum up time spent, when it was hard to JVM to allocate next object
197
// (i.e.: when JVM has spent more than 1 second to allocate new object):
198
double totalDelay = 0;
199
long timeMark = System.currentTimeMillis();
200
try {
201
for (; index < poolSize; index++) {
202
//-------------------------
203
pool[index] = new Object();
204
long nextTimeMark = System.currentTimeMillis();
205
long elapsed = nextTimeMark - timeMark;
206
timeMark = nextTimeMark;
207
//----------------------
208
if (elapsed > 1000) {
209
double seconds = elapsed / 1000.0;
210
if (TRACE_ON)
211
out.println(
212
"pool[" + index + "]=new Object(); // elapsed " + seconds + "s");
213
totalDelay += seconds;
214
if (totalDelay > 60) {
215
if (TRACE_ON)
216
out.println(
217
"Memory allocation became slow; so, heap seems exhausted.");
218
break;
219
}
220
}
221
}
222
} catch (OutOfMemoryError oome) {
223
if (TRACE_ON)
224
log[messages++] = "Heap seems exhausted - OutOfMemoryError thrown.";
225
226
// Do not release any byte once allocated:
227
pool[index++] = oome;
228
}
229
230
if (index > poolSize - 1000) {
231
if (WARN_ON)
232
log[messages++] = "Warning: pool[] is full; so, checks would not be enough hard...";
233
}
234
235
// Check ClassNotFoundException (positive):
236
try {
237
trash = Class.forName("nsk.stress.except.except002$Abra$Cadabra"); // correct - should pass
238
// trash = Class.forName("nsk.stress.except.except002.Abra.Cadabra"); // incorrect - should fail
239
if (TRACE_ON)
240
log[messages++] = "Success: ClassNotFoundException (positive)";
241
} catch (ClassNotFoundException cnfe) {
242
log[messages++] = "Failure: ClassNotFoundException (positive)";
243
exitCode = 2;
244
} catch (OutOfMemoryError oome) {
245
if (WARN_ON)
246
log[messages++] = "Skipped: ClassNotFoundException (positive)";
247
skipped++;
248
}
249
250
return exitCode;
251
}
252
253
/**
254
* Re-call to <code>run(args,out)</code>, and return JCK-like exit status.
255
* (The stream <code>out</code> is assigned to <code>System.out</code> here.)
256
*
257
* @see #run(String[], PrintStream)
258
*/
259
public static void main(String args[]) {
260
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
261
// Last try. If there is some exception outside the code, test should end correctly
262
@Override
263
public void uncaughtException(Thread t, Throwable e) {
264
try {
265
pool = null;
266
log = null;
267
System.gc();
268
if (e instanceof OutOfMemoryError) {
269
try {
270
System.out.println("OOME : Test Skipped");
271
System.exit(0);
272
} catch (Throwable ignore) {
273
} // No code in the handler can provoke correct exceptions.
274
} else {
275
e.printStackTrace();
276
throw (RuntimeException) e;
277
}
278
} catch (OutOfMemoryError oome) {
279
}
280
}
281
});
282
int exitCode = run(args, System.out);
283
System.exit(exitCode + 95);
284
// JCK-like exit status.
285
}
286
287
/**
288
* This class should be used to check <code>ClassNotFoundException</code>
289
* and <code>IllegalAccessException</code>.
290
*/
291
private static class Abra {
292
/**
293
* Will try to incorrectly find this class as <code>Cadabra</code>
294
* instead of <code>Abra$Cadabra</code>.
295
*/
296
public static class Cadabra {
297
}
298
299
}
300
301
}
302
303