Repository navigation
ev/io_uring: arm boot/real timers natively - #516
Conversation
…llback Add a native_wall_timers backend capability. When set, checkTimers stops folding boot/real deadlines into the poll timeout (no 10s cap) and instead hands the per-clock heap minimums to the backend via syncWallTimers. io_uring arms one IORING_OP_TIMEOUT SQE per active wall clock with IORING_TIMEOUT_ABS | BOOTTIME/REALTIME (both >= 5.15; the ring already requires 6.1+ for DEFER_TASKRUN). The kernel re-evaluates these across suspend and clock steps, so boot/real timers fire exactly rather than within the cap. A fired timeout (-ETIME) makes poll report a timeout so the loop re-runs checkTimers; re-arming on a changed deadline cancels the old SQE first. Other backends keep the capped fallback unchanged. Tested on io_uring (native) and epoll (fallback): full suite green on both.
📝 WalkthroughWalkthroughThe io_uring backend now arms wall-clock timers natively via absolute-deadline ChangesNative wall-clock timer support
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 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 |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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 18-19: The USER_DATA_WALL_BOOT and USER_DATA_WALL_REAL constants
use fixed sentinel values that cannot distinguish between different timeout
instances, causing old timeout completions to interfere with current timeouts
and incorrectly clear wall_armed. Modify the user_data encoding to include a
generation counter alongside the clock tag by using the low 2 bits for the clock
identifier and the remaining bits for a generation counter. Increment the
generation counter when re-arming a timeout (around line 796), use the previous
generation value when canceling a timeout (around line 779), and add a
generation validation check before treating a timeout completion as current (in
the verification logic around lines 866-870) to ensure the arriving CQE matches
the currently armed timeout generation.
- Around line 809-811: The issue is that drainPending() continues to consume SQ
space even when internal SQEs need to be armed (wall timer or waker), causing a
cycle where critical re-arms fail repeatedly. Skip calling drainPending()
whenever wall_needs_rearm or waker_needs_rearm is set, so that SQ space is
reserved for the critical internal operations to get their SQEs on the next
poll. Apply this fix at all locations where drainPending() is called in the poll
function, including the areas around line 844-847 and 899-903 in addition to the
current location on line 811.
🪄 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: 2c4812f8-df3c-4ed5-ba93-4d2c87b03bf9
📒 Files selected for processing (4)
src/ev/backends/io_uring.zigsrc/ev/completion.zigsrc/ev/loop.zigsrc/ev/test/timer.zig
Two issues from the #516 review: - Generation race in the wall-timer user_data. Reusing a fixed sentinel per (re)arm meant a stale timeout CQE arriving after a re-arm could clear the current wall_armed and leak a duplicate SQE. user_data now uses the low bit to tell internal ops from *Completion pointers (which are aligned) and packs a kind + generation; a stale generation is ignored on the CQE. - drainPending starved the wall re-arm. The wall_needs_rearm / zero-timeout hack couldn't make progress because drainPending refills the SQ before the next checkTimers re-arms. Replaced with a fold fallback: syncWallTimer is now per-clock and returns whether it armed; on false the loop folds that clock's capped remaining into the poll timeout, so it re-evaluates within the cap regardless of SQ pressure.
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/ev/backends/io_uring.zig (1)
779-820: 🎯 Functional Correctness | 🔴 CriticalThe
IORING_TIMEOUT_BOOTTIMEandIORING_TIMEOUT_REALTIMEconstants are not defined in Zig's standard library and must be manually declared in this project.The arm/disarm + generation flow is sound, and
prep_timeout_removeis confirmed available instd.os.linux.io_uring_sqe. However, the code at lines 786 and 789 referenceslinux.IORING_TIMEOUT_BOOTTIMEandlinux.IORING_TIMEOUT_REALTIME, which the Zig stdlib does not provide. These constants (andIORING_TIMEOUT_ABSat line 815) need to be manually defined locally in your project—typically as:pub const IORING_TIMEOUT_BOOTTIME: u32 = 4; pub const IORING_TIMEOUT_REALTIME: u32 = 8; pub const IORING_TIMEOUT_ABS: u32 = 1;Ensure these definitions exist in your
linuxnamespace or wherever you're importing them from, or the code will not compile.🤖 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 779 - 820, The syncWallTimer function references three io_uring timeout constants (IORING_TIMEOUT_BOOTTIME, IORING_TIMEOUT_REALTIME, and IORING_TIMEOUT_ABS) at lines 786, 789, and 815 that are not provided by Zig's standard library. You need to define these constants in your project's linux namespace with the values IORING_TIMEOUT_BOOTTIME as 4, IORING_TIMEOUT_REALTIME as 8, and IORING_TIMEOUT_ABS as 1, then ensure the linux namespace in syncWallTimer can access these definitions. Add these constant definitions to whichever file or module provides the linux namespace that the io_uring backend is using.
🤖 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.
Outside diff comments:
In `@src/ev/backends/io_uring.zig`:
- Around line 779-820: The syncWallTimer function references three io_uring
timeout constants (IORING_TIMEOUT_BOOTTIME, IORING_TIMEOUT_REALTIME, and
IORING_TIMEOUT_ABS) at lines 786, 789, and 815 that are not provided by Zig's
standard library. You need to define these constants in your project's linux
namespace with the values IORING_TIMEOUT_BOOTTIME as 4, IORING_TIMEOUT_REALTIME
as 8, and IORING_TIMEOUT_ABS as 1, then ensure the linux namespace in
syncWallTimer can access these definitions. Add these constant definitions to
whichever file or module provides the linux namespace that the io_uring backend
is using.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 0ff7a481-42f5-4f34-bd7a-8b65fcb328dc
📒 Files selected for processing (2)
src/ev/backends/io_uring.zigsrc/ev/loop.zig
🚧 Files skipped from review as they are similar to previous changes (1)
- src/ev/loop.zig
Follows the per-clock timer heaps work (#515). With those, boot/real timers fire correctly on every backend via a capped poll-timeout fallback. This makes io_uring arm them natively so they're exact rather than bounded by the cap.
What
native_wall_timersbackend capability. When set,checkTimersstops folding boot/real deadlines into the poll timeout (no 10s cap) and instead hands the per-clock heap minimums tobackend.syncWallTimers(boot, real).IORING_OP_TIMEOUTSQE per active wall clock withIORING_TIMEOUT_ABS | BOOTTIME/REALTIME. The kernel re-evaluates these across suspend and clock steps, so boot/real fire exactly. A fired timeout (-ETIME) makespollreport a timeout so the loop re-runscheckTimers; re-arming on a changed deadline cancels the old SQE first.SQ-full handling
Mirrors the existing waker: if
armWallcan't get an SQE, it setswall_needs_rearmandpolluses a zero timeout so theenter2drains the SQ and the next scan re-arms. Per-timer armed state lives inwall_armed[idx]; the bool is only the "don't block" gate (an OR over the two timers), so a partial failure stays flagged and the already-armed timer is left untouched. Bounded to one non-blocking cycle, never a blocking wait or a spin.Kernel requirement
No extra gate: the ring already requires ≥6.1 (
DEFER_TASKRUN), and theBOOTTIME/REALTIMEtimeout flags are ≥5.15, so they're always available where this backend runs.Testing
Not covered
The "exact across suspend/clock-step" behavior relies on the kernel's hrtimer semantics and isn't unit-tested (suspend isn't reproducible in a unit test). epoll/kqueue/IOCP native arming remain future work.