From 2483d42ec4743248ee010d68a69bb1985f7c9ed9 Mon Sep 17 00:00:00 2001 From: Gaurav Sharma Date: Sun, 10 Mar 2019 23:20:38 -0700 Subject: [PATCH] Reenabling tests for direct memory probe. --- .../com/github/jaemons/DirectMemoryProbe.java | 5 + .../github/jaemons/DirectMemoryProbeTest.java | 94 +++++++++++-------- 2 files changed, 59 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/github/jaemons/DirectMemoryProbe.java b/src/main/java/com/github/jaemons/DirectMemoryProbe.java index 3ba608d..077ff5a 100644 --- a/src/main/java/com/github/jaemons/DirectMemoryProbe.java +++ b/src/main/java/com/github/jaemons/DirectMemoryProbe.java @@ -36,6 +36,10 @@ public DirectMemoryProbe(final long probeMillis) { start(); } + public enum DirectMemoryPool { + CODE_CACHE, METASPACE, COMPRESSED_CLASS_SPACE, DIRECT_MAPPED; + } + @Override public void run() { logger.info("Initialized probe"); @@ -61,6 +65,7 @@ public void run() { } final DirectMemorySnapshot directMemorySnapshot = new DirectMemorySnapshot(); directMemorySnapshot.probeTime = System.currentTimeMillis(); + directMemorySnapshot.poolName = "Direct/Mapped"; final MemoryUsage offHeapUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage(); directMemorySnapshot.initMemory = offHeapUsage.getInit(); diff --git a/src/test/java/com/github/jaemons/DirectMemoryProbeTest.java b/src/test/java/com/github/jaemons/DirectMemoryProbeTest.java index 460276e..9c60688 100644 --- a/src/test/java/com/github/jaemons/DirectMemoryProbeTest.java +++ b/src/test/java/com/github/jaemons/DirectMemoryProbeTest.java @@ -12,6 +12,8 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.junit.Test; import com.github.jaemons.DirectMemoryProbe.BufferSnapshot; @@ -28,34 +30,41 @@ * @author gaurav */ public final class DirectMemoryProbeTest { + private static final Logger logger = + LogManager.getLogger(DirectMemoryProbeTest.class.getSimpleName()); @Test public void testSnapshots() throws Exception { - long probeFrequency = 10L; + long probeFrequency = 10L, probeTime = -1L; final DirectMemoryProbe probe = new DirectMemoryProbe(probeFrequency); // wait for probe to wake-up and collect data LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(probeFrequency * 4)); // step 1: query probe for latest snapshot of direct memory usage List snapshots = probe.getDirectMemoryUsage(); + assertEquals(4, snapshots.size()); for (final DirectMemorySnapshot snapshot : snapshots) { - long probeTime = snapshot.probeTime; + // logger.info(snapshot); + probeTime = snapshot.probeTime; assertTrue(probeTime < System.currentTimeMillis()); - // assertEquals(2, snapshot.bufferSnapshots.size()); - // assertTrue(-1L, snapshot.maxMemory); - - // expect no direct memory usage - for (final BufferSnapshot bufferSnapshot : snapshot.bufferSnapshots) { - if (bufferSnapshot.poolName.equals("direct")) { - assertEquals(0L, bufferSnapshot.bufferCount); - assertEquals(0L, bufferSnapshot.memoryUsed); - assertEquals(0L, bufferSnapshot.capacityEstimate); - } - - else if (bufferSnapshot.poolName.equals("mapped")) { - assertEquals(0L, bufferSnapshot.bufferCount); - assertEquals(0L, bufferSnapshot.memoryUsed); - assertEquals(0L, bufferSnapshot.capacityEstimate); + + if (snapshot.poolName.equals("Direct/Mapped")) { + assertEquals(2, snapshot.bufferSnapshots.size()); + assertEquals(-1L, snapshot.maxMemory); + + // expect no direct memory usage + for (final BufferSnapshot bufferSnapshot : snapshot.bufferSnapshots) { + if (bufferSnapshot.poolName.equals("direct")) { + assertEquals(0L, bufferSnapshot.bufferCount); + assertEquals(0L, bufferSnapshot.memoryUsed); + assertEquals(0L, bufferSnapshot.capacityEstimate); + } + + else if (bufferSnapshot.poolName.equals("mapped")) { + assertEquals(0L, bufferSnapshot.bufferCount); + assertEquals(0L, bufferSnapshot.memoryUsed); + assertEquals(0L, bufferSnapshot.capacityEstimate); + } } } } @@ -84,8 +93,10 @@ else if (bufferSnapshot.poolName.equals("mapped")) { // ensure non-stale snapshot // assertTrue(probeTime < snapshot.probeTime); // probeTime = snapshot.probeTime; - // assertEquals(2, snapshot.bufferSnapshots.size()); - // assertEquals(-1L, snapshot.maxMemory); + + if (snapshot.poolName.equals("Direct/Mapped")) { + assertEquals(2, snapshot.bufferSnapshots.size()); + assertEquals(-1L, snapshot.maxMemory); // expect probe to find direct memory usage for (final BufferSnapshot bufferSnapshot : snapshot.bufferSnapshots) { @@ -101,36 +112,39 @@ else if (bufferSnapshot.poolName.equals("mapped")) { assertEquals(mappedBufferCapacity, bufferSnapshot.capacityEstimate); } } + } } - /* // step 3: garbage collect allocated direct buffer, probe should reflect zero usage DirectMemoryProbe.gcOffHeapBuffer(directBuffer); DirectMemoryProbe.gcOffHeapBuffer(mappedBuffer); LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(probeFrequency * 4)); - snapshot = probe.getDirectMemoryUsage(); - // ensure non-stale snapshot - assertTrue(probeTime < snapshot.probeTime); - probeTime = snapshot.probeTime; - assertEquals(2, snapshot.bufferSnapshots.size()); - assertEquals(-1L, snapshot.maxMemory); - - // expect no direct memory usage since buffers were both garbage-collected - for (final BufferSnapshot bufferSnapshot : snapshot.bufferSnapshots) { - if (bufferSnapshot.poolName.equals("direct")) { - assertEquals(0L, bufferSnapshot.bufferCount); - assertEquals(0L, bufferSnapshot.memoryUsed); - assertEquals(0L, bufferSnapshot.capacityEstimate); - } - - else if (bufferSnapshot.poolName.equals("mapped")) { - assertEquals(0L, bufferSnapshot.bufferCount); - assertEquals(0L, bufferSnapshot.memoryUsed); - assertEquals(0L, bufferSnapshot.capacityEstimate); + snapshots = probe.getDirectMemoryUsage(); + for (final DirectMemorySnapshot snapshot : snapshots) { + // ensure non-stale snapshot + // assertTrue(probeTime < snapshot.probeTime); + // probeTime = snapshot.probeTime; + if (snapshot.poolName.equals("Direct/Mapped")) { + assertEquals(2, snapshot.bufferSnapshots.size()); + assertEquals(-1L, snapshot.maxMemory); + + // expect no direct memory usage since buffers were both garbage-collected + for (final BufferSnapshot bufferSnapshot : snapshot.bufferSnapshots) { + if (bufferSnapshot.poolName.equals("direct")) { + assertEquals(0L, bufferSnapshot.bufferCount); + assertEquals(0L, bufferSnapshot.memoryUsed); + assertEquals(0L, bufferSnapshot.capacityEstimate); + } + + else if (bufferSnapshot.poolName.equals("mapped")) { + assertEquals(0L, bufferSnapshot.bufferCount); + assertEquals(0L, bufferSnapshot.memoryUsed); + assertEquals(0L, bufferSnapshot.capacityEstimate); + } + } } } - */ /* * // step 4: allocate netty bytebuf directBufferCapacity = 8; final ByteBuf nettyByteBuf = new