feat(libkernel): implement pthread_getcpuclockid and per-thread CPU clocks #1676

Merged
Recapi merged 1 commit from upstream-pr/1676 into main 2026-10-09 22:01:19 +00:00
Recapi commented 2026-10-08 16:13:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Recapi on 2026-10-08T16:13:45Z.


What

pthread_getcpuclockid in libkernel, and clock_gettime/clock_getres for the clock it returns.

  • As in FreeBSD (thr_getcpuclockid.c, kern_clock_getcpuclockid2), the clock is CPUCLOCK_BIT | tid. Guest threads had no thread id, so every PthreadPrivate gets one from a registry when it is constructed (created and adopted threads) and leaves it in its destructor.
  • clock_gettime on that clock returns the thread's CPU time: on Linux from the host thread's CPU clock, taken on the thread when it starts or is adopted; on Windows from GetThreadTimes on its handle. A thread that has finished or does not exist returns -1 with EINVAL, like tdfind failing in kern_clock_gettime.
  • clock_getres on it returns the resolution of CLOCK_THREAD_CPUTIME_ID. Process CPU clocks (CPUCLOCK_PROCESS_BIT) throw.
  • pthread_getcpuclockid returns EINVAL for a null thread and EFAULT for a null output.

Caller: PPSA99005 (blackbearreloaded/ps5-opengl v1.0.1) imports pthread_getcpuclockid (#1578; 19 of the 34 catalog titles in the audit there). In its SDK (ps5-opengl-sdk 1.0.1) the only reference is Mesa's libmesa_util.a u_thread.c.o: util_thread_get_time_nano (src/util/u_thread.c) calls pthread_getcpuclockid and then clock_gettime on the result, for the calling thread and for queue threads, so the clock has to work in clock_gettime too.

Log

PPSA99005 (blackbearreloaded/ps5-opengl showcase v1.0.1), relinked on Linux (Ubuntu, GCC 15.2.0, main f1451396, bundled libc.prx kept) and run in a bwrap sandbox. On main it stops on mkstemp, as in #1578:

./app.elf: symbol lookup error: ./app.elf: undefined symbol: uEtgPWgPxOk

With the open PRs for its earlier absent imports merged locally (#1978, #1869, #1656, #1729, #1613, #1714, #1482, #1650, #1872, #1641, #1685, #1859; no conflicts), the stops are:

Merged on top Stop
the 12 PRs above Wh7HbV7JFqc (getrlimit, #1675)
+ #1675 JZKw5+Wrnaw (pthread_sigmask, no PR yet)
+ #1675, a local throwaway pthread_sigmask export nnoxZUHK+SA (pthread_getcpuclockid, #1676)
+ #1675, #1676, the throwaway export, #1529 no absent import; Mesa starts its threads and the title then aborts in the AGC driver (#2026)

Each stop is the loader's line, e.g. ./app.elf: symbol lookup error: ./app.elf: undefined symbol: Wh7HbV7JFqc. The throwaway pthread_sigmask only exports the NID and throws when called; it was not called before the abort.

Tested

Ubuntu, Linux 7.0, GCC 15.2.0, full build with BUILD_TESTING=ON. New test guest_thread_cpu_clock: own clock tracks CLOCK_THREAD_CPUTIME_ID, another thread's clock reads that thread and does not advance while the caller burns CPU, EINVAL after join, resolution, null arguments. Full ctest --repeat until-fail:3: 465/465 (together with #1675). Patched libkernel.prx exports nnoxZUHK+SA.
The project's Build workflow on the fork (Linux, Windows MinGW, relinker on Ubuntu/Windows/macOS) passes; the run is linked in a comment below. Run with PPSA99005 as above.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4789653021 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1676). Originally posted by [Recapi](https://github.com/Recapi) on 2026-10-08T16:13:45Z. --- ### What `pthread_getcpuclockid` in libkernel, and `clock_gettime`/`clock_getres` for the clock it returns. - As in FreeBSD (`thr_getcpuclockid.c`, `kern_clock_getcpuclockid2`), the clock is `CPUCLOCK_BIT | tid`. Guest threads had no thread id, so every `PthreadPrivate` gets one from a registry when it is constructed (created and adopted threads) and leaves it in its destructor. - `clock_gettime` on that clock returns the thread's CPU time: on Linux from the host thread's CPU clock, taken on the thread when it starts or is adopted; on Windows from `GetThreadTimes` on its handle. A thread that has finished or does not exist returns -1 with `EINVAL`, like `tdfind` failing in `kern_clock_gettime`. - `clock_getres` on it returns the resolution of `CLOCK_THREAD_CPUTIME_ID`. Process CPU clocks (`CPUCLOCK_PROCESS_BIT`) throw. - `pthread_getcpuclockid` returns `EINVAL` for a null thread and `EFAULT` for a null output. Caller: PPSA99005 ([blackbearreloaded/ps5-opengl](https://github.com/blackbearreloaded/ps5-opengl) v1.0.1) imports `pthread_getcpuclockid` (#1578; 19 of the 34 catalog titles in the audit there). In its SDK (ps5-opengl-sdk 1.0.1) the only reference is Mesa's `libmesa_util.a` `u_thread.c.o`: `util_thread_get_time_nano` (`src/util/u_thread.c`) calls `pthread_getcpuclockid` and then `clock_gettime` on the result, for the calling thread and for queue threads, so the clock has to work in `clock_gettime` too. ### Log PPSA99005 ([blackbearreloaded/ps5-opengl](https://github.com/blackbearreloaded/ps5-opengl) showcase v1.0.1), relinked on Linux (Ubuntu, GCC 15.2.0, `main` f1451396, bundled `libc.prx` kept) and run in a `bwrap` sandbox. On `main` it stops on `mkstemp`, as in #1578: ``` ./app.elf: symbol lookup error: ./app.elf: undefined symbol: uEtgPWgPxOk ``` With the open PRs for its earlier absent imports merged locally (#1978, #1869, #1656, #1729, #1613, #1714, #1482, #1650, #1872, #1641, #1685, #1859; no conflicts), the stops are: | Merged on top | Stop | |---|---| | the 12 PRs above | `Wh7HbV7JFqc` (`getrlimit`, #1675) | | + #1675 | `JZKw5+Wrnaw` (`pthread_sigmask`, no PR yet) | | + #1675, a local throwaway `pthread_sigmask` export | `nnoxZUHK+SA` (`pthread_getcpuclockid`, #1676) | | + #1675, #1676, the throwaway export, #1529 | no absent import; Mesa starts its threads and the title then aborts in the AGC driver (#2026) | Each stop is the loader's line, e.g. `./app.elf: symbol lookup error: ./app.elf: undefined symbol: Wh7HbV7JFqc`. The throwaway `pthread_sigmask` only exports the NID and throws when called; it was not called before the abort. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. --> Ubuntu, Linux 7.0, GCC 15.2.0, full build with `BUILD_TESTING=ON`. New test `guest_thread_cpu_clock`: own clock tracks `CLOCK_THREAD_CPUTIME_ID`, another thread's clock reads that thread and does not advance while the caller burns CPU, `EINVAL` after join, resolution, null arguments. Full `ctest --repeat until-fail:3`: 465/465 (together with #1675). Patched `libkernel.prx` exports `nnoxZUHK+SA`. The project's Build workflow on the fork (Linux, Windows MinGW, relinker on Ubuntu/Windows/macOS) passes; the run is linked in a comment below. Run with PPSA99005 as above. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
Recapi commented 2026-10-08 16:57:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Recapi on 2026-10-08T16:57:22Z.


CI is waiting for first-time contributor approval here, so I ran the project's Build workflow (build.yml, workflow_dispatch) on the fork for this head (942a105c): https://github.com/Recapi/AnyPS5/actions/runs/37876696314

  • linux: 467/467, guest_thread_cpu_clock passed
  • windows (MinGW): 458/458, guest_thread_cpu_clock passed
  • relinker on ubuntu, windows and macos: passed

Overlap: no textual conflict with the open PRs that change these files (git merge-tree against each; #1128 already conflicts with main). The EFAULT constant is now local to pthread_getcpuclockid, so it neither conflicts with nor duplicates the file-scope GUEST_EFAULT #1922 adds to Posix/Thread.cpp.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6064892328 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1676#issuecomment-6064892328). Originally posted by [Recapi](https://github.com/Recapi) on 2026-10-08T16:57:22Z. --- CI is waiting for first-time contributor approval here, so I ran the project's Build workflow (`build.yml`, `workflow_dispatch`) on the fork for this head (`942a105c`): https://github.com/Recapi/AnyPS5/actions/runs/37876696314 - linux: 467/467, `guest_thread_cpu_clock` passed - windows (MinGW): 458/458, `guest_thread_cpu_clock` passed - relinker on ubuntu, windows and macos: passed Overlap: no textual conflict with the open PRs that change these files (`git merge-tree` against each; #1128 already conflicts with `main`). The `EFAULT` constant is now local to `pthread_getcpuclockid`, so it neither conflicts with nor duplicates the file-scope `GUEST_EFAULT` #1922 adds to `Posix/Thread.cpp`.
popcoder565 commented 2026-10-09 10:03:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T10:03:59Z.


First Windows run, since the description says it wasn't tested on Windows.

Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Merged onto main b7218b3c together with nine other small libc/libkernel PRs: #1975, #1976, #1977, #1859, #1685, #1675, #1641, #1656, #1807.

  • guest_thread_cpu_clock passes.
  • Full ctest: 510/510 (one GPU test skipped for a device limit).

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078729617 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1676#issuecomment-6078729617). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T10:03:59Z. --- First Windows run, since the description says it wasn't tested on Windows. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Merged onto `main` b7218b3c together with nine other small libc/libkernel PRs: #1975, #1976, #1977, #1859, #1685, #1675, #1641, #1656, #1807. - `guest_thread_cpu_clock` passes. - Full ctest: 510/510 (one GPU test skipped for a device limit). _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
adriadam10 commented 2026-10-09 22:01:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T22:01:15Z.


Checked against FreeBSD 11 (thr_getcpuclockid.c, kern_clock_getcpuclockid2): same clock id layout and error returns. Full build + ctest on Linux green (guest_thread_cpu_clock passes, and fails to link without the change); Windows MinGW run by @popcoder565 also green. Process CPU clocks throw instead of guessing, which is right. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6089979126 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1676#issuecomment-6089979126). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T22:01:15Z. --- Checked against FreeBSD 11 (`thr_getcpuclockid.c`, `kern_clock_getcpuclockid2`): same clock id layout and error returns. Full build + ctest on Linux green (`guest_thread_cpu_clock` passes, and fails to link without the change); Windows MinGW run by @popcoder565 also green. Process CPU clocks throw instead of guessing, which is right. Merging.
github-actions[bot] commented 2026-10-09 22:08:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T22:08:44Z.


Progress report (f145139 → 942a105)

➖ System libraries: 84.76% (+0.0%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel pthread_getcpuclockid_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090072086 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1676#issuecomment-6090072086). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T22:08:44Z. --- <!-- progress-report --> ### Progress report (f145139 → 942a105) ➖ **System libraries**: 84.76% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `pthread_getcpuclockid_nid_postfix` | </details>
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!1676
No description provided.