Repository navigation
ev/io_uring: replace fragile FUTEX waker with eventfd multishot poll - #503
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughReplaces the FUTEX-based waker in the io_uring backend with an eventfd-backed multishot poll ( Changesio_uring eventfd multishot waker
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
The io_uring waker (a FUTEX_WAIT SQE woken cross-thread via futex2_wake) lost cross-thread wakeups, making latency-bound request/response (e.g. a cold QUIC handshake) ~4x slower than epoll (#494). Two problems compounded: - The FUTEX_WAIT was (re)armed only at the end of poll(), so an executor's first/cold poll blocked with no waiter in the ring and a cross-thread loop.wake() in that window was lost until the 10ms fallback. - Even when the FUTEX_WAIT was armed before blocking, futex2_wake did not reliably wake it: an experiment arming the waiter before enter2 (closing the window above) still left ~half of cross-thread wakes ~11ms late. The SHARED-futex global-hash workaround already in this file shows the path is fragile; the remaining interaction is not fully pinned down. Rather than chase the futex behavior, replace the waker with an eventfd registered as a permanent multishot poll (IORING_POLL_ADD_MULTI), mirroring the epoll backend's persistent eventfd registration: - Armed once in init(); the kernel keeps it armed across wakes (F_MORE), so the loop is never deaf and there is no per-wake re-arming. - wake() writes the eventfd; drainWaker() does one coalesced read to clear the level-triggered readiness. - A rare F_MORE drop is recovered by the top-of-poll armWaker(), with a non-blocking enter2 fallback when the SQ is full. - Removes the 10ms fallback cap and the SHARED-futex kernel-bug workaround. Fixes #494
f6b8d99 to
f8eae4b
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/ev/backends/io_uring.zig (1)
187-187: 🧹 Nitpick | 🔵 Trivial | 💤 Low valueConsider asserting
armWaker()succeeds ininit().The SQ is empty immediately after ring creation, so this should never fail. An assertion makes that invariant explicit and prevents silent breakage if someone adds SQE submissions before this line later:
- _ = self.armWaker(); + std.debug.assert(self.armWaker());🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/ev/backends/io_uring.zig` at line 187, The `armWaker()` call in the `init()` function is currently discarding its return value with the blank identifier pattern. Since the submission queue is empty at this point in initialization, the call should always succeed. Replace the `_ = self.armWaker();` line with an assertion that verifies `armWaker()` succeeds, making this invariant explicit and preventing silent failures if SQE submissions are added before this call in the future.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/ev/backends/io_uring.zig`:
- Around line 206-207: The constant linux.IORING_POLL_ADD_MULTI does not exist
in Zig's standard library, causing a compilation failure. Replace the current
approach of calling prep_poll_add followed by manually setting sqe.len with a
direct call to Zig's built-in prep_poll_multishot() method. Pass the same
parameters (self.waker_eventfd and linux.POLL.IN) to this function instead,
which handles multishot polling in the intended way.
---
Nitpick comments:
In `@src/ev/backends/io_uring.zig`:
- Line 187: The `armWaker()` call in the `init()` function is currently
discarding its return value with the blank identifier pattern. Since the
submission queue is empty at this point in initialization, the call should
always succeed. Replace the `_ = self.armWaker();` line with an assertion that
verifies `armWaker()` succeeds, making this invariant explicit and preventing
silent failures if SQE submissions are added before this call in the future.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 8f743413-567d-400b-9726-3b6682624948
📒 Files selected for processing (1)
src/ev/backends/io_uring.zig
| sqe.prep_poll_add(self.waker_eventfd, linux.POLL.IN); | ||
| sqe.len = linux.IORING_POLL_ADD_MULTI; |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Check if IORING_POLL_ADD_MULTI is defined in Zig's std.os.linux
rg -n "IORING_POLL_ADD_MULTI" --type zigRepository: lalinsky/zio
Length of output: 132
🏁 Script executed:
head -50 src/ev/backends/io_uring.zigRepository: lalinsky/zio
Length of output: 2742
🏁 Script executed:
rg -n "const linux|`@import`.*linux" src/ev/backends/io_uring.zig --type zig -A 2Repository: lalinsky/zio
Length of output: 662
🏁 Script executed:
rg -n "IORING_POLL" --type zigRepository: lalinsky/zio
Length of output: 132
🌐 Web query:
IORING_POLL_ADD_MULTI Zig std.os.linux constant
💡 Result:
The constant IORING_POLL_ADD_MULTI is a Linux kernel flag used to enable multishot mode for io_uring poll operations [1][2]. While it is a fundamental Linux kernel constant defined for the io_uring interface [1][3], it is not typically exposed as a direct constant within Zig's std.os.linux namespace. In Zig, you can utilize this flag by applying it to the length (len) field of the submission queue entry (SQE) [4]. Usage in Zig: When preparing a poll request, you can set the flag manually: const IORING_POLL_ADD_MULTI = 1; // 1U << 0 sqe.len |= IORING_POLL_ADD_MULTI; Alternatively, the standard library often provides higher-level helper functions. Instead of manually manipulating SQE fields, it is recommended to use the provided prep function, which abstracts the multishot configuration [5][4]: // Use the built-in multishot preparation function sqe.prep_poll_multishot(fd, poll_mask); When using multishot mode, the kernel will generate multiple completion queue entries (CQEs) for a single submitted SQE [1][4]. These CQEs will include the IORING_CQE_F_MORE flag to signal that the application should expect further notifications for that request [1][4].
Citations:
- 1: https://lore.gnuweeb.org/io-uring/[email protected]/
- 2: jk-ozlabs/linux@88e41cf
- 3: https://lore.gnuweeb.org/io-uring/[email protected]/T/
- 4: https://linuxman7.com/linux/man-pages/man7/io_uring_multishot.7.html
- 5: https://www.man7.org/linux/man-pages/man3/io_uring_prep_poll_add.3.html
IORING_POLL_ADD_MULTI is not defined in Zig's std.os.linux—this will fail to compile.
The constant doesn't exist in the standard library. You need to either define it locally as const IORING_POLL_ADD_MULTI = 1; or better yet, use Zig's built-in prep_poll_multishot() function instead of manually manipulating sqe.len. The latter is the intended approach and cleaner.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/ev/backends/io_uring.zig` around lines 206 - 207, The constant
linux.IORING_POLL_ADD_MULTI does not exist in Zig's standard library, causing a
compilation failure. Replace the current approach of calling prep_poll_add
followed by manually setting sqe.len with a direct call to Zig's built-in
prep_poll_multishot() method. Pass the same parameters (self.waker_eventfd and
linux.POLL.IN) to this function instead, which handles multishot polling in the
intended way.
Problem
On the io_uring backend, latency-bound request/response (e.g. a cold QUIC handshake) was ~4x slower than on epoll (#494). The waker was a
FUTEX_WAITSQE woken cross-thread viafutex2_wake. Two separate problems compounded:Arming window. The
FUTEX_WAITwas (re)armed only at the end ofpoll(), so an executor's first/cold poll blocked with no waiter in the ring. A cross-threadloop.wake()in that window (a.newtask scheduled to a sleeping home executor, or any cross-runtime wake) was lost until the 10ms fallback.Unreliable futex wake delivery. Even with the
FUTEX_WAITarmed before blocking,futex2_wakedid not reliably wake it. An experiment that armed the waiter beforeenter2— fully closing problem Make it possible to usestd.crypto.tls.Client#1 — still left ~half of cross-thread wakes ~11ms late. So the arming window is not the whole story: the SHARED-futex →FUTEX_WAITpath itself drops wakes. The SHARED-futex "global hash pivot" workaround already in this file is evidence the path is fragile; the precise remaining kernel interaction isn't pinned down.epoll has neither problem because its eventfd waker is registered persistently in
initand uses ordinary poll readiness.Fix
Rather than chase the futex behavior, replace the waker with an eventfd registered as a permanent multishot poll (
IORING_POLL_ADD_MULTI), mirroring the epoll backend:init(); the kernel keeps it armed across wakes (F_MORE), so the loop is never deaf and there's no per-wake re-arming.wake()writes the eventfd;drainWaker()does one coalesced read to clear the level-triggered readiness.F_MOREdrop (e.g. CQ overflow) is recovered by the single top-of-pollarmWaker(), with a non-blockingenter2fallback if the SQ is full.Validation
Minimal repro: one task per executor scheduled onto cold executors, measuring scheduled→running latency.
The middle row is the key evidence: closing the arming window alone is not sufficient — the futex path itself is the problem, and the eventfd waker resolves it.
zig build test -Dbackend=io_uring: no new failures vs baseline.Fixes #494