Skip to content

feat(main): warn at startup when the kernel clocksource is slow - #1682

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
feat/clocksource-warning
Sep 29, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
feat/clocksource-warning

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Summary

Addresses #500 the way @Geal proposed there: a configuration check at startup. Sōzu calls Instant::now() throughout the request path (246 call sites in lib/src). When the kernel clocksource has no vDSO mode, each call becomes a clock_gettime syscall; the profile in #500 shows ~59% of CPU time under sys_clock_gettime -> read_hpet.

The main process now reads /sys/devices/system/clocksource/clocksource0/current_clocksource once, right after logging is set up in begin_main_process (bin/src/command/mod.rs), and emits one warn! with the commands to check and change it when the source is not vDSO-capable. Workers are not checked: the clocksource is system-wide, so one check per start is enough. An unreadable file is logged at debug level only, and non-Linux targets compile the check to a no-op.

  • slow_clocksource_warning(&str) -> Option<String> (bin/src/util.rs): pure decision, trims the sysfs newline, empty input yields no verdict.
  • warn_on_slow_clocksource() (bin/src/util.rs): thin I/O wrapper.
  • doc/debugging_strategies.md: new "Slow kernel clocksource" section; CHANGELOG.md Unreleased entry.

No new dependency. No protocol or security impact: one sysfs read and one log line at startup.

Why this list of fast clocksources

A clocksource is read from the vDSO only when it sets vdso_clock_mode to something other than VDSO_CLOCKMODE_NONE. Checked against Linux master at 72d3fcf802c4:

name source vDSO mode
tsc arch/x86/kernel/tsc.c:1177 VDSO_CLOCKMODE_TSC (L1163, L1186)
kvm-clock arch/x86/kernel/kvmclock.c:180 VDSO_CLOCKMODE_PVCLOCK, only with PVCLOCK_TSC_STABLE_BIT (L270-279)
xen arch/x86/xen/time.c VDSO_CLOCKMODE_PVCLOCK, only with PVCLOCK_TSC_STABLE_BIT (L470-485)
hyperv_clocksource_tsc_page drivers/clocksource/hyperv_timer.c:401 VDSO_CLOCKMODE_HVCLOCK where the arch has it (L409-413)
arch_sys_counter drivers/clocksource/arm_arch_timer.c:147 VDSO_CLOCKMODE_ARCHTIMER unless an erratum disables it (L59, L497)
riscv_clocksource drivers/clocksource/timer-riscv.c:96 VDSO_CLOCKMODE_ARCHTIMER (L101)
hpet arch/x86/kernel/hpet.c:853 none (no vdso_clock_mode in the file)
acpi_pm drivers/clocksource/acpi_pm.c:97 none

xen is in the fast list because it has the same conditional vDSO path as kvm-clock; flagging it would warn on modern Xen guests with a stable TSC. The classification is by name, so it is a heuristic: a listed source that lost its vDSO mode at runtime (unstable TSC, arm64 erratum) is not flagged.

Tests (red, then green)

Tests written first against a stub returning None with an empty fast list:

$ cargo test -p sozu --lib slow_clocksource
test result: FAILED. 0 passed; 3 failed; 0 ignored; 0 measured; 116 filtered out
    util::tests::slow_clocksource_warning_accepts_vdso_capable_sources
    util::tests::slow_clocksource_warning_flags_syscall_only_sources
    util::tests::slow_clocksource_warning_trims_the_sysfs_newline

After the implementation:

test util::tests::slow_clocksource_warning_accepts_vdso_capable_sources ... ok
test util::tests::slow_clocksource_warning_flags_syscall_only_sources ... ok
test util::tests::slow_clocksource_warning_trims_the_sysfs_newline ... ok
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 116 filtered out

Commands run (all exit 0)

  • cargo build --all-features --locked
  • cargo clippy --all-features --all-targets --locked -- -D warnings
  • cargo +nightly fmt --all -- --check
  • cargo test -p sozu --locked (119 passed in the lib and bin test targets)
  • python3 .github/scripts/check_doc_citations.py --self-test, --root ., --base origin/main, --audit --root .

Closes #500

Sōzu calls Instant::now() throughout the request path. When the kernel
clocksource has no vDSO mode (hpet, acpi_pm, jiffies), every call is a
clock_gettime syscall; the profile in #500 shows ~59% of CPU time in
sys_clock_gettime -> read_hpet.

The main process now reads
/sys/devices/system/clocksource/clocksource0/current_clocksource once at
startup and logs one warning, with the commands to check and change it,
unless the source is one that can set a vdso_clock_mode other than
VDSO_CLOCKMODE_NONE: tsc, kvm-clock, xen, hyperv_clocksource_tsc_page,
arch_sys_counter or riscv_clocksource. An unreadable file is logged at
debug level only; non-Linux targets skip the check.

The decision lives in the pure slow_clocksource_warning function, unit
tested for fast sources, slow sources and sysfs whitespace trimming.

Closes #500

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 488eff4 into main Sep 29, 2026
51 of 52 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the feat/clocksource-warning branch September 29, 2026 14:33
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.

overhead of time measurements

1 participant