Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/PhantomReference/PhantomReferenceEvilTest/PhantomReferenceEvilTest.java
41161 views
1
/*
2
* Copyright (c) 2004, 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
* @test
26
* @key stress randomness
27
*
28
* @summary converted from VM Testbase gc/gctests/PhantomReference/PhantomReferenceEvilTest.
29
* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit]
30
* VM Testbase readme:
31
* DESCRIPTION
32
* Test that verifies the PhantomReference handling in a more evil way.
33
* In this test, it will only keep every Xth object, thus causing more
34
* fragmentation and fill the heap with unused objects. This test should
35
* not throw any OOME during the test execution.
36
*
37
* COMMENTS
38
* This test was ported from JRockit test suite.
39
*
40
* @library /vmTestbase
41
* /test/lib
42
* @run main/othervm
43
* -XX:-UseGCOverheadLimit
44
* gc.gctests.PhantomReference.PhantomReferenceEvilTest.PhantomReferenceEvilTest
45
*/
46
47
package gc.gctests.PhantomReference.PhantomReferenceEvilTest;
48
49
import gc.gctests.PhantomReference.PhantomHelper;
50
import gc.gctests.PhantomReference.PRHelper;
51
import java.lang.ref.ReferenceQueue;
52
import java.util.ArrayList;
53
import java.util.Random;
54
import java.util.HashMap;
55
import nsk.share.TestFailure;
56
import nsk.share.gc.GC;
57
import nsk.share.gc.GCTestBase;
58
import nsk.share.gc.Memory;
59
import nsk.share.gc.gp.GarbageUtils;
60
import nsk.share.test.Stresser;
61
62
/**
63
* Tests for the PhantomReference handling in a more evil way.
64
*
65
* This test must be run with a mx value set to ensure
66
* Runtime.maxMemory() doesn't return 0.
67
*/
68
public class PhantomReferenceEvilTest extends GCTestBase {
69
70
/**
71
* Test that verifies the PhantomReference handling in a more evil way.
72
* In this test, it will only keep every Xth object, thus causing more
73
* fragmentation and fill the heap with unused objects. This test should
74
* not throw any OOME during the test execution.
75
*
76
* @return success if all phantom references were enqueued
77
*/
78
public final void run() {
79
long seed;
80
int minSize;
81
int maxSize;
82
int keepEveryXthObject;
83
double memPercentToFill;
84
long maxWaitTime;
85
long counter = 0;
86
long totalMemAlloced = 0;
87
long memToAlloc = 0;
88
long nrOfPrs = 0;
89
Runtime r = Runtime.getRuntime();
90
91
92
seed = runParams.getSeed();
93
minSize = 2048;
94
maxSize = 32768;
95
keepEveryXthObject = 5;
96
memPercentToFill = 0.45;
97
maxWaitTime = 30000;
98
memToAlloc = (long) (r.maxMemory() * memPercentToFill);
99
Random rndGenerator = new Random(seed);
100
long multiplier = maxSize - minSize;
101
ReferenceQueue rq = new ReferenceQueue();
102
HashMap hmHelper = new HashMap();
103
ArrayList alPhantomRefs = new ArrayList();
104
105
try {
106
try {
107
while (totalMemAlloced + Memory.getReferenceSize()
108
* hmHelper.size() < memToAlloc) {
109
int allocationSize = ((int) (rndGenerator.nextDouble()
110
* multiplier)) + minSize;
111
byte[] tmp = new byte[allocationSize];
112
113
if (counter % keepEveryXthObject == 0) {
114
Integer ik = Integer.valueOf(tmp.hashCode());
115
if (hmHelper.containsKey(ik)) {
116
PhantomHelper ph = (PhantomHelper) hmHelper.get(ik);
117
ph.increaseHashCounter();
118
hmHelper.put(ik, ph);
119
} else {
120
hmHelper.put(ik, new PhantomHelper(tmp.hashCode()));
121
}
122
123
PRHelper prh = new PRHelper(tmp, rq);
124
prh.setReferentHashCode(tmp.hashCode());
125
alPhantomRefs.add(prh);
126
totalMemAlloced +=
127
Memory.getArraySize(allocationSize, Memory.getByteSize())
128
+ Memory.getReferenceSize()
129
+ Memory.getReferenceObjectSize();
130
131
//Make sure the temporary object is dereferenced
132
prh = null;
133
nrOfPrs++;
134
}
135
136
// Make sure the temporary object is dereferenced
137
tmp = null;
138
counter++;
139
if (counter == Long.MAX_VALUE) {
140
counter = 0;
141
}
142
}
143
} catch (OutOfMemoryError oome) {
144
alPhantomRefs.clear();
145
hmHelper.clear();
146
log.info(nrOfPrs + " phantom refs had been allocated when "
147
+ "OOME occured");
148
throw new TestFailure("OutOfMemoryException was thrown. This should "
149
+ "not happen during the execution of this test.");
150
}
151
152
153
Stresser stresser = new Stresser(runParams.getStressOptions());
154
stresser.start(0);
155
GarbageUtils.eatMemory(stresser);
156
if (!stresser.continueExecution()) {
157
return; //we couldn't be sure that FullGC is triggered
158
}
159
160
String retInfo = PhantomHelper.checkAllHashCodes(
161
rq, hmHelper, maxWaitTime);
162
if (retInfo != null) {
163
alPhantomRefs.clear();
164
hmHelper.clear();
165
throw new TestFailure(retInfo);
166
}
167
168
log.info(nrOfPrs + " phantom refs were allocated during the test");
169
} finally {
170
// Make sure the ArrayList:s are live at the end of the test
171
// to make sure that the references gets enqueued.
172
alPhantomRefs.clear();
173
hmHelper.clear();
174
alPhantomRefs = null;
175
hmHelper = null;
176
}
177
}
178
179
public static void main(String[] args) {
180
GC.runTest(new PhantomReferenceEvilTest(), args);
181
}
182
}
183
184