Skip to content

Console CPU instructions: monitorx / mwaitx are rewritten in place at their first fault - #2618

Open
lukastaro wants to merge 1 commit into
xcl-dev:mainfrom
lukastaro:pr-patch-waits
Open

lukastaro wants to merge 1 commit into
xcl-dev:mainfrom
lukastaro:pr-patch-waits

Conversation

@lukastaro

@lukastaro lukastaro commented Nov 26, 2026 •

Copy link
Copy Markdown

A title that paces its frames with monitorx / mwaitx spins on them. On a CPU without them (Intel) every one raises an illegal-instruction exception that eh emulates, the same instruction again and again. clwb is already rewritten to nops at its first fault; this does the same for these two:

  • monitorx → nops, mwaitx → pause + nops (a wait that ended at once; the title checks its own condition again, as it must after any mwaitx).
  • The bytes are written with one atomic 8-byte compare-exchange when the instruction lies inside an aligned 8-byte word, so a thread at the same instruction runs either the old one (and is emulated) or the new one, never a mix. Otherwise the instruction stays emulated as before.

Tested

  • NVIDIA GeForce RTX 5060 Ti (driver 621.40), Intel Core i7-14700K, Windows 10 22H2 (19045), release builds.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 19aa5c2: 1426 passed, 11 failed, 47 ignored. On main b49be18 the same run: 1425 passed, 10 failed, 47 ignored, the same 10 failures (fuzz_validate_and_gpu_0–_3, the two link_tests, vram::tests::d32s8_targets_take_their_own_size_of_video_memory and the three test_title_* integration tests). The 11th, list_x::tests::a_reset_while_the_last_submit_runs_records_on_another_pc_list, passed 3 of 3 times alone.
  • New tests: monitorx_and_mwaitx_are_rewritten_at_their_first_fault (both rewritten, rip stepped over them), code_across_an_aligned_word_is_not_rewritten.
  • A retail title on the machine above (a lab tree with this change; same build and settings with and without it, one run each): two sites rewritten at 3.9 s; in its world 1.24 → 2–3.5 presents per second.

Regression run

main:

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 5060 Ti
title: eccf25ce
seconds: 60
exit: 0x0
frames shown: 1489 (PresentX 1489)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15106 MB

PR:

xcl-smoke v1
commit: 19aa5c28b3ae4ed912c36c7a2fd18dae59d46c13
gpu: NVIDIA GeForce RTX 5060 Ti
title: eccf25ce
seconds: 60
exit: 0x0
frames shown: 1588 (PresentX 1588)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15252 MB

The 60 s stay in the title's menus; the two sites are rewritten in the first seconds.

Not tested

  • AMD CPUs: they run monitorx / mwaitx natively and never reach this path.
  • An instruction that crosses an aligned 8-byte word in a real title: it stays emulated, as before (covered by the second test only).
  • Other titles than the one above.

AI use

  • Claude Code (Claude Opus 5.5) wrote the change, the tests and this description; the runs above were made on the hardware named.

🤖 Generated with Claude Code

… their first fault

A title that paces its frames with monitorx / mwaitx spins on them; on a CPU
without them every one was an emulated illegal-instruction exception. Like
clwb, each site is now rewritten at its first fault: monitorx to nops, mwaitx
to pause + nops. The bytes go in with one atomic 8-byte compare-exchange when
the instruction lies inside an aligned 8-byte word; otherwise it stays emulated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016SP4yAAy7ztGPWPR7JHKKa
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 26 ignored.

Thanks!

— Devin

@luckyteagirl luckyteagirl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two source-derived concurrency points on the current head. No local build or title run. AI-assisted source review: OpenAI Codex (GPT-6; exact serving variant unavailable) and a Luna subagent.

