fix(libkernel): measure the TSC frequency on one CPU #9461
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9461"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T22:40:03Z.
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 (7cae6190d4, after @MarcZX10's review).Tested
Rebased onto
main110a18ba01, 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 #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 basemain110a18ba01, 5 runs each. These three tests were the only aborts (0xc0000409) in a fullctestofmaind6732b51a6on 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:maina6dcea9279fails 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
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T22:57:39Z.
Review: APPROVED
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.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T23:06:38Z.
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
maingives today. I documented that fallback in the libkernel debt file (7cae6190d4) instead of adding a retry for a case I can't produce.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:34:23Z.
Review: DISMISSED
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.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-22T04:53:09Z.
@MarcZX10 rebased onto
main110a18ba01asc8bb2bd31b, with no conflicts. The focused CPU-frequency, current-CPU and process-time tests pass 3 of 3, and conventions are clean.On the overlap entries: #9059 adds one line at the same place in
docs/dev/debt/libkernel.mdthat this PR adds one at. Both are additions, so the second to land keeps both. TheTime.cppand other debt-file entries are PRs that already conflict withmain, and this PR'sTime.cppchange is the one class plus the one line insceKernelGetTscFrequency. The Tested section names the new head.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-22T09:24:24Z.
A full
ctestofmainon the Windows 11 VM from the description turned up two more tests this fixes:guest_system_configurationandguest_ampr_markersabort there for the same reason asguest_current_cpu. Against this PR's base (main110a18ba01), 5 runs each:mainfailsguest_system_configuration5 of 5,guest_current_cpu4 of 5 andguest_ampr_markers3 of 5. The headc8bb2bd31bpasses all three 5 of 5.guest_ampr_markerschecks the AMPR time-counter write, which issceKernelGetProcessTimeCounter(), the elapsed time scaled by the TSC frequency, so it's 0 when the frequency is. Added to Tested.AI-assisted: yes (Claude Code)