fix(libkernel): answer process CPU clocks like FreeBSD instead of throwing #9501
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9501"
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 GitamarT045T3R on 2026-10-21T23:33:27Z.
What
clock_gettime,sceKernelClockGettimeandclock_getresthrew "unsupported process CPU clock_id" for any clock id withCPUCLOCK_PROCESS_BITset. FreeBSD 11 handles these ids as follows:clock_gettimeon the guest's own processget_cputimereads the process's CPU timeCLOCK_PROCESS_CPUTIME_IDclock_gettimeon any other pidpgetfails, soEINVALEINVAL(SCE_KERNEL_ERROR_EINVALfromsceKernelClockGettime), askillalready finds no other processclock_getreson any CPU clockkern_clock_getresgoes tocputimewithout a lookupTested
Windows 10 Pro 22H2 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Ninja, Release, on
main0c6a5bd59:guest_thread_cpu_clockgets a new case:CLOCK_PROCESS_CPUTIME_ID;EINVALand leaves the output untouched, andsceKernelClockGettimereturnsSCE_KERNEL_ERROR_EINVAL;main'sTime.cppand passes with this change.guest_cpu_time_clocksandguest_freebsd_clocksstill pass.python tools/check_conventions.py --base origin/mainpasses.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)Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T00:03:46Z.
Review: COMMENTED
Linux run of the current change, since the description only lists Windows ("Not built or run on Linux or macOS").
Machine: Linux x86_64, i7-8700 (6 cores / 12 threads), Ubuntu 26.04, GCC 15.2, Release,
BUILD_TESTING=ON. Head4437c742amerged ontomaina6dcea927(the merge touches onlyTime.cppandGuestThreadCpuClock.cpp).guest_thread_cpu_clockpasses with the change (the guest's own process CPU clock advances and matchesCLOCK_PROCESS_CPUTIME_ID, another pid returnsEINVAL, both clocks report the resolution).libkernel/Time/Time.cpptomainand keeping the test abortsguest_thread_cpu_clock:terminate called after throwing an instance of 'std::runtime_error'what(): clock: unsupported process CPU clock_id -1072494148so the regression reproduces on Linux as well and the test covers the change.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T01:31:58Z.
Review: CHANGES_REQUESTED
The process-clock path is consistent with the encoded pid: it resolves only the guest process, leaves the output unchanged for a non-self pid, and uses the host process CPU clock for the accepted case. FreeBSD 11’s CPU-clock encoding marks process clocks with both high bits and carries the pid in the remaining bits; its
clock_getrespath treats CPU clocks uniformly. The regression covers elapsed process CPU time, own versus other pid, output preservation and matching resolution. The hosted eligibility, conventions, intake, labels and report checks pass. The author and another reviewer report focused Windows and Linux runs respectively; the relinker/product jobs were skipped, and I did not run local tests.The overlap check could not compare old-base #5021 and #7201. Manual inspection shows both add macOS thread CPU-clock handling in the
clock_gettimepath; currentmainnow routes that path throughReadClock, which this PR also changes for encoded process clocks. Please coordinate with those authors so the macOS thread-clock cases and this process-clock dispatch are integrated on the rebased path. Record the agreed order, withDepends onif one must land first; keepnoneonly after all affected authors confirm the changes compose without a prerequisite. The current checklist saysDepends on: nonewithout recording that coordination.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T02:08:20Z.
I still do not see an author coordination update for the macOS CPU-clock overlap. #7201 adds a macOS
clock_gettimethread-CPU fallback, and #5021 also changes that path; this PR changes the sharedReadClockdispatch to decode process CPU clocks. Please coordinate with the #7201/#5021 authors on a concrete rebase order or confirm the exact changes compose in either order, then record required predecessors inDepends on. I have not claimed a code defect here; this is the repository overlap rule.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T02:08:24Z.
Review: CHANGES_REQUESTED
The process CPU-clock implementation and reported Windows/Linux regression runs look sound, but the shared
ReadClockpath overlaps open #5021 and #7201, which add macOS thread CPU-clock handling. The checklist still saysDepends on: noneand there is no author coordination note confirming an order or either-order composition. Please agree the integration/rebase plan with those authors and update the checklist/overlap note. Hosted eligibility, conventions, intake, labels and report checks pass; product/relinker jobs are skipped. I did not run local tests. This request is for repository overlap coordination, not a claimed code defect.Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-22T04:11:57Z.
Linux data point for
4437c742a5: WSL2 Ubuntu 24.04 (virtualised kernel, not bare-metal), GCC 13.3.0, Release,BUILD_TESTING=ON, PR head alone (not merged ontomain; its merge-base is0c6a5bd59).ctest -R '^guest_thread_cpu_clock$'passes 3 of 3.libkernel/Time/Time.cpprestored to the merge-base (the PR's tests kept): fails (Subprocess aborted).Not tested: macOS, Windows, a title run.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:25:30Z.
Review: DISMISSED
The encoded process-clock behavior and the reported Windows/Linux regression runs look technically consistent with the cited FreeBSD semantics. The remaining process requirement is the unresolved overlap: #5021 and #7201 both touch macOS CPU-clock handling in
Time.cpp, while this PR changes the sharedReadClockdispatch. No author-agreed integration order is recorded, and the branch is based on0c6a5bd593rather than current upstream mainab347d144d. Please coordinate with both authors, record the required order inDepends on, rebase onto current main, manually preserve the affected macOS cases, and rerun the focused clock test plus conventions check. Hosted product jobs are skipped. I did not run local tests.Imported from boykopovar/AnyPS5. Originally posted by GitamarT045T3R on 2026-10-22T08:08:06Z.
Rebased onto
maind6eafa54d(81e34fe18).guest_thread_cpu_clock,guest_cpu_time_clocksandguest_freebsd_clockspass on Windows, andcheck_conventionspasses.Landing order with #7201 (@marion909) and #5021 (@yabanana): neither depends on this PR or this PR on them, and either order works.
CLOCK_THREAD_CPUTIME_ID(14), which reads the calling thread throughthread_info, inside the non-Windows branch of what is nowReadClock, plus a helper and the Mach includes.CPUCLOCK_BITset, at the top ofReadClockbefore any per-host branch, and theIsCpuClockhelper. Clock 14 does not go through it, so their macOS case keeps working unchanged after this lands.mainand already conflict with it inTime.cpp, becauseclock_gettime's body moved intoReadClock, so they rebase there anyway. On macOS the guest's own process clock goes toCLOCK_PROCESS_CPUTIME_ID(15), which the existing native switch already maps.So
Depends onstays none.Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-10-22T09:09:15Z.
Verified against the cited FreeBSD source at the cited commit (
34bd3475) before this stage of the debate gets settled elsewhere:CPUCLOCK_BIT = 0x80000000,CPUCLOCK_PROCESS_BIT = 0x40000000,CPUCLOCK_ID_MASK = ~(...)inkern_time.care byte-identical to the pre-existingCPU_CLOCK_BIT/CPU_CLOCK_PROCESS_BIT/CPU_CLOCK_ID_MASKinPthread.hpp, so0xC0000000|pidis exactlyMAKE_PROCESS_CPUCLOCK(pid)— the guest encoding is FreeBSD's, and what a title built for a FreeBSD libkernel passes never threw-or-was-wrong here before, it chose between throwing (any process clock) and succeeding (thread clocks). The routing is right per case: the guest's own process clock delegates to the existing id-15 answer (CLOCK_PROCESS_CPUTIME_IDon POSIX,GetProcessTimesuser+kernel total on Windows — the process-wide total, which is whatget_cputime/CLOCK_PROCESS_CPUTIME_IDgives in FreeBSD); a foreign pid reaches EINVAL with the outputtimespecuntouched, which matches FreeBSD'spget-failure→EINVALunder this project's single-process sandbox (no lookup means the pid-visibility question can't even arise); andclock_getresfor every CPU clock now flows through the existingIsCpuClockbranch to the thread-clock resolution with no process/thread lookup, which is precisely FreeBSD'skern_clock_getresdefault→cputime behavior (thedefault:arm jumps to cputime with nopget/tdlookup) — the predicate simplification alone fixes that arm, which is why no second hunk was needed. Thread-clock behavior is bit-identical (IsCpuClockreturns the same value for them; the throw only sat on the process-bit case).The test extends the pre-registered
guest_thread_cpu_clock_testsand pins each branch: own-pid clock advances with the burn and brackets id-15 read; foreign pid fails both entry points witherrno == EINVAL/SCE_KERNEL_ERROR_EINVALand the outputtimespecstill at its sentinel;clock_getresequal to the thread-clock resolution for own and foreign. Milliseconds of runtime. No debt entry owed — nothing here is unverified console behavior, everything follows a cited source.