Skip to content

fix(libkernel): scale the reported TSC frequency under APS5_TIME_SCALE - #12206

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
XairRrR:fix/libkernel-tsc-time-scale
Oct 29, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
XairRrR:fix/libkernel-tsc-time-scale

Conversation

@XairRrR

@XairRrR XairRrR commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

What

APS5_TIME_SCALE scaled only the ticks sceKernelReadTsc returns. A title that executes rdtsc itself and divides by sceKernelGetTscFrequency (or machdep.tsc_freq) saw real time, so the scale had no effect on it, and its own TSC disagreed with sceKernelReadTsc.

The scale now goes into the reported frequency instead: sceKernelGetTscFrequency returns the measured host frequency divided by the scale, and sceKernelReadTsc returns raw rdtsc. Ticks over the reported frequency follow the scale for both, and sceKernelGetProcessTimeCounter, which already counts scaled time at that frequency, stays consistent. The measurement moves to HostTscFrequency_nid_no_patch, which the JobManager worker spin uses so it keeps spinning for real microseconds.

Without APS5_TIME_SCALE nothing changes.

Tested

Arch Linux 7.2.11-zen, GCC 16.2.1, Ninja, Release, on main 3ca715e:

  • New guest_process_time_rdtsc and guest_process_time_rdtsc_scaled (scale 0.5): over a 50 ms sleep, native rdtsc ticks and sceKernelReadTsc ticks over the reported frequency must both come to the scaled time. With Time.cpp from main, guest_process_time_rdtsc_scaled fails: native rdtsc ticks over the reported TSC frequency do not follow the time scale.
  • The existing guest_process_time* tests, including test(libkernel): cover sceKernelReadTsc's rate against sceKernelGetTscFrequency #11594's guest_process_time_tsc_scaled, pass.
  • Full ctest -j8 (this branch together with my five other branches of today, on the same main): 2619 of 2620 pass; agc_driver_mesh_barycentric_emulation fails the same way on main on this GPU (the pixel shader did not emulate barycentrics).
  • tools/check_conventions.py --base origin/main passes.
  • Found while slowing PPSA05684 (Uncharted: Legacy of Thieves Collection) down with APS5_TIME_SCALE to chase a timing-dependent crash; no title behaviour is claimed to depend on it.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 4563 core/libs/prx/libkernel/CMakeLists.txt
PR 5021 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Time/Time.cpp
PR 6936 core/libs/prx/libkernel/CMakeLists.txt
PR 7192 core/libs/prx/libkernel/CMakeLists.txt
PR 7804 core/libs/prx/libkernel/Time/Time.cpp
PR 9156 core/libs/prx/libkernel/CMakeLists.txt
PR 9293 core/libs/prx/libkernel/CMakeLists.txt
PR 9297 core/libs/prx/libkernel/CMakeLists.txt
PR 9302 core/libs/prx/libkernel/CMakeLists.txt
PR 9326 core/libs/prx/libkernel/CMakeLists.txt
PR 9383 core/libs/prx/libkernel/CMakeLists.txt
PR 9389 core/libs/prx/libkernel/CMakeLists.txt
PR 9557 core/libs/prx/libkernel/CMakeLists.txt
PR 9566 core/libs/prx/libkernel/CMakeLists.txt
PR 10249 core/libs/prx/libSceJobManager/Export.cpp
PR 10645 core/libs/prx/libkernel/CMakeLists.txt
PR 11002 core/libs/prx/libkernel/CMakeLists.txt
PR 11625 core/libs/prx/libkernel/CMakeLists.txt

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The clock model is internally consistent: raw TSC ticks are paired with the scaled reported frequency, while the JobManager spin duration uses the host frequency. The new test checks native rdtsc, sceKernelReadTsc, and the reported frequency together. I found no separate logic issue by static inspection. This branch is 60 commits behind current main; please rebase and rerun the time tests (including scaled and unscaled) on the rebased head before approval.

@JJenkx

JJenkx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

A Windows run, since the description lists none. Head e80ed4b2b0 was merged onto main 82d5f01f28 with no conflicts. Setup: Windows 11 Enterprise 10.0.22621, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON.

libs builds, and all 8 guest_process_time* tests pass in each of 3 runs, including the *_scaled variants that run under APS5_TIME_SCALE.

AI-assisted: yes (Claude Code).

@maskym

maskym commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Windows result for head e80ed4b2b0, merged into main 87b810a1db (the PR's Tested section names no Windows run).

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. Build clean.

The tree also carried the open PRs #12290, #12288, #12285, #12269, #12265, #12232, #12201, #12199, #12197, #12165, #12146, all merged cleanly, so the run is a joint one; the tests of this PR are guest_process_time (0.08 s), guest_process_time_counter (0.08 s), guest_process_time_counter_scaled (0.08 s), guest_process_time_rdtsc (0.09 s), guest_process_time_rdtsc_scaled (0.09 s), guest_process_time_scaled (0.07 s), guest_process_time_tsc (0.15 s), guest_process_time_tsc_scaled (0.14 s), all passed (every test registered from the tree's changed test files: 38/38). Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

Titles that execute rdtsc themselves ignored APS5_TIME_SCALE, because
only sceKernelReadTsc scaled its ticks. The reported TSC frequency is
now divided by the scale and sceKernelReadTsc returns raw ticks, so
ticks over the reported frequency follow the scale for both. The
JobManager worker spin keeps real time through the host frequency.

AI-assisted: yes
@XairRrR
XairRrR force-pushed the fix/libkernel-tsc-time-scale branch from e80ed4b to 5da3632 Compare October 28, 2026 19:01
@XairRrR

XairRrR commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main e4da19a (head 5da3632), no conflicts. The eight guest_process_time* tests, scaled and unscaled, pass on the rebased head (in the combined validation run below, with #12207, #12208, #12210 and #12238 also merged).

Rebased on main e4da19a. Arch Linux 7.2.11-zen, GCC 16.2.1, Release, RTX 4070 Ti SUPER (NVIDIA 615.78.08), Khronos validation layers 1.4.363. VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 VK_LOADER_DEBUG=error,warn,layer ctest -R '^(agc|guest_video_out|video_out|guest_process_time)' on main plus #12206, #12207, #12208, #12210 and #12238 rebased: 1145/1145 pass. The loader inserted the layer 1000 times, no log line says it was not found, and the only VUID is VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605 (18 times), the one maskym's run reports on main (#6782). I did not run main alone on Linux; maskym's paired Windows run above covers that side.

AI-assisted: yes

@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the scaled frequency against sceKernelGetProcessTimeCounter, machdep.tsc_freq and the JobManager spin (still on the host frequency); guest_process_time_rdtsc_scaled fails on the old Time.cpp, and the guest_process_time* tests passed on Windows per @JJenkx and @maskym; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants