Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/gctest02/gctest02.java
41155 views
1
/*
2
* Copyright (c) 2002, 2021, 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
//gctest02.java
24
25
26
/*
27
* @test
28
* @key randomness
29
*
30
* @summary converted from VM Testbase gc/gctests/gctest02.
31
* VM Testbase keywords: [gc]
32
*
33
* @library /vmTestbase
34
* /test/lib
35
* @run main/othervm gc.gctests.gctest02.gctest02 100
36
*/
37
38
package gc.gctests.gctest02;
39
40
import nsk.share.TestFailure;
41
import nsk.share.TestBug;
42
import nsk.share.test.LocalRandom;
43
44
/* stress testing
45
create 16 memory evil threads requesting to allocate
46
the object of sizes from 8 to ( 2 ^ 19).
47
The live time of objects is random (0 ~ 1000).
48
Here we let the threads that reference the objects
49
to simulate the object life time.
50
*/
51
52
class PopulationException extends Exception {
53
//this exception is used to signal that we've
54
//reached the end of the test
55
}
56
57
class ThreadCount {
58
static int count= 0;
59
static synchronized void inc() { count++; }
60
static synchronized void dec() { count --; }
61
static synchronized int get() { return count; }
62
}
63
64
class Person {
65
String name;
66
int ssid;
67
int age;
68
int buf[];
69
int bufsz;
70
static int populationCount = 0;
71
static int populationLimit = 0;
72
73
Person(String n, int ssid, int age, int bufsz)
74
throws PopulationException {
75
name = n;
76
this.ssid = ssid;
77
this.age = age;
78
if ( bufsz > 0 ) {
79
this.bufsz = bufsz;
80
this.buf = new int[bufsz];
81
}
82
incPopulation();
83
if (getPopulation() > getPopulationLimit()) {
84
throw new PopulationException();
85
}
86
}
87
public static synchronized int getPopulationLimit() {
88
return populationLimit;
89
}
90
public static synchronized void setPopulationLimit(int newLimit) {
91
populationLimit = newLimit;
92
}
93
public static synchronized int getPopulation() {
94
return populationCount;
95
}
96
public static synchronized void incPopulation() {
97
populationCount ++;
98
}
99
100
}
101
102
// hr (humane resource) dept is using objects.
103
// Put the hr thread to sleep to keep the reference to objects
104
class hr extends Thread {
105
Person pp;
106
int lifetime;
107
108
hr(Person p, int l) {
109
pp = p;
110
lifetime = l;
111
}
112
113
public void run() {
114
// just sleep to emulate the life time of object referenced by p
115
try { sleep(lifetime); }
116
catch (InterruptedException e) {}
117
}
118
}
119
120
class Memevil extends Thread {
121
int sum;
122
int bufsz = 64;
123
boolean debug = false;
124
125
Memevil(int bufsz) {
126
sum = 0;
127
this.bufsz = bufsz;
128
}
129
/* Person object is live short, it will be garbage after
130
* control returns
131
*/
132
private boolean doit() {
133
try {
134
Person p = new Person("Duke", 100, 100, bufsz);
135
hr useit = new hr(p, (int)(100*LocalRandom.random()));
136
useit.start();
137
return true;
138
}
139
catch (PopulationException e) {
140
return false;
141
}
142
catch (OutOfMemoryError e ) {
143
System.err.println(getName() + ": Out of Memory");
144
return false;
145
}
146
}
147
public void run() {
148
while ( doit() ) {
149
if ( LocalRandom.random() > 0.6668) {
150
try {
151
sleep(10); // to be nice
152
}
153
catch (InterruptedException e) {
154
}
155
}
156
Thread.yield();
157
}
158
//we've reached the population limit, so we're exiting the thread
159
ThreadCount.dec();
160
}
161
}
162
163
class Escaper extends Thread {
164
public void run() {
165
while ( ThreadCount.get() > 0 ) {
166
int buf[] = new int[32];
167
{
168
Thread.yield();
169
}
170
}
171
}
172
}
173
174
public class gctest02 {
175
public static void main(String args[] ) {
176
int bufsz = 8;
177
int peopleLimit = 1000;
178
Memevil me=null;
179
if (args.length > 0)
180
{
181
try
182
{
183
peopleLimit = Integer.valueOf(args[0]).intValue();
184
}
185
catch (NumberFormatException e)
186
{
187
throw new TestBug(
188
"Bad input to gctest02. Expected integer, got: ->"
189
+ args[0] + "<-", e);
190
}
191
}
192
193
Person.setPopulationLimit(peopleLimit);
194
for (int ii=0; ii<40; ii++) {
195
bufsz = 8;
196
Person.populationCount = 0;
197
Escaper you = new Escaper();
198
you.setName("Escaper");
199
ThreadCount.inc();
200
you.start();
201
me = new Memevil(bufsz);
202
me.setName("Memevil" + bufsz);
203
bufsz = 2*bufsz;
204
me.start();
205
Thread.yield();
206
for (int i=1; i<11; i++) {
207
ThreadCount.inc();
208
me = new Memevil(bufsz);
209
me.setName("Memevil" + bufsz);
210
bufsz = 2*bufsz;
211
me.start();
212
Thread.yield();
213
}
214
try {
215
you.join();
216
}
217
catch (InterruptedException e) {
218
throw new TestFailure("InterruptedException in gctest2.main()");
219
}
220
for (int i=1; i<11; i++) {
221
try { me.join(); }
222
catch (InterruptedException e) {
223
throw new TestFailure("InterruptedException in gctest2.main()");
224
}
225
}
226
}
227
System.out.println("Test passed.");
228
}
229
}
230
231