feat(libkernel): process, thread and clock queries on macOS hosts #7804

Merged
mugurc merged 1 commit from upstream-pr/7804 into main 2026-11-08 09:59:05 +00:00
mugurc commented 2026-10-19 08:13:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-19T08:13:53Z.


What

Process, thread and clock queries on macOS hosts. They read /proc or call glibc extensions (pthread_getattr_np, pthread_getcpuclockid, sched_getcpu, RUSAGE_THREAD), so libkernel does not compile on macOS without this. Part of the macOS series announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6124344298).

  • The program arguments come from _NSGetArgc and _NSGetArgv instead of /proc/self/cmdline.
  • The current CPU comes from pthread_cpu_number_np.
  • getrusage for a thread, the CPU clock of a guest thread and clock_gettime with the thread CPU clock (14) read the Mach thread_info of the thread. macOS has no pthread_getcpuclockid, and under Rosetta its own CLOCK_THREAD_CPUTIME_ID cannot stand in for 14: it advances by about 1/41.7 of the thread's CPU time (24 MHz ticks read as nanoseconds) and reports a 1 ns resolution, so the guest thread's clock and clock 14 would disagree.
  • The stack limits of a thread come from pthread_get_stackaddr_np and pthread_get_stacksize_np.
  • SleepNanos keeps errno: macOS's nanosleep leaves ETIMEDOUT in it when it returns 0, and the guest's errno is the host's. This hunk is in the non-Windows branch, so Linux runs it too; Linux's nanosleep leaves errno alone on success, so the only change there is that a sleep interrupted and then finished no longer leaves EINTR behind.
  • guest_pthread_stack allows two frames between the stack top and the first local on macOS, where that local lies 4,464 bytes below the top.

The whole-port PRs #7201 and #7332 contain these functions too; #7332 carries this commit from the series.

Tested

macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2. main c1ca83479 with the rest of the macOS series (ci/macos-series-2 on my fork) and the open macOS fixes #6667, #6668, #6969, #6853, #6836, #7524, #6685 and #6686, #6928's change, and three changes from #7332; the libraries do not link on macOS without the series.

  • Without this PR libkernel does not compile on macOS (pthread_getattr_np, pthread_getcpuclockid, sched_getcpu, RUSAGE_THREAD). With it guest_getrusage, guest_current_cpu, guest_available_cpumask, guest_thread_cpu_clock, guest_cpu_time_clocks, guest_freebsd_clocks, guest_nanosleep, guest_system_configuration and guest_pthread_stack pass.
  • With clock 14 left to the host's CLOCK_THREAD_CPUTIME_ID, guest_thread_cpu_clock aborts. A probe built for x86-64 and run under Rosetta: three busy loops that thread_info measures at 58, 77 and 76 ms move CLOCK_THREAD_CPUTIME_ID by 1.4, 1.8 and 1.8 ms. Built for arm64, the two agree to the microsecond (130.028 ms each), and CLOCK_PROCESS_CPUTIME_ID agrees with getrusage under Rosetta too (373.2 ms each), so only the thread clock needs thread_info.
  • Without the errno restore in SleepNanos, guest_nanosleep and guest_system_configuration abort. nanosleep that returns 0 left ETIMEDOUT in errno in 20 of 20 calls, under Rosetta and natively.
  • ninja, ninja libs and ctest -j4: 1535 tests, 930 passed, 19 failed, the rest skipped (no Vulkan loader in the environment); none of the 19 is among the tests above, and they are the ones listed in #7631.
  • Linux and Windows: not run. Apart from SleepNanos, every change is in an __APPLE__ branch.

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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4917911740 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7804). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-19T08:13:53Z. --- ### What Process, thread and clock queries on macOS hosts. They read `/proc` or call glibc extensions (`pthread_getattr_np`, `pthread_getcpuclockid`, `sched_getcpu`, `RUSAGE_THREAD`), so `libkernel` does not compile on macOS without this. Part of the macOS series announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6124344298). - The program arguments come from `_NSGetArgc` and `_NSGetArgv` instead of `/proc/self/cmdline`. - The current CPU comes from `pthread_cpu_number_np`. - `getrusage` for a thread, the CPU clock of a guest thread and `clock_gettime` with the thread CPU clock (14) read the Mach `thread_info` of the thread. macOS has no `pthread_getcpuclockid`, and under Rosetta its own `CLOCK_THREAD_CPUTIME_ID` cannot stand in for 14: it advances by about 1/41.7 of the thread's CPU time (24 MHz ticks read as nanoseconds) and reports a 1 ns resolution, so the guest thread's clock and clock 14 would disagree. - The stack limits of a thread come from `pthread_get_stackaddr_np` and `pthread_get_stacksize_np`. - `SleepNanos` keeps `errno`: macOS's `nanosleep` leaves `ETIMEDOUT` in it when it returns 0, and the guest's `errno` is the host's. This hunk is in the non-Windows branch, so Linux runs it too; Linux's `nanosleep` leaves `errno` alone on success, so the only change there is that a sleep interrupted and then finished no longer leaves `EINTR` behind. - `guest_pthread_stack` allows two frames between the stack top and the first local on macOS, where that local lies 4,464 bytes below the top. The whole-port PRs #7201 and #7332 contain these functions too; #7332 carries this commit from the series. ### Tested macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2. `main` c1ca83479 with the rest of the macOS series (`ci/macos-series-2` on my fork) and the open macOS fixes #6667, #6668, #6969, #6853, #6836, #7524, #6685 and #6686, #6928's change, and three changes from #7332; the libraries do not link on macOS without the series. - Without this PR `libkernel` does not compile on macOS (`pthread_getattr_np`, `pthread_getcpuclockid`, `sched_getcpu`, `RUSAGE_THREAD`). With it `guest_getrusage`, `guest_current_cpu`, `guest_available_cpumask`, `guest_thread_cpu_clock`, `guest_cpu_time_clocks`, `guest_freebsd_clocks`, `guest_nanosleep`, `guest_system_configuration` and `guest_pthread_stack` pass. - With clock 14 left to the host's `CLOCK_THREAD_CPUTIME_ID`, `guest_thread_cpu_clock` aborts. A probe built for x86-64 and run under Rosetta: three busy loops that `thread_info` measures at 58, 77 and 76 ms move `CLOCK_THREAD_CPUTIME_ID` by 1.4, 1.8 and 1.8 ms. Built for arm64, the two agree to the microsecond (130.028 ms each), and `CLOCK_PROCESS_CPUTIME_ID` agrees with `getrusage` under Rosetta too (373.2 ms each), so only the thread clock needs `thread_info`. - Without the `errno` restore in `SleepNanos`, `guest_nanosleep` and `guest_system_configuration` abort. `nanosleep` that returns 0 left `ETIMEDOUT` in `errno` in 20 of 20 calls, under Rosetta and natively. - `ninja`, `ninja libs` and `ctest -j4`: 1535 tests, 930 passed, 19 failed, the rest skipped (no Vulkan loader in the environment); none of the 19 is among the tests above, and they are the ones listed in #7631. - Linux and Windows: not run. Apart from `SleepNanos`, every change is in an `__APPLE__` branch. ### 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) - [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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
imellag commented 2026-10-24 16:16:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-24T16:16:14Z.

