Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReferenceGC/WeakReferenceGC.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
24
25
/*
26
* @test
27
* @key randomness
28
*
29
* @summary converted from VM Testbase gc/gctests/WeakReferenceGC.
30
* VM Testbase keywords: [gc, nonconcurrent]
31
* VM Testbase readme:
32
* *******************************************
33
* set timeout = 25 when running this test
34
* *******************************************
35
* This tests checks to see if the garbage collector enqueues
36
* a weak reference when referrent has been turned to garbage.
37
* Weak references are enqueued in a non-deterministic way
38
* by the garbage collector, so the test uses a heuristic to
39
* determine whether the references are being enqueued in a timely
40
* manner which in turn determines whether outcome of the test.
41
* IF the user invokes the test with the following command line
42
* parameters :
43
* java WeakReferenceGC -qFactor 0.9 -gcCount 5
44
* the test expects that 90% of all objects with only weak references
45
* pointing to them will be enqueued within 5 calls to the garbage collector.
46
* When I ran the test, I consistently got figures of 98% enqueueing
47
* with just 1 call to the garbage collector. So if this test fails,
48
* at its current settings, the garbage collector is not performing as well
49
* as it used to.
50
* The test creates circular linked lists of size 0.1Meg each. The number
51
* of lists created can be set via the -numLists flag. The default
52
* value is 50.
53
* The circular linked lists have both strong and weak references pointing
54
* to them. The strong and weak references are kept in arrays.
55
* The strong references are all nulled out and System.gc() is called
56
* explicitly and the heuristic is applied. If the test does not
57
* satisfy the heuristic or an OutOfMemory exception is thrown,
58
* the test fails.
59
* Array containing Each circular linked list Array containing
60
* weak references is 0.1 Meg each and has strong references
61
* to linked lists. a weak reference, W<n> and a to linked lists.
62
* strong reference, x<n>
63
* pointing to it.
64
* ---- ---------------------------- -----
65
* | | | ---- ---- <-| | |
66
* | W1 |--> -->| |---.......>| | <---- | x1 |
67
* | | -->| |<---.......| |<-- | |
68
* ---- | ---- 1000 --- | -----
69
* ---------------------------
70
* . .
71
* . .
72
* . .
73
* . .
74
* . . 10
75
* . .
76
* ---- ---------------------------- -----
77
* | | | ---- ---- <-| | |
78
* | Wn |--> -->| |---.......>| | <---- | xn |
79
* | | -->| |<---.......| |<-- | |
80
* ---- | ---- 1000 --- | -----
81
* ---------------------------
82
*
83
* @library /vmTestbase
84
* /test/lib
85
* @run main/othervm
86
* gc.gctests.WeakReferenceGC.WeakReferenceGC
87
* -numList 50
88
* -qFactor 0.9
89
* -gcCount 5
90
* -iter 100
91
*/
92
93
package gc.gctests.WeakReferenceGC;
94
95
import java.util.*;
96
import java.lang.ref.*;
97
import nsk.share.TestFailure;
98
import nsk.share.gc.GC;
99
import nsk.share.gc.ThreadedGCTest;
100
import nsk.share.gc.gp.GarbageUtils;
101
102
public class WeakReferenceGC extends ThreadedGCTest {
103
104
// A heuristic is used to determine the outcome(pass/fail
105
// status) of a test. If 90% of all objects that have
106
// _only_ weak references pointing to them are garbage
107
// collected with 5 explicit calls to the garbage collector
108
// the test is deemed a pass. The following two variables
109
// are used to define this heuristic: gcCount, qFactor
110
111
static String args[];
112
113
CircularLinkedList holder[];
114
115
int loopCount = 100; // # of times test is performed
116
117
int memory_reserve[];
118
int gcCount = 5;
119
int numLists = 50; // number of linked lists
120
float qFactor = (float) 0.9;
121
ReferenceQueue refQueue;
122
Vector results;
123
WeakReference wholder[];
124
125
public static void main(String[] args) {
126
WeakReferenceGC.args = args;
127
GC.runTest(new WeakReferenceGC(), args);
128
}
129
130
WeakReferenceGC() {
131
holder = new CircularLinkedList[numLists];
132
wholder = new WeakReference[numLists];
133
refQueue = new ReferenceQueue();
134
results = new Vector();
135
}
136
137
protected Runnable createRunnable(int i) {
138
return i > 0 ? null : new Worker();
139
}
140
141
private void dumpTestResults() {
142
double objectsRecovered;
143
144
System.out.println("Percentage of Objects" + " # of GC's");
145
System.out.println(" Recovered " + " Required");
146
for (int i = 0; i < results.size(); i++) {
147
Statistic s = (Statistic) results.elementAt(i);
148
objectsRecovered = Math.rint((float) (s.numEnqueued)
149
/ (float) (numLists) * 100.0);
150
System.out.println(" " + objectsRecovered
151
+ " % " + s.iterations);
152
}
153
}
154
155
private boolean hasPassed() {
156
boolean passed;
157
passed = true; // assume passed till proven otherwise
158
159
for (int i = 0; i < results.size(); i++) {
160
Statistic s = (Statistic) results.elementAt(i);
161
if ((s.iterations > gcCount)
162
|| (s.numEnqueued < (int) (numLists * qFactor))) {
163
passed = false;
164
break; // test failed
165
}
166
}
167
return passed;
168
}
169
170
private void parseTestParams(String args[]) {
171
for (int i = 0; i < args.length; i++) {
172
if (args[i].compareTo("-numList") == 0) {
173
numLists = Integer.valueOf(args[++i]).intValue();
174
} else if (args[i].compareTo("-qFactor") == 0) {
175
qFactor = Float.valueOf(args[++i]).floatValue();
176
} else if (args[i].compareTo("-gcCount") == 0) {
177
gcCount = Integer.valueOf(args[++i]).intValue();
178
} else if (args[i].compareTo("-iter") == 0) {
179
loopCount = Integer.valueOf(args[++i]).intValue();
180
// } else {
181
// System.err.println("usage : " +
182
// "java WeakReferenceGC [-numList <n>] " +
183
// "[-qFactor <0.x>] [-gcCount <n>] [-iter <n>]");
184
// throw new TestBug("Invalid arguments");
185
}
186
}
187
}
188
189
private void persistentGC() {
190
int numEnqueued, iter, qCriterion;
191
192
numEnqueued = 0; // number of weakReference enqueued
193
iter = 0;
194
qCriterion = (int) (numLists * qFactor);
195
196
while ((numEnqueued < qCriterion) && (iter <= gcCount)) {
197
iter++;
198
if (!getExecutionController().continueExecution()) {
199
return;
200
}
201
if (GarbageUtils.eatMemory(getExecutionController()) == 0) {
202
return; // We were unable to provoke OOME before timeout is over
203
}
204
try {
205
while ((numEnqueued < numLists) &&
206
(refQueue.remove(1000) != null)) {
207
numEnqueued++;
208
}
209
} catch (InterruptedException ie) {
210
}
211
}
212
results.addElement((new Statistic(iter, numEnqueued)));
213
}
214
215
private void runTest() {
216
int iter;
217
iter = 1;
218
try {
219
do {
220
for (int i = 0; i < numLists; i++) {
221
holder[i] = new CircularLinkedList();
222
holder[i].addNelements(1000);
223
wholder[i] = new WeakReference(holder[i], refQueue);
224
}
225
226
for (int i = 0; i < numLists; i++) {
227
holder[i] = null;
228
}
229
230
if (!getExecutionController().continueExecution()) {
231
return;
232
}
233
persistentGC();
234
235
iter++;
236
} while (iter <= loopCount);
237
} catch (OutOfMemoryError e) {
238
memory_reserve = null;
239
System.gc();
240
throw new TestFailure("Failed at iteration=" + loopCount);
241
}
242
}
243
244
//We can't override run() method in WeakReferenceGC, so we are carrying out it to Worker
245
private class Worker implements Runnable {
246
@Override
247
public void run() {
248
parseTestParams(args);
249
runTest();
250
dumpTestResults();
251
boolean passed = hasPassed();
252
if (passed == true) {
253
log.info("Test passed.");
254
} else {
255
log.error("Test failed.");
256
setFailed(true);
257
}
258
}
259
}
260
}
261
262
class Statistic {
263
int iterations;
264
int numEnqueued;
265
266
Statistic(int i, int num) {
267
iterations = i;
268
numEnqueued = num;
269
}
270
271
}
272
273