Skip to content

gta6rt: title CPU affinity for mapped images - #2632

Closed
Nhympex0 wants to merge 1 commit into
ItsNotRexed:mainfrom
Nhympex0:title-cpu-affinity
Closed

Nhympex0 wants to merge 1 commit into
ItsNotRexed:mainfrom
Nhympex0:title-cpu-affinity

Conversation

@Nhympex0

Copy link
Copy Markdown
Contributor

Claimed in #2300 (claim).

A console title's affinity masks name its own cores (7 by default, 2 SMT threads each). Passed through unchanged, a mask like 0x7F pins a title onto 3.5 of the host's physical cores, and bits above the host's count fail. runtime/shims/cpu_affinity.cpp translates them for mapped images:

  • console core i → the host's i-th physical core in group 0, both SMT siblings (GetLogicalProcessorInformationEx(RelationProcessorCore) ∩ the process mask), wrapping when the host has fewer cores;
  • SetThreadAffinityMask, GetProcessAffinityMask, SetProcessAffinityMask, SetThreadIdealProcessor, SetThreadIdealProcessorEx translate masks and indices both ways; bits outside the console's cores → ERROR_INVALID_PARAMETER;
  • SetThreadSelectedCpuSets / SetProcessDefaultCpuSets forward host CPU-set IDs unchanged (they come from the host's GetSystemCpuSetInformation);
  • LEONIDA_TITLE_CORES (1–64) overrides the console core count.

Registered under kernel32.dll, kernelbase.dll and api-ms-win-core-processthreads-l1-1-{0..3}.dll. One bit per console core is an assumption (marked as such in the code and docs) until the executable can confirm it. Docs: new "Title CPU affinity" section in docs/runtime_imports.md.

Tested on 49475796 + this branch, Windows 11 24H2, Ryzen 7 7800X3D (8 cores / 16 threads), RTX 5070 Ti (NVIDIA 621.01), MSVC 14.44, Ninja RelWithDebInfo:

  • cpu_affinity: 38/38 checks — synthetic 8-core and 2-core SMT topologies, N=1/N=64 edges, env parsing, and the shims on a real suspended thread (confinement checked with GetThreadGroupAffinity).
  • Full runtime ctest (excluding d3d12_draw, which fails on main on this GPU independently of this change): 124/124 passed, 5 skipped.

No game files used or added. AI-assisted: written with Gemini, reviewed, built and tested with Claude Code.

A console title's affinity masks name its own cores (7 by default, SMT on).
Passed through unchanged they would pin a title onto half the host's physical
cores, or name processors the host lacks. The shims now translate:

- console core i maps to the host's i-th physical core in group 0 (both SMT
  siblings, from GetLogicalProcessorInformationEx(RelationProcessorCore) and
  the process mask), wrapping when the host has fewer cores;
- SetThreadAffinityMask, Get/SetProcessAffinityMask, SetThreadIdealProcessor
  and SetThreadIdealProcessorEx translate masks and indices both ways; a mask
  with bits outside the console's cores is ERROR_INVALID_PARAMETER;
- SetThreadSelectedCpuSets/SetProcessDefaultCpuSets pass host CPU-set IDs
  through unchanged;
- LEONIDA_TITLE_CORES (1..64) overrides the console core count.

Registered under kernel32, kernelbase and api-ms-win-core-processthreads
l1-1-0..3. One bit per console core is an assumption until the executable can
confirm it. cpu_affinity: 38/38 checks (synthetic topologies plus a real
suspended thread).

