From 771cfaec22248a52b97400e9b29522b94c7be536 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sun, 10 Jul 2016 12:54:30 +0200 Subject: [PATCH] [#5520] Correctly include all PoolSubpage metrics Motivation: Because of a bug we missed to include the first PoolSubpage when collection metrics. Modifications: - Correctly include all subpages - Add unit test Result: Correctly include all subpages --- .../main/java/io/netty/buffer/PoolArena.java | 61 ++++++++----------- .../buffer/PooledByteBufAllocatorTest.java | 26 ++++++++ 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/PoolArena.java b/buffer/src/main/java/io/netty/buffer/PoolArena.java index 646cedb059..a8e856b947 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolArena.java +++ b/buffer/src/main/java/io/netty/buffer/PoolArena.java @@ -431,7 +431,7 @@ abstract class PoolArena implements PoolArenaMetric { private static List subPageMetricList(PoolSubpage[] pages) { List metrics = new ArrayList(); - for (int i = 1; i < pages.length; i ++) { + for (int i = 0; i < pages.length; i ++) { PoolSubpage head = pages[i]; if (head.next == head) { continue; @@ -587,49 +587,36 @@ abstract class PoolArena implements PoolArenaMetric { .append(q100) .append(StringUtil.NEWLINE) .append("tiny subpages:"); - for (int i = 1; i < tinySubpagePools.length; i ++) { - PoolSubpage head = tinySubpagePools[i]; - if (head.next == head) { - continue; - } - - buf.append(StringUtil.NEWLINE) - .append(i) - .append(": "); - PoolSubpage s = head.next; - for (;;) { - buf.append(s); - s = s.next; - if (s == head) { - break; - } - } - } + appendPoolSubPages(buf, tinySubpagePools); buf.append(StringUtil.NEWLINE) .append("small subpages:"); - for (int i = 1; i < smallSubpagePools.length; i ++) { - PoolSubpage head = smallSubpagePools[i]; - if (head.next == head) { - continue; - } - - buf.append(StringUtil.NEWLINE) - .append(i) - .append(": "); - PoolSubpage s = head.next; - for (;;) { - buf.append(s); - s = s.next; - if (s == head) { - break; - } - } - } + appendPoolSubPages(buf, smallSubpagePools); buf.append(StringUtil.NEWLINE); return buf.toString(); } + private static void appendPoolSubPages(StringBuilder buf, PoolSubpage[] subpages) { + for (int i = 0; i < subpages.length; i ++) { + PoolSubpage head = subpages[i]; + if (head.next == head) { + continue; + } + + buf.append(StringUtil.NEWLINE) + .append(i) + .append(": "); + PoolSubpage s = head.next; + for (;;) { + buf.append(s); + s = s.next; + if (s == head) { + break; + } + } + } + } + static final class HeapArena extends PoolArena { HeapArena(PooledByteBufAllocator parent, int pageSize, int maxOrder, int pageShifts, int chunkSize) { diff --git a/buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java b/buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java index b23b1352b9..55a44c4490 100644 --- a/buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java +++ b/buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java @@ -94,6 +94,32 @@ public class PooledByteBufAllocatorTest { assertEquals(max, m.maxUsage()); } + @Test + public void testSmallSubpageMetric() { + PooledByteBufAllocator allocator = new PooledByteBufAllocator(true, 1, 1, 8192, 11, 0, 0, 0); + ByteBuf buffer = allocator.heapBuffer(500); + try { + PoolArenaMetric metric = allocator.heapArenas().get(0); + PoolSubpageMetric subpageMetric = metric.smallSubpages().get(0); + assertEquals(1, subpageMetric.maxNumElements() - subpageMetric.numAvailable()); + } finally { + buffer.release(); + } + } + + @Test + public void testTinySubpageMetric() { + PooledByteBufAllocator allocator = new PooledByteBufAllocator(true, 1, 1, 8192, 11, 0, 0, 0); + ByteBuf buffer = allocator.heapBuffer(1); + try { + PoolArenaMetric metric = allocator.heapArenas().get(0); + PoolSubpageMetric subpageMetric = metric.tinySubpages().get(0); + assertEquals(1, subpageMetric.maxNumElements() - subpageMetric.numAvailable()); + } finally { + buffer.release(); + } + } + @Test public void testFreePoolChunk() { int chunkSize = 16 * 1024 * 1024;