Repository navigation
Round up the NIO select timeout again to not busy spin (#18137) - #18138
Conversation
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]>
f558604 to
c1c29b8
Compare
franz1981
left a comment
There was a problem hiding this comment.
It trades precision (for submillis remainder) Vs CPU usage, but spinning for long is quite bad so I prefer it that way tbh. Let's see the CI
|
@DavideD please sign our icla: https://netty.io. Let me know once done |
|
@DavideD can you also double check if the same problem exists in either kqueue, epoll or io_uring transport ? |
|
good point @normanmaurer ! |
|
I think only NIO is affected, all the other transports seem to be using nanoseconds, instead of rounded milliseconds:
So only NIO is affected, because I signed the ICLA. |
✅ All tests passed ✅🏷️ Commit: c1c29b8 Learn more about TestLens at testlens.app/docs. |
|
Thanks @DavideD ❤️ |
|
Auto-port PR for 5.0: #18141 |
…#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]>
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]