Fix/streaming pool and guards lock #334

Manually merged
github merged 3 commits from github-bot/gh-334 into main 2026-12-03 17:03:04 +00:00 AGit
Owner

@⁠delacor on GitHub · 2026-12-03 16:13 UTC · original
delacor:fix/streaming-pool-and-guards-lock → main · +22 −4 · 2 files · 3 commits · merged 2026-12-03 16:55 UTC

Streaming and guard-fault waits during load

Two waits stall frames while the world streams in. Neither is I/O and neither is
the GPU: both are a thread waiting for a lock, or for a worker.

The machine and the conditions

One machine only: Ryzen 7 3700X (8 cores, 16 threads), Radeon RX 7800 XT under
RADV 26.3.1 with Resizable BAR on (BAR0 16 GiB), Wine 11.22, Xbox Series X
profile, ray tracing off, 2560×1440 output. The frame clock followed the title's
own interval, 33.333 ms in the world, so a frame's budget is 33.3 ms.

Where every number below comes from

The emulator's own counters, printed once a second and once every five seconds:

line what it holds
dstorage: last <n> s requests in the window, MB/s, busy_workers, waiting, staging waits
R lines, VICTORY_DSTORAGE_LOG=<file> one per read: the title's Submit timestamp, the worker's start, its end

busy_workers is the sum of the per-read worker durations divided by the window
length: the average number of workers running a read at once. waiting is the
number of reads the title has submitted that no worker has begun.


Fix 1 — dstorage: the pool was eight workers, whatever the machine

What was wrong

const WORKERS: usize = 8 sized the DirectStorage worker pool independently of
the host. On this 16-thread machine, eight was the ceiling.

What it costs

A read is short. From the per-read log, the read itself, from a worker
starting it to its end:

started → done, 94,515 reads
median 0 ms
90th percentile 2 ms
99th percentile 23 ms

The wait for a worker, from the title's Submit call to a worker beginning
the read, is what costs:

submit → worker start 8 workers 16 workers (this change)
median 25 ms 25 ms
90th percentile 289 ms 219 ms
99th percentile 1451 ms 880 ms
worst 2021 ms 1513 ms

With eight workers the pool ran 7.5 of 8 busy (93 %) in its busiest sampled
five-second window, with 883 reads queued behind it. Two other suspects were
measured and cleared first:

  • the scheduler's lock: 48.6 ms of waiting per five seconds across all
    workers, 7.4 ms for the title's own thread;
  • start() refusing work: it never returns nothing while reads wait
    (starts_with_reads_waiting = 0).

So the limit was the number of workers. At sixteen the same windows show
13.3 of 16 busy (83 %).

The median does not move, and that is expected: the title delivers about 4,000
reads a second
of 12 KiB median (90th percentile 128 KiB), so the queue is
never empty. This change removes the tail, not the average.

What it does

The pool is the machine's own core count, capped at 64, by default.
VICTORY_DSTORAGE_WORKERS=<n> asks for another count. The 64 cap matters
because each worker keeps a read-back buffer of up to 4 MiB.


Fix 2 — gpu: a remap took the guards whole, under one hold

What was wrong

guard_shared took the guards' write lock and, while still holding it, removed
every block the remap had brought from the two sets that record where title
memory is. A large remap brings thousands of blocks.

What it costs

The guard-fault handler must take that same lock. Its own instrumentation
(VICTORY_FAULT_SLOW_MS) splits the handler into parts, and the parts say which
one waits:

total 14.27 ms | open the block 14.26 | guards lock 14.22 | texture 0.02 | descriptor 0.02
total 15.70 ms | open the block 15.70 | guards lock 15.68 | texture 0.00 | descriptor 0.00

14.2 of 14.3 ms is the wait for the guards lock, with the texture write and
the descriptor fallback at 0.02 ms in the same faults.

That wait lands inside a frame: one measured frame took 144.0 ms, of which
100.8 ms was the guard-fault handler's span (a fault looked at) and 25.0 ms
texture refresh.

The holder was named by a hold timer (VICTORY_GUARD_SLOW_MS): guard_shared's
per-block walk, with one hold of 167.5 ms measured and 126 holds over 3 ms
per run.

What it does

The blocks are taken 256 at a time, with the lock released between parts.
Guards-lock time per run fell from 3027 ms to 907 ms, and holds over 3 ms
from 126 to 52.

It is safe to split: the walk changes no block's protection, and a block is only
touched by the thread mapping it, so a fault that finds the lock between two
parts never looks at a half-changed state of its own block.

What is left

The holds that remain are the card writing and title memory changing, not this
walk. In a 100-second run after the fix: 2 holds over 2 ms, longest 195 ms, and
41 guard faults over 2 ms. That is about 1 ms of a second's work, not a
frame-rate problem.


