Commit 31c5780
fix: calibrate TSC frequency instead of scraping /proc/cpuinfo (#392)
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>1 parent 6037e62 commit 31c5780
1 file changed
Lines changed: 52 additions & 36 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
99 | 108 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
104 | 118 | | |
105 | 119 | | |
106 | 120 | | |
107 | 121 | | |
108 | 122 | | |
109 | | - | |
110 | 123 | | |
111 | 124 | | |
112 | | - | |
113 | | - | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
114 | 129 | | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
138 | 160 | | |
139 | 161 | | |
140 | 162 | | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | 163 | | |
148 | 164 | | |
149 | 165 | | |
| |||
0 commit comments