Skip to content

Clean up NioEventLoop - #9799

Merged
normanmaurer merged 2 commits into
netty:4.1from
njhill:nioeventloop-cleanup
Nov 26, 2019
Merged

normanmaurer merged 2 commits into
netty:4.1from
njhill:nioeventloop-cleanup

Conversation

@njhill

@njhill njhill commented Nov 23, 2019

Copy link
Copy Markdown
Member

Motivation

The event loop implementations had become somewhat tangled over time and work was done recently to streamline EpollEventLoop. NioEventLoop would benefit from the same treatment and it is more straightforward now that we can follow the same structure as was done for epoll.

Modifications

Untangle NioEventLoop logic and mirror what's now done in EpollEventLoop w.r.t. the volatile selector wake-up guard and scheduled task deadline handling.

Some common refinements to EpollEventLoop have also been included - to use constants for the "special" deadline/wakeup volatile values and to avoid some unnecessary calls to System.nanoTime() on task-only iterations.

Result

Hopefully cleaner, more efficient and less fragile NIO transport implementation.

Motivation

The event loop implementations had become somewhat tangled over time and
work was done recently to streamline EpollEventLoop. NioEventLoop would
benefit from the same treatment and it is more straighforward now that
we can follow the same structure as was done for epoll.

Modifications

Untangle NioEventLoop logic and mirror what's now done in EpollEventLoop
w.r.t. the volatile selector wake-up guard and scheduled task deadline
handling.

Some common refinements to EpollEventLoop have also been included - to
use constants for the "special" deadline/wakeup volatile values and to
avoid some unnecessary calls to System.nanoTime() on task-only
iterations.

Result

Hopefully cleaner, more efficient and less fragile NIO transport
implementation.
@netty-bot

Copy link
Copy Markdown

Can one of the admins verify this patch?

@njhill

njhill commented Nov 23, 2019 •

Copy link
Copy Markdown
Member Author

This should address #9112, #9782 and #8644, and probably #4543 and #8698.

@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

1 similar comment
@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

@normanmaurer normanmaurer added this to the 4.1.44.Final milestone Nov 25, 2019
// NONE when EL is waiting with no wakeup scheduled
// other value T when EL is waiting with wakeup scheduled at time T
private final AtomicLong nextWakeupNanos = new AtomicLong(-1L);
private final AtomicLong nextWakeupNanos = new AtomicLong(AWAKE);

