diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/RetainableByteBufferPool.java b/jetty-io/src/main/java/org/eclipse/jetty/io/RetainableByteBufferPool.java index a4bd9d3754c2..76595c161a6c 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/RetainableByteBufferPool.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/RetainableByteBufferPool.java @@ -13,9 +13,14 @@ package org.eclipse.jetty.io; +import java.io.IOException; import java.nio.ByteBuffer; +import org.eclipse.jetty.util.annotation.ManagedAttribute; +import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.component.Container; +import org.eclipse.jetty.util.component.Dumpable; +import org.eclipse.jetty.util.statistic.SampleStatistic; /** *

A {@link RetainableByteBuffer} pool.

@@ -55,4 +60,56 @@ static RetainableByteBufferPool findOrAdapt(Container container, ByteBufferPool } return retainableByteBufferPool; } + + /** + * A Pool wrapper that collects sample statistics on the acquired + * buffers. + * @see SampleStatistic + */ + @ManagedObject + class SampledPool implements RetainableByteBufferPool, Dumpable + { + private final RetainableByteBufferPool _pool; + private final SampleStatistic _heap = new SampleStatistic(); + private final SampleStatistic _direct = new SampleStatistic(); + + public SampledPool(RetainableByteBufferPool pool) + { + _pool = pool; + } + + @ManagedAttribute("Heap acquire samples") + public SampleStatistic getHeapSample() + { + return _heap; + } + + @ManagedAttribute("Direct acquire samples") + public SampleStatistic getDirectSample() + { + return _direct; + } + + @Override + public RetainableByteBuffer acquire(int size, boolean direct) + { + (direct ? _direct : _heap).record(size); + return _pool.acquire(size, direct); + } + + @Override + public void dump(Appendable out, String indent) throws IOException + { + Dumpable.dumpObjects(out, indent, this, + Dumpable.named("heap", _heap), + Dumpable.named("direct", _direct), + _pool); + } + + @ManagedAttribute("Pool") + public RetainableByteBufferPool getPool() + { + return _pool; + } + } } diff --git a/jetty-io/src/test/java/org/eclipse/jetty/io/ArrayRetainableByteBufferPoolTest.java b/jetty-io/src/test/java/org/eclipse/jetty/io/ArrayRetainableByteBufferPoolTest.java index ee7b95723b4f..d181b69daa37 100644 --- a/jetty-io/src/test/java/org/eclipse/jetty/io/ArrayRetainableByteBufferPoolTest.java +++ b/jetty-io/src/test/java/org/eclipse/jetty/io/ArrayRetainableByteBufferPoolTest.java @@ -13,6 +13,7 @@ package org.eclipse.jetty.io; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -316,9 +317,9 @@ public void testAcquireRelease() } @Test - public void testLogBuckets() + public void testLogBuckets() throws IOException { - ArrayRetainableByteBufferPool pool = new ArrayRetainableByteBufferPool.LogBuckets(); + RetainableByteBufferPool.SampledPool pool = new RetainableByteBufferPool.SampledPool(new ArrayRetainableByteBufferPool.LogBuckets()); assertThat(pool.acquire(1, false).capacity(), is(1)); assertThat(pool.acquire(2, false).capacity(), is(2)); assertThat(pool.acquire(3, false).capacity(), is(4)); @@ -332,7 +333,7 @@ public void testLogBuckets() b = pool.acquire(capacity, false); assertThat(b.capacity(), Matchers.is(capacity)); - if (capacity >= pool.getMaxCapacity()) + if (capacity >= ((ArrayRetainableByteBufferPool)pool.getPool()).getMaxCapacity()) break; b = pool.acquire(capacity + 1, false);