Skip to content

ev/epoll: use epoll_pwait2 for nanosecond timeout precision - #500

Merged
lalinsky merged 1 commit into
mainfrom
fix/epoll-pwait2
Jun 21, 2026
Merged

lalinsky merged 1 commit into
mainfrom
fix/epoll-pwait2

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

Stacked on #499 (fix/timer-timeout-rounding) — review/merge that first; this PR's diff against its base is only the epoll change.

Problem

epoll_wait accepts a millisecond timeout, so even with the ceiling fix in #499 a sub-millisecond timer deadline is rounded up to a full millisecond, adding up to ~1ms of latency/jitter to every short timer.

Fix

Use epoll_pwait2 (Linux 5.11+), which takes a nanosecond timespec, so the loop sleeps exactly until the next deadline. Prefer it, and the first time it returns ENOSYS (kernel < 5.11), latch a per-loop flag and fall back to epoll_wait for the rest of that loop's lifetime.

  • New epoll_pwait2 wrapper in src/os/linux.zig (uses std.os.linux.kernel_timespec; the ms epoll_wait stays via std).
  • waitEvents in the epoll backend does the prefer/fallback.

Verification

  • epoll timer/cancel tests pass (23/23).
  • strace on a 6.6 kernel: 2284 epoll_pwait2, 0 epoll_pwait (fallback only on ENOSYS).
  • Default io_uring build still compiles.

Refs #493

@coderabbitai

coderabbitai Bot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a43917d6-14df-42ee-9f1c-46e19d920efa

📥 Commits

Reviewing files that changed from the base of the PR and between 7a6f799 and cbf14a7.

📒 Files selected for processing (2)
  • src/ev/backends/epoll.zig
  • src/os/linux.zig
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/os/linux.zig
  • src/ev/backends/epoll.zig

📝 Walkthrough

Walkthrough

Adds epoll_pwait2 nanosecond-precision timeout support to the epoll backend. A raw syscall wrapper is added in src/os/linux.zig, and the epoll backend gains a waitEvents helper with a runtime ENOSYS fallback to the existing millisecond epoll_wait path.

Changes

Nanosecond-precision epoll timeout via epoll_pwait2

Layer / File(s) Summary
epoll_pwait2 syscall wrapper
src/os/linux.zig
Exports epoll_pwait2 using linux.syscall6 with null sigmask and NSIG/8 mask size. Returns raw usize; the caller handles errno. Null timeout means block indefinitely; older kernels return ENOSYS.
epoll backend waitEvents integration
src/ev/backends/epoll.zig
Adds os_linux import and epoll_pwait2_supported bool field. New private waitEvents converts a Duration to kernel_timespec, calls epoll_pwait2, maps EINTR to zero events, and permanently flips epoll_pwait2_supported to false on ENOSYS, then retries with epoll_wait. poll now calls waitEvents instead of the prior inline epoll_wait.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A nanosecond knocks, the kernel hears,
pwait2 steps in after years.
Old boxes shrug and send ENOSYS back,
The fallback flips — no panic, no crack.
Precision wins where milliseconds blur. ⏱️

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding epoll_pwait2 support to the epoll backend for nanosecond-precision timeouts.
Description check ✅ Passed The description is directly related to the changeset, explaining the problem (millisecond rounding latency), the solution (epoll_pwait2 with nanosecond precision), implementation details, and verification results.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/epoll-pwait2

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Base automatically changed from fix/timer-timeout-rounding to main June 21, 2026 16:51
epoll_wait only accepts a millisecond timeout, so a sub-millisecond timer
deadline rounds to the nearest millisecond. Use epoll_pwait2 (Linux 5.11+),
which takes a nanosecond timespec, so the loop sleeps exactly until the next
deadline instead of waking early.

Prefer epoll_pwait2 and, the first time it returns ENOSYS (kernel < 5.11),
latch a flag and fall back to epoll_wait for the rest of the loop's lifetime.

Refs #493

Co-Authored-By: Claude Opus 4.8 <[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.

1 participant