Title mwaitx with a timer waits with tpause on WAITPKG processors #457

Open
MesoPlz wants to merge 2 commits from MesoPlz/Darner:pr-tpause-waits into Main
Contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

  • The problem on main: measured, not read from the code only. On an i7-13700K the busiest title thread used 0.6 of a core, 39% of it in the kernel. A sampling profile (DARNER_PROFILE=1, DARNER_PROFILE_THREADS=title) put 88% of that thread's busy samples in one fence wait of the title (+0x4fb0ad0 of game_scarlett_master.exe 1.0.2.1): monitorx, mwaitx with its timer (about 100 us), Sleep(0), again.

On the console mwaitx sleeps until the monitored line is written or its timer runs out. On Intel it faults, and console_insn replaces it at its first fault with pause (DARNER_PATCH_WAITS, on). So on Intel the loop went round at about a microsecond a pass, a Sleep(0) system call each time.

This change: where the processor has WAITPKG (CPUID.7.0:ECX bit 5; Intel since Alder Lake and Tremont), an mwaitx that faults with mov ecx, imm32; xor eax, eax right before it has that mov replaced by a jump to a stub. The stub waits with tpause (C0.1) until the title's timer in EBX (when imm has bit 1, as mwaitx reads it) or 32K TSC cycles, the shorter. Then it sets ECX, EAX and the flags as the original instructions leave them, and jumps back past the mwaitx. tpause does not wake on the write to the line, so a write is seen at most 32K cycles (about 10 us) late.

The jump is written with the same 16-byte compare-exchange patch and the same stub regions (put_stub) as the clzero and SSE4a replacements. The xor and the mwaitx stay in place for a thread already past the jump. Any other form (another register setup, the jump crossing 16 bytes, no WAITPKG) gets the pause replacement as before. DARNER_WAIT_TPAUSE=0 turns it off. AMD processors run mwaitx natively and never reach this.

How it was tested

  • Tested revision: 9392dea (this branch, on main d91d8e4) for the gates. The game runs used a local test build with this change on f3840d7 plus #334, #337, #274, #397, #312, #304, #295 and #95 (all off-by-default switches unset), with DARNER_WAIT_TPAUSE=0 (A) and unset (B), and later the same change on 47d21cd.

  • Commands and results:

    • cargo xtask policy: passed
    • cargo clippy --workspace --all-targets --locked -- -D warnings: passed, no warnings
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: passed, no warnings
    • cargo test --locked -p darner-loader: 93 + 2 + 2 passed, 0 failed, including the new a_timed_mwaitx_becomes_a_tpause (runs the stub when the processor has WAITPKG, as this one does).
  • Changes a default: yes, for processors with WAITPKG (Intel Alder Lake and later); the replacement for mwaitx there is the tpause stub instead of pause.

  • A/B result: the same opening scene of one save, three one-minute windows from the moment the world appears, one run each:

    A (DARNER_WAIT_TPAUSE=0) B (on), run 1 B (on), run 2
    fps (median, per minute) 32 / 32 / 32 32 / 32 / 32 32 / 32 / 32
    frame time (median) 31.6 / 31.6 / 31.7 ms 31.4 / 31.5 / 31.5 ms 31.6 / 31.8 / 31.4 ms
    kernel time of the waiting thread (10 s, GetThreadTimes) 0.21 core 0.08 core
    kernel time of the process (same 10 s) 1.58 cores 1.34 cores

    So: no frame-rate change (the thread waits for the card either way), about 0.13 of a core less in the kernel. The log says an mwaitx waits with tpause now once, at the title's site.

  • Gates run on that revision: gates 1 and 2 in full; of gate 3 only darner-loader's tests. The full cargo test --workspace was not run on 9392dea; on the test build darner-shader --lib had one failure, sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders, which fails the same way on main 47d21cd (#389, #227).

  • Gameplay: RTX 4080 16 GB (driver 621.40), i7-13700K, 64 GB, Windows 11 26300; Series X profile, 1440p with DLSS Quality, frame generation off, hardware ray tracing on.

  • Hardware dependence: only processors with WAITPKG take the new path. Tested on one (Raptor Lake). Not tested on Alder Lake, on Intel processors without WAITPKG (they keep pause), or under a hypervisor that hides WAITPKG.

  • Coverage gaps: one title build, one scene; no run longer than a few minutes; other titles' mwaitx sites only take this path if they have the same mov ecx, imm32; xor eax, eax before them.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #314 (SAFETY comments, also in console_insn.rs; no overlap in what it changes)
  • Replaces or overlaps: none found

🤖 Generated with Claude Code

