test(libkernel): compare Windows thread CPU clocks with the process cycle time #23756

Merged
ShootanoJumper merged 2 commits from upstream-pr/23756 into main 2026-11-20 04:57:28 +00:00
ShootanoJumper commented 2026-11-19 15:58:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ShootanoJumper on 2026-11-19T15:58:54Z.


What

guest_thread_cpu_clock fails intermittently on Windows on main (ed1def4dd4). The failing check is the lower bound in DistinctReadingsWhileBurning:

Require(previous - start >= process - process / 10 - 2 * coarseClockStep);

Since #23238, the thread CPU clocks read QueryThreadCycleTime when the TSC is invariant. process still comes from clock 2, though, which reads GetProcessTimes. That clock is sampled at the scheduler tick, and on a busy machine it over-counts by far more than two ticks. In a standalone program running the test's 200 ms burn 150 times on a Ryzen 7 5700X while a build was running:

  • GetProcessTimes read ahead of wall time in 106 of 150 burns. The thread cycle clock never did.
  • GetProcessTimes read up to 144 ms more than the thread cycle clock, and the test's check failed in 20–38 of 150 burns.

On Windows with an invariant TSC, the test now takes the reference from QueryProcessCycleTime, converted with the same HostTscFrequency_nid_no_patch. Elsewhere it still uses clock 2. The tolerances are unchanged, and no product code changes.

The second commit fixes the same mismatch in reverse, found by 4le22io. ProcessClocksNameOnlyTheGuest burned 200 ms on the thread clock and then required the tick-sampled process clock to have advanced at least 150 ms, which it can fall short of under load. It now burns on the process clock itself.

Changing clock 2 itself to cycle time doesn't work: GuestFreebsdClocks requires VIRTUAL ≤ PROF ≤ PROCESS_CPUTIME, and VIRTUAL (user time only) can only come from GetProcessTimes.

The fix-libkernel-windows-cpu-clock debt note says a thread clock and the process clocks "can differ by about one step". Under load I measured up to about 9 steps. I left the note alone because it's still a pending fragment.

Tested

Windows 11 23H2 (22631), AMD Ryzen 7 5700X, WinLibs GCC 15.2.0 r7, Release, main ed1def4dd4, with a parallel build running on the machine:

pass fail
guest_thread_cpu_clock on main 32 8
with this PR 40 0

Under a 16-thread spinning load, each variant run alone 100 times: main 16 failures, first commit 2, both commits 0.

