feat(libkernel): process, thread and clock queries on macOS hosts #7804
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7804"
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 mugurc on 2026-10-19T08:13:53Z.
What
Process, thread and clock queries on macOS hosts. They read
/procor call glibc extensions (pthread_getattr_np,pthread_getcpuclockid,sched_getcpu,RUSAGE_THREAD), solibkerneldoes not compile on macOS without this. Part of the macOS series announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6124344298)._NSGetArgcand_NSGetArgvinstead of/proc/self/cmdline.pthread_cpu_number_np.getrusagefor a thread, the CPU clock of a guest thread andclock_gettimewith the thread CPU clock (14) read the Machthread_infoof the thread. macOS has nopthread_getcpuclockid, and under Rosetta its ownCLOCK_THREAD_CPUTIME_IDcannot 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.pthread_get_stackaddr_npandpthread_get_stacksize_np.SleepNanoskeepserrno: macOS'snanosleepleavesETIMEDOUTin it when it returns 0, and the guest'serrnois the host's. This hunk is in the non-Windows branch, so Linux runs it too; Linux'snanosleepleaveserrnoalone on success, so the only change there is that a sleep interrupted and then finished no longer leavesEINTRbehind.guest_pthread_stackallows 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.
mainc1ca83479with the rest of the macOS series (ci/macos-series-2on 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.libkerneldoes not compile on macOS (pthread_getattr_np,pthread_getcpuclockid,sched_getcpu,RUSAGE_THREAD). With itguest_getrusage,guest_current_cpu,guest_available_cpumask,guest_thread_cpu_clock,guest_cpu_time_clocks,guest_freebsd_clocks,guest_nanosleep,guest_system_configurationandguest_pthread_stackpass.CLOCK_THREAD_CPUTIME_ID,guest_thread_cpu_clockaborts. A probe built for x86-64 and run under Rosetta: three busy loops thatthread_infomeasures at 58, 77 and 76 ms moveCLOCK_THREAD_CPUTIME_IDby 1.4, 1.8 and 1.8 ms. Built for arm64, the two agree to the microsecond (130.028 ms each), andCLOCK_PROCESS_CPUTIME_IDagrees withgetrusageunder Rosetta too (373.2 ms each), so only the thread clock needsthread_info.errnorestore inSleepNanos,guest_nanosleepandguest_system_configurationabort.nanosleepthat returns 0 leftETIMEDOUTinerrnoin 20 of 20 calls, under Rosetta and natively.ninja,ninja libsandctest -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.SleepNanos, every change is in an__APPLE__branch.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 imellag on 2026-10-24T16:16:14Z.
Review: APPROVED
Tested head
4fd1093119on Apple M4 Pro, macOS 27.0.1, Apple clang, x86-64 Release under Rosetta 2.Alone on
main3fcb5e4d76:Pthread/src/Thread.cppandSystem/src/Process.cppnow 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_nanosleepandguest_pthread_stackpass.guest_system_configurationaborts inCheckVmmapSysctl, whichmainadded after this PR's base (20764888c3, 2026-10-17).kern.proc.vmmapleaveserrnoat 2 becausesceKernelVirtualQueryreads/proc/self/mapson macOS. This doesn't come from this PR: the test passes oncef66a4df741is added (details on #7631).AI-assisted: yes