Search in sources :

Example 6 with ByteBufAllocator

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

the class AbstractNioChannel method newDirectBuffer.

/**
     * Returns an off-heap copy of the specified {@link ByteBuf}, and releases the specified holder.
     * The caller must ensure that the holder releases the original {@link ByteBuf} when the holder is released by
     * this method.  Note that this method does not create an off-heap copy if the allocation / deallocation cost is
     * too high, but just returns the original {@link ByteBuf}..
     */
protected final ByteBuf newDirectBuffer(ReferenceCounted holder, ByteBuf buf) {
    final int readableBytes = buf.readableBytes();
    if (readableBytes == 0) {
        ReferenceCountUtil.safeRelease(holder);
        return Unpooled.EMPTY_BUFFER;
    }
    final ByteBufAllocator alloc = alloc();
    if (alloc.isDirectBufferPooled()) {
        ByteBuf directBuf = alloc.directBuffer(readableBytes);
        directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
        ReferenceCountUtil.safeRelease(holder);
        return directBuf;
    }
    final ByteBuf directBuf = ByteBufUtil.threadLocalDirectBuffer();
    if (directBuf != null) {
        directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
        ReferenceCountUtil.safeRelease(holder);
        return directBuf;
    }
    // Allocating and deallocating an unpooled direct buffer is very expensive; give up.
    if (holder != buf) {
        // Ensure to call holder.release() to give the holder a chance to release other resources than its content.
        buf.retain();
        ReferenceCountUtil.safeRelease(holder);
    }
    return buf;
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf)

Example 7 with ByteBufAllocator

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

the class AbstractNioChannel method newDirectBuffer.

/**
     * Returns an off-heap copy of the specified {@link ByteBuf}, and releases the original one.
     * Note that this method does not create an off-heap copy if the allocation / deallocation cost is too high,
     * but just returns the original {@link ByteBuf}..
     */
protected final ByteBuf newDirectBuffer(ByteBuf buf) {
    final int readableBytes = buf.readableBytes();
    if (readableBytes == 0) {
        ReferenceCountUtil.safeRelease(buf);
        return Unpooled.EMPTY_BUFFER;
    }
    final ByteBufAllocator alloc = alloc();
    if (alloc.isDirectBufferPooled()) {
        ByteBuf directBuf = alloc.directBuffer(readableBytes);
        directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
        ReferenceCountUtil.safeRelease(buf);
        return directBuf;
    }
    final ByteBuf directBuf = ByteBufUtil.threadLocalDirectBuffer();
    if (directBuf != null) {
        directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
        ReferenceCountUtil.safeRelease(buf);
        return directBuf;
    }
    // Allocating and deallocating an unpooled direct buffer is very expensive; give up.
    return buf;
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf)

Example 8 with ByteBufAllocator

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

the class AbstractTestsuiteTest method run.

protected void run() throws Throwable {
    List<TestsuitePermutation.BootstrapFactory<T>> combos = newFactories();
    for (ByteBufAllocator allocator : newAllocators()) {
        int i = 0;
        for (TestsuitePermutation.BootstrapFactory<T> e : combos) {
            cb = e.newInstance();
            configure(cb, allocator);
            logger.info(String.format("Running: %s %d of %d with %s", testName.getMethodName(), ++i, combos.size(), StringUtil.simpleClassName(allocator)));
            try {
                Method m = getClass().getMethod(TestUtils.testMethodName(testName), clazz);
                m.invoke(this, cb);
            } catch (InvocationTargetException ex) {
                throw ex.getCause();
            }
        }
    }
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 9 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator 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 10 with ByteBufAllocator

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

the class Http2FrameWriterBenchmark method boostrapEmbeddedEnv.

private static Environment boostrapEmbeddedEnv(final EnvironmentType environmentType) {
    final ByteBufAllocator alloc = environmentType.params().clientAllocator();
    final EmbeddedEnvironment env = new EmbeddedEnvironment(new DefaultHttp2FrameWriter());
    final Http2Connection connection = new DefaultHttp2Connection(false);
    Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, env.writer());
    Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, new DefaultHttp2FrameReader());
    Http2ConnectionHandler connectionHandler = new Http2ConnectionHandlerBuilder().encoderEnforceMaxConcurrentStreams(false).frameListener(new Http2FrameAdapter()).codec(decoder, encoder).build();
    env.context(new EmbeddedChannelWriteReleaseHandlerContext(alloc, connectionHandler) {

        @Override
        protected void handleException(Throwable t) {
            handleUnexpectedException(t);
        }
    });
    return env;
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) DefaultHttp2Connection(io.netty.handler.codec.http2.DefaultHttp2Connection) Http2FrameAdapter(io.netty.handler.codec.http2.Http2FrameAdapter) DefaultHttp2Connection(io.netty.handler.codec.http2.DefaultHttp2Connection) Http2Connection(io.netty.handler.codec.http2.Http2Connection) DefaultHttp2ConnectionDecoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionDecoder) Http2ConnectionHandler(io.netty.handler.codec.http2.Http2ConnectionHandler) DefaultHttp2FrameWriter(io.netty.handler.codec.http2.DefaultHttp2FrameWriter) DefaultHttp2ConnectionEncoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder) Http2ConnectionDecoder(io.netty.handler.codec.http2.Http2ConnectionDecoder) DefaultHttp2ConnectionDecoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionDecoder) Http2ConnectionHandlerBuilder(io.netty.handler.codec.http2.Http2ConnectionHandlerBuilder) EmbeddedChannelWriteReleaseHandlerContext(io.netty.microbench.channel.EmbeddedChannelWriteReleaseHandlerContext) Http2ConnectionEncoder(io.netty.handler.codec.http2.Http2ConnectionEncoder) DefaultHttp2ConnectionEncoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder) DefaultHttp2FrameReader(io.netty.handler.codec.http2.DefaultHttp2FrameReader)

Aggregations

ByteBufAllocator (io.netty.buffer.ByteBufAllocator)27 ByteBuf (io.netty.buffer.ByteBuf)12 Channel (io.netty.channel.Channel)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)3 URI (java.net.URI)3 Test (org.junit.Test)3 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)2 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Publisher (org.reactivestreams.Publisher)2 Context (ratpack.handling.Context)2 Streams (ratpack.stream.Streams)2 MetricFilter (com.codahale.metrics.MetricFilter)1