Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/stress/gclocker/TestExcessGCLockerCollections.java
32285 views
1
/*
2
* Copyright (c) 2019, 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.stress.gclocker;
25
26
// Based on Kim Barrett;s test for JDK-8048556
27
28
/*
29
* @test TestExcessGCLockerCollections
30
* @key gc
31
* @bug 8048556
32
* @summary Check for GC Locker initiated GCs that immediately follow another
33
* GC and so have very little needing to be collected.
34
* @library /testlibrary
35
* @run driver/timeout=1000 gc.stress.gclocker.TestExcessGCLockerCollections 300 4 2
36
*/
37
38
import java.util.HashMap;
39
import java.util.Map;
40
41
import java.util.zip.Deflater;
42
43
import java.util.ArrayList;
44
import java.util.Arrays;
45
46
import javax.management.MBeanServer;
47
import javax.management.Notification;
48
import javax.management.NotificationListener;
49
import javax.management.openmbean.CompositeData;
50
import java.lang.management.ManagementFactory;
51
import java.lang.management.GarbageCollectorMXBean;
52
import java.lang.management.MemoryUsage;
53
import java.util.List;
54
import com.sun.management.GarbageCollectionNotificationInfo;
55
import com.sun.management.GcInfo;
56
57
import com.oracle.java.testlibrary.Asserts;
58
import com.oracle.java.testlibrary.ProcessTools;
59
import com.oracle.java.testlibrary.OutputAnalyzer;
60
61
class TestExcessGCLockerCollectionsStringConstants {
62
// Some constant strings used in both GC logging and error detection
63
static public final String GCLOCKER_CAUSE = "GCLocker Initiated GC";
64
static public final String USED_TOO_LOW = "TOO LOW";
65
static public final String USED_OK = "OK";
66
}
67
68
class TestExcessGCLockerCollectionsAux {
69
static private final int LARGE_MAP_SIZE = 64 * 1024;
70
71
static private final int MAP_ARRAY_LENGTH = 4;
72
static private final int MAP_SIZE = 1024;
73
74
static private final int BYTE_ARRAY_LENGTH = 128 * 1024;
75
76
static private void println(String str) { System.out.println(str); }
77
static private void println() { System.out.println(); }
78
79
static private volatile boolean keepRunning = true;
80
81
static Map<Integer,String> populateMap(int size) {
82
Map<Integer,String> map = new HashMap<Integer,String>();
83
for (int i = 0; i < size; i += 1) {
84
Integer keyInt = Integer.valueOf(i);
85
String valStr = "value is [" + i + "]";
86
map.put(keyInt,valStr);
87
}
88
return map;
89
}
90
91
static private class AllocatingWorker implements Runnable {
92
private final Object[] array = new Object[MAP_ARRAY_LENGTH];
93
private int arrayIndex = 0;
94
95
private void doStep() {
96
Map<Integer,String> map = populateMap(MAP_SIZE);
97
array[arrayIndex] = map;
98
arrayIndex = (arrayIndex + 1) % MAP_ARRAY_LENGTH;
99
}
100
101
public void run() {
102
while (keepRunning) {
103
doStep();
104
}
105
}
106
}
107
108
static private class JNICriticalWorker implements Runnable {
109
private int count;
110
111
private void doStep() {
112
byte[] inputArray = new byte[BYTE_ARRAY_LENGTH];
113
for (int i = 0; i < inputArray.length; i += 1) {
114
inputArray[i] = (byte) (count + i);
115
}
116
117
Deflater deflater = new Deflater();
118
deflater.setInput(inputArray);
119
deflater.finish();
120
121
byte[] outputArray = new byte[2 * inputArray.length];
122
deflater.deflate(outputArray);
123
124
count += 1;
125
}
126
127
public void run() {
128
while (keepRunning) {
129
doStep();
130
}
131
}
132
}
133
134
static class GCNotificationListener implements NotificationListener {
135
static private final double MIN_USED_PERCENT = 40.0;
136
137
static private final List<String> newGenPoolNames = Arrays.asList(
138
"G1 Eden Space", // OpenJDK G1GC: -XX:+UseG1GC
139
"PS Eden Space", // OpenJDK ParallelGC: -XX:+ParallelGC
140
"Par Eden Space", // OpenJDK ConcMarkSweepGC: -XX:+ConcMarkSweepGC
141
"Eden Space" // OpenJDK SerialGC: -XX:+UseSerialGC
142
// OpenJDK ConcMarkSweepGC: -XX:+ConcMarkSweepGC -XX:-UseParNewGC
143
);
144
145
@Override
146
public void handleNotification(Notification notification, Object handback) {
147
try {
148
if (notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
149
GarbageCollectionNotificationInfo info =
150
GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());
151
152
String gc_cause = info.getGcCause();
153
154
if (gc_cause.equals(TestExcessGCLockerCollectionsStringConstants.GCLOCKER_CAUSE)) {
155
Map<String, MemoryUsage> memory_before_gc = info.getGcInfo().getMemoryUsageBeforeGc();
156
157
for (String newGenPoolName : newGenPoolNames) {
158
MemoryUsage usage = memory_before_gc.get(newGenPoolName);
159
if (usage == null) continue;
160
161
double startTime = ((double) info.getGcInfo().getStartTime()) / 1000.0;
162
long used = usage.getUsed();
163
long committed = usage.getCommitted();
164
long max = usage.getMax();
165
double used_percent = (((double) used) / Math.max(committed, max)) * 100.0;
166
167
System.out.printf("%6.3f: (%s) %d/%d/%d, %8.4f%% (%s)\n",
168
startTime, gc_cause, used, committed, max, used_percent,
169
((used_percent < MIN_USED_PERCENT) ? TestExcessGCLockerCollectionsStringConstants.USED_TOO_LOW
170
: TestExcessGCLockerCollectionsStringConstants.USED_OK));
171
}
172
}
173
}
174
} catch (RuntimeException ex) {
175
System.err.println("Exception during notification processing:" + ex);
176
ex.printStackTrace();
177
}
178
}
179
180
public static boolean register() {
181
try {
182
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
183
184
// Get the list of MX
185
List<GarbageCollectorMXBean> gc_mxbeans = ManagementFactory.getGarbageCollectorMXBeans();
186
187
// Create the notification listener
188
GCNotificationListener gcNotificationListener = new GCNotificationListener();
189
190
for (GarbageCollectorMXBean gcbean : gc_mxbeans) {
191
// Add notification listener for the MXBean
192
mbeanServer.addNotificationListener(gcbean.getObjectName(), gcNotificationListener, null, null);
193
}
194
} catch (Exception ex) {
195
System.err.println("Exception during mbean registration:" + ex);
196
ex.printStackTrace();
197
// We've failed to set up, terminate
198
return false;
199
}
200
201
return true;
202
}
203
}
204
205
static public Map<Integer,String> largeMap;
206
207
static public void main(String args[]) {
208
long durationSec = Long.parseLong(args[0]);
209
int allocThreadNum = Integer.parseInt(args[1]);
210
int jniCriticalThreadNum = Integer.parseInt(args[2]);
211
212
println("Running for " + durationSec + " secs");
213
214
if (!GCNotificationListener.register()) {
215
println("failed to register GC notification listener");
216
System.exit(-1);
217
}
218
219
largeMap = populateMap(LARGE_MAP_SIZE);
220
221
println("Starting " + allocThreadNum + " allocating threads");
222
for (int i = 0; i < allocThreadNum; i += 1) {
223
new Thread(new AllocatingWorker()).start();
224
}
225
226
println("Starting " + jniCriticalThreadNum + " jni critical threads");
227
for (int i = 0; i < jniCriticalThreadNum; i += 1) {
228
new Thread(new JNICriticalWorker()).start();
229
}
230
231
long durationMS = (long) (1000 * durationSec);
232
long start = System.currentTimeMillis();
233
long now = start;
234
long soFar = now - start;
235
while (soFar < durationMS) {
236
try {
237
Thread.sleep(durationMS - soFar);
238
} catch (Exception e) {
239
}
240
now = System.currentTimeMillis();
241
soFar = now - start;
242
}
243
println("Done.");
244
keepRunning = false;
245
}
246
}
247
248
public class TestExcessGCLockerCollections {
249
private static final String USED_OK_LINE =
250
"\\(" + TestExcessGCLockerCollectionsStringConstants.GCLOCKER_CAUSE + "\\)"
251
+ " .* " +
252
"\\(" + TestExcessGCLockerCollectionsStringConstants.USED_OK + "\\)";
253
private static final String USED_TOO_LOW_LINE =
254
"\\(" + TestExcessGCLockerCollectionsStringConstants.GCLOCKER_CAUSE + "\\)"
255
+ " .* " +
256
"\\(" + TestExcessGCLockerCollectionsStringConstants.USED_TOO_LOW + "\\)";
257
258
private static final String[] COMMON_OPTIONS = new String[] {
259
"-Xmx1G", "-Xms1G", "-Xmn256M" };
260
261
public static void main(String args[]) throws Exception {
262
if (args.length < 3) {
263
System.out.println("usage: TestExcessGCLockerCollections" +
264
" <duration sec> <alloc threads>" +
265
" <jni critical threads>");
266
throw new RuntimeException("Invalid arguments");
267
}
268
269
ArrayList<String> finalArgs = new ArrayList<String>();
270
finalArgs.addAll(Arrays.asList(COMMON_OPTIONS));
271
finalArgs.add(TestExcessGCLockerCollectionsAux.class.getName());
272
finalArgs.addAll(Arrays.asList(args));
273
274
// GC and other options obtained from test framework.
275
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
276
true, finalArgs.toArray(new String[0]));
277
OutputAnalyzer output = new OutputAnalyzer(pb.start());
278
output.shouldHaveExitValue(0);
279
//System.out.println("------------- begin stdout ----------------");
280
//System.out.println(output.getStdout());
281
//System.out.println("------------- end stdout ----------------");
282
output.stdoutShouldMatch(USED_OK_LINE);
283
output.stdoutShouldNotMatch(USED_TOO_LOW_LINE);
284
}
285
}
286
287