Calibrate TSC frequency instead of scraping /proc/cpuinfo#392
Open
bushidocodes wants to merge 1 commit into
Open
Calibrate TSC frequency instead of scraping /proc/cpuinfo#392bushidocodes wants to merge 1 commit into
bushidocodes wants to merge 1 commit into
Conversation
The runtime times everything with __getcycles() (rdtsc), the invariant TSC, which ticks at a fixed rate independent of the core's current p-state. But runtime_get_processor_speed_MHz() derived its cycles<->us conversion factor from the instantaneous "cpu MHz" in /proc/cpuinfo, which reports the core's current operating frequency. That value varies under frequency scaling and TurboBoost (and exhibits natural per-read variance on recent Intel parts even under the performance governor), so the conversion factor was both wrong (it is not the TSC rate) and unstable across startups, corrupting deadline math. The previous workaround was hardcoding a value via SLEDGE_PROC_MHZ. Calibrate the TSC rate directly instead: count rdtsc cycles over timed CLOCK_MONOTONIC_RAW windows (5 x 20 ms) and take the maximum rate. A deschedule during a window only ever stretches the measured wall time, lowering the apparent rate, so the max rejects that noise. This measures the actual TSC frequency and is stable regardless of governor/p-state. The SLEDGE_PROC_MHZ override is preserved. Verified: calibrates 3793 MHz, stable across 6 restarts, matching an independent 1-second rdtsc measurement (3792.9 MHz); override and end-to-end request serving both work. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The runtime times everything with
__getcycles()—rdtsc, the invariant TSC, which ticks at a fixed rate regardless of the core's current p-state. Butruntime_get_processor_speed_MHz()derived its cycles↔microseconds conversion factor by scraping the instantaneouscpu MHzfrom/proc/cpuinfo, which reports the core's current operating frequency. That value varies under frequency scaling / TurboBoost (and shows natural per-read variance on recent Intel parts even under the performance governor), so the factor was both wrong (it is not the TSC rate) and unstable across startups — corrupting deadline math. The prior workaround was hardcoding a value viaSLEDGE_PROC_MHZ.Fix
Calibrate the TSC rate directly: count
rdtsccycles over timedCLOCK_MONOTONIC_RAWwindows (5 × 20 ms) and take the maximum rate. A deschedule during a window only ever stretches the measured wall time (lowering the apparent rate), so the max rejects that noise. This measures the actual TSC frequency and is stable regardless of governor/p-state. TheSLEDGE_PROC_MHZoverride is preserved.Single-file change (
runtime/src/main.c).Verification
Built and run in the Docker dev environment:
rdtscmeasurement reads 3792.9 MHz (~0.003% error).SLEDGE_PROC_MHZ=2100reports 2100 MHz.Closes #246.
🤖 Generated with Claude Code