Repository navigation
runtime: scheduler event counters and a metrics monitor thread - #636
Conversation
Per-executor counters for the events every tuning discussion this month has needed numbers for: parks split by doze/full, searcher elections, steal attempts/hits and how often the hint's victim paid off, dispatched wake batches and their sizes, batched wake claims, and load-shed wakes. Summed across executors by Runtime.schedulerMetrics(); the counters are written without atomics by their owning threads, so the snapshot is approximate while executors run and exact once quiesced. Counters sit only on cold scheduler paths (a park, a steal scan, a drain pass), never on the per-push hot path, so they are compiled in by default; -Dscheduler-metrics=false strips them to nothing.
The metrics_log_interval runtime option starts a thread that logs the scheduler counters' per-interval deltas at that cadence. A dedicated thread rather than an executor timer, so it keeps reporting even when every executor is wedged or asleep, which is exactly when the numbers matter most. Stopped and joined first thing in deinit, since it reads executor state; a futex wait bounds each sleep so shutdown never waits out an interval. Warns and stays off when the counters are compiled out.
Build option names should be valid Zig identifiers, matching the zio_options field they populate.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe runtime adds configurable scheduler counters, exposes aggregated metrics, and optionally logs interval deltas through a monitor thread. A build option controls compilation. Runtime options control monitoring. Tests cover collection and monitor lifecycle. ChangesScheduler metrics
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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: 4
🤖 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 `@docs/changelog.md`:
- Around line 7-14: Align the changelog’s “-Dscheduler_metrics=false strips
them” statement with the implementation: update the scheduler metrics
compile-time gating so disabled builds remove SchedulerMetrics storage and
aggregation, or revise the changelog to describe the behavior accurately. Focus
on the SchedulerMetrics definitions and Runtime.schedulerMetrics aggregation
path.
In `@src/runtime.zig`:
- Around line 345-346: Update the public comment describing the scheduler event
counters build option in runtime.zig to use the registered name
scheduler_metrics instead of scheduler-metrics. Keep the surrounding
documentation unchanged.
- Around line 1470-1475: Update the metrics monitor setup around
runMetricsMonitor to handle metrics_enabled and builtin.single_threaded
separately: retain the compiled-out warning only when metrics are disabled, and
log that the monitor thread is unavailable for single-threaded builds when
metrics are enabled. Update the runtime-option documentation for
metrics_log_interval to document this single-threaded constraint.
- Around line 449-450: Update Executor.metrics in src/runtime.zig to use
per-field atomic counters when scheduler metrics are enabled, while selecting
zero/void storage when -Dscheduler_metrics=false so the feature compiles out as
documented. Update all metric increments, Runtime.schedulerMetrics(), and
monitor reads to use the conditional storage consistently. In docs/changelog.md
lines 7-14, document the scheduler metric storage and compile-out behavior
change.
🪄 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 Plus
Run ID: 0b665e37-bac3-4c92-b1d9-7e8c97e9ffab
📒 Files selected for processing (4)
build.zigdocs/changelog.mdsrc/os/root.zigsrc/runtime.zig
With scheduler_metrics=false the per-executor counter struct is now zero-bit and the aggregation loop compiles away, matching the changelog's strip claim. The metrics_enabled doc comment names the registered option (scheduler_metrics, not scheduler-metrics), and a single-threaded build with metrics_log_interval set now warns that the monitor thread is unavailable rather than claiming the counters are compiled out. Reported by review on #636.
NetBSD was the one supported POSIX target whose Futex resolved to void: FreeBSD, OpenBSD, DragonFly and Darwin all have implementations, and anything built on os.Futex (the metrics monitor in #636 being the first) failed to compile there. NetBSD >= 9 provides a Linux-compatible futex through the __futex syscall (FUTEX_WAIT with a relative timeout, FUTEX_WAKE, FUTEX_PRIVATE_FLAG). libc ships no ___futex stub, so the call goes through the generic syscall(2) entry point with SYS___futex (166), which the libc symbol list does provide. Also adds a timedWait test (expiry and already-changed-value paths) so CI exercises the syscall on every futex platform, not just the wake path the existing test covers.
Per-executor counters for the scheduler events every tuning discussion has needed numbers for, plus an optional monitor thread that logs them periodically.
Counters
Runtime.schedulerMetrics()returns a summed snapshot of ten per-executor counters: parks split by doze/full, searcher elections, steal attempts/hits and how often the hint's victim paid off, dispatched wake batches and their sizes, batched wake claims, and load-shed wakes. Writes are plain increments by the owning thread, so the snapshot is approximate while executors run and exact once quiesced.Counters sit only on cold scheduler paths (a park entry, a steal scan, a drain pass), never on the per-push hot path, so they are compiled in by default;
-Dscheduler_metrics=falsestrips them to nothing (the option name is a valid Zig identifier, matching thezio_optionsfield; the older hyphenated options will be renamed separately). Ping-pong measures identically with counters on.Monitor thread
The
metrics_log_intervalruntime option starts a thread that logs the counters' per-interval deltas at that cadence:A dedicated thread rather than an executor timer, so it keeps reporting even when every executor is wedged or asleep, which is when the numbers matter most. Stopped and joined first thing in
deinit(it reads executor state); the sleep is a futex timed wait on the stop word, so shutdown never waits out an interval. Warns and stays off if the counters are compiled out.Testing
Full suite with metrics on (both new tests included: counter assertions through an
ev.Asyncwait that provably crosses the dispatched queue, and a monitor lifecycle test at 5ms cadence), 601/601 with-Dscheduler_metrics=false, epoll suite, Windows cross-compile.First intended use: watching steal/drain rates on the tcp pipe workload to chase the ~4% lat/pipe question from #633.