Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/g1/unloading/UnloadingTest.java
41159 views
1
/*
2
* Copyright (c) 2014, 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
package gc.g1.unloading;
25
26
import java.lang.Thread.UncaughtExceptionHandler;
27
import java.lang.management.*;
28
import java.util.Collection;
29
import java.util.List;
30
import java.util.Random;
31
import java.util.concurrent.atomic.AtomicLong;
32
33
import gc.g1.unloading.check.Assertion;
34
import gc.g1.unloading.check.AssertionContainer;
35
import gc.g1.unloading.check.ClassAssertion;
36
import gc.g1.unloading.configuration.*;
37
import gc.g1.unloading.loading.*;
38
import nsk.share.gc.GCTestBase;
39
import nsk.share.test.ExecutionController;
40
import nsk.share.test.Stresser;
41
import nsk.share.test.Tests;
42
43
import jtreg.SkippedException;
44
45
/**
46
* This class contains main method. It's entry point for all configurations.
47
*
48
*/
49
public class UnloadingTest extends GCTestBase {
50
51
private static String[] args;
52
53
private TestConfiguration configuration;
54
55
private AssertionContainer assertionContainer = new AssertionContainer();
56
57
private Random random;
58
59
private static final String classNamePrefix = "ClassAbc_";
60
61
private static final long DELAY = 300;
62
63
private static AtomicLong systemGcCallsCounter = new AtomicLong(0);
64
65
public static void main(String[] args) {
66
UnloadingTest.args = args;
67
Tests.runTest(new UnloadingTest(), args);
68
}
69
70
@Override
71
public void run() {
72
configuration = TestConfiguration.createTestConfiguration(args);
73
74
checkIfG1Used();
75
checkFlags();
76
77
ExecutionController stresser = new Stresser(args);
78
stresser.start(1);
79
80
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
81
@Override
82
public void uncaughtException(Thread t, Throwable e) {
83
System.out.println("Throwable \"" + e + "\" in thread " + t.getName() + ", id=" + t.getId());
84
e.printStackTrace();
85
try {
86
checkGCCounters();
87
} catch (Throwable thr) {
88
thr.printStackTrace();
89
}
90
System.exit(2);
91
}
92
});
93
94
random = new Random(runParams.getSeed());
95
ClassLoadingHelper classLoadingHelper = new ClassLoadingHelper(stresser, random.nextLong(), configuration);
96
97
int classesCounter = 0;
98
while (stresser.continueExecution()) {
99
Collection<Assertion> assertions = null;
100
String className = classNamePrefix + (classesCounter++);
101
102
try {
103
Thread.sleep(DELAY);
104
} catch (InterruptedException | IllegalArgumentException e) {
105
throw new RuntimeException("Something went wrong in ClassLoadingHelper", e);
106
}
107
108
if (random.nextBoolean()) {
109
assertions = classLoadingHelper.loadClassThatGonnaDie(className);
110
} else {
111
assertions = classLoadingHelper.loadClassThatGonnaLive(className);
112
}
113
114
System.gc();
115
long systemGCCalls = systemGcCallsCounter.incrementAndGet();
116
117
assertionContainer.enqueue(assertions, systemGCCalls);
118
119
check(assertionContainer.getElder(systemGCCalls - configuration.getNumberOfGCsBeforeCheck()));
120
121
if (configuration.getNumberOfChecksLimit() >= 0 &&
122
ClassAssertion.getCounterOfCheckedAlive() >= configuration.getNumberOfChecksLimit() &&
123
ClassAssertion.getCounterOfCheckedUnloaded() >= configuration.getNumberOfChecksLimit()) {
124
System.out.println("Exiting because numberOfChecksLimit exceeded.");
125
stresser.finish();
126
break;
127
}
128
}
129
130
System.out.println("ClassAssertion.getCounterOfCheckedAlive() = " + ClassAssertion.getCounterOfCheckedAlive());
131
System.out.println("ClassAssertion.getCounterOfCheckedUnloaded() = " + ClassAssertion.getCounterOfCheckedUnloaded());
132
checkGCCounters();
133
if (System.getProperty("FailTestIfNothingChecked") != null) {
134
if (ClassAssertion.getCounterOfCheckedAlive() == 0 || ClassAssertion.getCounterOfCheckedUnloaded() == 0) {
135
throw new RuntimeException("Test was useless. Smthng not checked: " + ClassAssertion.getCounterOfCheckedAlive() + " " +
136
ClassAssertion.getCounterOfCheckedUnloaded());
137
}
138
}
139
}
140
141
private void check(Collection<Assertion> assertions) {
142
if (assertions.isEmpty()) {
143
return;
144
}
145
for (Assertion assertion : assertions) {
146
assertion.check();
147
assertion.cleanup();
148
}
149
}
150
151
private static void checkGCCounters() {
152
// System.out.println("WhiteBox.getWhiteBox().g1GetTotalCollections() = \t" + WhiteBox.getWhiteBox().g1GetTotalCollections());
153
// System.out.println("WhiteBox.getWhiteBox().g1GetTotalFullCollections() = \t" + WhiteBox.getWhiteBox().g1GetTotalFullCollections());
154
GarbageCollectorMXBean oldGenBean = null;
155
for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
156
System.out.println("bean.getName() = \t\"" + bean.getName() + "\", bean.getCollectionCount() = \t" + bean.getCollectionCount());
157
if (bean.getName().contains("Old")) {
158
oldGenBean = bean;
159
}
160
}
161
// if (WhiteBox.getWhiteBox().g1GetTotalFullCollections() != 0 || (oldGenBean != null && oldGenBean.getCollectionCount() != 0)) {
162
if (oldGenBean != null && oldGenBean.getCollectionCount() != 0) {
163
throw new RuntimeException("Full gc happened. Test was useless.");
164
}
165
}
166
167
private void checkIfG1Used() {
168
for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
169
if (!bean.getName().contains("G1")) {
170
throw new SkippedException("This test was created to cover G1 class unloading feature. It should be ran with -XX:+UseG1GC");
171
}
172
}
173
}
174
175
private void checkFlags() {
176
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
177
List<String> arguments = runtimeMxBean.getInputArguments();
178
for (String argument : arguments) {
179
if (argument.contains("ExplicitGCInvokesConcurrent")) {
180
return;
181
}
182
}
183
throw new RuntimeException("This test supposed to be ran with -XX:+ExplicitGCInvokesConcurrent flag");
184
}
185
186
}
187
188