Skip to content

Intel: mwaitx waits sleep on the fence value (VICTORY_MWAITX_SLEEP=1) - #1029

Open
alexis27yt wants to merge 1 commit into
fxceless:mainfrom
alexis27yt:intel-mwaitx
Open

alexis27yt wants to merge 1 commit into
fxceless:mainfrom
alexis27yt:intel-mwaitx

Conversation

@alexis27yt

Copy link
Copy Markdown

On an Intel processor every monitorx and mwaitx of the title's fence wait is an illegal-instruction exception (README: ~280,000 a second in game), so one hardware thread is kept busy in Windows' exception dispatch for the whole wait. On a 6-core part that thread may share a core with the layer's submit thread.

Two switches, both off by default:

  • VICTORY_MWAITX_SLEEP=1: once a wait has spun VICTORY_MWAITX_SPIN_US (50 by default), mwaitx sleeps with WaitOnAddress on the monitored 8 bytes (up to 1 ms); fence::set calls WakeByAddressAll on the fence value when it moves. Any other writer is seen within 1 ms (timeBeginPeriod(1)). The 5-second line now counts sleeps woken and timed out.
  • VICTORY_MWAITX_PATCH=1: monitorx / mwaitx patched in place at their first exception (nop dword [rax]; pause + nop), as clwb already is.

Measured on an i5-12400F + RTX 4070 SUPER with a new hand-run test that runs the title's loop shape (monitorx, check, mwaitx, again) through the same emulation, 5 waits of 1 s each (cargo test --release -p victory-loader the_fence_wait_loop_costs -- --ignored --nocapture):

mode waiting thread's CPU exceptions/s seen late
base 99.7 % 948,544 1 µs
SLEEP 0.3 % 1,629 80 µs (worst 307 µs)
PATCH 98.4 % 0 6 µs

Not measured in game yet (frame rate effect unknown); the tester's machine has no game files. Unit tests added for the stand-in bytes, a patched page running, and a wake on a moving word; all pass on the i5-12400F. Builds clean with clippy, no new warnings.

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

Copy link
Copy Markdown
Author

Side note, not part of this PR: copies and compares on a 6-core part are bound by memory, not by thread count.

While looking at the submit thread I measured copy_jobs + find_pieces (through wide) on 256 MiB, as a frame does them, on the same i5-12400F (6 cores / 12 threads), with a local switch for the kept threads and an AVX2 equality in place of == (median of 8 rounds):

kept + calling threads compare copy GB/s compare GB/s ms per 80 MB (copy + compare)
4 (today's default here) == 24.1 25.5 6.3
7 AVX2 26.1 27.8 5.8
9 == 25.2 27.6 5.9
12 == 25.5 25.7 6.1
3 == 19.6 21.8 7.6

From 4 threads on, all settings sit at 25-27 GB/s, inside run-to-run noise: the default (available_parallelism / 4, 3 kept here) already reaches this machine's RAM. AVX2 gives nothing consistent over MSVC's memcmp. So I am not proposing either change.

What it does say: 80 MB of copy + compare costs ~6 ms of raw memory time here, against the ~80 ms a frame your README measures for those stages in game. If that holds, most of that time goes to per-range work (thousands of small ranges, locks, title reads, guard faults) rather than to moving bytes. I can't profile that without game files, but I hope the number helps.

@Proudnoob4

Copy link
Copy Markdown

Tested in-game on Windows 10 Pro (22H2), Intel i5-13600K (6P+8E), RTX 4080 16 GB (driver 621.01, Series X profile), 420 s run in the prison cell scene (ca8829f + #1014 + #1015 + #1016 + #1029 + #1032 stacked, compared against our 600 s 465b94f run with the same base switches VICTORY_ASYNC_SUBMIT=1 VICTORY_QUEUE2=1 VICTORY_COPY_PLAIN=1 VICTORY_INDIRECT_WINDOW=1 VICTORY_JOIN_PARTS=1 VICTORY_BACK_OWN_VIEW=1 VICTORY_INDIRECT_GATHER=1 plus VICTORY_RELAY=2 VICTORY_LATE_WAIT=3 VICTORY_GROUP_SYNC=2 VICTORY_WIDE_BACK=1 VICTORY_CARD_BUFFERS_MIN_MB=1 VICTORY_CARD_BUFFERS_MB=1024).

Real-game Intel i5-13600K A/B (VICTORY_MWAITX_SLEEP=1 on vs off in the same cell scene):

AI-assisted (Gemini 4 Hydrogen).

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.

2 participants