Extend metric utils

This commit is contained in:
Andrea Cavalli 2021-09-09 15:30:28 +02:00
parent 2fd311f67a
commit 9c167a322a
3 changed files with 70 additions and 46 deletions

View File

@ -1,8 +1,5 @@
package io.netty5.buffer.api.pool;
import io.netty5.buffer.api.pool.PoolArenaMetric;
import io.netty5.buffer.api.pool.PooledBufferAllocator;
import io.netty5.util.internal.ReflectionUtil;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
@ -22,7 +19,7 @@ public class MetricUtils {
MethodHandle handle = null;
try {
// Find the class
var pooledBufferClass = Class.forName("io.netty5.buffer.api.pool.PooledBufferAllocatorMetric");
var pooledBufferClass = Class.forName("io.netty5.buffer.api.pool.PooledBufferAllocatorMetricUtils");
// Find the handle of the method
handle = lookup.findVirtual(pooledBufferClass, "arenaMetrics", MethodType.methodType(List.class));
} catch (NoSuchMethodException | IllegalAccessException | ClassNotFoundException ignored) {

View File

@ -0,0 +1,60 @@
package io.netty5.buffer.api.pool;
import java.util.List;
public class PooledBufferAllocatorMetricUtils implements BufferAllocatorMetric {
private final PooledBufferAllocator allocator;
@SuppressWarnings("RedundantThrows")
public PooledBufferAllocatorMetricUtils(PooledBufferAllocator allocator) throws Throwable {
this.allocator = allocator;
}
/**
* Return the number of arenas.
*/
public int numArenas() {
return allocator.numArenas();
}
/**
* Return a {@link List} of all {@link PoolArenaMetric}s that are provided by this pool.
*/
public List<PoolArenaMetric> arenaMetrics() {
return allocator.arenaMetrics();
}
/**
* Return the number of thread local caches used by this {@link PooledBufferAllocator}.
*/
public int numThreadLocalCaches() {
return allocator.numThreadLocalCaches();
}
/**
* Return the size of the small cache.
*/
public int smallCacheSize() {
return allocator.smallCacheSize();
}
/**
* Return the size of the normal cache.
*/
public int normalCacheSize() {
return allocator.normalCacheSize();
}
/**
* Return the chunk size for an arena.
*/
public int chunkSize() {
return allocator.chunkSize();
}
@Override
public long usedMemory() {
return allocator.usedMemory();
}
}

View File

@ -2,40 +2,23 @@ package it.cavallium.dbengine.netty;
import io.netty5.buffer.api.pool.BufferAllocatorMetric;
import io.netty5.buffer.api.pool.PooledBufferAllocator;
import io.netty5.buffer.api.pool.PooledBufferAllocatorMetricUtils;
import java.lang.reflect.Field;
public class JMXPooledNettyMonitoring extends JMXNettyMonitoring implements JMXNettyMonitoringMBean {
private final PooledBufferAllocator alloc;
private final BufferAllocatorMetric metric;
private Field smallCacheSize;
private Field numThreadLocalCaches;
private Field normalCacheSize;
private Field chunkSize;
private PooledBufferAllocatorMetricUtils metricUtils;
public JMXPooledNettyMonitoring(String name, PooledBufferAllocator alloc) {
super(name, alloc.isDirectBufferPooled(), alloc.metric());
this.alloc = alloc;
this.metric = alloc.metric();
try {
//noinspection JavaReflectionMemberAccess
numThreadLocalCaches = metric.getClass().getDeclaredField("numThreadLocalCaches");
} catch (NoSuchFieldException e) {
}
try {
//noinspection JavaReflectionMemberAccess
smallCacheSize = metric.getClass().getDeclaredField("smallCacheSize");
} catch (NoSuchFieldException e) {
}
try {
//noinspection JavaReflectionMemberAccess
normalCacheSize = metric.getClass().getDeclaredField("normalCacheSize");
} catch (NoSuchFieldException e) {
}
try {
//noinspection JavaReflectionMemberAccess
chunkSize = metric.getClass().getDeclaredField("chunkSize");
} catch (NoSuchFieldException e) {
this.metricUtils = new PooledBufferAllocatorMetricUtils(alloc);
} catch (Throwable e) {
this.metricUtils = null;
}
}
@ -51,11 +34,7 @@ public class JMXPooledNettyMonitoring extends JMXNettyMonitoring implements JMXN
@Override
public Integer getNumThreadLocalCachesArenas() {
try {
return numThreadLocalCaches.getInt(metric);
} catch (IllegalAccessException e) {
return 0;
}
return metricUtils != null ? metricUtils.numThreadLocalCaches() : 0;
}
@Deprecated
@ -66,28 +45,16 @@ public class JMXPooledNettyMonitoring extends JMXNettyMonitoring implements JMXN
@Override
public Integer getSmallCacheSize() {
try {
return smallCacheSize.getInt(metric);
} catch (IllegalAccessException e) {
return 0;
}
return metricUtils != null ? metricUtils.smallCacheSize() : 0;
}
@Override
public Integer getNormalCacheSize() {
try {
return normalCacheSize.getInt(metric);
} catch (IllegalAccessException e) {
return 0;
}
return metricUtils != null ? metricUtils.normalCacheSize() : 0;
}
@Override
public Integer getChunkSize() {
try {
return chunkSize.getInt(metric);
} catch (IllegalAccessException e) {
return 0;
}
return metricUtils != null ? metricUtils.chunkSize() : 0;
}
}