Skip to content

Commit

Permalink
Reenabling tests for direct memory probe.
Browse files Browse the repository at this point in the history
  • Loading branch information
gsharma committed Mar 11, 2019
1 parent fcdbd0c commit 2483d42
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 40 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/github/jaemons/DirectMemoryProbe.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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();
Expand Down
94 changes: 54 additions & 40 deletions src/test/java/com/github/jaemons/DirectMemoryProbeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<DirectMemorySnapshot> 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);
}
}
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit 2483d42

Please sign in to comment.