Repository navigation
fix(libkernel): measure the TSC frequency on one CPU - #9461
Conversation
MarcZX10
left a comment
There was a problem hiding this comment.
The fix pins the calibration interval to one CPU and restores the original affinity with RAII; I reviewed the Windows and Linux paths and the reported VM reproduction. The new test range also catches both the prior zero reading and the reported implausible high value. The author reports 10/10 Windows VM passes after the change and 3/3 Linux runs across the relevant tests; hosted build/test jobs are all skipped for this path, while eligibility, conventions, intake, labels and report pass. One non-blocking edge case: if either platform’s affinity query/set call fails, the helper silently leaves calibration unpinned and continues with the original potentially cross-CPU measurement. Please consider making that failure path explicit or documenting why it cannot occur on supported hosts.
|
Thanks @MarcZX10 for the review. On the failure path: the pin always asks for the CPU the thread is already running on, and that CPU is always in its allowed set (the process affinity mask on Windows; the thread's cpuset, including a cgroup's, on Linux). So I don't know of a host where the set fails. If it did, falling back to the unpinned measurement gives the same result AI-assisted: yes (Claude Code) |
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
MarcZX10
left a comment
There was a problem hiding this comment.
The affinity edge case raised in the prior review is now addressed in the debt note, and the author’s updated Windows VM/Linux results are recorded. The checked head still uses base a6dcea927, 47 upstream commits behind current main (110a18ba01). Please rebase/update and rerun the focused CPU-frequency/current-CPU and process-time tests plus conventions and overlap/debt checks on current main. The overlap report has incomplete comparisons in libkernel debt and a Time.cpp warning; please verify those entries remain additive after the update. I did not rerun local tests.
7cae619 to
c8bb2bd
Compare
|
@MarcZX10 rebased onto On the overlap entries: #9059 adds one line at the same place in AI-assisted: yes (Claude Code) |
|
A full AI-assisted: yes (Claude Code) |
c8bb2bd to
65e0c25
Compare
|
@MarcZX10 rebased onto current Overlap on the new base ( AI-assisted: yes (Claude Code) |
sceKernelGetTscFrequency times 20 ms of TSC ticks against the monotonic clock. A thread that moves to another CPU during the sleep reads its end value from another TSC. Where the TSCs of the CPUs differ, as on a Windows VM whose vCPU 0 counts about 3.8 billion ticks ahead of the others, the frequency came out as 0 or as 180 GHz. Pin the calibrating thread to the CPU it runs on and restore its affinity afterwards. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ment Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
65e0c25 to
de5306d
Compare
|
@MarcZX10
AI-assisted: yes (Claude Code) |
Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.
|
Checked the Windows and Linux pin/restore paths, the unpinned fallback (recorded in the libkernel debt file) and the new 100 MHz–10 GHz range check in |
|
Thanks for checking both pin paths and the range check, and for merging, @adriadam10. AI-assisted: yes (Claude Code) |
What
sceKernelGetTscFrequency(andsceKernelGetCpuFrequency, which returns the same value) measures the TSC rate once per process. It reads the TSC and the monotonic clock, sleeps 20 ms and reads both again. If the thread moves to another CPU during the sleep, the second TSC value comes from another CPU's counter. Where those counters aren't in step, the result is wrong for the rest of the process.On a Windows 11 VM (KVM, Intel i7-7700 host, 4 vCPUs), vCPU 0's TSC runs about 3.76 billion ticks (about 1.04 s at 3.6 GHz) ahead of vCPUs 1 to 3. I repeated the same 20 ms measurement 12 times there, letting the thread move freely:
On
main,guest_current_cpufails in 6 of 10 runs there, becausesceKernelGetCpuFrequency()returns 0. A title that divides by the frequency, or converts TSC deltas to time with it, misbehaves for the whole session.The calibration now pins its thread to the CPU it is running on, for the 20 ms, and restores the previous affinity afterwards. On Windows that's
SetThreadAffinityMaskwithGetCurrentProcessorNumber; on Linux,sched_getcpuwithpthread_setaffinity_np. macOS is unchanged.guest_current_cpualso checks that the frequency lies between 100 MHz and 10 GHz, so the 180 GHz case fails the test as well as 0.Guest code that runs
rdtscitself still sees the skew on such a host; this only fixes the reported frequency.If the pin can't be set, the measurement runs unpinned, as on
main; the libkernel debt file records that (7cae619, after @MarcZX10's review).Tested
Rebased onto
main110a18b, with no conflicts, as headc8bb2bd31b.guest_current_cpu,guest_system_configurationand the fourguest_process_timetests pass 3 of 3 on Linux (Ubuntu 26.04.1, GCC 15.2, Release), andtools/check_conventions.pyis clean. The overlap scan's only line-level entry is feat(libkernel): implement __tls_get_addr #9059, which also adds one line at the same place indocs/dev/debt/libkernel.md. Both are additions, so whichever lands second keeps both. The other entries are PRs that already conflict withmain. The results below are from the pre-rebase head.Windows 11 VM (above), WinLibs GCC 15.2.0, Release, head
c8bb2bd31bagainst its basemain110a18b, 5 runs each. These three tests were the only aborts (0xc0000409) in a fullctestofmaind6732b5 on this VM:guest_system_configuration(machdep.tsc_freqis 0):mainfails 5 of 5.guest_current_cpu:mainfails 4 of 5.guest_ampr_markers:mainfails 3 of 5, at its check thatsceAmprCommandBufferWriteAddressFromTimeCounterwrote a nonzero time. That value issceKernelGetProcessTimeCounter(), which scales the elapsed time by the TSC frequency, so it's 0 when the frequency is.Windows 11 VM (above), WinLibs GCC 15.2.0, Release,
guest_current_cpu, 10 runs each:maina6dcea9 fails 6 of 10 runs (sceKernelGetCpuFrequency()is 0;main's copy of the test only checks for more than 0). With this change, 10 of 10 pass, the new range check included.Linux (Ubuntu 26.04.1, GCC 15.2, Release), where the TSCs are in step:
guest_current_cpu, the 4guest_process_time*tests andguest_system_configurationpass 3 of 3.Not tested: macOS (no change there), a bare-metal host with unsynchronized TSCs.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code