Disable epoll bug woakraround by default

This commit is contained in:
Trustin Lee 2012-09-01 16:56:09 +09:00
parent 00f737c3a4
commit e781bd0b1e
2 changed files with 36 additions and 30 deletions

View File

@ -17,8 +17,8 @@ package io.netty.channel.socket.nio;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelException; import io.netty.channel.ChannelException;
import io.netty.channel.SingleThreadEventLoop;
import io.netty.channel.ChannelTaskScheduler; import io.netty.channel.ChannelTaskScheduler;
import io.netty.channel.SingleThreadEventLoop;
import io.netty.channel.socket.nio.AbstractNioChannel.NioUnsafe; import io.netty.channel.socket.nio.AbstractNioChannel.NioUnsafe;
import io.netty.logging.InternalLogger; import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory; import io.netty.logging.InternalLoggerFactory;
@ -26,9 +26,9 @@ import io.netty.logging.InternalLoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.nio.channels.CancelledKeyException; import java.nio.channels.CancelledKeyException;
import java.nio.channels.ClosedChannelException; import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey; import java.nio.channels.SelectionKey;
import java.nio.channels.Selector; import java.nio.channels.Selector;
import java.nio.channels.SelectableChannel;
import java.nio.channels.spi.SelectorProvider; import java.nio.channels.spi.SelectorProvider;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -49,17 +49,17 @@ final class NioEventLoop extends SingleThreadEventLoop {
/** /**
* Internal Netty logger. * Internal Netty logger.
*/ */
protected static final InternalLogger logger = InternalLoggerFactory private static final InternalLogger logger =
.getInstance(NioEventLoop.class); InternalLoggerFactory.getInstance(NioEventLoop.class);
static final int CLEANUP_INTERVAL = 256; // XXX Hard-coded value, but won't need customization. static final int CLEANUP_INTERVAL = 256; // XXX Hard-coded value, but won't need customization.
/** /**
* The NIO {@link Selector}. * The NIO {@link Selector}.
*/ */
protected Selector selector; Selector selector;
protected final SelectorProvider provider; private final SelectorProvider provider;
/** /**
* Boolean that controls determines if a blocked Selector.select should * Boolean that controls determines if a blocked Selector.select should
@ -67,7 +67,7 @@ final class NioEventLoop extends SingleThreadEventLoop {
* the select method and the select method will block for that time unless * the select method and the select method will block for that time unless
* waken up. * waken up.
*/ */
protected final AtomicBoolean wakenUp = new AtomicBoolean(); private final AtomicBoolean wakenUp = new AtomicBoolean();
private int cancelledKeys; private int cancelledKeys;
private boolean cleanedCancelledKeys; private boolean cleanedCancelledKeys;
@ -147,29 +147,31 @@ final class NioEventLoop extends SingleThreadEventLoop {
try { try {
long beforeSelect = System.nanoTime(); long beforeSelect = System.nanoTime();
int selected = SelectorUtil.select(selector); int selected = SelectorUtil.select(selector);
if (selected == 0) { if (SelectorUtil.EPOLL_BUG_WORKAROUND) {
long timeBlocked = System.nanoTime() - beforeSelect; if (selected == 0) {
if (timeBlocked < minSelectTimeout) { long timeBlocked = System.nanoTime() - beforeSelect;
// returned before the minSelectTimeout elapsed with nothing select. if (timeBlocked < minSelectTimeout) {
// this may be the cause of the jdk epoll(..) bug, so increment the counter // returned before the minSelectTimeout elapsed with nothing select.
// which we use later to see if its really the jdk bug. // this may be the cause of the jdk epoll(..) bug, so increment the counter
selectReturnsImmediately ++; // which we use later to see if its really the jdk bug.
} else { selectReturnsImmediately ++;
selectReturnsImmediately = 0; } else {
} selectReturnsImmediately = 0;
if (selectReturnsImmediately == 10) { }
// The selector returned immediately for 10 times in a row, if (selectReturnsImmediately == 10) {
// so recreate one selector as it seems like we hit the // The selector returned immediately for 10 times in a row,
// famous epoll(..) jdk bug. // so recreate one selector as it seems like we hit the
selector = recreateSelector(); // famous epoll(..) jdk bug.
selectReturnsImmediately = 0; selector = recreateSelector();
selectReturnsImmediately = 0;
// try to select again // try to select again
continue; continue;
}
} else {
// reset counter
selectReturnsImmediately = 0;
} }
} else {
// reset counter
selectReturnsImmediately = 0;
} }
// 'wakenUp.compareAndSet(false, true)' is always evaluated // 'wakenUp.compareAndSet(false, true)' is always evaluated

View File

@ -31,6 +31,8 @@ final class SelectorUtil {
static final long SELECT_TIMEOUT = static final long SELECT_TIMEOUT =
SystemPropertyUtil.getLong("io.netty.selectTimeout", DEFAULT_SELECT_TIMEOUT); SystemPropertyUtil.getLong("io.netty.selectTimeout", DEFAULT_SELECT_TIMEOUT);
static final long SELECT_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(SELECT_TIMEOUT); static final long SELECT_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(SELECT_TIMEOUT);
static final boolean EPOLL_BUG_WORKAROUND =
SystemPropertyUtil.getBoolean("io.netty.epollBugWorkaround", false);
// Workaround for JDK NIO bug. // Workaround for JDK NIO bug.
// //
@ -49,8 +51,10 @@ final class SelectorUtil {
logger.debug("Unable to get/set System Property '" + key + "'", e); logger.debug("Unable to get/set System Property '" + key + "'", e);
} }
} }
if (logger.isDebugEnabled()) {
logger.debug("Using select timeout of " + SELECT_TIMEOUT); logger.debug("Using select timeout of " + SELECT_TIMEOUT);
logger.debug("Epoll-bug workaround enabled = " + EPOLL_BUG_WORKAROUND);
}
} }
static int select(Selector selector) throws IOException { static int select(Selector selector) throws IOException {