Skip to content

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

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
mugurc:feat/libkernel-process-clock-macos
Nov 8, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
mugurc:feat/libkernel-process-clock-macos

Conversation

@mugurc

@mugurc mugurc commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

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 (#1 (comment)).

  • 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 c1ca834 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 feat(libkernel): direct memory mappings on macOS hosts #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

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4563 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 5021 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Process.cpp
core/libs/prx/libkernel/Time/Time.cpp
core/libs/prx/libkernel/Time/TimedWait.cpp
core/libs/tests/GuestPthreadStack.cpp
docs/dev/debt/libkernel.md
PR 6149 docs/dev/debt/libkernel.md
PR 6740 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7088 docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7149 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/tests/GuestPthreadStack.cpp
docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 7464 docs/dev/debt/libkernel.md
PR 8592 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/Time/TimedWait.cpp
PR 9156 docs/dev/debt/libkernel.md
PR 9162 core/libs/prx/libkernel/System/src/Process.cpp
PR 9199 docs/dev/debt/libkernel.md
PR 9293 docs/dev/debt/libkernel.md
PR 9297 docs/dev/debt/libkernel.md
PR 9302 docs/dev/debt/libkernel.md
PR 9389 docs/dev/debt/libkernel.md
PR 9557 docs/dev/debt/libkernel.md
PR 9566 docs/dev/debt/libkernel.md
PR 9677 core/libs/prx/libkernel/Time/Time.cpp
PR 9938 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 10442 docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11661 docs/dev/debt/libkernel.md
PR 11689 docs/dev/debt/libkernel.md
PR 11698 docs/dev/debt/libkernel.md
PR 11896 docs/dev/debt/libkernel.md
PR 12130 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/Time/Time.cpp
core/libs/prx/libkernel/Time/TimedWait.cpp
PR 12544 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12592 core/libs/prx/libkernel/Time/Time.cpp
PR 12642 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/Time/Time.cpp
core/libs/prx/libkernel/Time/TimedWait.cpp
PR 12682 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 12688 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 12700 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 12965 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 14108 docs/dev/debt/libkernel.md
PR 14128 docs/dev/debt/libkernel.md
PR 14190 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 14212 docs/dev/debt/libkernel.md
PR 14497 docs/dev/debt/libkernel.md
PR 14756 docs/dev/debt/libkernel.md
PR 15037 docs/dev/debt/libkernel.md
PR 15524 docs/dev/debt/libkernel.md
PR 15567 core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 15785 docs/dev/debt/libkernel.md
PR 15806 core/libs/prx/libkernel/Time/Time.cpp
PR 15807 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/Time/Time.cpp
docs/dev/debt/libkernel.md
PR 16622 docs/dev/debt/libkernel.md

ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 19, 2026
…queries on macOS hosts (@mugurc)

This branch already carried this part of mugurc's macOS series; boykopovar#7804
is the same change split out for main. Merging it records the PR here
and in Depends on, so landing it first only shrinks this branch. Its
only lines this branch lacked are Time.cpp's Mach and pthread includes
in a second place, which duplicate the ones the branch already has, so
the merge keeps a single copy and the code is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 19, 2026
…queries on macOS hosts (@mugurc)

This branch already carried this part of mugurc's macOS series; boykopovar#7804
is the same change split out for main. Merging it records the PR here
and in Depends on, so landing it first only shrinks this branch. Its
only lines this branch lacked are Time.cpp's Mach and pthread includes
in a second place, which duplicate the ones the branch already has, so
the merge keeps a single copy and the code is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriadam10 adriadam10 self-assigned this Oct 21, 2026
@krishalivaibhav

Copy link
Copy Markdown
Contributor

Same setup. Merged after #7631 with no code conflicts: Pthread/src/Thread.cpp and System/src/Process.cpp now compile, 9 to 7 failed objects in ninja libs -k 0. guest_pthread_stack is not runnable yet because libkernel does not link on macOS (6 other objects fail, none in this PR).
AI-assisted: yes (Claude).

@imellag imellag left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Gonic0

Gonic0 commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

main 5939f08d89 merged into this head, in case it saves you the work. System/src/Process.cpp keeps the macOS branch filling native above main's result/WriteGuest, and Time/Time.cpp uses main's existing __APPLE__ include block. No other file conflicts.

Merge: Gonic0/AnyPS5 branch carry/7804-main, commit 918c898243, parents 4fd1093119 and 5939f08d89.

Tested on macOS 27.3, Apple M5 Pro, Apple clang, x86_64 Release under Rosetta 2, on that merge plus #4630 and #16591: libs and relinker build, and guest_getrusage, guest_current_cpu, guest_available_cpumask, guest_thread_cpu_clock, guest_cpu_time_clocks, guest_freebsd_clocks, guest_nanosleep, guest_nanosleep_long_request and guest_pthread_stack pass. Linux and Windows not built.

AI-assisted: yes

@adriadam10
adriadam10 force-pushed the feat/libkernel-process-clock-macos branch from 4fd1093 to d05d509 Compare November 8, 2026 01:01
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 8, 2026
@adriadam10
adriadam10 merged commit 9594527 into boykopovar:main Nov 8, 2026
12 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Rebased onto current main as requested: in System/src/Process.cpp the macOS getrusage branch now fills native above the shared copy into result that is written with WriteGuest (the Mach thread_info status variable is renamed so it doesn't shadow result), and in Time/Time.cpp pthread.h joins main's existing __APPLE__ include block. Rest of the diff unchanged and checked; libkernel and the touched tests build and pass after the rebase; rebased and merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants