Thread affinity: console masks with processors the PC lacks are fitted to the process's (#106) #221
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!221
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/221/head"
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?
@yous0serious on GitHub · 2026-11-23 21:15 UTC · original
yous0serious:hybrid-cpu-affinity→main· +96 −0 · 2 files · 1 commits · merged 2026-11-23 21:24 UTCAddresses the "Hybrid CPU affinity masks & core counts" item of #106 – the part @ivall's triage there marked as wanted: "masks for > available cores are worth clamping". The design decision stays as it is: the title sees the PC's processors (HANDOFF, "MicrosoftGame.Config"), so nothing is remapped for masks a title builds from them.
Relates to #106 (only this item; the other items of that issue are untouched, so it should stay open).
Problem
SetThreadAffinityMask/SetThreadGroupAffinityfromkernelx/kernel32legacy/ theapi-ms-win-core-*sets went straight to Windows. A console title can pin threads with masks for the console's 7 cores / 14 threads (e.g.0x40,0x3000,0x7F). Windows answersERROR_INVALID_PARAMETER(87) and does not change the affinity when such a mask1 << 12on a 4c / 8t CPU), orstart /affinity, Process Lasso, a job object, …).A title that checks the result (most console code does: the console never fails this) asserts or takes an error path.
Measured with
SetThreadAffinityMaskdirectly on an i7-13700KF (24 logical processors, Raptor Lake hybrid):0xFFFFFF(all)0x7F0xFFFFFF0x1000000FF(one bit past the last processor)0xFFFFFF0xFF000000(only processors that do not exist)0xF(start /affinity F)0x7F,0x3F,0x40Change (
hle/kernelx.rs)SetThreadAffinityMaskandSetThreadGroupAffinity(group 0).fit_affinity_mask(mask, allowed)withallowed= the process's affinity mask:0, orallowed == 0→ unchanged;0x7Fon a 4-processor process →0xF);1 << 6on 4 processors →1 << 2), so threads pinned to different console cores stay spread instead of piling onto one.SetThreadAffinityMask: mask 0x10 names processors outside the process's 0xf – 0x1).GetLastErrorare Windows'.About the E-core part of #106
#106 also suggested console masks get pinned onto E-cores on hybrid Intel CPUs. On this i7-13700KF that is not the case: Windows numbers the P-core threads first (0–15), the E-cores last (16–23), so the console's masks (bits 0–13) land on P-cores. Nothing is changed for that here.
Tests
hle::kernelx::tests::affinity_masks_outside_the_process_are_fitted– the mask arithmetic (unchanged / cut / dealt round-robin, non-contiguous process masks).hle::kernelx::tests::set_thread_affinity_mask_takes_console_masks– on a fresh thread, the hookedSetThreadAffinityMaskwith a processor the process lacks succeeds and the thread's affinity is the fitted mask;SetThreadGroupAffinitywith a partly-outside mask succeeds. Only XCL's answer is asserted (Wine's own may differ, cf. #213).resolver::tests::console_user32_and_legacy_dlls_reach_the_pc_functions–kernel32legacy!SetThreadAffinityMask(Fortnite's import) now resolves to kernelx;GetProcessAffinityMaskstill to Windows.Run on Windows 11, i7-13700KF, RTX 4090 (driver 32.0.16.2101), Rust 1.100 MSVC:
cargo test --workspace --release --no-fail-fast -- --test-threads=4: 624 passed, 0 failed (2SKIPPED … no wave64, expected on NVIDIA), after rebasing on0cd0a53.start /affinity F <test exe> affinity): pass, with the log lines above.Overlaps
None in content. Other open PRs touch
hle/kernelx.rsin different hunks: #187, #194, #197, #203, #213 (#213 adds tests near the end ofmod tests; this PR adds its tests at the start). Rebased on currentmain(0cd0a53).AI-assisted (GitHub Copilot), reviewed and tested locally.
@ivall on GitHub · 2026-11-23 21:24 UTC · original
Merged (main
d4f17ca). Checked on an RX 9070 (RDNA4) with 11 PRs merged together on mainaf20b4b:cargo build --workspace && cargo test --workspace -- --test-threads=4- 605 unit tests passed, 0 failed (20 ignored), the integration and e2e tests passed; and the retail game still reaches its first menu at 30 fps (720 draws per 2 s).