8244291: Test: gc/z/TestGarbageCollectorMXBean.java failed: "unexpected cycles"

Reviewed-by: eosterlund, tschatzl
This commit is contained in:
Per Lidén 2020-05-20 10:54:11 +02:00
parent e9b8463d3d
commit 58a34f7c32

View File

@ -34,13 +34,20 @@ import java.lang.management.ManagementFactory;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import javax.management.Notification; import javax.management.Notification;
import javax.management.NotificationBroadcaster; import javax.management.NotificationEmitter;
import javax.management.NotificationListener; import javax.management.NotificationListener;
import javax.management.openmbean.CompositeData; import javax.management.openmbean.CompositeData;
import com.sun.management.GarbageCollectionNotificationInfo; import com.sun.management.GarbageCollectionNotificationInfo;
public class TestGarbageCollectorMXBean { public class TestGarbageCollectorMXBean {
private static final long startTime = System.nanoTime();
private static void log(String msg) {
final String elapsedSeconds = String.format("%.3fs", (System.nanoTime() - startTime) / 1_000_000_000.0);
System.out.println("[" + elapsedSeconds + "] (" + Thread.currentThread().getName() + ") " + msg);
}
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
final long M = 1024 * 1024; final long M = 1024 * 1024;
final long initialCapacity = Long.parseLong(args[0]) * M; final long initialCapacity = Long.parseLong(args[0]) * M;
@ -67,61 +74,61 @@ public class TestGarbageCollectorMXBean {
final var memoryUsageBeforeGC = info.getGcInfo().getMemoryUsageBeforeGc().get("ZHeap"); final var memoryUsageBeforeGC = info.getGcInfo().getMemoryUsageBeforeGc().get("ZHeap");
final var memoryUsageAfterGC = info.getGcInfo().getMemoryUsageAfterGc().get("ZHeap"); final var memoryUsageAfterGC = info.getGcInfo().getMemoryUsageAfterGc().get("ZHeap");
System.out.println(name + " (" + type + ")"); log(name + " (" + type + ")");
System.out.println(" Id: " + id); log(" Id: " + id);
System.out.println(" Action: " + action); log(" Action: " + action);
System.out.println(" Cause: " + cause); log(" Cause: " + cause);
System.out.println(" StartTime: " + startTime); log(" StartTime: " + startTime);
System.out.println(" EndTime: " + endTime); log(" EndTime: " + endTime);
System.out.println(" Duration: " + duration); log(" Duration: " + duration);
System.out.println(" MemoryUsageBeforeGC: " + memoryUsageBeforeGC); log(" MemoryUsageBeforeGC: " + memoryUsageBeforeGC);
System.out.println(" MemoryUsageAfterGC: " + memoryUsageAfterGC); log(" MemoryUsageAfterGC: " + memoryUsageAfterGC);
System.out.println(); log("");
if (name.equals("ZGC")) { if (name.equals("ZGC")) {
cycles.incrementAndGet(); cycles.incrementAndGet();
} else { } else {
System.out.println("ERROR: Name"); log("ERROR: Name");
errors.incrementAndGet(); errors.incrementAndGet();
} }
if (!action.equals("end of major GC")) { if (!action.equals("end of major GC")) {
System.out.println("ERROR: Action"); log("ERROR: Action");
errors.incrementAndGet(); errors.incrementAndGet();
} }
if (memoryUsageBeforeGC.getInit() != initialCapacity) { if (memoryUsageBeforeGC.getInit() != initialCapacity) {
System.out.println("ERROR: MemoryUsageBeforeGC.init"); log("ERROR: MemoryUsageBeforeGC.init");
errors.incrementAndGet(); errors.incrementAndGet();
} }
if (memoryUsageBeforeGC.getUsed() > initialCapacity) { if (memoryUsageBeforeGC.getUsed() > initialCapacity) {
System.out.println("ERROR: MemoryUsageBeforeGC.used"); log("ERROR: MemoryUsageBeforeGC.used");
errors.incrementAndGet(); errors.incrementAndGet();
} }
if (memoryUsageBeforeGC.getCommitted() != initialCapacity) { if (memoryUsageBeforeGC.getCommitted() != initialCapacity) {
System.out.println("ERROR: MemoryUsageBeforeGC.committed"); log("ERROR: MemoryUsageBeforeGC.committed");
errors.incrementAndGet(); errors.incrementAndGet();
} }
if (memoryUsageBeforeGC.getMax() != maxCapacity) { if (memoryUsageBeforeGC.getMax() != maxCapacity) {
System.out.println("ERROR: MemoryUsageBeforeGC.max"); log("ERROR: MemoryUsageBeforeGC.max");
errors.incrementAndGet(); errors.incrementAndGet();
} }
if (!cause.equals("System.gc()")) { if (!cause.equals("System.gc()")) {
System.out.println("ERROR: Cause"); log("ERROR: Cause");
errors.incrementAndGet(); errors.incrementAndGet();
} }
if (startTime > endTime) { if (startTime > endTime) {
System.out.println("ERROR: StartTime"); log("ERROR: StartTime");
errors.incrementAndGet(); errors.incrementAndGet();
} }
if (endTime - startTime != duration) { if (endTime - startTime != duration) {
System.out.println("ERROR: Duration"); log("ERROR: Duration");
errors.incrementAndGet(); errors.incrementAndGet();
} }
}; };
@ -131,22 +138,25 @@ public class TestGarbageCollectorMXBean {
// Register GC event listener // Register GC event listener
for (final var collector : ManagementFactory.getGarbageCollectorMXBeans()) { for (final var collector : ManagementFactory.getGarbageCollectorMXBeans()) {
final NotificationBroadcaster broadcaster = (NotificationBroadcaster)collector; final NotificationEmitter emitter = (NotificationEmitter)collector;
broadcaster.addNotificationListener(listener, null, null); emitter.addNotificationListener(listener, null, null);
} }
final int minCycles = 5; final int minCycles = 5;
// Run GCs // Run GCs
for (int i = 0; i < minCycles; i++) { for (int i = 0; i < minCycles; i++) {
log("Starting GC " + i);
System.gc(); System.gc();
} }
// Wait at most 60 seconds // Wait at most 90 seconds
for (int i = 0; i < 60; i++) { for (int i = 0; i < 90; i++) {
log("Waiting...");
Thread.sleep(1000); Thread.sleep(1000);
if (cycles.get() >= minCycles) { if (cycles.get() >= minCycles) {
// All events received log("All events received!");
break; break;
} }
} }
@ -154,12 +164,12 @@ public class TestGarbageCollectorMXBean {
final int actualCycles = cycles.get(); final int actualCycles = cycles.get();
final int actualErrors = errors.get(); final int actualErrors = errors.get();
System.out.println(" minCycles: " + minCycles); log(" minCycles: " + minCycles);
System.out.println("actualCycles: " + actualCycles); log("actualCycles: " + actualCycles);
System.out.println("actualErrors: " + actualErrors); log("actualErrors: " + actualErrors);
// Verify number of cycle events // Verify number of cycle events
if (cycles.get() < minCycles) { if (actualCycles < minCycles) {
throw new Exception("Unexpected cycles"); throw new Exception("Unexpected cycles");
} }