Search in sources :

Example 16 with PooledByteBufAllocator

use of io.netty.buffer.PooledByteBufAllocator in project incubator-pulsar by apache.

the class AllocatorStatsGenerator method generate.

public static AllocatorStats generate(String allocatorName) {
    PooledByteBufAllocator allocator = null;
    if ("default".equals(allocatorName)) {
        allocator = PooledByteBufAllocator.DEFAULT;
    } else if ("ml-cache".equals(allocatorName)) {
        allocator = EntryCacheImpl.ALLOCATOR;
    } else {
        throw new IllegalArgumentException("Invalid allocator name : " + allocatorName);
    }
    AllocatorStats stats = new AllocatorStats();
    stats.directArenas = allocator.metric().directArenas().stream().map(AllocatorStatsGenerator::newPoolArenaStats).collect(Collectors.toList());
    stats.heapArenas = allocator.metric().heapArenas().stream().map(AllocatorStatsGenerator::newPoolArenaStats).collect(Collectors.toList());
    stats.numDirectArenas = allocator.metric().numDirectArenas();
    stats.numHeapArenas = allocator.metric().numHeapArenas();
    stats.numThreadLocalCaches = allocator.metric().numThreadLocalCaches();
    stats.normalCacheSize = allocator.metric().normalCacheSize();
    stats.smallCacheSize = allocator.metric().smallCacheSize();
    stats.tinyCacheSize = allocator.metric().tinyCacheSize();
    return stats;
}
Also used : AllocatorStats(org.apache.pulsar.common.stats.AllocatorStats) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Example 17 with PooledByteBufAllocator

use of io.netty.buffer.PooledByteBufAllocator in project netty by netty.

the class AbstractSslEngineThroughputBenchmark method setup.

@Setup(Level.Iteration)
public final void setup() throws Exception {
    ByteBufAllocator allocator = new PooledByteBufAllocator(true);
    initEngines(allocator);
    initHandshakeBuffers();
    wrapDstBuffer = allocateBuffer(clientEngine.getSession().getPacketBufferSize() << 2);
    wrapSrcBuffer = allocateBuffer(messageSize);
    byte[] bytes = new byte[messageSize];
    PlatformDependent.threadLocalRandom().nextBytes(bytes);
    wrapSrcBuffer.put(bytes);
    wrapSrcBuffer.flip();
    // Complete the initial TLS handshake.
    if (!doHandshake()) {
        throw new IllegalStateException();
    }
    doSetup();
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Setup(org.openjdk.jmh.annotations.Setup)

Example 18 with PooledByteBufAllocator

use of io.netty.buffer.PooledByteBufAllocator in project netty by netty.

the class PooledByteBufAllocatorAlignBenchmark method doSetup.

@Setup
public void doSetup() {
    PooledByteBufAllocator pooledAllocator = new PooledByteBufAllocator(true, 4, 4, 8192, 11, 0, 0, 0, true, cacheAlign);
    pooledDirectBuffer = pooledAllocator.directBuffer(size + 64);
    sizeMask = size - 1;
    if (cacheAlign == 0) {
        long addr = pooledDirectBuffer.memoryAddress();
        // make sure address is miss-aligned
        if (addr % 64 == 0) {
            alignOffset = 63;
        }
        int off = 0;
        for (int c = 0; c < size; c++) {
            off = (off + OFFSET_ADD) & sizeMask;
            if ((addr + off + alignOffset) % BLOCK == 0) {
                throw new IllegalStateException("Misaligned address is not really aligned");
            }
        }
    } else {
        alignOffset = 0;
        int off = 0;
        long addr = pooledDirectBuffer.memoryAddress();
        for (int c = 0; c < size; c++) {
            off = (off + OFFSET_ADD) & sizeMask;
            if ((addr + off) % BLOCK != 0) {
                throw new IllegalStateException("Aligned address is not really aligned");
            }
        }
    }
    bytes = new byte[BLOCK];
    rand.nextBytes(bytes);
}
Also used : PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Setup(org.openjdk.jmh.annotations.Setup)

Example 19 with PooledByteBufAllocator

use of io.netty.buffer.PooledByteBufAllocator in project vert.x by eclipse.

the class VertxHandler method safeBuffer.

/**
 * Copy and release the {@code buf} when necessary.
 *
 * <p> This methods assuming the has full ownership of the buffer.
 *
 * <p> This method assumes that pooled buffers are allocated by {@code PooledByteBufAllocator}
 *
 * <p> The returned buffer will not need to be released and can be wrapped by a {@link io.vertx.core.buffer.Buffer}.
 *
 * @param buf the buffer
 * @return a safe buffer to use
 */
public static ByteBuf safeBuffer(ByteBuf buf) {
    if (buf != Unpooled.EMPTY_BUFFER && (buf.alloc() instanceof PooledByteBufAllocator || buf instanceof CompositeByteBuf)) {
        try {
            if (buf.isReadable()) {
                ByteBuf buffer = VertxByteBufAllocator.DEFAULT.heapBuffer(buf.readableBytes());
                buffer.writeBytes(buf, buf.readerIndex(), buf.readableBytes());
                return buffer;
            } else {
                return Unpooled.EMPTY_BUFFER;
            }
        } finally {
            buf.release();
        }
    }
    return buf;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Example 20 with PooledByteBufAllocator

use of io.netty.buffer.PooledByteBufAllocator 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)

Aggregations

PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)60 Test (org.junit.Test)37 LocalAddress (io.netty.channel.local.LocalAddress)29 ByteBuf (io.netty.buffer.ByteBuf)28 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)25 Subscription (rx.Subscription)25 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)24 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)24 DisposableWrapper (org.jocean.idiom.DisposableWrapper)20 IOException (java.io.IOException)18 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)17 ConnectException (java.net.ConnectException)17 CertificateException (java.security.cert.CertificateException)17 SSLException (javax.net.ssl.SSLException)17 TransportException (org.jocean.http.TransportException)17 TestSubscriber (rx.observers.TestSubscriber)16 Channel (io.netty.channel.Channel)9 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)8