Review: APPROVED


Tested head 4fd1093119 on Apple M4 Pro, macOS 27.0.1, Apple clang, x86-64 Release under Rosetta 2.

Alone on main 3fcb5e4d76: Pthread/src/Thread.cpp and System/src/Process.cpp now compile, with no new build failures.

With the other open macOS build PRs (the same stack as in my review of #7631): guest_getrusage, guest_current_cpu, guest_available_cpumask, guest_thread_cpu_clock, guest_cpu_time_clocks, guest_freebsd_clocks, guest_nanosleep and guest_pthread_stack pass.

guest_system_configuration aborts in CheckVmmapSysctl, which main added after this PR's base (20764888c3, 2026-10-17). kern.proc.vmmap leaves errno at 2 because sceKernelVirtualQuery reads /proc/self/maps on macOS. This doesn't come from this PR: the test passes once f66a4df741 is added (details on #7631).

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638920816 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7804#pullrequestreview-5638920816). Originally posted by [imellag](https://github.com/imellag) on 2026-10-24T16:16:14Z. **Review:** APPROVED --- Tested head `4fd1093119` on Apple M4 Pro, macOS 27.0.1, Apple clang, x86-64 Release under Rosetta 2. **Alone on `main` `3fcb5e4d76`:** `Pthread/src/Thread.cpp` and `System/src/Process.cpp` now compile, with no new build failures. **With the other open macOS build PRs** (the same stack as in my review of #7631): `guest_getrusage`, `guest_current_cpu`, `guest_available_cpumask`, `guest_thread_cpu_clock`, `guest_cpu_time_clocks`, `guest_freebsd_clocks`, `guest_nanosleep` and `guest_pthread_stack` pass. `guest_system_configuration` aborts in `CheckVmmapSysctl`, which `main` added after this PR's base (`20764888c3`, 2026-10-17). `kern.proc.vmmap` leaves `errno` at 2 because `sceKernelVirtualQuery` reads `/proc/self/maps` on macOS. This doesn't come from this PR: the test passes once `f66a4df741` is added (details on #7631). AI-assisted: yes
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!7804
No description provided.