https://claude.ai/code/session_01DnqmuVZnMPVji1pieTnKH3

## Contributor role (optional) - Fluxer ID: ## What it changes - The problem on main: measured, not read from the code only. On an i7-13700K the busiest title thread used 0.6 of a core, 39% of it in the kernel. A sampling profile (`DARNER_PROFILE=1`, `DARNER_PROFILE_THREADS=title`) put 88% of that thread's busy samples in one fence wait of the title (+0x4fb0ad0 of game_scarlett_master.exe 1.0.2.1): `monitorx`, `mwaitx` with its timer (about 100 us), `Sleep(0)`, again. On the console `mwaitx` sleeps until the monitored line is written or its timer runs out. On Intel it faults, and `console_insn` replaces it at its first fault with `pause` (`DARNER_PATCH_WAITS`, on). So on Intel the loop went round at about a microsecond a pass, a `Sleep(0)` system call each time. This change: where the processor has WAITPKG (CPUID.7.0:ECX bit 5; Intel since Alder Lake and Tremont), an `mwaitx` that faults with `mov ecx, imm32; xor eax, eax` right before it has that `mov` replaced by a jump to a stub. The stub waits with `tpause` (C0.1) until the title's timer in EBX (when `imm` has bit 1, as `mwaitx` reads it) or 32K TSC cycles, the shorter. Then it sets ECX, EAX and the flags as the original instructions leave them, and jumps back past the `mwaitx`. `tpause` does not wake on the write to the line, so a write is seen at most 32K cycles (about 10 us) late. The jump is written with the same 16-byte compare-exchange `patch` and the same stub regions (`put_stub`) as the `clzero` and SSE4a replacements. The `xor` and the `mwaitx` stay in place for a thread already past the jump. Any other form (another register setup, the jump crossing 16 bytes, no WAITPKG) gets the `pause` replacement as before. `DARNER_WAIT_TPAUSE=0` turns it off. AMD processors run `mwaitx` natively and never reach this. ## How it was tested - Tested revision: 9392dea (this branch, on main d91d8e4) for the gates. The game runs used a local test build with this change on f3840d7 plus #334, #337, #274, #397, #312, #304, #295 and #95 (all off-by-default switches unset), with `DARNER_WAIT_TPAUSE=0` (A) and unset (B), and later the same change on 47d21cd. - Commands and results: - `cargo xtask policy`: passed - `cargo clippy --workspace --all-targets --locked -- -D warnings`: passed, no warnings - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: passed, no warnings - `cargo test --locked -p darner-loader`: 93 + 2 + 2 passed, 0 failed, including the new `a_timed_mwaitx_becomes_a_tpause` (runs the stub when the processor has WAITPKG, as this one does). - Changes a default: yes, for processors with WAITPKG (Intel Alder Lake and later); the replacement for `mwaitx` there is the `tpause` stub instead of `pause`. - A/B result: the same opening scene of one save, three one-minute windows from the moment the world appears, one run each: | | A (`DARNER_WAIT_TPAUSE=0`) | B (on), run 1 | B (on), run 2 | |---|---|---|---| | fps (median, per minute) | 32 / 32 / 32 | 32 / 32 / 32 | 32 / 32 / 32 | | frame time (median) | 31.6 / 31.6 / 31.7 ms | 31.4 / 31.5 / 31.5 ms | 31.6 / 31.8 / 31.4 ms | | kernel time of the waiting thread (10 s, `GetThreadTimes`) | 0.21 core | | 0.08 core | | kernel time of the process (same 10 s) | 1.58 cores | | 1.34 cores | So: no frame-rate change (the thread waits for the card either way), about 0.13 of a core less in the kernel. The log says `an mwaitx waits with tpause now` once, at the title's site. - Gates run on that revision: gates 1 and 2 in full; of gate 3 only `darner-loader`'s tests. The full `cargo test --workspace` was not run on 9392dea; on the test build `darner-shader --lib` had one failure, `sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders`, which fails the same way on main 47d21cd (#389, #227). - Gameplay: RTX 4080 16 GB (driver 621.40), i7-13700K, 64 GB, Windows 11 26300; Series X profile, 1440p with DLSS Quality, frame generation off, hardware ray tracing on. - Hardware dependence: only processors with WAITPKG take the new path. Tested on one (Raptor Lake). Not tested on Alder Lake, on Intel processors without WAITPKG (they keep `pause`), or under a hypervisor that hides WAITPKG. - Coverage gaps: one title build, one scene; no run longer than a few minutes; other titles' `mwaitx` sites only take this path if they have the same `mov ecx, imm32; xor eax, eax` before them. ## Checklist - [x] I ran this change in the game, or say below why it can't be (docs, tests only). - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #314 (SAFETY comments, also in `console_insn.rs`; no overlap in what it changes) - Replaces or overlaps: none found 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01DnqmuVZnMPVji1pieTnKH3
The title's fence wait (+0x4fb0ad0, game 1.0.2.1) is monitorx, mwaitx for its
timer, Sleep(0), again. On Intel the mwaitx became pause, so the loop went round
at about a microsecond a pass: its thread 0.6 of a core, 39% in the kernel
(i7-13700K). Where the processor has WAITPKG, the mov ecx, imm32 before an
mwaitx (with xor eax, eax between) becomes a jump to a stub that waits with
tpause until the title's timer (EBX) or 32K TSC cycles, then sets ECX, EAX and
the flags as the original leaves them. DARNER_WAIT_TPAUSE=0: pause, as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01DnqmuVZnMPVji1pieTnKH3
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01DnqmuVZnMPVji1pieTnKH3
TrustNo1 changed title from [pr-performance] Title mwaitx with a timer waits with tpause on WAITPKG processors to Title mwaitx with a timer waits with tpause on WAITPKG processors 2026-12-06 03:28:46 +00:00
Owner