AI-assisted (written with Gemini, reviewed and tested with Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@UlteriorMotive00

Copy link
Copy Markdown
Contributor

Tested 2b81aaca on Windows 11 Home 26300, AMD Ryzen 7 9800X3D (8 cores / 16 threads, one processor group), RTX 5070 (617.42), MSVC 14.44, Ninja RelWithDebInfo: pass

AI-assisted independent run on a second machine with the same 8C/16T layout as yours, a different CPU generation (Zen 5 X3D).

  • cpu_affinity_test: 38 check(s), 0 failure(s), including the real-thread checks (SetThreadAffinityMask(thread, 0x1) confines the thread to host core 0's logical processors, the 1<<63 and N rejections, and SetThreadIdealProcessorEx). The CPU-set calls took the forwarding path.
  • With LEONIDA_TITLE_CORES=3: also 38/38.
  • Full runtime ctest: 125/125.

Not covered: hosts with more than 64 logical processors or several processor groups, and fewer physical cores than the console's 7, except through the synthetic topologies in the test.

@gkissel

gkissel commented Nov 22, 2026

Copy link
Copy Markdown

Tested 2b81aaca on a Ryzen 7 5800H (8 cores / 16 threads). The Windows build was cross-compiled with clang-cl 23.1.4 and xwin SDK 10.0.26100 and run under Wine 11.21 on Linux. That's the setup Linux players will use with Proton.

  • cpu_affinity: 37/38. The one failure is SetThreadIdealProcessorEx sets core 0 and translates previous to console index. It's Wine, not the translation. With WINEDEBUG=fixme+all, Wine prints fixme:thread:SetThreadIdealProcessorEx (...): stub and returns FALSE. (NtSetInformationThread ThreadIdealProcessor is a stub too, but SetThreadIdealProcessor still passes.)
  • Restricted process masks: taskset -c 0-3 and taskset -c 0,2,4,6 (one SMT sibling per core) give the same result, 37/38 with only that check failing. So the topology ∩ process-mask path holds when the host hides cores.
  • LEONIDA_TITLE_CORES=64: one extra failure, SetThreadIdealProcessor(thread, N) fails with ERROR_INVALID_PARAMETER. With N = 64, N equals MAXIMUM_PROCESSORS, the query value, so the call succeeds as a query. That looks like a test edge at N=64 rather than a shim bug. Maybe skip that check when N == MAXIMUM_PROCESSORS?

A suggestion for the Wine/Proton path: the ideal processor is only a scheduling hint. set_thread_ideal_processor_ex could treat a host FALSE as success, returning console core 0 as previous, instead of passing the failure to the title. On Wine, a title that checks the return value would otherwise take an error path that never happens on the console. If you'd rather keep it strict, the test could accept FALSE + a Wine stub (as the CPU-set checks already accept ERROR_CALL_NOT_IMPLEMENTED).

AI-assisted.

@grahamster2

Copy link
Copy Markdown

Tested 2b81aac merged onto main ec8ec4c (clean merge) on Windows 11 26200, Ryzen 5 7600X, MSVC 19.44 (VS 2022 Build Tools), Ninja, RelWithDebInfo: pass.

The 7600X has 6 physical cores / 12 threads, fewer than the 7 console cores, so the real-thread checks run on a host where the mapping wraps (the PR's own run was on an 8-core host).

  • cpu_affinity_test: 38 checks, 0 failures; also 38/38 with LEONIDA_TITLE_CORES=3.
  • Full ctest -j 4 with LEONIDA_VK_DEVICE=NVIDIA (RTX 3080, 610.88): 120/120.
  • Full ctest -j 2 with LEONIDA_VK_DEVICE=AMD (7600X iGPU, gfx1036, AMD 24.30.66): 119/120. The one failure is d3d12_vulkan_device at command_queue(host.p, rt_desc) == S_OK, which main has on this AMD host in an elevated shell too (Tests: skip the host realtime-queue check when the host driver refuses it #2422 handles it). Nothing from this PR.

@bielfreedom

Copy link
Copy Markdown

Tested 2b81aaca (on its base 49475796) on Windows 11 Pro build 26200, MSVC build of runtime/ (RelWithDebInfo, Ninja), AMD Radeon RX 9070 XT (driver 32.0.32029.2010), Vulkan validation on: pass.

  • On an 8-core / 16-thread CPU (AMD Ryzen 7 7700), cpu_affinity passes.
  • Full ctest: 116/116 pass, 0 failures, 0 validation errors.

AI-assisted (Claude Code). No game files.

@XFlawless1

Copy link
Copy Markdown
Contributor

Tested 2b81aaca merged onto main (ec8ec4c9): pass on both GPUs.

Windows 11 Pro 26300, MSVC 19.44 (VS 2022 Build Tools), Ninja RelWithDebInfo via scripts/build_runtime.ps1, Vulkan SDK 1.4.363.0. Full ctest -j1 once per GPU with LEONIDA_VK_DEVICE: AMD Radeon iGPU of a Ryzen 7 9800X3D (RDNA2 0x13C0, driver 32.0.21042.62) and NVIDIA RTX 5080 (621.14). AI-assisted (Claude Code). No game files.

  • Radeon: 123/123. RTX 5080: 123/123. The new cpu_affinity passes on both, here on a Ryzen 7 9800X3D: 8 cores / 16 threads, one CCD, one processor group. That makes it a second real topology alongside the 7800X3D in the description.
  • main alone at ec8ec4c9: 122/122 pass on both GPUs. The only tests not run are those that need game files or the validation layer (shader_library_corpus, bink_*, fsr_dispatch, bvh_builder, vk_sync_validation, d3d12_first_light), plus d3d12_rgb9e5_rtv on the RTX 5080. All of these are skipped on main too.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants