Repository navigation
Thread affinity: console masks with processors the PC lacks are fitted to the process's (#106) - #221
Merged
Merged
Conversation
…d to the process's (xcl-dev#106) The title sees the PC's processors, so SetThreadAffinityMask / SetThreadGroupAffinity masks made from them pass unchanged. A console mask (its 7 cores / 14 threads) naming processors outside the process's affinity – a PC with fewer processors, or a process limited to some – made Windows fail the call with ERROR_INVALID_PARAMETER: when none of its processors is there, or any is outside the process's mask. Such a mask is now cut to the processors there are; when none is left, each console processor goes to the PC's processor at its index modulo their count. Logged once per mask. Systems with more than one processor group pass masks unchanged. Measured on an i7-13700KF (24 logical processors, P-cores 0-15 first): 0x7F passes, 0xFF000000 fails (87); restricted to 0xF (start /affinity F) 0x7F, 0x3F and 0x40 all fail (87). The tests check XCL's answer only (Wine's own may differ), also run under start /affinity F. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
yous0serious
force-pushed
the
hybrid-cpu-affinity
branch
from
November 23, 2026 21:17
1c929aa to
7b029e2
Compare
Collaborator
|
Merged (main d4f17ca). Checked on an RX 9070 (RDNA4) with 11 PRs merged together on main af20b4b: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses 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. Tests under Wine: Windows' own answers are checked only on Windows #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.