Also measured and cleared

  • A larger staging buffer: VICTORY_DSTORAGE_STAGING_MB=128 against the
    title's 32 MiB made the median wait worse, 25 → 37 ms. Not the limit.
  • Reads of the card's memory: with Resizable BAR on, reads come from RAM at
    about 2 GB/s, and the read is 0.09 ms per frame of texture-refresh time.
    Texture refreshes are already run in parallel at submission
    (refresh_textures spreads the due textures over the worker threads and
    reports the wall time), so a prefetch bought nothing: the wall time is
    0.1–2.3 ms per frame, while detiling (1.59 ms) and hashing (0.69 ms) of
    thread time already overlap.

What this does not fix

Steady state was already at the title's own cap: median 30.0 fps, 90th
percentile 30.1 fps, mean frame 33.0 ms, with the frame clock following the
title. The remaining stalls are about one frame a second, and after both fixes
these two waits contribute well under a frame's budget. Further frame-rate work
has to attack the title's own recording, not these waits.

Reproducing

With this PR alone, the streaming-pool figures:

./run.sh play VICTORY_WARM=0 VICTORY_DSTORAGE_LOG=/tmp/ds.log

Read dstorage: last for busy_workers and waiting, the R lines for
submit → start, and started → done.

The guards-lock figures need the investigation branch, which carries the hold
timer and the fault handler's parts:

./run.sh play VICTORY_WARM=0 VICTORY_GUARD_SLOW_MS=3 VICTORY_FAULT_SLOW_MS=3 \
  VICTORY_TEXTURE_COSTS=1 VICTORY_LONG_FRAME_MS=50
**@⁠delacor** on GitHub · 2026-12-03 16:13 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/334) `delacor:fix/streaming-pool-and-guards-lock` → `main` · +22 −4 · 2 files · 3 commits · merged 2026-12-03 16:55 UTC # Streaming and guard-fault waits during load Two waits stall frames while the world streams in. Neither is I/O and neither is the GPU: both are a thread waiting for a lock, or for a worker. ## The machine and the conditions One machine only: Ryzen 7 3700X (8 cores, 16 threads), Radeon RX 7800 XT under RADV 26.3.1 with Resizable BAR on (BAR0 16 GiB), Wine 11.22, Xbox Series X profile, ray tracing off, 2560×1440 output. The frame clock followed the title's own interval, 33.333 ms in the world, so a frame's budget is **33.3 ms**. ## Where every number below comes from The emulator's own counters, printed once a second and once every five seconds: | line | what it holds | |---|---| | `dstorage: last <n> s` | requests in the window, MB/s, `busy_workers`, `waiting`, staging waits | | `R` lines, `VICTORY_DSTORAGE_LOG=<file>` | one per read: the title's `Submit` timestamp, the worker's start, its end | `busy_workers` is the sum of the per-read worker durations divided by the window length: the average number of workers running a read at once. `waiting` is the number of reads the title has submitted that no worker has begun. --- ## Fix 1 — `dstorage`: the pool was eight workers, whatever the machine ### What was wrong `const WORKERS: usize = 8` sized the DirectStorage worker pool independently of the host. On this 16-thread machine, eight was the ceiling. ### What it costs A read is short. From the per-read log, **the read itself**, from a worker starting it to its end: | started → done, 94,515 reads | | |---|---| | median | 0 ms | | 90th percentile | 2 ms | | 99th percentile | 23 ms | **The wait for a worker**, from the title's `Submit` call to a worker beginning the read, is what costs: | submit → worker start | 8 workers | 16 workers (this change) | |---|---|---| | median | 25 ms | 25 ms | | 90th percentile | 289 ms | **219 ms** | | 99th percentile | 1451 ms | **880 ms** | | worst | 2021 ms | **1513 ms** | With eight workers the pool ran **7.5 of 8 busy (93 %)** in its busiest sampled five-second window, with **883 reads queued** behind it. Two other suspects were measured and cleared first: - **the scheduler's lock**: 48.6 ms of waiting per five seconds across all workers, 7.4 ms for the title's own thread; - **`start()` refusing work**: it never returns nothing while reads wait (`starts_with_reads_waiting = 0`). So the limit was the number of workers. At sixteen the same windows show **13.3 of 16 busy (83 %)**. The median does not move, and that is expected: the title delivers about **4,000 reads a second** of **12 KiB median** (90th percentile 128 KiB), so the queue is never empty. This change removes the tail, not the average. ### What it does The pool is the machine's own core count, capped at 64, by default. `VICTORY_DSTORAGE_WORKERS=<n>` asks for another count. The 64 cap matters because each worker keeps a read-back buffer of up to 4 MiB. --- ## Fix 2 — `gpu`: a remap took the guards whole, under one hold ### What was wrong `guard_shared` took the guards' write lock and, while still holding it, removed every block the remap had brought from the two sets that record where title memory is. A large remap brings thousands of blocks. ### What it costs The guard-fault handler must take that same lock. Its own instrumentation (`VICTORY_FAULT_SLOW_MS`) splits the handler into parts, and the parts say which one waits: ``` total 14.27 ms | open the block 14.26 | guards lock 14.22 | texture 0.02 | descriptor 0.02 total 15.70 ms | open the block 15.70 | guards lock 15.68 | texture 0.00 | descriptor 0.00 ``` **14.2 of 14.3 ms is the wait for the guards lock**, with the texture write and the descriptor fallback at 0.02 ms in the same faults. That wait lands inside a frame: one measured frame took **144.0 ms**, of which **100.8 ms** was the guard-fault handler's span (`a fault looked at`) and 25.0 ms texture refresh. The holder was named by a hold timer (`VICTORY_GUARD_SLOW_MS`): `guard_shared`'s per-block walk, with **one hold of 167.5 ms** measured and **126 holds over 3 ms** per run. ### What it does The blocks are taken 256 at a time, with the lock released between parts. Guards-lock time per run fell from **3027 ms to 907 ms**, and holds over 3 ms from **126 to 52**. It is safe to split: the walk changes no block's protection, and a block is only touched by the thread mapping it, so a fault that finds the lock between two parts never looks at a half-changed state of its own block. ### What is left The holds that remain are the card writing and title memory changing, not this walk. In a 100-second run after the fix: 2 holds over 2 ms, longest 195 ms, and 41 guard faults over 2 ms. That is about **1 ms of a second's work**, not a frame-rate problem. --- ## Also measured and cleared - **A larger staging buffer**: `VICTORY_DSTORAGE_STAGING_MB=128` against the title's 32 MiB made the median wait **worse**, 25 → 37 ms. Not the limit. - **Reads of the card's memory**: with Resizable BAR on, reads come from RAM at about 2 GB/s, and the read is **0.09 ms per frame** of texture-refresh time. Texture refreshes are already run in parallel at submission (`refresh_textures` spreads the due textures over the worker threads and reports the wall time), so a prefetch bought nothing: the wall time is **0.1–2.3 ms per frame**, while detiling (1.59 ms) and hashing (0.69 ms) of thread time already overlap. ## What this does not fix Steady state was already at the title's own cap: median **30.0 fps**, 90th percentile 30.1 fps, mean frame **33.0 ms**, with the frame clock following the title. The remaining stalls are about one frame a second, and after both fixes these two waits contribute well under a frame's budget. Further frame-rate work has to attack the title's own recording, not these waits. ## Reproducing With this PR alone, the streaming-pool figures: ```sh ./run.sh play VICTORY_WARM=0 VICTORY_DSTORAGE_LOG=/tmp/ds.log ``` Read `dstorage: last` for `busy_workers` and `waiting`, the `R` lines for submit → start, and `started → done`. The guards-lock figures need the investigation branch, which carries the hold timer and the fault handler's parts: ```sh ./run.sh play VICTORY_WARM=0 VICTORY_GUARD_SLOW_MS=3 VICTORY_FAULT_SLOW_MS=3 \ VICTORY_TEXTURE_COSTS=1 VICTORY_LONG_FRAME_MS=50 ```
The pool was `const WORKERS: usize = 8`, whatever the machine: on the
sixteen-core test host it ran 7.48 of those 8 busy with 883 requests queued.
What a request waits for is a worker — the reads themselves take 0-2 ms —
and that wait is 25 ms at the median, 289 at the 90th percentile and 1451 at
the 99th, between the title's submit and a worker starting it.

