* Implemented send buffer pooling
* Renamed ReadBufferPool to SocketReceiveBufferPool
This commit is contained in:
parent
1a04586b54
commit
32ef9e3e52
@ -19,7 +19,6 @@ import static org.jboss.netty.channel.Channels.*;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@ -33,6 +32,7 @@ import org.jboss.netty.channel.ChannelFuture;
|
||||
import org.jboss.netty.channel.ChannelPipeline;
|
||||
import org.jboss.netty.channel.ChannelSink;
|
||||
import org.jboss.netty.channel.MessageEvent;
|
||||
import org.jboss.netty.channel.socket.nio.SocketSendBufferPool.SendBuffer;
|
||||
import org.jboss.netty.util.internal.LinkedTransferQueue;
|
||||
import org.jboss.netty.util.internal.ThreadLocalBoolean;
|
||||
|
||||
@ -71,7 +71,7 @@ class NioSocketChannel extends AbstractChannel
|
||||
boolean writeSuspended;
|
||||
|
||||
MessageEvent currentWriteEvent;
|
||||
ByteBuffer currentWriteBuffer;
|
||||
SendBuffer currentWriteBuffer;
|
||||
|
||||
public NioSocketChannel(
|
||||
Channel parent, ChannelFactory factory,
|
||||
|
@ -43,6 +43,7 @@ import org.jboss.netty.channel.ChannelException;
|
||||
import org.jboss.netty.channel.ChannelFuture;
|
||||
import org.jboss.netty.channel.MessageEvent;
|
||||
import org.jboss.netty.channel.ReceiveBufferSizePredictor;
|
||||
import org.jboss.netty.channel.socket.nio.SocketSendBufferPool.SendBuffer;
|
||||
import org.jboss.netty.logging.InternalLogger;
|
||||
import org.jboss.netty.logging.InternalLoggerFactory;
|
||||
import org.jboss.netty.util.ThreadRenamingRunnable;
|
||||
@ -79,7 +80,8 @@ class NioWorker implements Runnable {
|
||||
private final Queue<Runnable> writeTaskQueue = new LinkedTransferQueue<Runnable>();
|
||||
private volatile int cancelledKeys; // should use AtomicInteger but we just need approximation
|
||||
|
||||
private final ReadBufferPool readBufferPool = new ReadBufferPool();
|
||||
private final SocketReceiveBufferPool recvBufferPool = new SocketReceiveBufferPool();
|
||||
private final SocketSendBufferPool sendBufferPool = new SocketSendBufferPool();
|
||||
|
||||
NioWorker(int bossId, int id, Executor executor) {
|
||||
this.bossId = bossId;
|
||||
@ -334,7 +336,7 @@ class NioWorker implements Runnable {
|
||||
fireExceptionCaught(channel, t);
|
||||
}
|
||||
} else {
|
||||
ByteBuffer bb = readBufferPool.acquire(buffer.writableBytes());
|
||||
ByteBuffer bb = recvBufferPool.acquire(buffer.writableBytes());
|
||||
try {
|
||||
while ((ret = ch.read(bb)) > 0) {
|
||||
readBytes += ret;
|
||||
@ -350,7 +352,7 @@ class NioWorker implements Runnable {
|
||||
} finally {
|
||||
bb.flip();
|
||||
buffer.writeBytes(bb);
|
||||
readBufferPool.release(bb);
|
||||
recvBufferPool.release(bb);
|
||||
}
|
||||
}
|
||||
|
||||
@ -452,6 +454,7 @@ class NioWorker implements Runnable {
|
||||
|
||||
int writtenBytes = 0;
|
||||
|
||||
final SocketSendBufferPool sendBufferPool = this.sendBufferPool;
|
||||
final SocketChannel ch = channel.socket;
|
||||
final Queue<MessageEvent> writeBuffer = channel.writeBuffer;
|
||||
final int writeSpinCount = channel.getConfig().getWriteSpinCount();
|
||||
@ -459,7 +462,8 @@ class NioWorker implements Runnable {
|
||||
channel.inWriteNowLoop = true;
|
||||
for (;;) {
|
||||
MessageEvent evt = channel.currentWriteEvent;
|
||||
ByteBuffer buf;
|
||||
SendBuffer buf;
|
||||
ByteBuffer bb;
|
||||
if (evt == null) {
|
||||
if ((channel.currentWriteEvent = evt = writeBuffer.poll()) == null) {
|
||||
removeOpWrite = true;
|
||||
@ -468,27 +472,31 @@ class NioWorker implements Runnable {
|
||||
}
|
||||
|
||||
ChannelBuffer origBuf = (ChannelBuffer) evt.getMessage();
|
||||
channel.currentWriteBuffer = buf = origBuf.toByteBuffer();
|
||||
channel.currentWriteBuffer = buf = sendBufferPool.acquire(origBuf);
|
||||
bb = buf.buffer;
|
||||
} else {
|
||||
buf = channel.currentWriteBuffer;
|
||||
bb = buf.buffer;
|
||||
}
|
||||
|
||||
try {
|
||||
for (int i = writeSpinCount; i > 0; i --) {
|
||||
int localWrittenBytes = ch.write(buf);
|
||||
int localWrittenBytes = ch.write(bb);
|
||||
if (localWrittenBytes != 0) {
|
||||
writtenBytes += localWrittenBytes;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!buf.hasRemaining()) {
|
||||
if (!bb.hasRemaining()) {
|
||||
// Successful write - proceed to the next message.
|
||||
buf.release();
|
||||
ChannelFuture future = evt.getFuture();
|
||||
channel.currentWriteEvent = null;
|
||||
channel.currentWriteBuffer = null;
|
||||
evt = null;
|
||||
buf = null;
|
||||
bb = null;
|
||||
future.setSuccess();
|
||||
} else {
|
||||
// Not written fully - perhaps the kernel buffer is full.
|
||||
@ -499,11 +507,13 @@ class NioWorker implements Runnable {
|
||||
} catch (AsynchronousCloseException e) {
|
||||
// Doesn't need a user attention - ignore.
|
||||
} catch (Throwable t) {
|
||||
buf.release();
|
||||
ChannelFuture future = evt.getFuture();
|
||||
channel.currentWriteEvent = null;
|
||||
channel.currentWriteBuffer = null;
|
||||
buf = null;
|
||||
evt = null;
|
||||
bb = null;
|
||||
future.setFailure(t);
|
||||
fireExceptionCaught(channel, t);
|
||||
if (t instanceof IOException) {
|
||||
|
@ -23,14 +23,14 @@ import java.nio.ByteBuffer;
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
* @version $Rev$, $Date$
|
||||
*/
|
||||
final class ReadBufferPool {
|
||||
final class SocketReceiveBufferPool {
|
||||
|
||||
private static final int POOL_SIZE = 4;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private final SoftReference<ByteBuffer>[] pool = new SoftReference[POOL_SIZE];
|
||||
|
||||
ReadBufferPool() {
|
||||
SocketReceiveBufferPool() {
|
||||
super();
|
||||
}
|
||||
|
@ -0,0 +1,164 @@
|
||||
/*
|
||||
* Copyright 2010 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat 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 org.jboss.netty.channel.socket.nio;
|
||||
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
* @version $Rev: 2174 $, $Date: 2010-02-19 09:57:23 +0900 (Fri, 19 Feb 2010) $
|
||||
*/
|
||||
final class SocketSendBufferPool {
|
||||
|
||||
private static final int DEFAULT_PREALLOCATION_SIZE = 65536;
|
||||
private static final int ALIGN_SHIFT = 4;
|
||||
private static final int ALIGN_MASK = 15;
|
||||
|
||||
static {
|
||||
assert (DEFAULT_PREALLOCATION_SIZE & ALIGN_MASK) == 0;
|
||||
}
|
||||
|
||||
private PreallocationRef poolHead = null;
|
||||
private Preallocation current = new Preallocation(DEFAULT_PREALLOCATION_SIZE);
|
||||
|
||||
SocketSendBufferPool() {
|
||||
super();
|
||||
}
|
||||
|
||||
final SendBuffer acquire(ChannelBuffer src) {
|
||||
if (src.isDirect()) {
|
||||
return new SendBuffer(null, src.toByteBuffer());
|
||||
}
|
||||
if (src.readableBytes() > DEFAULT_PREALLOCATION_SIZE) {
|
||||
return new SendBuffer(null, src.toByteBuffer());
|
||||
}
|
||||
|
||||
SendBuffer dst = acquire(src.readableBytes());
|
||||
ByteBuffer dstbuf = dst.buffer;
|
||||
dstbuf.mark();
|
||||
src.getBytes(src.readerIndex(), dstbuf);
|
||||
dstbuf.reset();
|
||||
return dst;
|
||||
}
|
||||
|
||||
private final SendBuffer acquire(int size) {
|
||||
assert size <= DEFAULT_PREALLOCATION_SIZE;
|
||||
Preallocation current = this.current;
|
||||
ByteBuffer buffer = current.buffer;
|
||||
int remaining = buffer.remaining();
|
||||
|
||||
if (size < remaining) {
|
||||
int nextPos = buffer.position() + size;
|
||||
ByteBuffer slice = buffer.duplicate();
|
||||
buffer.position(align(nextPos));
|
||||
slice.limit(nextPos);
|
||||
current.refCnt ++;
|
||||
return new SendBuffer(current, slice);
|
||||
} else if (size > remaining) {
|
||||
this.current = current = getPreallocation();
|
||||
buffer = current.buffer;
|
||||
ByteBuffer slice = buffer.duplicate();
|
||||
buffer.position(align(size));
|
||||
slice.limit(size);
|
||||
current.refCnt ++;
|
||||
return new SendBuffer(current, slice);
|
||||
} else { // size == remaining
|
||||
current.refCnt ++;
|
||||
this.current = getPreallocation0();
|
||||
return new SendBuffer(current, current.buffer);
|
||||
}
|
||||
}
|
||||
|
||||
private final Preallocation getPreallocation() {
|
||||
Preallocation current = this.current;
|
||||
if (current.refCnt == 0) {
|
||||
current.buffer.clear();
|
||||
return current;
|
||||
}
|
||||
|
||||
return getPreallocation0();
|
||||
}
|
||||
|
||||
private final Preallocation getPreallocation0() {
|
||||
PreallocationRef ref = poolHead;
|
||||
if (ref != null) {
|
||||
do {
|
||||
Preallocation p = ref.get();
|
||||
ref = ref.next;
|
||||
|
||||
if (p != null) {
|
||||
poolHead = ref;
|
||||
return p;
|
||||
}
|
||||
} while (ref != null);
|
||||
|
||||
poolHead = ref;
|
||||
}
|
||||
|
||||
return new Preallocation(DEFAULT_PREALLOCATION_SIZE);
|
||||
}
|
||||
|
||||
private static final int align(int pos) {
|
||||
int q = pos >>> ALIGN_SHIFT;
|
||||
int r = pos & ALIGN_MASK;
|
||||
if (r != 0) {
|
||||
q ++;
|
||||
}
|
||||
return q << ALIGN_SHIFT;
|
||||
}
|
||||
|
||||
private final class Preallocation {
|
||||
final ByteBuffer buffer;
|
||||
int refCnt;
|
||||
|
||||
Preallocation(int capacity) {
|
||||
buffer = ByteBuffer.allocateDirect(capacity);
|
||||
}
|
||||
}
|
||||
|
||||
private final class PreallocationRef extends SoftReference<Preallocation> {
|
||||
final PreallocationRef next;
|
||||
|
||||
PreallocationRef(Preallocation prealloation, PreallocationRef next) {
|
||||
super(prealloation);
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
final class SendBuffer {
|
||||
private final Preallocation parent;
|
||||
final ByteBuffer buffer;
|
||||
|
||||
SendBuffer(Preallocation parent, ByteBuffer buffer) {
|
||||
this.parent = parent;
|
||||
this.buffer = buffer;
|
||||
}
|
||||
|
||||
void release() {
|
||||
final Preallocation parent = this.parent;
|
||||
if (parent != null && -- parent.refCnt == 0) {
|
||||
parent.buffer.clear();
|
||||
if (parent != current) {
|
||||
poolHead = new PreallocationRef(parent, poolHead);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user