Search in sources :

Example 1 with PooledByteBufAllocatorMetric

use of io.netty.buffer.PooledByteBufAllocatorMetric in project ratpack by ratpack.

the class PooledByteBufAllocatorMetricSet method initMetrics.

private void initMetrics() {
    final PooledByteBufAllocatorMetric metric = pooledByteBufAllocator.metric();
    metrics.put("numDirectArenas", (Gauge<Integer>) () -> metric.numDirectArenas());
    metrics.put("numHeapArenas", (Gauge<Integer>) () -> metric.numHeapArenas());
    metrics.put("numThreadLocalCaches", (Gauge<Integer>) () -> metric.numThreadLocalCaches());
    metrics.put("smallCacheSize", (Gauge<Integer>) () -> metric.smallCacheSize());
    metrics.put("tinyCacheSize", (Gauge<Integer>) () -> metric.tinyCacheSize());
    metrics.put("normalCacheSize", (Gauge<Integer>) () -> metric.normalCacheSize());
    metrics.put("chunkSize", (Gauge<Integer>) () -> metric.chunkSize());
    metrics.put("usedDirectMemory", (Gauge<Long>) () -> metric.usedDirectMemory());
    metrics.put("usedHeapMemory", (Gauge<Long>) () -> metric.usedHeapMemory());
    if (includeArenas) {
        final Iterator<PoolArenaMetric> directArenasIterator = metric.directArenas().iterator();
        initPoolArenaMetrics(directArenasIterator);
        final Iterator<PoolArenaMetric> heapArenasIterator = metric.heapArenas().iterator();
        initPoolArenaMetrics(heapArenasIterator);
    }
}
Also used : PoolArenaMetric(io.netty.buffer.PoolArenaMetric) PooledByteBufAllocatorMetric(io.netty.buffer.PooledByteBufAllocatorMetric)

Example 2 with PooledByteBufAllocatorMetric

use of io.netty.buffer.PooledByteBufAllocatorMetric in project spring-framework by spring-projects.

the class AbstractDataBufferAllocatingTests method verifyAllocations.

private void verifyAllocations() {
    if (this.bufferFactory instanceof NettyDataBufferFactory) {
        ByteBufAllocator allocator = ((NettyDataBufferFactory) this.bufferFactory).getByteBufAllocator();
        if (allocator instanceof PooledByteBufAllocator) {
            Instant start = Instant.now();
            while (true) {
                PooledByteBufAllocatorMetric metric = ((PooledByteBufAllocator) allocator).metric();
                long total = getAllocations(metric.directArenas()) + getAllocations(metric.heapArenas());
                if (total == 0) {
                    return;
                }
                if (Instant.now().isBefore(start.plus(Duration.ofSeconds(5)))) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException ex) {
                    // ignore
                    }
                    continue;
                }
                assertThat(total).as("ByteBuf Leak: " + total + " unreleased allocations").isEqualTo(0);
            }
        }
    }
}
Also used : UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Instant(java.time.Instant) PooledByteBufAllocatorMetric(io.netty.buffer.PooledByteBufAllocatorMetric) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Example 3 with PooledByteBufAllocatorMetric

use of io.netty.buffer.PooledByteBufAllocatorMetric in project ambry by linkedin.

the class NettyByteBufLeakHelper method afterTest.

/**
 * Method to call at any method that is tagged {@link org.junit.After} to verify there is no leak within this test case.
 */
public void afterTest() {
    if (cachedEnabled || disabled) {
        return;
    }
    PooledByteBufAllocatorMetric metric = PooledByteBufAllocator.DEFAULT.metric();
    List<PoolArenaMetric> heaps = metric.heapArenas();
    List<PoolArenaMetric> directs = metric.directArenas();
    // Wait up to 1 sec before declaring failure since deallocations may happen in background with some delay.
    int checkTimeoutInMs = 1000;
    if (!TestUtils.checkAndSleep(activeDirectAllocations, () -> directs.stream().mapToLong(PoolArenaMetric::numActiveAllocations).sum(), checkTimeoutInMs)) {
        long currentActiveDirectAllocations = directs.stream().mapToLong(PoolArenaMetric::numActiveAllocations).sum();
        long currentDirectAllocations = directs.stream().mapToLong(PoolArenaMetric::numAllocations).sum();
        long currentDirectDeallocations = directs.stream().mapToLong(PoolArenaMetric::numDeallocations).sum();
        String message = String.format("DirectMemoryLeak: [allocation|deallocation] before test[%d|%d], after test[%d|%d]", directAllocations, directDeallocations, currentDirectAllocations, currentDirectDeallocations);
        Assert.assertEquals(message, activeDirectAllocations, currentActiveDirectAllocations);
    }
    if (!TestUtils.checkAndSleep(activeHeapAllocations, () -> heaps.stream().mapToLong(PoolArenaMetric::numActiveAllocations).sum(), checkTimeoutInMs)) {
        long currentActiveHeapAllocations = heaps.stream().mapToLong(PoolArenaMetric::numActiveAllocations).sum();
        long currentHeapAllocations = heaps.stream().mapToLong(PoolArenaMetric::numAllocations).sum();
        long currentHeapDeallocations = heaps.stream().mapToLong(PoolArenaMetric::numDeallocations).sum();
        String message = String.format("HeapMemoryLeak: [allocation|deallocation] before test[%d|%d], after test[%d|%d]", heapAllocations, heapDeallocations, currentHeapAllocations, currentHeapDeallocations);
        Assert.assertEquals(message, activeHeapAllocations, currentActiveHeapAllocations);
    }
}
Also used : PoolArenaMetric(io.netty.buffer.PoolArenaMetric) PooledByteBufAllocatorMetric(io.netty.buffer.PooledByteBufAllocatorMetric)