Test needed — commit 9392dea6; what to check:

  • The problem on main: measured, not read from the code only. On an i7-13700K the busiest title thread used 0.6 of a core, 39% of it in the kernel. A sampling profile (DARNER_PROFILE=1, DARNER_PROFILE_THREADS=title) put 88% of that thread's busy samples in one fence wait of the title (+0x4fb0ad0 of game_scarlett_master.exe 1.0.2.1): monitorx, mwaitx with its timer (about 100 us), Sleep(0), again.

On the console mwaitx sleeps until the monitored line is written or its timer runs out. On Intel it faults, and console_insn replaces it at its first fault with pause (DARNER_PATCH_WAITS, on). So on Intel the loop went round at about a microsecond a pass, a Sleep(0) system call each time.

This change: where the processor has WAITPKG (CPUID.7.0:ECX bit 5; Intel since Alder Lake and Tremont), an mwaitx that faults with mov ecx, imm32; xor eax, eax right before it has that mov replaced by a jump to a stub. The stub waits with tpause (C0.1) until the title's timer in EBX (when imm has bit 1, as mwaitx reads it) or 32K TSC cycles, the shorter. Then it sets ECX, EAX and the flags as the original instructions leave them, and jumps back past the mwaitx. tpause

Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture.

Reply testing to take this run; it's free again after 24 h with no results.

**Test needed** — commit `9392dea6`; what to check: - The problem on main: measured, not read from the code only. On an i7-13700K the busiest title thread used 0.6 of a core, 39% of it in the kernel. A sampling profile (`DARNER_PROFILE=1`, `DARNER_PROFILE_THREADS=title`) put 88% of that thread's busy samples in one fence wait of the title (+0x4fb0ad0 of game_scarlett_master.exe 1.0.2.1): `monitorx`, `mwaitx` with its timer (about 100 us), `Sleep(0)`, again. On the console `mwaitx` sleeps until the monitored line is written or its timer runs out. On Intel it faults, and `console_insn` replaces it at its first fault with `pause` (`DARNER_PATCH_WAITS`, on). So on Intel the loop went round at about a microsecond a pass, a `Sleep(0)` system call each time. This change: where the processor has WAITPKG (CPUID.7.0:ECX bit 5; Intel since Alder Lake and Tremont), an `mwaitx` that faults with `mov ecx, imm32; xor eax, eax` right before it has that `mov` replaced by a jump to a stub. The stub waits with `tpause` (C0.1) until the title's timer in EBX (when `imm` has bit 1, as `mwaitx` reads it) or 32K TSC cycles, the shorter. Then it sets ECX, EAX and the flags as the original instructions leave them, and jumps back past the `mwaitx`. `tpause` Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture. Reply `testing` to take this run; it's free again after 24 h with no results.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'pr-tpause-waits:MesoPlz-pr-tpause-waits'
git switch 'MesoPlz-pr-tpause-waits'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'Main'
git merge --no-ff 'MesoPlz-pr-tpause-waits'
git switch 'MesoPlz-pr-tpause-waits'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'MesoPlz-pr-tpause-waits'
git switch 'MesoPlz-pr-tpause-waits'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'MesoPlz-pr-tpause-waits'
git switch 'Main'
git merge --squash 'MesoPlz-pr-tpause-waits'
git switch 'Main'
git merge --ff-only 'MesoPlz-pr-tpause-waits'
git switch 'Main'
git merge 'MesoPlz-pr-tpause-waits'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!457
No description provided.