fix(linux): process_threads() counts the process's affinity mask on Linux #1669

Open
Molier wants to merge 1 commit from Molier/Longshot:fix/linux-process-threads into main
Contributor

Summary

process_threads() (host/hos_thread.c) answered 0 ("not known") on Linux, which some callers read as "assume
every logical processor"; on Windows it reads GetProcessAffinityMask. On Linux it now reads the process's own
affinity mask through sched_getaffinity/sysconf(_SC_NPROCESSORS_ONLN), matching the Windows branch's intent (the
threads the process may actually use, not just the machine's total). To see it on main: call process_threads()
on Linux and get 0 regardless of taskset/cgroup limits; read host/hos_thread.c — there is no Linux branch at all,
only #if defined(_WIN32).

Read first: host/hos_thread.c (the new Linux branch next to the existing Windows one), tests/process_threads_test.c.

Testing

Tested: Linux (Ubuntu), host compile clean (-fsyntax-only; one pre-existing unrelated warning, unused function 'pick_cache_cores', not from this change). tests/run_all.py process_threads: tests/test_process_threads.py
passes on the rebased head. The fix's own test fails on main without this commit: run against a clean
upstream/main @ 89c3d21e checkout with only tests/process_threads_test.c and tests/test_process_threads.py
copied in, FAIL: process_threads() has no Linux branch (GetProcessAffinityMask of the process, else sysconf _SC_NPROCESSORS_ONLN) — confirming main still answers 0 on Linux.

Still to test:

  • Linux under a taskset/cgroup CPU limit (fewer logical processors than the machine has): confirm
    process_threads() now returns the limited count, not the machine's full count (by hand).

Measurements

Nothing to measure: a correctness fix (a missing return value), not a performance change on its own; any thread-count
decision downstream of it (core reservation, worker counts) is unchanged in its logic, only in what count it is
fed on Linux.

Risks

A caller that already special-cased "0 means assume every core" on Linux would now get a real, possibly smaller,
count; a search of the host tree for process_threads() call sites found none that branch on 0 specially (it is
used as a plain thread/core count). Turning it off is not applicable — this restores a return value, there is no
switch.

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC (game-free checks; no visible in-game effect to screenshot).
  • tests/run_all.py process_threads passes.
  • Nothing to measure (see Measurements).
  • No game files, build output or hand-edited generated files; third_party/ untouched.

When it applies

  • A fix: tests/test_process_threads.py fails on main and passes here (see Testing).
  • Host code builds on Linux with UTF-8 paths; Windows branch untouched.
  • AI assistance recorded with Assisted-by: trailers.

Ready to merge

  • The cgroup/taskset cross-check (Still to test) has run.
  • Approved in review, every comment answered.
  • CI passes on the head.
## Summary `process_threads()` (`host/hos_thread.c`) answered 0 ("not known") on Linux, which some callers read as "assume every logical processor"; on Windows it reads `GetProcessAffinityMask`. On Linux it now reads the process's own affinity mask through `sched_getaffinity`/`sysconf(_SC_NPROCESSORS_ONLN)`, matching the Windows branch's intent (the threads the process may actually use, not just the machine's total). To see it on `main`: call `process_threads()` on Linux and get 0 regardless of `taskset`/cgroup limits; read `host/hos_thread.c` — there is no Linux branch at all, only `#if defined(_WIN32)`. Read first: `host/hos_thread.c` (the new Linux branch next to the existing Windows one), `tests/process_threads_test.c`. ## Testing **Tested:** Linux (Ubuntu), host compile clean (`-fsyntax-only`; one pre-existing unrelated warning, `unused function 'pick_cache_cores'`, not from this change). `tests/run_all.py process_threads`: `tests/test_process_threads.py` passes on the rebased head. The fix's own test fails on `main` without this commit: run against a clean `upstream/main @ 89c3d21e` checkout with only `tests/process_threads_test.c` and `tests/test_process_threads.py` copied in, `FAIL: process_threads() has no Linux branch (GetProcessAffinityMask of the process, else sysconf _SC_NPROCESSORS_ONLN)` — confirming `main` still answers 0 on Linux. **Still to test:** - Linux under a `taskset`/cgroup CPU limit (fewer logical processors than the machine has): confirm `process_threads()` now returns the limited count, not the machine's full count (by hand). ## Measurements Nothing to measure: a correctness fix (a missing return value), not a performance change on its own; any thread-count decision downstream of it (core reservation, worker counts) is unchanged in its logic, only in what count it is fed on Linux. ## Risks A caller that already special-cased "0 means assume every core" on Linux would now get a real, possibly smaller, count; a search of the host tree for `process_threads()` call sites found none that branch on 0 specially (it is used as a plain thread/core count). Turning it off is not applicable — this restores a return value, there is no switch. ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [x] Tested on my own PC (game-free checks; no visible in-game effect to screenshot). - [x] `tests/run_all.py process_threads` passes. - [x] Nothing to measure (see Measurements). - [x] No game files, build output or hand-edited generated files; `third_party/` untouched. ### When it applies - [x] A fix: `tests/test_process_threads.py` fails on `main` and passes here (see Testing). - [x] Host code builds on Linux with UTF-8 paths; Windows branch untouched. - [x] AI assistance recorded with `Assisted-by:` trailers. ### Ready to merge - [ ] The cgroup/taskset cross-check (Still to test) has run. - [ ] Approved in review, every comment answered. - [ ] CI passes on the head.
Member