At the machine's own sixteen the pool runs 13.33 of 16 busy and the wait's
tail halves: p90 289 -> 219 ms, p99 1451 -> 880, longest 2021 -> 1513 ms.
The median stays 25 ms, the arrival rate of about 4,000 reads a second of
66 KiB each.

Measured on the RX 7800 XT with the frame clock following the title's
interval (the fix above this one in the series); without it the run is paced
at 10 ms a frame and says nothing of a world's waits.

Two suspects were measured and cleared before the pool was found: the
scheduler's lock takes 48.6 ms of waiting per five seconds across all
workers and 7.4 ms for the title's thread, and `start()` never returns
nothing while reads wait.

`VICTORY_DSTORAGE_WORKERS=<n>` asks for another count; 64 caps a host with
hundreds of cores, where each worker keeps a read-back buffer of up to
4 MiB.
`guard_shared` took the guards alone and, still holding them, removed every
block a remap brought from the sets of what is where. A big remap brings
thousands of blocks: one hold of 167.5 ms was measured, all of it under the
lock, and a guard fault waits for that lock — 14.22 of 14.27 ms, 15.68 of
15.70, with the texture write and the descriptor fallback at 0.02 ms in the
same faults. That is a frame of 144.0 ms whose 100.78 is "a fault looked at".

The blocks are now taken 256 at a time: guards-lock time per minute fell from
3026.9 ms to 907.3 ms and holds over 3 ms from 126 to 52. The holds that
remain are the card writing and title memory changing, not this walk.

Measured on the RX 7800 XT, sixteen cores, the frame clock following the
title's interval (the fix above this one in the series).

The parts may be taken apart: the sets change no block's protection, and a
block is only touched by the thread mapping it, so a fault that finds the
lock between two parts is never looking at a half-changed state of its own
block.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-03 16:15:15 +00:00
github manually merged commit 7035066283 into main 2026-12-03 17:02:59 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.