2012-12-05 22:11:48 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2012 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package io.netty.buffer;
|
|
|
|
|
2013-06-10 12:52:56 +02:00
|
|
|
import io.netty.util.Recycler;
|
2013-01-11 14:47:54 +01:00
|
|
|
import io.netty.util.ResourceLeak;
|
|
|
|
|
2012-12-05 22:11:48 +01:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.ByteOrder;
|
|
|
|
|
2013-02-10 05:10:09 +01:00
|
|
|
abstract class PooledByteBuf<T> extends AbstractReferenceCountedByteBuf {
|
2012-12-05 22:11:48 +01:00
|
|
|
|
2013-04-22 10:07:01 +02:00
|
|
|
private final ResourceLeak leak;
|
2013-06-10 12:52:56 +02:00
|
|
|
private final Recycler.Handle recyclerHandle;
|
2013-01-11 14:47:54 +01:00
|
|
|
|
2012-12-05 22:11:48 +01:00
|
|
|
protected PoolChunk<T> chunk;
|
|
|
|
protected long handle;
|
|
|
|
protected T memory;
|
|
|
|
protected int offset;
|
|
|
|
protected int length;
|
2012-12-19 08:48:53 +01:00
|
|
|
private int maxLength;
|
2012-12-05 22:11:48 +01:00
|
|
|
|
|
|
|
private ByteBuffer tmpNioBuf;
|
|
|
|
|
2013-06-10 12:52:56 +02:00
|
|
|
protected PooledByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) {
|
2012-12-05 22:11:48 +01:00
|
|
|
super(maxCapacity);
|
2013-04-22 10:07:01 +02:00
|
|
|
leak = leakDetector.open(this);
|
2013-06-10 12:52:56 +02:00
|
|
|
this.recyclerHandle = recyclerHandle;
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
void init(PoolChunk<T> chunk, long handle, int offset, int length, int maxLength) {
|
2012-12-05 22:11:48 +01:00
|
|
|
assert handle >= 0;
|
2012-12-19 09:35:32 +01:00
|
|
|
assert chunk != null;
|
2012-12-17 08:02:21 +01:00
|
|
|
|
2012-12-05 22:11:48 +01:00
|
|
|
this.chunk = chunk;
|
|
|
|
this.handle = handle;
|
2012-12-19 09:35:32 +01:00
|
|
|
memory = chunk.memory;
|
2012-12-05 22:11:48 +01:00
|
|
|
this.offset = offset;
|
|
|
|
this.length = length;
|
2012-12-19 08:48:53 +01:00
|
|
|
this.maxLength = maxLength;
|
2012-12-05 22:11:48 +01:00
|
|
|
setIndex(0, 0);
|
|
|
|
tmpNioBuf = null;
|
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
void initUnpooled(PoolChunk<T> chunk, int length) {
|
2012-12-19 09:35:32 +01:00
|
|
|
assert chunk != null;
|
|
|
|
|
|
|
|
this.chunk = chunk;
|
|
|
|
handle = 0;
|
|
|
|
memory = chunk.memory;
|
|
|
|
offset = 0;
|
|
|
|
this.length = maxLength = length;
|
|
|
|
setIndex(0, 0);
|
|
|
|
tmpNioBuf = null;
|
|
|
|
}
|
|
|
|
|
2012-12-05 22:11:48 +01:00
|
|
|
@Override
|
2012-12-17 10:27:30 +01:00
|
|
|
public final int capacity() {
|
2012-12-05 22:11:48 +01:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-17 10:27:30 +01:00
|
|
|
public final ByteBuf capacity(int newCapacity) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2012-12-19 08:48:53 +01:00
|
|
|
|
|
|
|
// If the request capacity does not require reallocation, just update the length of the memory.
|
2012-12-19 09:35:32 +01:00
|
|
|
if (chunk.unpooled) {
|
|
|
|
if (newCapacity == length) {
|
2012-12-19 08:48:53 +01:00
|
|
|
return this;
|
|
|
|
}
|
2012-12-19 09:35:32 +01:00
|
|
|
} else {
|
|
|
|
if (newCapacity > length) {
|
|
|
|
if (newCapacity <= maxLength) {
|
|
|
|
length = newCapacity;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
} else if (newCapacity < length) {
|
|
|
|
if (newCapacity > maxLength >>> 1) {
|
|
|
|
if (maxLength <= 512) {
|
|
|
|
if (newCapacity > maxLength - 16) {
|
|
|
|
length = newCapacity;
|
|
|
|
setIndex(Math.min(readerIndex(), newCapacity), Math.min(writerIndex(), newCapacity));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
} else { // > 512 (i.e. >= 1024)
|
2012-12-19 08:48:53 +01:00
|
|
|
length = newCapacity;
|
|
|
|
setIndex(Math.min(readerIndex(), newCapacity), Math.min(writerIndex(), newCapacity));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
2012-12-19 09:35:32 +01:00
|
|
|
} else {
|
|
|
|
return this;
|
2012-12-19 08:48:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reallocation required.
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 13:40:19 +02:00
|
|
|
chunk.arena.reallocate(this, newCapacity, true);
|
2012-12-05 22:11:48 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-17 10:27:30 +01:00
|
|
|
public final ByteBufAllocator alloc() {
|
2012-12-05 22:11:48 +01:00
|
|
|
return chunk.arena.parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-17 10:27:30 +01:00
|
|
|
public final ByteOrder order() {
|
2012-12-05 22:11:48 +01:00
|
|
|
return ByteOrder.BIG_ENDIAN;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-17 10:27:30 +01:00
|
|
|
public final ByteBuf unwrap() {
|
2012-12-05 22:11:48 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-12-17 10:27:30 +01:00
|
|
|
protected final ByteBuffer internalNioBuffer() {
|
2012-12-05 22:11:48 +01:00
|
|
|
ByteBuffer tmpNioBuf = this.tmpNioBuf;
|
|
|
|
if (tmpNioBuf == null) {
|
|
|
|
this.tmpNioBuf = tmpNioBuf = newInternalNioBuffer(memory);
|
|
|
|
}
|
|
|
|
return tmpNioBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract ByteBuffer newInternalNioBuffer(T memory);
|
|
|
|
|
|
|
|
@Override
|
2013-02-10 05:10:09 +01:00
|
|
|
protected final void deallocate() {
|
2012-12-05 22:11:48 +01:00
|
|
|
if (handle >= 0) {
|
|
|
|
final long handle = this.handle;
|
|
|
|
this.handle = -1;
|
|
|
|
memory = null;
|
|
|
|
chunk.arena.free(chunk, handle);
|
2013-07-23 07:06:58 +02:00
|
|
|
if (leak != null) {
|
2013-06-10 12:52:56 +02:00
|
|
|
leak.close();
|
|
|
|
} else {
|
|
|
|
recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private void recycle() {
|
|
|
|
Recycler.Handle recyclerHandle = this.recyclerHandle;
|
|
|
|
if (recyclerHandle != null) {
|
|
|
|
((Recycler<Object>) recycler()).recycle(this, recyclerHandle);
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 12:52:56 +02:00
|
|
|
protected abstract Recycler<?> recycler();
|
|
|
|
|
2012-12-17 10:27:30 +01:00
|
|
|
protected final int idx(int index) {
|
2012-12-05 22:11:48 +01:00
|
|
|
return offset + index;
|
|
|
|
}
|
|
|
|
}
|