perf(agc): pin the queue workers to the performance cores on Linux by default #626

Merged
oneandonlydean merged 1 commit from upstream-pr/626 into main 2026-10-08 09:36:53 +00:00
oneandonlydean commented 2026-10-05 08:35:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T08:35:08Z.


What

CpuTopology::Get and Pin do nothing off Windows, so APS5_WORKER_AFFINITY=1 has no effect on Linux. This PR implements both on Linux and makes pinning the default there:

  • Get reads the process affinity with sched_getaffinity, and the core classes from /sys/devices/cpu_core/cpus (performance) and /sys/devices/cpu_atom/cpus (efficiency). A CPU counts as hybrid only when both lists exist and are disjoint.
  • Pin restricts the calling thread with pthread_setaffinity_np, clipped to the process affinity. A non-null thread handle is not supported on Linux and returns 0; no caller passes one.
  • WorkerAffinityMask pins the queue workers, the draw front end and the presenter to the performance cores by default on Linux. APS5_NO_WORKER_AFFINITY=1 opts out.
  • Windows is unchanged: it keeps the APS5_WORKER_AFFINITY=1 opt-in. APS5_WORKER_AFFINITY_MASK=<hex> still picks the cores on any platform.

This only applies to hybrid CPUs (performance and efficiency cores, e.g. Intel 12th to 14th gen). On a non-hybrid CPU (AMD, older Intel) the kernel exposes no cpu_core/cpu_atom lists, the layout is not hybrid, and nothing is pinned, exactly as today. The guest's own threads are never pinned. It is scheduling only; results are unchanged.

Why

On my Linux kernel every CPU reports the same cpu_capacity (1024), and there is no ITMT or per-core priority, so the scheduler treats an efficiency core like a performance core. Sampled during an unpinned run, the three hottest host threads sat on an efficiency core in 3, 7 and 4 of 40 samples; pinned, 0 of 40. Only about 2 to 2.5 cores are busy, so the performance cores are not oversubscribed.

Pinning is the better default on Linux because it costs nothing where it does not apply and the gain is large where it does. PPSA21564 on a hybrid CPU, same build, pinned against unpinned, interleaved, 2 runs each (fps):

intro title menu intro level (space) intro level (ships)
unpinned 45.5 46.7 45.4 37.2 31.7
pinned 45.5 49.0 47.7 41.3 37.6
change 0% +5% +5% +11% +19%

The hub level, on an earlier build, 3 runs each: 6.56 to 7.92 fps (+21%). The world map went from 26.4 to 27.5 fps (+4%, 5 and 4 runs).

Pinning the whole process instead (taskset) was no better than pinning these threads, so only they are pinned.

Before #586 (now merged), PPSA21564 hit a GPU hang (Xid 109) far more often with the workers pinned: 13 of 15 runs pinned against 0 of 3 unpinned. The hang was the per-invocation LDS array that #586 fixes; a faster worker only made it more frequent. With #586, pinned runs hung 0 times in 18.

