diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractByteBufferPool.java b/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractByteBufferPool.java index 8a89c1dbd28..0b599e47596 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractByteBufferPool.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractByteBufferPool.java @@ -22,6 +22,7 @@ import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicLong; import java.util.function.Consumer; +import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.annotation.ManagedOperation; @@ -80,7 +81,18 @@ abstract class AbstractByteBufferPool implements ByteBufferPool } } - @ManagedOperation(value = "Returns the memory occupied by this ByteBufferPool in bytes", impact = "INFO") + @ManagedAttribute("The bytes retained by direct ByteBuffers") + public long getDirectMemory() + { + return getMemory(true); + } + + @ManagedAttribute("The bytes retained by heap ByteBuffers") + public long getHeapMemory() + { + return getMemory(false); + } + public long getMemory(boolean direct) { AtomicLong memory = direct ? _directMemory : _heapMemory; diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayByteBufferPool.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayByteBufferPool.java index 971f8bb5332..274f15885ad 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayByteBufferPool.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayByteBufferPool.java @@ -19,8 +19,11 @@ package org.eclipse.jetty.io; import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Objects; import java.util.function.IntFunction; +import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; /** @@ -196,6 +199,26 @@ public class ArrayByteBufferPool extends AbstractByteBufferPool return bucket; } + @ManagedAttribute("The number of pooled direct ByteBuffers") + public long getDirectByteBufferCount() + { + return getByteBufferCount(true); + } + + @ManagedAttribute("The number of pooled heap ByteBuffers") + public long getHeapByteBufferCount() + { + return getByteBufferCount(false); + } + + private long getByteBufferCount(boolean direct) + { + return Arrays.stream(bucketsFor(direct)) + .filter(Objects::nonNull) + .mapToLong(Bucket::size) + .sum(); + } + // Package local for testing ByteBufferPool.Bucket[] bucketsFor(boolean direct) { diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/MappedByteBufferPool.java b/jetty-io/src/main/java/org/eclipse/jetty/io/MappedByteBufferPool.java index 98772e1a765..41646203542 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/MappedByteBufferPool.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/MappedByteBufferPool.java @@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; import org.eclipse.jetty.util.BufferUtil; +import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; /** @@ -182,6 +183,25 @@ public class MappedByteBufferPool extends AbstractByteBufferPool return bucket; } + @ManagedAttribute("The number of pooled direct ByteBuffers") + public long getDirectByteBufferCount() + { + return getByteBufferCount(true); + } + + @ManagedAttribute("The number of pooled heap ByteBuffers") + public long getHeapByteBufferCount() + { + return getByteBufferCount(false); + } + + private long getByteBufferCount(boolean direct) + { + return bucketsFor(direct).values().stream() + .mapToLong(Bucket::size) + .sum(); + } + // Package local for testing ConcurrentMap bucketsFor(boolean direct) { diff --git a/jetty-server/src/main/config/etc/jetty-bytebufferpool.xml b/jetty-server/src/main/config/etc/jetty-bytebufferpool.xml new file mode 100644 index 00000000000..6da3421a938 --- /dev/null +++ b/jetty-server/src/main/config/etc/jetty-bytebufferpool.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/jetty-server/src/main/config/etc/jetty.xml b/jetty-server/src/main/config/etc/jetty.xml index 961f95435e5..0fae969d287 100644 --- a/jetty-server/src/main/config/etc/jetty.xml +++ b/jetty-server/src/main/config/etc/jetty.xml @@ -26,6 +26,10 @@ + + + + diff --git a/jetty-server/src/main/config/modules/bytebufferpool.mod b/jetty-server/src/main/config/modules/bytebufferpool.mod new file mode 100644 index 00000000000..7a813148b6e --- /dev/null +++ b/jetty-server/src/main/config/modules/bytebufferpool.mod @@ -0,0 +1,27 @@ +DO NOT EDIT - See: https://www.eclipse.org/jetty/documentation/current/startup-modules.html + +[description] +Configures the ByteBufferPool used by ServerConnectors. + +[xml] +etc/jetty-bytebufferpool.xml + +[ini-template] +### Server ByteBufferPool Configuration +## Minimum capacity to pool ByteBuffers +#jetty.byteBufferPool.minCapacity=0 + +## Maximum capacity to pool ByteBuffers +#jetty.byteBufferPool.maxCapacity=65536 + +## Capacity factor +#jetty.byteBufferPool.factor=1024 + +## Maximum queue length for each bucket (-1 for unbounded) +#jetty.byteBufferPool.maxQueueLength=-1 + +## Maximum heap memory retainable by the pool (-1 for unlimited) +#jetty.byteBufferPool.maxHeapMemory=-1 + +## Maximum direct memory retainable by the pool (-1 for unlimited) +#jetty.byteBufferPool.maxDirectMemory=-1 diff --git a/jetty-server/src/main/config/modules/server.mod b/jetty-server/src/main/config/modules/server.mod index 038f1836b65..dd039a01fb1 100644 --- a/jetty-server/src/main/config/modules/server.mod +++ b/jetty-server/src/main/config/modules/server.mod @@ -11,6 +11,7 @@ logging [depend] threadpool +bytebufferpool [lib] lib/servlet-api-3.1.jar