diff --git a/buffer/src/main/java/io/netty/buffer/PoolArena.java b/buffer/src/main/java/io/netty/buffer/PoolArena.java index 2d03f61d45..9b19d27946 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolArena.java +++ b/buffer/src/main/java/io/netty/buffer/PoolArena.java @@ -65,6 +65,7 @@ abstract class PoolArena implements PoolArenaMetric { private final LongCounter allocationsTiny = PlatformDependent.newLongCounter(); private final LongCounter allocationsSmall = PlatformDependent.newLongCounter(); private final LongCounter allocationsHuge = PlatformDependent.newLongCounter(); + private final LongCounter activeBytesHuge = PlatformDependent.newLongCounter(); private long deallocationsTiny; private long deallocationsSmall; @@ -242,13 +243,17 @@ abstract class PoolArena implements PoolArenaMetric { } private void allocateHuge(PooledByteBuf buf, int reqCapacity) { - buf.initUnpooled(newUnpooledChunk(reqCapacity), reqCapacity); + PoolChunk chunk = newUnpooledChunk(reqCapacity); + activeBytesHuge.add(chunk.chunkSize()); + buf.initUnpooled(chunk, reqCapacity); allocationsHuge.increment(); } void free(PoolChunk chunk, long handle, int normCapacity, PoolThreadCache cache) { if (chunk.unpooled) { + int size = chunk.chunkSize(); destroyChunk(chunk); + activeBytesHuge.add(-size); deallocationsHuge.decrement(); } else { SizeClass sizeClass = sizeClass(normCapacity); @@ -537,6 +542,19 @@ abstract class PoolArena implements PoolArenaMetric { return max(numHugeAllocations() - numHugeDeallocations(), 0); } + @Override + public long numActiveBytes() { + long val = activeBytesHuge.value(); + synchronized (this) { + for (int i = 0; i < chunkListMetrics.size(); i++) { + for (PoolChunkMetric m: chunkListMetrics.get(i)) { + val += m.chunkSize(); + } + } + } + return max(0, val); + } + protected abstract PoolChunk newChunk(int pageSize, int maxOrder, int pageShifts, int chunkSize); protected abstract PoolChunk newUnpooledChunk(int capacity); protected abstract PooledByteBuf newByteBuf(int maxCapacity); diff --git a/buffer/src/main/java/io/netty/buffer/PoolArenaMetric.java b/buffer/src/main/java/io/netty/buffer/PoolArenaMetric.java index 927d84a15c..1b308982cd 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolArenaMetric.java +++ b/buffer/src/main/java/io/netty/buffer/PoolArenaMetric.java @@ -127,4 +127,9 @@ public interface PoolArenaMetric { * Return the number of currently active huge allocations. */ long numActiveHugeAllocations(); + + /** + * Return the number of active bytes that are currently allocated by the arena. + */ + long numActiveBytes(); }