Comment thread crates/xcl/src/eh.rs
let mut bytes = was.to_le_bytes();
bytes[off..off + new.len()].copy_from_slice(new);
// another thread may have rewritten it first: then it holds the same bytes already
let _ = cell.compare_exchange(was, u64::from_le_bytes(bytes), Ordering::AcqRel, Ordering::Acquire);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A failed compare_exchange is currently ignored, but another thread may have rewritten a different monitorx/mwaitx site in this same aligned 8-byte word. If both load the original word, one CAS wins and the other loses; the losing site remains unchanged even though rewrite_code returns true, so later visits fault again. Could this retry after a lost CAS by reloading and merging only the requested bytes? A deterministic test with two adjacent sites would cover it. This is a source-derived interleaving, not a measured title failure.

Comment thread crates/xcl/src/eh.rs
// SAFETY: `word` is the aligned word around code the processor just fetched; it is made
// writable for the one store and given its protection back
unsafe {
if VirtualProtect(word as *const c_void, 8, PAGE_EXECUTE_READWRITE, &mut old) == 0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VirtualProtect(word, 8, ...) changes the whole containing page. Two concurrent calls on that page can save RX/RWX respectively; if the first restores RX before the second restores its saved RWX, the page stays writable and executable. SEEN is unlocked before this span. Could the protect/CAS/restore span be serialized for calls touching the same page, with a concurrent-patch test asserting the original protection afterwards? This is a source-derived interleaving, not a measured title failure.

@DavidM1911

Copy link
Copy Markdown
Contributor

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…tream xcl-dev#2618)

Windows eh.rs hunks verbatim reference-only (cfg(windows)-only):
rewrite_code (single atomic 8-byte store inside one aligned word, so a
concurrent thread runs the old or the new instruction, never a mix),
the Monitorx (nops) / Mwaitx (pause + nops) first-fault arms, and both
upstream tests verbatim.

Linux eh_linux.rs twin: rewrite_code via mprotect(PROT_READ|PROT_WRITE|
PROT_EXEC) over the page range covering the word, restoring the previous
protection read from /proc/self/maps (x86(-64) needs no cache flush: its
I-cache is coherent with data writes); cross-word instructions stay
emulated. Tests: code_across_an_aligned_word_is_not_rewritten verbatim,
plus a mmap-backed monitorx/mwaitx rewrite test with the same byte
sequences (no emulate_console_only call site exists on Linux yet, so the
helper is test-only for now: one dead_code warning, like the tree's
others).

Failing-first: with the store disabled both Linux tests FAILED, green
after; eh group 18/18 green.
bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 26, 2026
Repeated monitorx and mwaitx emulation can raise tens of thousands of illegal-instruction exceptions per second on Intel CPUs. Replace each aligned site atomically with nops or pause plus nops after its first fault.

Serialize page protection changes, retry word replacement after interference, preserve adjacent rewrites, restore RX protection, flush the instruction cache, and recognize exceptions already in flight at a newly rewritten site.

Adapted from PR xcl-dev#2618 by Tarosul and Claude Code (Claude Opus 5.5). That PR measured a retail workload improving from 1.24 to 2-3.5 presents/s. The Windows GNU cross-target check passes; native Windows tests and smoke were unavailable here.

Assisted-by: Codex (GPT-5.6-sol)
bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 26, 2026
Repeated monitorx and mwaitx emulation can raise tens of thousands of illegal-instruction exceptions per second on Intel CPUs. Replace each aligned site atomically with nops or pause plus nops after its first fault.

Serialize page protection changes, retry word replacement after interference, preserve adjacent rewrites, restore RX protection, flush the instruction cache, and recognize exceptions already in flight at a newly rewritten site.

Adapted from PR xcl-dev#2618 by Tarosul and Claude Code (Claude Opus 5.5). That PR measured a retail workload improving from 1.24 to 2-3.5 presents/s. The Windows GNU cross-target check passes; native Windows tests and smoke were unavailable here.

Assisted-by: Codex (GPT-5.6-sol)
@kole07139

Copy link
Copy Markdown

The case in your "Not tested" list (an instruction across an aligned 8-byte word in a real title) happens on another build of the retail title (title: 16b5e5d4), and it is the monitorx site: only mwaitx is rewritten, so monitorx stays emulated every time it runs. Two runs per arm show no change in the world's present rate, but that does not test the PR's idea while half of it is inactive.

What the log shows

I added a local log line (not part of the PR) in the two new match arms, printing what rewrite_code returned:

lab: monitorx in <title exe>+0x4fb0bd7 rewritten in place: false
lab: mwaitx   in <title exe>+0x4fb0c00 rewritten in place: true
lab: monitorx in <title exe>+0x4fb0bd7 rewritten in place: false   (the same line at each later fault of that site, up to the log's cap)

In the title's .text, +0x…bd7 holds 0F 01 FA: monitorx, 3 bytes, no prefix, at bd7–bd9. So off = 7 and off + len > 8. rewrite_code refuses it by design, which is the case code_across_an_aligned_word_is_not_rewritten tests. The image base is 64 KiB-aligned, so this happens on every run of this build. Your runs used title: eccf25ce, where both sites were rewritten, so that build's monitorx must lie inside one word.

A possible way to cover it (not tried): bd7–bd9 lie inside the aligned 16-byte block bd0–bdf. A lock cmpxchg16b on that block would give the same guarantee as the 8-byte exchange: a thread runs either the old instruction or the new one.

Numbers

Same tree, same cache snapshot copied for each run, 300 s each, runs alternated (3d, 3e, 3d, 3e). Presents are counted in fixed windows after the title's first ListX #101, which is where it enters the world:

run this PR first #101 presents 0–30 s after 30–95 s after removal / crash
Y2 no 82.4 s 104 14 none
Y4 no 81.9 s 159 15 none (presents stop at 181 s on a 28 s cold compute pipeline build)
Y3 yes 87.9 s 136 15 none
Y5 yes 101.6 s 25 14 none

From 30 s after entry, all four runs present about 0.23 times a second with or without the PR. At that rate this tree is bound by something other than the title's CPU pacing loop. So these runs do not show whether the rewrite helps: monitorx is still emulated, and the frame rate is held down elsewhere.

Tested

Not tested

  • The whole suite; title: eccf25ce; a version that also rewrites a monitorx across a word; more than two runs per arm; AMD (not relevant: AMD CPUs run these natively).

AI use

  • Claude Code (Claude Opus 5.5) built the trees, added the local log line, ran the series, read the logs and wrote this report.

@throny

throny commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Test report: in the world on an Intel CPU with an RTX 4080 16 GB. No gain here, because this machine waits for the GPU; one figure worth a look in the intro.

Tested

without (3 runs) with (2 runs)
presents per second in the world 0.18, 0.21, 0.20 0.21, 0.22
presents per second, 40–120 s (intro and loading) 30.1, 30.1, 30.1 27.3, 28.3
end device removed in 3 of 3 device removed in 2 of 2
  • In the world this card is limited by the GPU (93 % of the submitting thread's time is inside PresentX, see D3D12.X: instrument submitting-thread call breakdown under XCL_LOOK_STATS=2 #2729), so the CPU time this PR gives back cannot show as frames here. That fits your numbers coming from a setup where the CPU was the limit.
  • The intro figure is the part to check: with the PR the 30 fps stretch reads 27–28 in both runs. Two runs against three, and the window may simply start a little later (I did not line up the first frame), so I would not call it a regression yet; but a pause loop where the title expected to sleep could show up exactly like that.
  • No crash or hang from the rewrite.

Not tested

  • CPU use of the title's waiting threads before and after (that would show the PR's effect directly on this machine), the PR's test, the suite.

AI use: Claude Code (Claude Fable 5.2) ran this on the owner's machine and wrote the report.

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.

5 participants