The full ctest run on main (--parallel 6) also failed guest_thread_cpu_clock with 0xc0000409 (abort()). Not tested on Linux or macOS, where the changed path doesn't apply (#ifdef _WIN32).

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5398331675 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23756). Originally posted by [ShootanoJumper](https://github.com/ShootanoJumper) on 2026-11-19T15:58:54Z. --- ### What `guest_thread_cpu_clock` fails intermittently on Windows on `main` (`ed1def4dd4`). The failing check is the lower bound in `DistinctReadingsWhileBurning`: ``` Require(previous - start >= process - process / 10 - 2 * coarseClockStep); ``` Since #23238, the thread CPU clocks read `QueryThreadCycleTime` when the TSC is invariant. `process` still comes from clock 2, though, which reads `GetProcessTimes`. That clock is sampled at the scheduler tick, and on a busy machine it over-counts by far more than two ticks. In a standalone program running the test's 200 ms burn 150 times on a Ryzen 7 5700X while a build was running: - `GetProcessTimes` read **ahead of wall time** in 106 of 150 burns. The thread cycle clock never did. - `GetProcessTimes` read up to 144 ms more than the thread cycle clock, and the test's check failed in 20–38 of 150 burns. On Windows with an invariant TSC, the test now takes the reference from `QueryProcessCycleTime`, converted with the same `HostTscFrequency_nid_no_patch`. Elsewhere it still uses clock 2. The tolerances are unchanged, and no product code changes. The second commit fixes the same mismatch in reverse, found by 4le22io. `ProcessClocksNameOnlyTheGuest` burned 200 ms on the thread clock and then required the tick-sampled process clock to have advanced at least 150 ms, which it can fall short of under load. It now burns on the process clock itself. Changing clock 2 itself to cycle time doesn't work: `GuestFreebsdClocks` requires VIRTUAL ≤ PROF ≤ PROCESS_CPUTIME, and VIRTUAL (user time only) can only come from `GetProcessTimes`. The `fix-libkernel-windows-cpu-clock` debt note says a thread clock and the process clocks "can differ by about one step". Under load I measured up to about 9 steps. I left the note alone because it's still a pending fragment. ### Tested Windows 11 23H2 (22631), AMD Ryzen 7 5700X, WinLibs GCC 15.2.0 r7, Release, `main` `ed1def4dd4`, with a parallel build running on the machine: | | pass | fail | |---|---|---| | `guest_thread_cpu_clock` on main | 32 | 8 | | with this PR | 40 | 0 | Under a 16-thread spinning load, each variant run alone 100 times: main 16 failures, first commit 2, both commits 0. The full ctest run on `main` (`--parallel 6`) also failed `guest_thread_cpu_clock` with `0xc0000409` (`abort()`). Not tested on Linux or macOS, where the changed path doesn't apply (`#ifdef _WIN32`). ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
PrivacyOnion commented 2026-11-19 16:11:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T16:11:22Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 41f216c425:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_thread_cpu_clock_tests built cleanly.
  • CTest (guest_thread_cpu_clock):
    1/1 Test #5615: guest_thread_cpu_clock ...........   Passed    1.75 sec
    100% tests passed, 0 tests failed out of 1
    
  • Code reading:
    • Verified ProcessCpuNanos Windows implementation in core/libs/tests/GuestThreadCpuClock.cpp: HostHasInvariantTsc() guard, QueryProcessCycleTime return check, and tsc-to-nanos arithmetic prevent 64-bit integer overflow by computing (cycles / frequency * 1e9 + cycles % frequency * 1e9 / frequency).
    • Verified non-Windows and non-invariant TSC fallback in ProcessCpuNanos returns Nanos(processCpuClock) with processCpuClock = 2, preserving previous behavior on Linux/other platforms.
    • Verified DistinctReadingsWhileBurning lower and upper bound assertions in core/libs/tests/GuestThreadCpuClock.cpp accurately compare thread clock advancement against process elapsed time and monotonic wall clock.
    • Verified compliance with AGENTS.md conventions: no prohibited inline comments added, no unhandled stub exports, no special-cased title IDs, and no unwanted files added.
    • Verified movement of HostHasInvariantTsc above ProcessCpuNanos ensures valid declaration order and clean C++ compilation across MSVC and GCC.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5978775371 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23756#pullrequestreview-5978775371). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-19T16:11:22Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `41f216c425`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_thread_cpu_clock_tests` built cleanly. - **CTest** (`guest_thread_cpu_clock`): ``` 1/1 Test #5615: guest_thread_cpu_clock ........... Passed 1.75 sec 100% tests passed, 0 tests failed out of 1 ``` - **Code reading**: - Verified ProcessCpuNanos Windows implementation in core/libs/tests/GuestThreadCpuClock.cpp: HostHasInvariantTsc() guard, QueryProcessCycleTime return check, and tsc-to-nanos arithmetic prevent 64-bit integer overflow by computing (cycles / frequency * 1e9 + cycles % frequency * 1e9 / frequency). - Verified non-Windows and non-invariant TSC fallback in ProcessCpuNanos returns Nanos(processCpuClock) with processCpuClock = 2, preserving previous behavior on Linux/other platforms. - Verified DistinctReadingsWhileBurning lower and upper bound assertions in core/libs/tests/GuestThreadCpuClock.cpp accurately compare thread clock advancement against process elapsed time and monotonic wall clock. - Verified compliance with AGENTS.md conventions: no prohibited inline comments added, no unhandled stub exports, no special-cased title IDs, and no unwanted files added. - Verified movement of HostHasInvariantTsc above ProcessCpuNanos ensures valid declaration order and clean C++ compilation across MSVC and GCC. Looks Good To Me
MarcZX10 commented 2026-11-19 16:17:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T16:17:48Z.

Review: COMMENTED


Reviewed — comments

The source file and author’s rationale were reviewed at head 41f216c425630a126ec6b688d7116e8f76a34467; the prepared diff contains no hunks, so I could not verify which lines changed.

What I ran

Inspected the prepared description, commit list, manifest, and diff, then read core/libs/tests/GuestThreadCpuClock.cpp and targeted Windows clock implementation context through the repository reader. No tests or builds were run.

Results

Check Result Note
Source review passed Test source and clock conversion context read
Diff review not run Prepared diff has no hunks
Tests not run No test command was run

Negative control

Not run; this review did not execute tests.

Findings

Blocking

None identified in the source inspected.

Non-blocking

The changed-file manifest is empty and the prepared diff has no hunks, despite the description naming core/libs/tests/GuestThreadCpuClock.cpp. The code’s changed lines and exact patch therefore could not be verified.

Sources

Not verified

The patch itself and changed-line numbers were not available in the prepared diff or manifest. No builds, tests, or platform behavior were independently verified. Read: description.md, commits.md, manifest.json, diff.patch, README.md, core/libs/tests/GuestThreadCpuClock.cpp, and targeted ranges in core/libs/prx/libkernel/Time/Time.cpp. Excluded from the bundle by rule: core/libs/tests/GuestThreadCpuClock.cpp (as stated in the description); its head text was read through the reader.

To get this to a good state

Nothing is required. Optional: include the changed-file entry and patch hunks in the prepared review bundle so the changed lines can be checked.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5978894906 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23756#pullrequestreview-5978894906). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-19T16:17:48Z. **Review:** COMMENTED --- # Reviewed — comments The source file and author’s rationale were reviewed at head `41f216c425630a126ec6b688d7116e8f76a34467`; the prepared diff contains no hunks, so I could not verify which lines changed. ## What I ran Inspected the prepared description, commit list, manifest, and diff, then read `core/libs/tests/GuestThreadCpuClock.cpp` and targeted Windows clock implementation context through the repository reader. No tests or builds were run. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Test source and clock conversion context read | | Diff review | not run | Prepared diff has no hunks | | Tests | not run | No test command was run | ## Negative control Not run; this review did not execute tests. ## Findings ### Blocking None identified in the source inspected. ### Non-blocking The changed-file manifest is empty and the prepared diff has no hunks, despite the description naming `core/libs/tests/GuestThreadCpuClock.cpp`. The code’s changed lines and exact patch therefore could not be verified. ## Sources - [GuestThreadCpuClock.cpp at the reviewed commit](https://github.com/boykopovar/AnyPS5/blob/41f216c425630a126ec6b688d7116e8f76a34467/core/libs/tests/GuestThreadCpuClock.cpp#L145-L180) - [Time.cpp at the reviewed commit](https://github.com/boykopovar/AnyPS5/blob/41f216c425630a126ec6b688d7116e8f76a34467/core/libs/prx/libkernel/Time/Time.cpp#L428-L450) ## Not verified The patch itself and changed-line numbers were not available in the prepared diff or manifest. No builds, tests, or platform behavior were independently verified. Read: `description.md`, `commits.md`, `manifest.json`, `diff.patch`, `README.md`, `core/libs/tests/GuestThreadCpuClock.cpp`, and targeted ranges in `core/libs/prx/libkernel/Time/Time.cpp`. Excluded from the bundle by rule: `core/libs/tests/GuestThreadCpuClock.cpp` (as stated in the description); its head text was read through the reader. ## To get this to a good state Nothing is required. Optional: include the changed-file entry and patch hunks in the prepared review bundle so the changed lines can be checked.
MarcZX10 commented 2026-11-19 23:12:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T23:12:02Z.

Review: COMMENTED


Reviewed — no blocking findings

The prepared manifest and diff contain no changed-file entries, so this review could not verify the submitted code or justify a formal approval.

What I ran

  • Read the prepared description, commit list, manifest, diff, and changed-file inventory through the review reader.
  • Read core/libs/tests/GuestThreadCpuClock.cpp at the pinned head to inspect the described test logic.
  • No commands or tests were run.

Results

Check Result Note
Review bundle passed Description and metadata read
Patch inspection not run Diff and manifest list no changes
Tests not run No tests executed

Findings

Blocking

None verified.

Non-blocking

None.

Not verified

The changed-file inventory is empty and diff.patch contains no hunks, so the PR changes and their changed lines could not be inspected. The available repository reader showed the described test file at head, but that does not establish the PR's diff. The excluded-files inventory was not available in the bundle.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5984912076 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23756#pullrequestreview-5984912076). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-19T23:12:02Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The prepared manifest and diff contain no changed-file entries, so this review could not verify the submitted code or justify a formal approval. ## What I ran - Read the prepared description, commit list, manifest, diff, and changed-file inventory through the review reader. - Read `core/libs/tests/GuestThreadCpuClock.cpp` at the pinned head to inspect the described test logic. - No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Review bundle | passed | Description and metadata read | | Patch inspection | not run | Diff and manifest list no changes | | Tests | not run | No tests executed | ## Findings ### Blocking None verified. ### Non-blocking None. ## Not verified The changed-file inventory is empty and `diff.patch` contains no hunks, so the PR changes and their changed lines could not be inspected. The available repository reader showed the described test file at head, but that does not establish the PR's diff. The excluded-files inventory was not available in the bundle. ## To get this to a good state Nothing is required.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!23756
No description provided.