Skip to content

Commit

Permalink
7903327: Refactor class 'GCProfiler.VMSupport'
Browse files Browse the repository at this point in the history
Reviewed-by: shade
  • Loading branch information
lgxbslgx authored and shipilev committed Sep 23, 2022
1 parent 622ed6e commit c1b3e75
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions jmh-core/src/main/java/org/openjdk/jmh/profile/GCProfiler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -202,6 +202,7 @@ static class VMSupport {
private static final boolean CHURN_AVAILABLE;
private static NotificationListener listener;
private static Multiset<String> churn;
private static boolean started;

static {
ALLOC_AVAILABLE = tryInitAlloc();
Expand Down Expand Up @@ -239,7 +240,8 @@ private static boolean tryInitChurn() {
throw new UnsupportedOperationException("GarbageCollectorMXBean cannot notify");
}
}
newListener();
churn = new HashMultiset<>();
listener = newListener();
return true;
} catch (Throwable e) {
System.out.println("Churn profiling is not available: " + e.getMessage());
Expand All @@ -257,7 +259,6 @@ private static long[] getAllocatedBytes(long[] threadIds) {
}

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

public static synchronized void startChurnProfile() {
if (!CHURN_AVAILABLE) return;
if (listener != null) {
if (started) {
throw new IllegalStateException("Churn profile already started");
}
listener = newListener();
started = true;
churn = new HashMultiset<>();
try {
for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
((NotificationEmitter) bean).addNotificationListener(listener, null, null);
Expand All @@ -319,7 +321,7 @@ public static synchronized void startChurnProfile() {

public static synchronized void finishChurnProfile() {
if (!CHURN_AVAILABLE) return;
if (listener == null) {
if (!started) {
throw new IllegalStateException("Churn profile already stopped");
}

Expand All @@ -337,7 +339,7 @@ public static synchronized void finishChurnProfile() {
// Do nothing
}
}
listener = null;
started = false;
}

public static synchronized Multiset<String> getChurn() {
Expand Down

0 comments on commit c1b3e75

Please sign in to comment.