Skip to content

Commit c1b3e75

Browse files
lgxbslgxshipilev
authored andcommitted
7903327: Refactor class 'GCProfiler.VMSupport'
Reviewed-by: shade
1 parent 622ed6e commit c1b3e75

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

jmh-core/src/main/java/org/openjdk/jmh/profile/GCProfiler.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -202,6 +202,7 @@ static class VMSupport {
202202
private static final boolean CHURN_AVAILABLE;
203203
private static NotificationListener listener;
204204
private static Multiset<String> churn;
205+
private static boolean started;
205206

206207
static {
207208
ALLOC_AVAILABLE = tryInitAlloc();
@@ -239,7 +240,8 @@ private static boolean tryInitChurn() {
239240
throw new UnsupportedOperationException("GarbageCollectorMXBean cannot notify");
240241
}
241242
}
242-
newListener();
243+
churn = new HashMultiset<>();
244+
listener = newListener();
243245
return true;
244246
} catch (Throwable e) {
245247
System.out.println("Churn profiling is not available: " + e.getMessage());
@@ -257,7 +259,6 @@ private static long[] getAllocatedBytes(long[] threadIds) {
257259
}
258260

259261
private static NotificationListener newListener() {
260-
churn = new HashMultiset<>();
261262
try {
262263
final Class<?> infoKlass = Class.forName("com.sun.management.GarbageCollectionNotificationInfo");
263264
final Field notifNameField = infoKlass.getField("GARBAGE_COLLECTION_NOTIFICATION");
@@ -304,10 +305,11 @@ public static HotspotAllocationSnapshot getSnapshot() {
304305

305306
public static synchronized void startChurnProfile() {
306307
if (!CHURN_AVAILABLE) return;
307-
if (listener != null) {
308+
if (started) {
308309
throw new IllegalStateException("Churn profile already started");
309310
}
310-
listener = newListener();
311+
started = true;
312+
churn = new HashMultiset<>();
311313
try {
312314
for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
313315
((NotificationEmitter) bean).addNotificationListener(listener, null, null);
@@ -319,7 +321,7 @@ public static synchronized void startChurnProfile() {
319321

320322
public static synchronized void finishChurnProfile() {
321323
if (!CHURN_AVAILABLE) return;
322-
if (listener == null) {
324+
if (!started) {
323325
throw new IllegalStateException("Churn profile already stopped");
324326
}
325327

@@ -337,7 +339,7 @@ public static synchronized void finishChurnProfile() {
337339
// Do nothing
338340
}
339341
}
340-
listener = null;
342+
started = false;
341343
}
342344

343345
public static synchronized Multiset<String> getChurn() {

0 commit comments

Comments
 (0)