private void adjustMaxBytesPerGatheringWrite(long attempted, long written, long oldMaxBytesPerGatheringWrite) { // By default we track the SO_SNDBUF when ever it is explicitly set. However some OSes may dynamically change // SO_SNDBUF (and other characteristics that determine how much data can be written at once) so we should try // make a best effort to adjust as OS behavior changes. if (attempted == written) { if (attempted << 1 > oldMaxBytesPerGatheringWrite) { config().setMaxBytesPerGatheringWrite(attempted << 1); } } else if (attempted > MAX_BYTES_PER_GATHERING_WRITE_ATTEMPTED_LOW_THRESHOLD && written < attempted >>> 1) { config().setMaxBytesPerGatheringWrite(attempted >>> 1); } }
private void adjustMaxBytesPerGatheringWrite(long attempted, long written, long oldMaxBytesPerGatheringWrite) { // By default we track the SO_SNDBUF when ever it is explicitly set. However some OSes may dynamically change // SO_SNDBUF (and other characteristics that determine how much data can be written at once) so we should try // make a best effort to adjust as OS behavior changes. if (attempted == written) { if (attempted << 1 > oldMaxBytesPerGatheringWrite) { config().setMaxBytesPerGatheringWrite(attempted << 1); } } else if (attempted > MAX_BYTES_PER_GATHERING_WRITE_ATTEMPTED_LOW_THRESHOLD && written < attempted >>> 1) { config().setMaxBytesPerGatheringWrite(attempted >>> 1); } }
/** * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}. * @param in the collection which contains objects to write. * @param buf the {@link ByteBuf} from which the bytes should be written * @return The value that should be decremented from the write quantum which starts at * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows: * <ul> * <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content) * is encountered</li> * <li>1 - if a single call to write data was made to the OS</li> * <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no * data was accepted</li> * </ul> */ private int writeBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception { int readableBytes = buf.readableBytes(); if (readableBytes == 0) { in.remove(); return 0; } if (buf.hasMemoryAddress() || buf.nioBufferCount() == 1) { return doWriteBytes(in, buf); } else { ByteBuffer[] nioBuffers = buf.nioBuffers(); return writeBytesMultiple(in, nioBuffers, nioBuffers.length, readableBytes, config().getMaxBytesPerGatheringWrite()); } }
/** * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}. * @param in the collection which contains objects to write. * @param buf the {@link ByteBuf} from which the bytes should be written * @return The value that should be decremented from the write quantum which starts at * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows: * <ul> * <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content) * is encountered</li> * <li>1 - if a single call to write data was made to the OS</li> * <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no * data was accepted</li> * </ul> */ private int writeBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception { int readableBytes = buf.readableBytes(); if (readableBytes == 0) { in.remove(); return 0; } if (buf.hasMemoryAddress() || buf.nioBufferCount() == 1) { return doWriteBytes(in, buf); } else { ByteBuffer[] nioBuffers = buf.nioBuffers(); return writeBytesMultiple(in, nioBuffers, nioBuffers.length, readableBytes, config().getMaxBytesPerGatheringWrite()); } }
/** * Attempt to write multiple {@link ByteBuf} objects. * @param in the collection which contains objects to write. * @return The value that should be decremented from the write quantum which starts at * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows: * <ul> * <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content) * is encountered</li> * <li>1 - if a single call to write data was made to the OS</li> * <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no * data was accepted</li> * </ul> * @throws Exception If an I/O error occurs. */ private int doWriteMultiple(ChannelOutboundBuffer in) throws Exception { final long maxBytesPerGatheringWrite = config().getMaxBytesPerGatheringWrite(); IovArray array = ((KQueueEventLoop) eventLoop()).cleanArray(); array.maxBytes(maxBytesPerGatheringWrite); in.forEachFlushedMessage(array); if (array.count() >= 1) { // TODO: Handle the case where cnt == 1 specially. return writeBytesMultiple(in, array); } // cnt == 0, which means the outbound buffer contained empty buffers only. in.removeBytes(0); return 0; }
@Override protected void doWrite(ChannelOutboundBuffer in) throws Exception { int writeSpinCount = config().getWriteSpinCount(); do { final int msgCount = in.size();
@Override protected void doWrite(ChannelOutboundBuffer in) throws Exception { int writeSpinCount = config().getWriteSpinCount(); do { final int msgCount = in.size();
@Override void readReady(final KQueueRecvByteAllocatorHandle allocHandle) { final ChannelConfig config = config(); if (shouldBreakReadReady(config)) { clearReadFilter0();
final long maxBytesPerGatheringWrite = config().getMaxBytesPerGatheringWrite(); if (PlatformDependent.hasUnsafe()) { IovArray array = ((KQueueEventLoop) eventLoop()).cleanArray();
@Override void readReady(final KQueueRecvByteAllocatorHandle allocHandle) { final ChannelConfig config = config(); if (shouldBreakReadReady(config)) { clearReadFilter0();
private void adjustMaxBytesPerGatheringWrite(long attempted, long written, long oldMaxBytesPerGatheringWrite) { // By default we track the SO_SNDBUF when ever it is explicitly set. However some OSes may dynamically change // SO_SNDBUF (and other characteristics that determine how much data can be written at once) so we should try // make a best effort to adjust as OS behavior changes. if (attempted == written) { if (attempted << 1 > oldMaxBytesPerGatheringWrite) { config().setMaxBytesPerGatheringWrite(attempted << 1); } } else if (attempted > MAX_BYTES_PER_GATHERING_WRITE_ATTEMPTED_LOW_THRESHOLD && written < attempted >>> 1) { config().setMaxBytesPerGatheringWrite(attempted >>> 1); } }
private void adjustMaxBytesPerGatheringWrite(long attempted, long written, long oldMaxBytesPerGatheringWrite) { // By default we track the SO_SNDBUF when ever it is explicitly set. However some OSes may dynamically change // SO_SNDBUF (and other characteristics that determine how much data can be written at once) so we should try // make a best effort to adjust as OS behavior changes. if (attempted == written) { if (attempted << 1 > oldMaxBytesPerGatheringWrite) { config().setMaxBytesPerGatheringWrite(attempted << 1); } } else if (attempted > MAX_BYTES_PER_GATHERING_WRITE_ATTEMPTED_LOW_THRESHOLD && written < attempted >>> 1) { config().setMaxBytesPerGatheringWrite(attempted >>> 1); } }
private void adjustMaxBytesPerGatheringWrite(long attempted, long written, long oldMaxBytesPerGatheringWrite) { // By default we track the SO_SNDBUF when ever it is explicitly set. However some OSes may dynamically change // SO_SNDBUF (and other characteristics that determine how much data can be written at once) so we should try // make a best effort to adjust as OS behavior changes. if (attempted == written) { if (attempted << 1 > oldMaxBytesPerGatheringWrite) { config().setMaxBytesPerGatheringWrite(attempted << 1); } } else if (attempted > MAX_BYTES_PER_GATHERING_WRITE_ATTEMPTED_LOW_THRESHOLD && written < attempted >>> 1) { config().setMaxBytesPerGatheringWrite(attempted >>> 1); } }
private void adjustMaxBytesPerGatheringWrite(long attempted, long written, long oldMaxBytesPerGatheringWrite) { // By default we track the SO_SNDBUF when ever it is explicitly set. However some OSes may dynamically change // SO_SNDBUF (and other characteristics that determine how much data can be written at once) so we should try // make a best effort to adjust as OS behavior changes. if (attempted == written) { if (attempted << 1 > oldMaxBytesPerGatheringWrite) { config().setMaxBytesPerGatheringWrite(attempted << 1); } } else if (attempted > MAX_BYTES_PER_GATHERING_WRITE_ATTEMPTED_LOW_THRESHOLD && written < attempted >>> 1) { config().setMaxBytesPerGatheringWrite(attempted >>> 1); } }
/** * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}. * @param in the collection which contains objects to write. * @param buf the {@link ByteBuf} from which the bytes should be written * @return The value that should be decremented from the write quantum which starts at * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows: * <ul> * <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content) * is encountered</li> * <li>1 - if a single call to write data was made to the OS</li> * <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no * data was accepted</li> * </ul> */ private int writeBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception { int readableBytes = buf.readableBytes(); if (readableBytes == 0) { in.remove(); return 0; } if (buf.hasMemoryAddress() || buf.nioBufferCount() == 1) { return doWriteBytes(in, buf); } else { ByteBuffer[] nioBuffers = buf.nioBuffers(); return writeBytesMultiple(in, nioBuffers, nioBuffers.length, readableBytes, config().getMaxBytesPerGatheringWrite()); } }
/** * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}. * @param in the collection which contains objects to write. * @param buf the {@link ByteBuf} from which the bytes should be written * @return The value that should be decremented from the write quantum which starts at * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows: * <ul> * <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content) * is encountered</li> * <li>1 - if a single call to write data was made to the OS</li> * <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no * data was accepted</li> * </ul> */ private int writeBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception { int readableBytes = buf.readableBytes(); if (readableBytes == 0) { in.remove(); return 0; } if (buf.hasMemoryAddress() || buf.nioBufferCount() == 1) { return doWriteBytes(in, buf); } else { ByteBuffer[] nioBuffers = buf.nioBuffers(); return writeBytesMultiple(in, nioBuffers, nioBuffers.length, readableBytes, config().getMaxBytesPerGatheringWrite()); } }
/** * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}. * @param in the collection which contains objects to write. * @param buf the {@link ByteBuf} from which the bytes should be written * @return The value that should be decremented from the write quantum which starts at * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows: * <ul> * <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content) * is encountered</li> * <li>1 - if a single call to write data was made to the OS</li> * <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no * data was accepted</li> * </ul> */ private int writeBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception { int readableBytes = buf.readableBytes(); if (readableBytes == 0) { in.remove(); return 0; } if (buf.hasMemoryAddress() || buf.nioBufferCount() == 1) { return doWriteBytes(in, buf); } else { ByteBuffer[] nioBuffers = buf.nioBuffers(); return writeBytesMultiple(in, nioBuffers, nioBuffers.length, readableBytes, config().getMaxBytesPerGatheringWrite()); } }
/** * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}. * @param in the collection which contains objects to write. * @param buf the {@link ByteBuf} from which the bytes should be written * @return The value that should be decremented from the write quantum which starts at * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows: * <ul> * <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content) * is encountered</li> * <li>1 - if a single call to write data was made to the OS</li> * <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no * data was accepted</li> * </ul> */ private int writeBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception { int readableBytes = buf.readableBytes(); if (readableBytes == 0) { in.remove(); return 0; } if (buf.hasMemoryAddress() || buf.nioBufferCount() == 1) { return doWriteBytes(in, buf); } else { ByteBuffer[] nioBuffers = buf.nioBuffers(); return writeBytesMultiple(in, nioBuffers, nioBuffers.length, readableBytes, config().getMaxBytesPerGatheringWrite()); } }
/** * Attempt to write multiple {@link ByteBuf} objects. * @param in the collection which contains objects to write. * @return The value that should be decremented from the write quantum which starts at * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows: * <ul> * <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content) * is encountered</li> * <li>1 - if a single call to write data was made to the OS</li> * <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no * data was accepted</li> * </ul> * @throws Exception If an I/O error occurs. */ private int doWriteMultiple(ChannelOutboundBuffer in) throws Exception { final long maxBytesPerGatheringWrite = config().getMaxBytesPerGatheringWrite(); IovArray array = ((KQueueEventLoop) eventLoop()).cleanArray(); array.maxBytes(maxBytesPerGatheringWrite); in.forEachFlushedMessage(array); if (array.count() >= 1) { // TODO: Handle the case where cnt == 1 specially. return writeBytesMultiple(in, array); } // cnt == 0, which means the outbound buffer contained empty buffers only. in.removeBytes(0); return 0; }
@Override protected void doWrite(ChannelOutboundBuffer in) throws Exception { int writeSpinCount = config().getWriteSpinCount(); do { final int msgCount = in.size();