@franz1981 franz1981 Nov 25, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is outside the scope of this PR, but why not an AtomicLongFieldUpdater?
It would allow to control its layout if profiling shows that's highly contended (and it seems the case 2 me, with many producers)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good followup PR (which is also true for EpollEventLoop.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I can do it in a follow-on. I used AtomicLong here to be consistent with EpollEventLoop. That was originally an atomic updater but @normanmaurer suggested changing it to AtomicLong in a prior PR here: #9605 (comment)... I am fine with either though!

} finally {
// Ensure we always run tasks.
final long ioTime = System.nanoTime() - ioStartTime;
runAllTasks(ioTime * (100 - ioRatio) / ioRatio);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this (100 - ioRatio) / ioRatio has to be called on each iteration? / seems quite heavy

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@franz1981 maybe also for a separate PR since it's always been like this AFAIK... though I think if we want to calculate an accurate percentage of the measured time then a div will have to be done at some point, unless we want do a floating point multiply (and conversion to/from fp).

Also we had been thinking of getting rid of this altogether, and always process all tasks (with some limit on number of outer iterations) - see #9590 (comment). After more thought I feel like that makes more sense actually... thoughts?

// AWAKE when EL is awake
// NONE when EL is waiting with no wakeup scheduled
// other value T when EL is waiting with wakeup scheduled at time T
private final AtomicLong nextWakeupNanos = new AtomicLong(AWAKE);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AtomicLongFiedUpdater as well

// Ensure we always run tasks.
final long ioTime = System.nanoTime() - ioStartTime;
runAllTasks(ioTime * (100 - ioRatio) / ioRatio);
ranTasks = runAllTasks(ioTime * (100 - ioRatio) / ioRatio);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(100 - ioRatio) / ioRatio should be done on each iteration?

@normanmaurer

normanmaurer commented Nov 25, 2019 via email

Copy link
Copy Markdown
Member

@franz1981

Copy link
Copy Markdown
Contributor

@njhill @normanmaurer Agree about the follow-up: it seems already ok as it is (it's a port indeed)!

@njhill

njhill commented Nov 25, 2019

Copy link
Copy Markdown
Member Author

Thanks @normanmaurer @franz1981, I also agree about the follow-up... my question in the comment above was kind of off-topic not meant for this PR! I will open another later to discuss elimination of ioRatio.

@normanmaurer
normanmaurer merged commit e208e96 into netty:4.1 Nov 26, 2019
@normanmaurer

Copy link
Copy Markdown
Member

@njhill thanks a lot... Please also back port to master.

@njhill
njhill deleted the nioeventloop-cleanup branch November 26, 2019 07:28
DavideD added a commit to DavideD/netty that referenced this pull request Oct 28, 2026
Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing
by 1_000_000, which rounds to the nearest millisecond, while the comment
says it rounds up. If the next scheduled task is less than 0.5 ms away
the timeout is 0, so select(...) calls selectNow() and returns. The task
is not due yet, so the event loop runs another iteration and does the
same again until the deadline is reached. An event loop that has I/O and
a pending timer therefore polls the selector in a loop: an application
with a timer that fires every ms, 4 event loops and 4000 requests/s
used about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since netty#9799, so the
timeout is only 0 if the deadline is within 5 microseconds. The 4.2
NioIoHandler was introduced (netty#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new
bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead
of spinning. Timers can fire up to 1 ms later than before, like in 4.1.
The epoll, kqueue and io_uring transports use nanosecond timeouts and are
not affected.

Assisted-by: Claude Opus 5.5 <[email protected]>
franz1981 pushed a commit to DavideD/netty that referenced this pull request Oct 28, 2026
Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing
by 1_000_000, which rounds to the nearest millisecond, while the comment
says it rounds up. If the next scheduled task is less than 0.5 ms away
the timeout is 0, so select(...) calls selectNow() and returns. The task
is not due yet, so the event loop runs another iteration and does the
same again until the deadline is reached. An event loop that has I/O and
a pending timer therefore polls the selector in a loop: an application
with a timer that fires every ms, 4 event loops and 4000 requests/s
used about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since netty#9799, so the
timeout is only 0 if the deadline is within 5 microseconds. The 4.2
NioIoHandler was introduced (netty#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new
bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead
of spinning. Timers can fire up to 1 ms later than before, like in 4.1.
The epoll, kqueue and io_uring transports use nanosecond timeouts and are
not affected.

Assisted-by: Claude Opus 5.5 <[email protected]>
franz1981 pushed a commit that referenced this pull request Oct 28, 2026
Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing
by 1_000_000, which rounds to the nearest millisecond, while the comment
says it rounds up. If the next scheduled task is less than 0.5 ms away
the timeout is 0, so select(...) calls selectNow() and returns. The task
is not due yet, so the event loop runs another iteration and does the
same again until the deadline is reached. An event loop that has I/O and
a pending timer therefore polls the selector in a loop: an application
with a timer that fires every ms, 4 event loops and 4000 requests/s
used about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since #9799, so the
timeout is only 0 if the deadline is within 5 microseconds. The 4.2
NioIoHandler was introduced (#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new
bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead
of spinning. Timers can fire up to 1 ms later than before, like in 4.1.
The epoll, kqueue and io_uring transports use nanosecond timeouts and are
not affected.

Assisted-by: Claude Opus 5.5 <[email protected]>
normanmaurer pushed a commit that referenced this pull request Oct 28, 2026
…#18137) (#18141)

Auto-port of #18138 to 5.0
Cherry-picked commit: 06518dc

---
Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing
by 1_000_000, which rounds to the nearest millisecond, while the comment
says it rounds up. If the next scheduled task is less than 0.5 ms away
the timeout is 0, so select(...) calls selectNow() and returns. The task
is not due yet, so the event loop runs another iteration and does the
same again until the deadline is reached. An event loop that has I/O and
a pending timer therefore polls the selector in a loop: an application
with a timer that fires every ms, 4 event loops and 4000 requests/s used
about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since #9799, so the
timeout is only 0 if the deadline is within 5 microseconds. The 4.2
NioIoHandler was introduced (#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new
bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead
of spinning. Timers can fire up to 1 ms later than before, like in 4.1.
The epoll, kqueue and io_uring transports use nanosecond timeouts and
are not affected.

Assisted-by: Claude Opus 5.5 <[email protected]>

Co-authored-by: Davide D'Alto <[email protected]>
ikpil added a commit to ikpil/Netty.NET that referenced this pull request Nov 29, 2026
…tty/netty#18137)

Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing
by 1_000_000, which rounds to the nearest millisecond, while the comment
says it rounds up. If the next scheduled task is less than 0.5 ms away
the timeout is 0, so select(...) calls selectNow() and returns. The task
is not due yet, so the event loop runs another iteration and does the
same again until the deadline is reached. An event loop that has I/O and
a pending timer therefore polls the selector in a loop: an application
with a timer that fires every ms, 4 event loops and 4000 requests/s
used about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since netty/netty#9799, so the
timeout is only 0 if the deadline is within 5 microseconds. The 4.2
NioIoHandler was introduced (netty/netty#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new
bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead
of spinning. Timers can fire up to 1 ms later than before, like in 4.1.
The epoll, kqueue and io_uring transports use nanosecond timeouts and are
not affected.

Assisted-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants