Skip to content

Commit

Permalink
Improve #6322 log buckets in RetainableByteBufferPool
Browse files Browse the repository at this point in the history
Sampled Pool

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Jul 13, 2021
1 parent 791403b commit d607f21
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
Expand Up @@ -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;

/**
* <p>A {@link RetainableByteBuffer} pool.</p>
Expand Down Expand Up @@ -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;
}
}
}
Expand Up @@ -13,6 +13,7 @@

package org.eclipse.jetty.io;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down

0 comments on commit d607f21

Please sign in to comment.