Possibly related, for context:

  • issue #1504 On Linux the Windows lock shim sleeps at once on a busy lock and makes a system call on every wake, even when nobody waits — Calls out Linux process_threads() being unknown and the 5 µs spin rule as a possible fix.
Possibly related, for context: - issue #1504 On Linux the Windows lock shim sleeps at once on a busy lock and makes a system call on every wake, even when nobody waits — Calls out Linux process_threads() being unknown and the 5 µs spin rule as a possible fix.
Molier changed title from fix(linux): process_threads() counts the process's processors, not the calling thread's to WIP: fix(linux): process_threads() counts the process's processors, not the calling thread's 2026-11-18 23:33:30 +00:00
Molier force-pushed fix/linux-process-threads from 23a6c7ba52
Some checks failed
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 4s
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
to 8308b77174
All checks were successful
PR description / Description follows the template (pull_request_target) Successful in 17s
2026-11-18 23:46:55 +00:00
Compare
Molier changed title from WIP: fix(linux): process_threads() counts the process's processors, not the calling thread's to WIP: fix(linux): process_threads() counts the process's affinity mask on Linux (was 0, "not known") 2026-11-18 23:51:03 +00:00
Molier force-pushed fix/linux-process-threads from 8308b77174
All checks were successful
PR description / Description follows the template (pull_request_target) Successful in 17s
to 17a3d4b5bc
Some checks failed
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Failing after 6s
2026-11-20 14:38:17 +00:00
Compare
Molier changed title from WIP: fix(linux): process_threads() counts the process's affinity mask on Linux (was 0, "not known") to fix(linux): process_threads() counts the process's affinity mask on Linux 2026-11-20 14:38:31 +00:00
Author
Contributor

Rebased onto main @ 89c3d21e. Applied without conflicts. Fix check as the new template asks: tests/test_process_threads.py passes on this head and fails on a clean main checkout (process_threads() has no Linux branch). This head counts the mask the posix shim captured at process start rather than sched_getaffinity(0), which on Linux is the calling thread's mask (the host takes the reserved core off game threads, so a game thread would count 14 on a 16-thread CPU). For reference, main's own log on Linux currently reads sleep: ... (0 logical processors, 0: not counted ...). Out of WIP; body rewritten in the new template.

Rebased onto `main` @ `89c3d21e`. Applied without conflicts. Fix check as the new template asks: `tests/test_process_threads.py` passes on this head and fails on a clean `main` checkout (`process_threads() has no Linux branch`). This head counts the mask the posix shim captured at process start rather than `sched_getaffinity(0)`, which on Linux is the calling thread's mask (the host takes the reserved core off game threads, so a game thread would count 14 on a 16-thread CPU). For reference, `main`'s own log on Linux currently reads `sleep: ... (0 logical processors, 0: not counted ...)`. Out of WIP; body rewritten in the new template.
Some checks failed
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Failing after 6s
This pull request doesn't have enough approvals yet. 0 of 1 approvals granted.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/linux-process-threads:Molier-fix/linux-process-threads'
git switch 'Molier-fix/linux-process-threads'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!1669
No description provided.