[pr-performance] Remote: pins written with the guarded copy, not WriteProcessMemory (cam-city median frame 111 -> 65-77 ms) #361

Open
cafe wants to merge 1 commit from cafe/remote-pins-safecopy into main AGit
Contributor

Contributor role (optional)

  • Fluxer ID: 1577752157942198273 (Cafe#9189)

What it changes

  • The problem on main: the bench remote (bench-remote builds, DARNER_REMOTE) writes every pin with WriteProcessMemory on our own process, at each Present and at every 8 ms pad poll. WriteProcessMemory asks the kernel about each page (NtQueryVirtualMemory) under the process's address-space lock, which GetWriteWatch (crate::watch) takes exclusively. In a CPU profile (WPR) of the kit's cam-city flight, the submitting threads waited about 370 ms a second in GetWriteWatch for that lock, and the pin writes were the main holder. So benchmark runs measured a slower Darner than players get.

write_own now tries safecopy::copy first (a plain copy with a fault exit, no kernel call) and falls back to WriteProcessMemory only when that fails, so pins on pages the copy cannot write (read-only image data with DARNER_REMOTE_PATCH=1) work as before. Only bench-remote builds run this code; release builds are unchanged. (#115 fixed the same lock contention for readable_len.)

How it was tested

  • Tested revision: main f3840d73d + the four translator forms of #312 + this commit (1df3ea537), bench-remote release build, against the same build without this commit.

  • Changes a default: no (benchmark builds only).

  • A/B result (RTX 2080 Ti, the kit's cam-city route, whole 172 s flight, pipelines warm, the kit unchanged, hardware-accelerated GPU scheduling on):

    A (WriteProcessMemory) B (this PR)
    median frame 110.8 ms 65.0, 76.5 ms
    fps 8.5 14.4, 12.9
    layer preparing (prep_ms, median) 102.0 ms 52.5, 65.1 ms
    card idle (card_idle_ms, median) 51.6 ms 1.2, 20.3 ms
    card busy (card_lists_ms, median) 56.1 ms 50.6, 50.5 ms

    The card's own time does not change; the frames were waiting on the CPU side. Earlier, with GPU scheduling off and the same copy without the fallback: 114.7, 118.6 -> 94.5, 97.0 ms (7.4, 8.2 -> 10.3, 10.0 fps).

  • Gates on 1df3ea537: cargo build --locked --release -p darner --features bench-remote ok; cargo clippy --locked --release -p darner-gpu --all-targets --features bench-remote -- -D warnings ok; cargo clippy --locked --release -p darner-shader -p darner-gpu --all-targets -- -D warnings ok. Not run: tests (nothing here has a test; the remote needs a running title).

  • Kit check: the kit's own checks passed in every run (camera at the start, pins status 1, route end reached).

  • Gameplay: RTX 2080 Ti, Windows 10 22H2, Series S profile, 2560x1440 output, DLSS Performance, hardware ray tracing, no frame generation.

  • Hardware dependence: none expected (a CPU-side lock); measured on one PC only.

  • Coverage gaps: one route; not measured with DARNER_REMOTE_PATCH=1 pins in the image (they take the fallback, as before).

  • Note: the kit itself also writes the same rows from outside with WriteProcessMemory before pinning them (foot.hover), which holds the same lock; skipping those 8 duplicated writes took the route a further 95 -> 88 ms (one run). That is in the kit, not in this repository.

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: #115 (merged: the same lock, for readable_len); #150 (write-watch tests) and #152 (A/B docs) touch nearby topics, not this code.
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: `1577752157942198273` (Cafe#9189) ## What it changes - The problem on main: the bench remote (`bench-remote` builds, `DARNER_REMOTE`) writes every pin with `WriteProcessMemory` on our own process, at each Present and at every 8 ms pad poll. `WriteProcessMemory` asks the kernel about each page (`NtQueryVirtualMemory`) under the process's address-space lock, which `GetWriteWatch` (`crate::watch`) takes exclusively. In a CPU profile (WPR) of the kit's cam-city flight, the submitting threads waited about 370 ms a second in `GetWriteWatch` for that lock, and the pin writes were the main holder. So benchmark runs measured a slower Darner than players get. `write_own` now tries `safecopy::copy` first (a plain copy with a fault exit, no kernel call) and falls back to `WriteProcessMemory` only when that fails, so pins on pages the copy cannot write (read-only image data with `DARNER_REMOTE_PATCH=1`) work as before. Only `bench-remote` builds run this code; release builds are unchanged. (#115 fixed the same lock contention for `readable_len`.) ## How it was tested - Tested revision: `main` f3840d73d + the four translator forms of #312 + this commit (1df3ea537), `bench-remote` release build, against the same build without this commit. - Changes a default: no (benchmark builds only). - A/B result (RTX 2080 Ti, the kit's cam-city route, whole 172 s flight, pipelines warm, the kit unchanged, hardware-accelerated GPU scheduling on): | | A (`WriteProcessMemory`) | B (this PR) | |---|---|---| | median frame | 110.8 ms | 65.0, 76.5 ms | | fps | 8.5 | 14.4, 12.9 | | layer preparing (`prep_ms`, median) | 102.0 ms | 52.5, 65.1 ms | | card idle (`card_idle_ms`, median) | 51.6 ms | 1.2, 20.3 ms | | card busy (`card_lists_ms`, median) | 56.1 ms | 50.6, 50.5 ms | The card's own time does not change; the frames were waiting on the CPU side. Earlier, with GPU scheduling off and the same copy without the fallback: 114.7, 118.6 -> 94.5, 97.0 ms (7.4, 8.2 -> 10.3, 10.0 fps). - Gates on 1df3ea537: `cargo build --locked --release -p darner --features bench-remote` ok; `cargo clippy --locked --release -p darner-gpu --all-targets --features bench-remote -- -D warnings` ok; `cargo clippy --locked --release -p darner-shader -p darner-gpu --all-targets -- -D warnings` ok. Not run: tests (nothing here has a test; the remote needs a running title). - Kit check: the kit's own checks passed in every run (camera at the start, pins status 1, route end reached). - Gameplay: RTX 2080 Ti, Windows 10 22H2, Series S profile, 2560x1440 output, DLSS Performance, hardware ray tracing, no frame generation. - Hardware dependence: none expected (a CPU-side lock); measured on one PC only. - Coverage gaps: one route; not measured with `DARNER_REMOTE_PATCH=1` pins in the image (they take the fallback, as before). - Note: the kit itself also writes the same rows from outside with `WriteProcessMemory` before pinning them (`foot.hover`), which holds the same lock; skipping those 8 duplicated writes took the route a further 95 -> 88 ms (one run). That is in the kit, not in this repository. ## 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: #115 (merged: the same lock, for `readable_len`); #150 (write-watch tests) and #152 (A/B docs) touch nearby topics, not this code. - Replaces or overlaps: none found
First-time contributor

A/B on a second card and PC: RTX 5070 12 GB. The fix does here what it says, with a larger effect.

Setup: i9-14900F, 32 GB, Windows 11 (build 26300), driver 621.40. Series X profile, 2560x1440 output, DLSS Quality, hardware ray tracing, no frame generation. The kit's cam-city route and bench-base save, the flight itself unchanged. A = main f3840d7, B = this PR's head 1df3ea5, both cargo build --locked --release -p darner --features bench-remote, same translator, shared warm shader cache. Flights alternated B, A, (a third config, not part of this), three rounds. Figures are the two scored parts of each flight (tag_seg 1 and 2 of the record).

A1 A2 A3 B1 B2 B3
median frame, ms 197.0 149.7 148.3 94.3 86.0 81.7
p95, ms 381 272 245 186 152 127
fps (frames / time) 4.47 5.96 6.01 9.23 10.65 11.28
prep_ms, median 173.4 134.2 131.8 72.8 64.3 65.9
card_idle_ms, median 158.1 111.5 105.9 53.1 44.1 39.8
card_lists_ms, median 34.1 35.8 35.8 37.2 36.1 39.9

As in your table, the card's own time does not move; the difference is all on the CPU side. Every flight listed passed the kit's own checks (camera on 100 % of updates, route end reached).

One flight not in the table: the first attempt at A3 hung partway through the route (0x887A0006, no page fault in the device-removed data) and was flown again. That is 1 hang in 4 flights without the fix and 0 in 6 flights with it, too few to say anything about the cause.

Limits: one PC; free physical memory was low during the flights (about 0.6-1.5 GB), so the absolute numbers are probably worse than on a PC with more RAM; the game's F1 settings are the ones from the author's wrapper with ray tracing and DLSS set as above.

Written with an AI assistant (Claude); the figures are measured on this PC.

A/B on a second card and PC: **RTX 5070 12 GB**. The fix does here what it says, with a larger effect. **Setup:** i9-14900F, 32 GB, Windows 11 (build 26300), driver 621.40. Series X profile, 2560x1440 output, DLSS Quality, hardware ray tracing, no frame generation. The kit's cam-city route and bench-base save, the flight itself unchanged. A = `main` f3840d7, B = this PR's head 1df3ea5, both `cargo build --locked --release -p darner --features bench-remote`, same translator, shared warm shader cache. Flights alternated B, A, (a third config, not part of this), three rounds. Figures are the two scored parts of each flight (`tag_seg` 1 and 2 of the record). | | A1 | A2 | A3 | B1 | B2 | B3 | |---|---|---|---|---|---|---| | median frame, ms | 197.0 | 149.7 | 148.3 | 94.3 | 86.0 | 81.7 | | p95, ms | 381 | 272 | 245 | 186 | 152 | 127 | | fps (frames / time) | 4.47 | 5.96 | 6.01 | 9.23 | 10.65 | 11.28 | | `prep_ms`, median | 173.4 | 134.2 | 131.8 | 72.8 | 64.3 | 65.9 | | `card_idle_ms`, median | 158.1 | 111.5 | 105.9 | 53.1 | 44.1 | 39.8 | | `card_lists_ms`, median | 34.1 | 35.8 | 35.8 | 37.2 | 36.1 | 39.9 | As in your table, the card's own time does not move; the difference is all on the CPU side. Every flight listed passed the kit's own checks (camera on 100 % of updates, route end reached). **One flight not in the table:** the first attempt at A3 hung partway through the route (`0x887A0006`, no page fault in the device-removed data) and was flown again. That is 1 hang in 4 flights without the fix and 0 in 6 flights with it, too few to say anything about the cause. **Limits:** one PC; free physical memory was low during the flights (about 0.6-1.5 GB), so the absolute numbers are probably worse than on a PC with more RAM; the game's F1 settings are the ones from the author's wrapper with ray tracing and DLSS set as above. Written with an AI assistant (Claude); the figures are measured on this PC.
Contributor

cam-city ABBA Benchmark on RTX 4090 + 7950X3D: Confirmed ~20% CPU-Side Win

Tested 1df3ea5 against main (f3840d7) in a full ABBA sequence (A1 -> B1 -> B2 -> A2) on the full 172 s cam-city route using bench-kit (with the foot.py pinsfix update).

Per-Run Results (all tagged rows, ~174–176 s)

Run Build Overall FPS Median FPS Median ms p95 ms p99 ms >100 ms prep_ms (med) card_idle_ms (med) card_lists_ms (med) pipe_made
A1 main (f3840d7) 8.25 9.38 106.63 149.72 209.70 61.7% 91.98 68.85 30.18 15
B1 PR #361 (1df3ea5) 10.63 12.42 80.52 118.98 165.48 14.6% 70.07 48.88 28.02 15
B2 PR #361 (1df3ea5) 12.47 12.71 78.67 110.59 144.05 9.9% 65.48 46.90 27.87 3
A2 main (f3840d7) 9.74 10.80 92.57 135.45 169.81 39.3% 83.50 61.45 28.90 3
Mean A main 9.00 10.09 99.60 142.58 189.75 50.5% 87.74 65.15 29.54 —
Mean B PR #361 11.55 (+28.4%) 12.57 (+24.5%) 79.59 (-20.1%) 114.78 (-19.5%) 154.76 (-18.4%) 12.3% 67.78 (-22.8%) 47.89 (-26.5%) 27.95 (-5.4%) —

Segment Breakdown (Median Frame Time)

  • Segment 1 (fast): A = 115.32, 108.08 ms (mean 111.70 ms) → B = 87.45, 88.11 ms (mean 87.78 ms, -21.4%)
  • Segment 2 (slow): A = 90.96, 87.40 ms (mean 89.18 ms) → B = 73.11, 70.83 ms (mean 71.97 ms, -19.3%)

Summary

  • Clear, repeatable win reproducing the PR's findings. Even comparing the two fully warmed tail runs (B2 vs A2, both pipe_made=3), median frame time drops from 92.57 ms to 78.67 ms (-15.0%) and overall FPS rises from 9.74 to 12.47 (+28.0%).
  • prep_ms (-22.8%) and card_idle_ms (-26.5%) account for nearly the entire improvement while GPU card_lists_ms stays flat (~28–29.5 ms), confirming the CPU-side address-space lock contention fix.
  • All 4 runs (plus an unmeasured warm-up on main) passed cam-bench.py clean checks (RESULT: camera route flown, clean) with zero crashes or device losses.

Environment

  • Hardware / OS: NVIDIA GeForce RTX 4090 (24 GB, driver 621.14), AMD Ryzen 9 7950X3D, Windows 11 Pro (build 26300).
  • Build: cargo build --release --locked -p darner --features bench-remote.
  • Settings: bench-kit (pinsfix foot.py), cam-city (172 s), Series X profile, HW RT enabled (DARNER_HW_RT=1, DARNER_BVH_BUILDER=1), play.rs DEFAULT_ENV tuning, no frame generation.
### `cam-city` ABBA Benchmark on RTX 4090 + 7950X3D: Confirmed ~20% CPU-Side Win Tested **`1df3ea5`** against **`main` (`f3840d7`)** in a full ABBA sequence (`A1 -> B1 -> B2 -> A2`) on the full 172 s `cam-city` route using `bench-kit` (with the `foot.py` pinsfix update). #### Per-Run Results (`all tagged rows`, ~174–176 s) | Run | Build | Overall FPS | Median FPS | Median ms | p95 ms | p99 ms | >100 ms | `prep_ms` (med) | `card_idle_ms` (med) | `card_lists_ms` (med) | `pipe_made` | |---|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:| | A1 | `main` (`f3840d7`) | 8.25 | 9.38 | 106.63 | 149.72 | 209.70 | 61.7% | 91.98 | 68.85 | 30.18 | 15 | | B1 | PR #361 (`1df3ea5`) | 10.63 | 12.42 | 80.52 | 118.98 | 165.48 | 14.6% | 70.07 | 48.88 | 28.02 | 15 | | B2 | PR #361 (`1df3ea5`) | 12.47 | 12.71 | 78.67 | 110.59 | 144.05 | 9.9% | 65.48 | 46.90 | 27.87 | 3 | | A2 | `main` (`f3840d7`) | 9.74 | 10.80 | 92.57 | 135.45 | 169.81 | 39.3% | 83.50 | 61.45 | 28.90 | 3 | | **Mean A** | **`main`** | **9.00** | **10.09** | **99.60** | **142.58** | **189.75** | **50.5%** | **87.74** | **65.15** | **29.54** | — | | **Mean B** | **PR #361** | **11.55 (+28.4%)** | **12.57 (+24.5%)** | **79.59 (-20.1%)** | **114.78 (-19.5%)** | **154.76 (-18.4%)** | **12.3%** | **67.78 (-22.8%)** | **47.89 (-26.5%)** | **27.95 (-5.4%)** | — | #### Segment Breakdown (Median Frame Time) - **Segment 1 (fast):** A = `115.32, 108.08 ms` (mean `111.70 ms`) → B = `87.45, 88.11 ms` (mean `87.78 ms`, **-21.4%**) - **Segment 2 (slow):** A = `90.96, 87.40 ms` (mean `89.18 ms`) → B = `73.11, 70.83 ms` (mean `71.97 ms`, **-19.3%**) #### Summary - **Clear, repeatable win** reproducing the PR's findings. Even comparing the two fully warmed tail runs (`B2` vs `A2`, both `pipe_made=3`), median frame time drops from `92.57 ms` to `78.67 ms` (**-15.0%**) and overall FPS rises from `9.74` to `12.47` (**+28.0%**). - `prep_ms` (-22.8%) and `card_idle_ms` (-26.5%) account for nearly the entire improvement while GPU `card_lists_ms` stays flat (~28–29.5 ms), confirming the CPU-side address-space lock contention fix. - All 4 runs (plus an unmeasured warm-up on `main`) passed `cam-bench.py` clean checks (`RESULT: camera route flown, clean`) with zero crashes or device losses. #### Environment - **Hardware / OS:** NVIDIA GeForce RTX 4090 (24 GB, driver `621.14`), AMD Ryzen 9 7950X3D, Windows 11 Pro (build `26300`). - **Build:** `cargo build --release --locked -p darner --features bench-remote`. - **Settings:** `bench-kit` (`pinsfix` `foot.py`), `cam-city` (172 s), Series X profile, HW RT enabled (`DARNER_HW_RT=1`, `DARNER_BVH_BUILDER=1`), `play.rs` `DEFAULT_ENV` tuning, no frame generation.
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 origin '+refs/pull/361/head:cafe/remote-pins-safecopy'
git switch 'cafe/remote-pins-safecopy'

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 'cafe/remote-pins-safecopy'
git switch 'cafe/remote-pins-safecopy'
git rebase 'main'
git switch 'main'
git merge --ff-only 'cafe/remote-pins-safecopy'
git switch 'cafe/remote-pins-safecopy'
git rebase 'main'
git switch 'main'
git merge --no-ff 'cafe/remote-pins-safecopy'
git switch 'main'
git merge --squash 'cafe/remote-pins-safecopy'
git switch 'main'
git merge --ff-only 'cafe/remote-pins-safecopy'
git switch 'main'
git merge 'cafe/remote-pins-safecopy'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
3 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!361
No description provided.