fix(libkernel): read process CPU time for clock id 21 #18470

Merged
THEMANnt merged 1 commit from upstream-pr/18470 into main 2026-11-12 12:16:18 +00:00
THEMANnt commented 2026-11-11 17:19:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by THEMANnt on 2026-11-11T17:19:02Z.


What

Implement clock ID 21 in clock_gettime and sceKernelClockGettime using the existing process CPU clock. On firmware 13.590.001, clock 21 advanced with process CPU use, matched clock 2 during idle and busy measurements, and returned values in microsecond steps. AnyPS5 now truncates the returned value to microseconds. The host CPU clock may update less often. Retail export behavior is unverified. Console measurements

The resolution query is covered separately by #17958. #18466 addresses clock ID 20 and touches the same source and debt entry. Either branch can be reviewed on main, with a small rebase after the first merges.

Tested

  • Windows x64, WinLibs GCC 15.2. Built libkernel and passed guest_freebsd_clocks, guest_cpu_time_clocks, guest_thread_cpu_clock, and public_headers.
  • The new regression checks both entry points against clock 2 and verifies microsecond output. Repository conventions passed.
  • No title run. This change is based on the console payload measurements.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5263767448 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18470). Originally posted by [THEMANnt](https://github.com/THEMANnt) on 2026-11-11T17:19:02Z. --- ### What Implement clock ID 21 in `clock_gettime` and `sceKernelClockGettime` using the existing process CPU clock. On firmware 13.590.001, clock 21 advanced with process CPU use, matched clock 2 during idle and busy measurements, and returned values in microsecond steps. AnyPS5 now truncates the returned value to microseconds. The host CPU clock may update less often. Retail export behavior is unverified. [Console measurements](https://github.com/boykopovar/AnyPS5/issues/15725) The resolution query is covered separately by #17958. #18466 addresses clock ID 20 and touches the same source and debt entry. Either branch can be reviewed on main, with a small rebase after the first merges. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Windows x64, WinLibs GCC 15.2. Built libkernel and passed `guest_freebsd_clocks`, `guest_cpu_time_clocks`, `guest_thread_cpu_clock`, and `public_headers`. - The new regression checks both entry points against clock 2 and verifies microsecond output. Repository conventions passed. - No title run. This change is based on the console payload measurements. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) - [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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
PrivacyOnion commented 2026-11-11 17:42:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-11T17:42:11Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_freebsd_clocks_tests built cleanly.
  • CTest (guest_freebsd_clocks):
    1/1 Test #4383: guest_freebsd_clocks .............   Passed    0.49 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/Time/Time.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4383: guest_freebsd_clocks .............Subprocess aborted***Exception:   0.42 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified in core/libs/prx/libkernel/Time/Time.cpp (ReadClock) that clock ID 21 delegates to ReadClock(2, tp), verifies the return code, and truncates tp->tv_nsec to microsecond precision (tp->tv_nsec -= tp->tv_nsec % 1000) without underflow or overflow risk since tv_nsec is bounded in [0, 1000000000).
    • Verified in core/libs/prx/libkernel/Time/Time.cpp (clock_gettime_nid_postfix) that clock ID 21 passes validation without being rejected by MeasuredInvalidClock ([22, 32), 3, 6) or MeasuredIoErrorClock ([16, 19]), executes ReadClock, and sets *__error_nid_postfix() = GUEST_EFAULT (14) on null tp.
    • Verified in core/libs/prx/libkernel/Time/Time.cpp (sceKernelClockGettime) that null pointer checks return SceKernelErrorEfault (0x8002000e) and errors from clock_gettime_nid_postfix map correctly through SceKernelError.
    • Verified cross-platform portability on Linux (ProcessCpuNanos via times()) and Windows (CpuTimeNanos via GetProcessTimes()), confirming thread safety, absence of locks, reentrancy, and LP64 / LLP64 type consistency.
    • Verified in core/libs/tests/GuestFreebsdClocks.cpp (Clock21TracksProcessCpuTime and OmittedTimeIsFaultAfterClockValidation) that both clock_gettime_nid_postfix and sceKernelClockGettime are tested against GUEST_CLOCK_PROF, microsecond alignment (% 1000 == 0) is validated, and null pointer handling is asserted.
    • Verified strict compliance with AGENTS.md: zero code comments added, general OS clock behavior implemented rather than special-casing a single title, docs/dev/debt/libkernel.md updated accurately, and PR template requirements fully satisfied.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5862983203 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18470#pullrequestreview-5862983203). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-11T17:42:11Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `b249a39f9a`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_freebsd_clocks_tests` built cleanly. - **CTest** (`guest_freebsd_clocks`): ``` 1/1 Test #4383: guest_freebsd_clocks ............. Passed 0.49 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/Time/Time.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4383: guest_freebsd_clocks .............Subprocess aborted***Exception: 0.42 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified in core/libs/prx/libkernel/Time/Time.cpp (ReadClock) that clock ID 21 delegates to ReadClock(2, tp), verifies the return code, and truncates tp->tv_nsec to microsecond precision (tp->tv_nsec -= tp->tv_nsec % 1000) without underflow or overflow risk since tv_nsec is bounded in [0, 1000000000). - Verified in core/libs/prx/libkernel/Time/Time.cpp (clock_gettime_nid_postfix) that clock ID 21 passes validation without being rejected by MeasuredInvalidClock ([22, 32), 3, 6) or MeasuredIoErrorClock ([16, 19]), executes ReadClock, and sets *__error_nid_postfix() = GUEST_EFAULT (14) on null tp. - Verified in core/libs/prx/libkernel/Time/Time.cpp (sceKernelClockGettime) that null pointer checks return SceKernelErrorEfault (0x8002000e) and errors from clock_gettime_nid_postfix map correctly through SceKernelError. - Verified cross-platform portability on Linux (ProcessCpuNanos via times()) and Windows (CpuTimeNanos via GetProcessTimes()), confirming thread safety, absence of locks, reentrancy, and LP64 / LLP64 type consistency. - Verified in core/libs/tests/GuestFreebsdClocks.cpp (Clock21TracksProcessCpuTime and OmittedTimeIsFaultAfterClockValidation) that both clock_gettime_nid_postfix and sceKernelClockGettime are tested against GUEST_CLOCK_PROF, microsecond alignment (% 1000 == 0) is validated, and null pointer handling is asserted. - Verified strict compliance with AGENTS.md: zero code comments added, general OS clock behavior implemented rather than special-casing a single title, docs/dev/debt/libkernel.md updated accurately, and PR template requirements fully satisfied. - Note: the AI-assisted checkbox can stay. Looks Good To Me
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!18470
No description provided.