Tested

  • Linux (CachyOS, x86-64, GCC): full build with BUILD_TESTING=ON; ctest passes (269/269).
  • New cpu_topology (Linux only):
    • cpu-list parsing: ranges, lists, a missing newline, CPUs past 63, an empty list, a reversed range, malformed ranges and separators, a missing file;
    • Get reads the process affinity, and the hybrid classes do not overlap;
    • Pin to one CPU of the process, read back with pthread_getaffinity_np;
    • a mask outside the process is not applied, and a thread handle is refused.
  • The Get/Pin checks fail against main's CpuTopology.hpp ("process affinity read") and pass with this PR.
  • PPSA21564 on my integration branch with this behaviour as the default: my visual regression check passes twice, with no Xid in those runs or in 3 more hang-check runs.
  • Not tested on Windows beyond CI; the Windows path is unchanged.

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 in TechnicalDebt
  • 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
  • Generic behaviour, not specific to one title
  • Depends on: none (#586, merged, fixed the hang pinning made more frequent)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4742279605 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/626). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T08:35:08Z. --- ### What `CpuTopology::Get` and `Pin` do nothing off Windows, so `APS5_WORKER_AFFINITY=1` has no effect on Linux. This PR implements both on Linux and makes pinning the default there: - `Get` reads the process affinity with `sched_getaffinity`, and the core classes from `/sys/devices/cpu_core/cpus` (performance) and `/sys/devices/cpu_atom/cpus` (efficiency). A CPU counts as hybrid only when both lists exist and are disjoint. - `Pin` restricts the calling thread with `pthread_setaffinity_np`, clipped to the process affinity. A non-null thread handle is not supported on Linux and returns 0; no caller passes one. - `WorkerAffinityMask` pins the queue workers, the draw front end and the presenter to the performance cores by default on Linux. `APS5_NO_WORKER_AFFINITY=1` opts out. - Windows is unchanged: it keeps the `APS5_WORKER_AFFINITY=1` opt-in. `APS5_WORKER_AFFINITY_MASK=<hex>` still picks the cores on any platform. This only applies to hybrid CPUs (performance and efficiency cores, e.g. Intel 12th to 14th gen). On a non-hybrid CPU (AMD, older Intel) the kernel exposes no `cpu_core`/`cpu_atom` lists, the layout is not hybrid, and nothing is pinned, exactly as today. The guest's own threads are never pinned. It is scheduling only; results are unchanged. ### Why On my Linux kernel every CPU reports the same `cpu_capacity` (1024), and there is no ITMT or per-core priority, so the scheduler treats an efficiency core like a performance core. Sampled during an unpinned run, the three hottest host threads sat on an efficiency core in 3, 7 and 4 of 40 samples; pinned, 0 of 40. Only about 2 to 2.5 cores are busy, so the performance cores are not oversubscribed. Pinning is the better default on Linux because it costs nothing where it does not apply and the gain is large where it does. PPSA21564 on a hybrid CPU, same build, pinned against unpinned, interleaved, 2 runs each (fps): | | intro | title | menu | intro level (space) | intro level (ships) | |---|---|---|---|---|---| | unpinned | 45.5 | 46.7 | 45.4 | 37.2 | 31.7 | | pinned | 45.5 | 49.0 | 47.7 | 41.3 | 37.6 | | change | 0% | +5% | +5% | +11% | +19% | The hub level, on an earlier build, 3 runs each: 6.56 to 7.92 fps (+21%). The world map went from 26.4 to 27.5 fps (+4%, 5 and 4 runs). Pinning the whole process instead (`taskset`) was no better than pinning these threads, so only they are pinned. Before #586 (now merged), PPSA21564 hit a GPU hang (Xid 109) far more often with the workers pinned: 13 of 15 runs pinned against 0 of 3 unpinned. The hang was the per-invocation LDS array that #586 fixes; a faster worker only made it more frequent. With #586, pinned runs hung 0 times in 18. ### Tested - Linux (CachyOS, x86-64, GCC): full build with `BUILD_TESTING=ON`; `ctest` passes (269/269). - New `cpu_topology` (Linux only): - cpu-list parsing: ranges, lists, a missing newline, CPUs past 63, an empty list, a reversed range, malformed ranges and separators, a missing file; - `Get` reads the process affinity, and the hybrid classes do not overlap; - `Pin` to one CPU of the process, read back with `pthread_getaffinity_np`; - a mask outside the process is not applied, and a thread handle is refused. - The `Get`/`Pin` checks fail against main's `CpuTopology.hpp` ("process affinity read") and pass with this PR. - PPSA21564 on my integration branch with this behaviour as the default: my visual regression check passes twice, with no Xid in those runs or in 3 more hang-check runs. - Not tested on Windows beyond CI; the Windows path is unchanged. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none (#586, merged, fixed the hang pinning made more frequent) - [x] 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!626
No description provided.