Example 4 with PooledByteBufAllocatorMetric

use of io.netty.buffer.PooledByteBufAllocatorMetric in project reactor-netty by reactor.

the class ByteBufAllocatorMetrics method registerMetrics.

void registerMetrics(String allocType, ByteBufAllocatorMetric metrics, ByteBufAllocator alloc) {
    MapUtils.computeIfAbsent(cache, metrics.hashCode() + "", key -> {
        Tags tags = Tags.of(ID.getKey(), key, TYPE.getKey(), allocType);
        Gauge.builder(USED_HEAP_MEMORY.getName(), metrics, ByteBufAllocatorMetric::usedHeapMemory).tags(tags).register(REGISTRY);
        Gauge.builder(USED_DIRECT_MEMORY.getName(), metrics, ByteBufAllocatorMetric::usedDirectMemory).tags(tags).register(REGISTRY);
        if (metrics instanceof PooledByteBufAllocatorMetric) {
            PooledByteBufAllocatorMetric pooledMetrics = (PooledByteBufAllocatorMetric) metrics;
            PooledByteBufAllocator pooledAlloc = (PooledByteBufAllocator) alloc;
            Gauge.builder(HEAP_ARENAS.getName(), pooledMetrics, PooledByteBufAllocatorMetric::numHeapArenas).tags(tags).register(REGISTRY);
            Gauge.builder(DIRECT_ARENAS.getName(), pooledMetrics, PooledByteBufAllocatorMetric::numDirectArenas).tags(tags).register(REGISTRY);
            Gauge.builder(THREAD_LOCAL_CACHES.getName(), pooledMetrics, PooledByteBufAllocatorMetric::numThreadLocalCaches).tags(tags).register(REGISTRY);
            Gauge.builder(SMALL_CACHE_SIZE.getName(), pooledMetrics, PooledByteBufAllocatorMetric::smallCacheSize).tags(tags).register(REGISTRY);
            Gauge.builder(NORMAL_CACHE_SIZE.getName(), pooledMetrics, PooledByteBufAllocatorMetric::normalCacheSize).tags(tags).register(REGISTRY);
            Gauge.builder(CHUNK_SIZE.getName(), pooledMetrics, PooledByteBufAllocatorMetric::chunkSize).tags(tags).register(REGISTRY);
            Gauge.builder(ACTIVE_HEAP_MEMORY.getName(), pooledAlloc, PooledByteBufAllocator::pinnedHeapMemory).tags(tags).register(REGISTRY);
            Gauge.builder(ACTIVE_DIRECT_MEMORY.getName(), pooledAlloc, PooledByteBufAllocator::pinnedDirectMemory).tags(tags).register(REGISTRY);
        }
        return metrics;
    });
}
Also used : PooledByteBufAllocatorMetric(io.netty.buffer.PooledByteBufAllocatorMetric) ByteBufAllocatorMetric(io.netty.buffer.ByteBufAllocatorMetric) PooledByteBufAllocatorMetric(io.netty.buffer.PooledByteBufAllocatorMetric) Tags(io.micrometer.api.instrument.Tags) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Example 5 with PooledByteBufAllocatorMetric

use of io.netty.buffer.PooledByteBufAllocatorMetric in project jocean-http by isdom.

the class PooledAllocatorStats method getActiveAllocationsInBytes.

public long getActiveAllocationsInBytes() {
    final PooledByteBufAllocatorMetric allocatorMetric = PooledByteBufAllocator.DEFAULT.metric();
    long totalBytes = 0;
    for (PoolArenaMetric poolArenaMetric : allocatorMetric.directArenas()) {
        totalBytes += activeAllocationsInBytes(poolArenaMetric);
    }
    for (PoolArenaMetric poolArenaMetric : allocatorMetric.heapArenas()) {
        totalBytes += activeAllocationsInBytes(poolArenaMetric);
    }
    return totalBytes;
}
Also used : PoolArenaMetric(io.netty.buffer.PoolArenaMetric) PooledByteBufAllocatorMetric(io.netty.buffer.PooledByteBufAllocatorMetric)

Aggregations

PooledByteBufAllocatorMetric (io.netty.buffer.PooledByteBufAllocatorMetric)7 PoolArenaMetric (io.netty.buffer.PoolArenaMetric)5 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)2 Tags (io.micrometer.api.instrument.Tags)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 ByteBufAllocatorMetric (io.netty.buffer.ByteBufAllocatorMetric)1 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)1 Instant (java.time.Instant)1 HashMap (java.util.HashMap)1 NettyDataBufferFactory (org.springframework.core.io.buffer.NettyDataBufferFactory)1