Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/OneeFinalizerTest/OneeFinalizerTest.java
41155 views
1
/*
2
* Copyright (c) 2011, 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/OneeFinalizerTest.
29
* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit]
30
* VM Testbase readme:
31
* DESCRIPTION
32
* Regression test that verifies that only one finalizer gets called.
33
*
34
* COMMENTS
35
* This test was ported from JRockit test suite.
36
*
37
* @library /vmTestbase
38
* /test/lib
39
* @run main/othervm
40
* -XX:-UseGCOverheadLimit
41
* -Xlog:gc:gc.log
42
* gc.gctests.OneeFinalizerTest.OneeFinalizerTest
43
*/
44
45
package gc.gctests.OneeFinalizerTest;
46
47
import nsk.share.TestFailure;
48
import nsk.share.gc.GC;
49
import nsk.share.gc.GCTestBase;
50
import nsk.share.gc.gp.GarbageUtils;
51
import nsk.share.test.Stresser;
52
53
/**
54
* Test that verifies that finalize() method is invoking only once.
55
*/
56
public class OneeFinalizerTest extends GCTestBase {
57
58
private GlobalSafeCounter[] finalizerCounters = null;
59
60
/**
61
* Helper class used for counting number of calls to finalizers.
62
*/
63
protected class GlobalSafeCounter {
64
65
private int counter;
66
67
/**
68
* Constructor that inits the global counter to 0.
69
*/
70
protected GlobalSafeCounter() {
71
counter = 0;
72
}
73
74
/**
75
* Reset the global counter to 0.
76
*/
77
protected final void resetCounter() {
78
synchronized (this) {
79
counter = 0;
80
}
81
}
82
83
/**
84
* Increase the global counter by 1.
85
*/
86
protected final void increaseCounter() {
87
synchronized (this) {
88
counter++;
89
}
90
}
91
92
/**
93
* Retrieve the global counter value.
94
*
95
* @return value of the global counter
96
*/
97
protected final int getCounterValue() {
98
int value;
99
100
synchronized (this) {
101
value = counter;
102
}
103
104
return value;
105
}
106
}
107
108
/**
109
* Helper class the implements finalize(), and that increments
110
* the global counters for each finalize() invokation.
111
*/
112
protected class FinalizedObject {
113
114
private final int counterIndex;
115
116
/**
117
* Constructor for the helper object which implements finalize().
118
*
119
* @param index Index for the counter in the global array, that
120
* corresponds to this object.
121
*/
122
protected FinalizedObject(int index) {
123
counterIndex = index;
124
}
125
126
/**
127
* Increases the global counter for this object when finalize()
128
* gets called (to make sure each finalizer gets called onee).
129
*/
130
@Override
131
protected final void finalize() {
132
finalizerCounters[counterIndex].increaseCounter();
133
}
134
}
135
136
private void initOneeFinalizerTest(int numberOfObjects) {
137
// NOTE: Set to null in case it's been used before (to prevent OOM)
138
finalizerCounters = null;
139
finalizerCounters = new GlobalSafeCounter[numberOfObjects];
140
141
for (int i = 0; i < numberOfObjects; i++) {
142
finalizerCounters[i] = new GlobalSafeCounter();
143
}
144
}
145
146
/**
147
* Tests that the finalize() method on each FinalizedObject instance
148
* has been called exactly one time.
149
*/
150
@Override
151
public void run() {
152
153
154
int numberOfObjects = 2000;
155
156
initOneeFinalizerTest(numberOfObjects);
157
158
FinalizedObject[] testObjects = new FinalizedObject[numberOfObjects];
159
160
// creates garbage
161
for (int i = 0; i < numberOfObjects; i++) {
162
testObjects[i] = new FinalizedObject(i);
163
}
164
165
if (testObjects[0].hashCode() == 212_85_06) {
166
System.out.println("Bingo!!!");
167
}
168
169
testObjects = null;
170
171
Stresser stresser = new Stresser(runParams.getStressOptions());
172
stresser.start(0);
173
/* force finalization */
174
GarbageUtils.eatMemory(stresser);
175
if (!stresser.continueExecution()) {
176
// may be we didn't eat all memory and didn't provoke GC
177
System.out.println("Passed without check");
178
return;
179
}
180
System.gc();
181
System.runFinalization();
182
System.gc();
183
System.runFinalization();
184
System.gc();
185
186
int numberOfFinalizersRunMoreThanOnce = 0;
187
int numberOfFinalizersNotRun = 0;
188
189
for (int i = 0; i < numberOfObjects; i++) {
190
int counter = finalizerCounters[i].getCounterValue();
191
if (counter > 1) {
192
numberOfFinalizersRunMoreThanOnce++;
193
System.err.println("Object #" + i + " counter = " + counter);
194
} else if (counter == 0) {
195
System.err.println("WARNING: Finalizer not run for object #" + i);
196
numberOfFinalizersNotRun++;
197
}
198
}
199
200
if (numberOfFinalizersNotRun > 0) {
201
System.err.println("WARNING: " + numberOfFinalizersNotRun + " finalizers not run");
202
}
203
204
if (numberOfFinalizersRunMoreThanOnce != 0) {
205
throw new TestFailure("OneeFinalizerTest failed. " + numberOfFinalizersRunMoreThanOnce + " errors");
206
}
207
System.out.println("Test passed.");
208
}
209
210
public static void main(String[] args) {
211
GC.runTest(new OneeFinalizerTest(), args);
212
}
213
}
214
215