Skip to content

Commit

Permalink
Add PooledByteBufAllocator.dumpStats() which allows to obtain a human…
Browse files Browse the repository at this point in the history
…-readable status of the allocator.

Motiviation:

Sometimes it is useful to dump the status of the PooledByteBufAllocator and log it. Doing this is currently a bit cumbersome as the user needs to basically iterate through all the metrics and compose the String. we would better provide an easy way to do this.

Modification:

Add dumpStats() method.

Result:

Easier to get a view into the status of the allocator.
  • Loading branch information
normanmaurer committed Apr 9, 2016
1 parent 9498d1a commit 200ca39
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
Expand Down Expand Up @@ -445,31 +446,33 @@ final PoolThreadCache threadCache() {
return threadCache.get();
}

// Too noisy at the moment.
//
//public String toString() {
// StringBuilder buf = new StringBuilder();
// int heapArenasLen = heapArenas == null ? 0 : heapArenas.length;
// buf.append(heapArenasLen);
// buf.append(" heap arena(s):");
// buf.append(StringUtil.NEWLINE);
// if (heapArenasLen > 0) {
// for (PoolArena<byte[]> a: heapArenas) {
// buf.append(a);
// }
// }
//
// int directArenasLen = directArenas == null ? 0 : directArenas.length;
//
// buf.append(directArenasLen);
// buf.append(" direct arena(s):");
// buf.append(StringUtil.NEWLINE);
// if (directArenasLen > 0) {
// for (PoolArena<ByteBuffer> a: directArenas) {
// buf.append(a);
// }
// }
//
// return buf.toString();
//}
/**
* Returns the status of the allocator (which contains all metrics) as string. Be aware this may be expensive
* and so should not called too frequently.
*/
public String dumpStats() {
int heapArenasLen = heapArenas == null ? 0 : heapArenas.length;
StringBuilder buf = new StringBuilder(512)
.append(heapArenasLen)
.append(" heap arena(s):")
.append(StringUtil.NEWLINE);
if (heapArenasLen > 0) {
for (PoolArena<byte[]> a: heapArenas) {
buf.append(a);
}
}

int directArenasLen = directArenas == null ? 0 : directArenas.length;

buf.append(directArenasLen)
.append(" direct arena(s):")
.append(StringUtil.NEWLINE);
if (directArenasLen > 0) {
for (PoolArena<ByteBuffer> a: directArenas) {
buf.append(a);
}
}

return buf.toString();
}
}

0 comments on commit 200ca39

Please sign in to comment.