Revert "Epoll: Avoid redundant EPOLL_CTL_MOD calls (#9397)"

This reverts commit 873988676a2b1bb9cc6e5c1a80e5b27725b1d75c.
This commit is contained in:
Norman Maurer 2019-09-12 11:42:04 +02:00
parent 8280252d0e
commit b409f8e7fa
3 changed files with 61 additions and 76 deletions

View File

@ -69,8 +69,7 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann
private volatile SocketAddress local;
private volatile SocketAddress remote;
protected int flags = Native.EPOLLET | Native.EPOLLIN;
protected int activeFlags;
protected int flags = Native.EPOLLET;
boolean inputClosedSeenErrorOnRead;
boolean epollInReadyRunnablePending;
@ -110,23 +109,17 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann
}
}
void setFlag(int flag) {
void setFlag(int flag) throws IOException {
if (!isFlagSet(flag)) {
flags |= flag;
updatePendingFlagsSet();
modifyEvents();
}
}
void clearFlag(int flag) {
void clearFlag(int flag) throws IOException {
if (isFlagSet(flag)) {
flags &= ~flag;
updatePendingFlagsSet();
}
}
private void updatePendingFlagsSet() {
if (isRegistered()) {
((EpollEventLoop) eventLoop()).updatePendingFlagsSet(this);
modifyEvents();
}
}
@ -248,33 +241,33 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann
((SocketChannelConfig) config).isAllowHalfClosure();
}
private Runnable clearEpollInTask;
final void clearEpollIn() {
// Only clear if registered with an EventLoop as otherwise
final EventLoop loop = isRegistered() ? eventLoop() : null;
final AbstractEpollUnsafe unsafe = (AbstractEpollUnsafe) unsafe();
if (loop == null || loop.inEventLoop()) {
unsafe.clearEpollIn0();
return;
}
// schedule a task to clear the EPOLLIN as it is not safe to modify it directly
Runnable clearFlagTask = clearEpollInTask;
if (clearFlagTask == null) {
clearEpollInTask = clearFlagTask = new Runnable() {
@Override
public void run() {
if (!unsafe.readPending && !config().isAutoRead()) {
// Still no read triggered so clear it now
unsafe.clearEpollIn0();
if (isRegistered()) {
final EventLoop loop = eventLoop();
final AbstractEpollUnsafe unsafe = (AbstractEpollUnsafe) unsafe();
if (loop.inEventLoop()) {
unsafe.clearEpollIn0();
} else {
// schedule a task to clear the EPOLLIN as it is not safe to modify it directly
loop.execute(new Runnable() {
@Override
public void run() {
if (!unsafe.readPending && !config().isAutoRead()) {
// Still no read triggered so clear it now
unsafe.clearEpollIn0();
}
}
}
};
});
}
} else {
// The EventLoop is not registered atm so just update the flags so the correct value
// will be used once the channel is registered
flags &= ~Native.EPOLLIN;
}
loop.execute(clearFlagTask);
}
void modifyEvents() throws IOException {
private void modifyEvents() throws IOException {
if (isOpen() && isRegistered()) {
((EpollEventLoop) eventLoop()).modify(this);
}
@ -418,7 +411,7 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann
// * The user called Channel.read() or ChannelHandlerContext.read() in channelReadComplete(...) method
//
// See https://github.com/netty/netty/issues/2254
clearEpollIn0();
clearEpollIn();
}
}
@ -448,7 +441,19 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann
}
// Clear the EPOLLRDHUP flag to prevent continuously getting woken up on this event.
clearFlag(Native.EPOLLRDHUP);
clearEpollRdHup();
}
/**
* Clear the {@link Native#EPOLLRDHUP} flag from EPOLL, and close on failure.
*/
private void clearEpollRdHup() {
try {
clearFlag(Native.EPOLLRDHUP);
} catch (IOException e) {
pipeline().fireExceptionCaught(e);
close(voidPromise());
}
}
/**
@ -468,7 +473,7 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann
// We attempted to shutdown and failed, which means the input has already effectively been
// shutdown.
}
clearEpollIn0();
clearEpollIn();
pipeline().fireUserEventTriggered(ChannelInputShutdownEvent.INSTANCE);
} else {
close(voidPromise());
@ -524,9 +529,16 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann
}
protected final void clearEpollIn0() {
assert !isRegistered() || eventLoop().inEventLoop();
readPending = false;
clearFlag(Native.EPOLLIN);
assert eventLoop().inEventLoop();
try {
readPending = false;
clearFlag(Native.EPOLLIN);
} catch (IOException e) {
// When this happens there is something completely wrong with either the filedescriptor or epoll,
// so fire the exception through the pipeline and close the Channel.
pipeline().fireExceptionCaught(e);
unsafe().close(unsafe().voidPromise());
}
}
@Override
@ -651,7 +663,7 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann
/**
* Finish the connect
*/
private boolean doFinishConnect() throws IOException {
private boolean doFinishConnect() throws Exception {
if (socket.finishConnect()) {
clearFlag(Native.EPOLLOUT);
if (requestedRemoteAddress instanceof InetSocketAddress) {

View File

@ -150,7 +150,8 @@ public class EpollChannelConfig extends DefaultChannelConfig {
if (mode == null) {
throw new NullPointerException("mode");
}
switch (mode) {
try {
switch (mode) {
case EDGE_TRIGGERED:
checkChannelNotRegistered();
((AbstractEpollChannel) channel).setFlag(Native.EPOLLET);
@ -161,6 +162,9 @@ public class EpollChannelConfig extends DefaultChannelConfig {
break;
default:
throw new Error();
}
} catch (IOException e) {
throw new ChannelException(e);
}
return this;
}

View File

@ -33,7 +33,6 @@ import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.io.IOException;
import java.util.BitSet;
import java.util.Queue;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
@ -68,8 +67,6 @@ class EpollEventLoop extends SingleThreadEventLoop {
private final FileDescriptor eventFd;
private final FileDescriptor timerFd;
private final IntObjectMap<AbstractEpollChannel> channels = new IntObjectHashMap<AbstractEpollChannel>(4096);
private final BitSet pendingFlagChannels = new BitSet();
private final boolean allowGrowing;
private final EpollEventArray events;
@ -274,7 +271,6 @@ class EpollEventLoop extends SingleThreadEventLoop {
assert inEventLoop();
int fd = ch.socket.intValue();
Native.epollCtlAdd(epollFd.intValue(), fd, ch.flags);
ch.activeFlags = ch.flags;
AbstractEpollChannel old = channels.put(fd, ch);
// We either expect to have no Channel in the map with the same FD or that the FD of the old Channel is already
@ -288,28 +284,6 @@ class EpollEventLoop extends SingleThreadEventLoop {
void modify(AbstractEpollChannel ch) throws IOException {
assert inEventLoop();
Native.epollCtlMod(epollFd.intValue(), ch.socket.intValue(), ch.flags);
ch.activeFlags = ch.flags;
}
void updatePendingFlagsSet(AbstractEpollChannel ch) {
pendingFlagChannels.set(ch.socket.intValue(), ch.flags != ch.activeFlags);
}
private void processPendingChannelFlags() {
// Call epollCtlMod for any channels that require event interest changes before epollWaiting
if (!pendingFlagChannels.isEmpty()) {
for (int fd = 0; (fd = pendingFlagChannels.nextSetBit(fd)) >= 0; pendingFlagChannels.clear(fd)) {
AbstractEpollChannel ch = channels.get(fd);
if (ch != null) {
try {
ch.modifyEvents();
} catch (IOException e) {
ch.pipeline().fireExceptionCaught(e);
ch.close();
}
}
}
}
}
/**
@ -326,14 +300,10 @@ class EpollEventLoop extends SingleThreadEventLoop {
// If we found another Channel in the map that is mapped to the same FD the given Channel MUST be closed.
assert !ch.isOpen();
} else {
ch.activeFlags = 0;
pendingFlagChannels.clear(fd);
if (ch.isOpen()) {
// Remove the epoll. This is only needed if it's still open as otherwise it will be automatically
// removed once the file-descriptor is closed.
Native.epollCtlDel(epollFd.intValue(), fd);
}
} else if (ch.isOpen()) {
// Remove the epoll. This is only needed if it's still open as otherwise it will be automatically
// removed once the file-descriptor is closed.
Native.epollCtlDel(epollFd.intValue(), fd);
}
}
@ -374,7 +344,6 @@ class EpollEventLoop extends SingleThreadEventLoop {
long timerFdDeadline = Long.MAX_VALUE;
for (;;) {
try {
processPendingChannelFlags();
int strategy = selectStrategy.calculateStrategy(selectNowSupplier, hasTasks());
switch (strategy) {
case SelectStrategy.CONTINUE: