HLE/dstorage: a queue's unsubmitted requests under its own lock #934

Open
0xkn0wh3r3 wants to merge 1 commit from upstream-pr/934 into main
0xkn0wh3r3 commented 2026-11-28 09:24:32 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by 0xkn0wh3r3 on 2026-11-28T09:24:32Z.


Based directly on main; independent of #933 (kernel writes without the guards' lock), which goes with it.

Why

Every enqueue of the title's (up to 4,095 requests a batch) took the DirectStorage scheduler's single lock (Lanes), which every worker also takes to start and to finish each read. So the title's thread queued behind the workers for each request it enqueued. Every submit also woke all the workers at once, whatever the batch held.

What

  • Each queue keeps what is enqueued and not yet submitted under a lock of its own (Unsubmitted, shared by the queue object and its lane).
  • What the scheduler holds of the queue (reads not started, statuses not written, reads running) is mirrored in an atomic, stored under the scheduler's lock after every change. It only grows at a submit, and a submit holds the queue's lock too. So an enqueue that finds room under the queue's lock alone really has it.
  • A full queue takes the scheduler's lock as before: it submits itself if nothing else could make room (a_full_queue_submits_itself), or waits for a finish with the queue's lock let go, so the title's submit is not held up.
  • A submit moves the commands in under both locks, the queue's first. That order is the same everywhere both are taken, so batches keep their order and status entries their place.
  • A submit wakes one worker per new read, up to the number of workers, instead of all of them. A worker goes on to the next read when it finishes one, so no read waits for a worker that wasn't woken. A start refused for staging space still wakes everyone at the next finish, as before.
  • VICTORY_DSTORAGE_QUEUE_LOCAL=0 turns it off: every enqueue takes the scheduler's lock and a submit wakes every worker, as before.

Before / after

cargo test --release -p victory-hle --lib enqueues_beside_busy_workers -- --ignored --nocapture (added here). Two threads, each with a queue of the title's size (4,159 entries), enqueue batches of 4,095 copies of 4 KiB from memory and a status entry, submit, and wait for the status, while all 24 workers run them. Runs with the switch on and off take turns, 7 of each, 1.5 s each; medians of the runs, on this branch:

title's thread enqueuing a batch enqueue 99th percentile batch, enqueue to status reads/s
before (VICTORY_DSTORAGE_QUEUE_LOCAL=0) 4.27 ms 30.5 us 10.77 ms 758,579
this branch 0.40 ms 0.5 us 10.85 ms 755,191

The enqueue time was lower in every pair of runs. End-to-end throughput is unchanged: the workers are the limit in this test, so what the change gives back is the title's thread's time.

Correctness

  • batches_of_many_threads_complete_in_order: four threads, each with a queue of 128 entries, enqueue batches of 100 reads and a status entry and submit, so each batch waits for room the one before leaves. Every status lands only after every read before it (its bytes are all there when it does). Run with the switch on and off.
  • Unchanged and passing: a_full_queue_submits_itself, half_a_queue_waits_for_the_submit, a_status_waits_for_every_earlier_read, statuses_complete_in_order_with_the_first_failure, priorities_share_by_bytes_about_tenfold, staging_bounds_compressed_bytes_in_flight, a_closed_queue_completes_nothing.
  • With VICTORY_DSTORAGE_HALF=1, only half_a_queue_waits_for_the_submit fails, since it asserts the opposite of half-submits.
  • Locally on this branch: the dstorage tests pass in three runs out of three; cargo test --workspace --locked passes with no failures; clippy with -D warnings reports nothing in dstorage.rs in either build (it reports one finding in gameinput.rs, already on main).
  • Not yet measured in play.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5538704045 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/934). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-28T09:24:32Z. --- Based directly on `main`; independent of #933 (kernel writes without the guards' lock), which goes with it. ## Why Every enqueue of the title's (up to 4,095 requests a batch) took the DirectStorage scheduler's single lock (`Lanes`), which every worker also takes to start and to finish each read. So the title's thread queued behind the workers for each request it enqueued. Every submit also woke all the workers at once, whatever the batch held. ## What - Each queue keeps what is enqueued and not yet submitted under a lock of its own (`Unsubmitted`, shared by the queue object and its lane). - What the scheduler holds of the queue (reads not started, statuses not written, reads running) is mirrored in an atomic, stored under the scheduler's lock after every change. It only grows at a submit, and a submit holds the queue's lock too. So an enqueue that finds room under the queue's lock alone really has it. - A full queue takes the scheduler's lock as before: it submits itself if nothing else could make room (`a_full_queue_submits_itself`), or waits for a finish with the queue's lock let go, so the title's submit is not held up. - A submit moves the commands in under both locks, the queue's first. That order is the same everywhere both are taken, so batches keep their order and status entries their place. - A submit wakes one worker per new read, up to the number of workers, instead of all of them. A worker goes on to the next read when it finishes one, so no read waits for a worker that wasn't woken. A start refused for staging space still wakes everyone at the next finish, as before. - `VICTORY_DSTORAGE_QUEUE_LOCAL=0` turns it off: every enqueue takes the scheduler's lock and a submit wakes every worker, as before. ## Before / after `cargo test --release -p victory-hle --lib enqueues_beside_busy_workers -- --ignored --nocapture` (added here). Two threads, each with a queue of the title's size (4,159 entries), enqueue batches of 4,095 copies of 4 KiB from memory and a status entry, submit, and wait for the status, while all 24 workers run them. Runs with the switch on and off take turns, 7 of each, 1.5 s each; medians of the runs, on this branch: | | title's thread enqueuing a batch | enqueue 99th percentile | batch, enqueue to status | reads/s | |---|---|---|---|---| | before (`VICTORY_DSTORAGE_QUEUE_LOCAL=0`) | 4.27 ms | 30.5 us | 10.77 ms | 758,579 | | this branch | 0.40 ms | 0.5 us | 10.85 ms | 755,191 | The enqueue time was lower in every pair of runs. End-to-end throughput is unchanged: the workers are the limit in this test, so what the change gives back is the title's thread's time. ## Correctness - `batches_of_many_threads_complete_in_order`: four threads, each with a queue of 128 entries, enqueue batches of 100 reads and a status entry and submit, so each batch waits for room the one before leaves. Every status lands only after every read before it (its bytes are all there when it does). Run with the switch on and off. - Unchanged and passing: `a_full_queue_submits_itself`, `half_a_queue_waits_for_the_submit`, `a_status_waits_for_every_earlier_read`, `statuses_complete_in_order_with_the_first_failure`, `priorities_share_by_bytes_about_tenfold`, `staging_bounds_compressed_bytes_in_flight`, `a_closed_queue_completes_nothing`. - With `VICTORY_DSTORAGE_HALF=1`, only `half_a_queue_waits_for_the_submit` fails, since it asserts the opposite of half-submits. - Locally on this branch: the dstorage tests pass in three runs out of three; `cargo test --workspace --locked` passes with no failures; clippy with `-D warnings` reports nothing in `dstorage.rs` in either build (it reports one finding in `gameinput.rs`, already on `main`). - Not yet measured in play.
Dukestar69 commented 2026-11-28 09:28:31 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-11-28T09:28:31Z.

Review: APPROVED


Tested locally merged onto main (63b902f).

  • Merge & Policy: Clean merge; cargo xtask policy passed (294 files ok).
  • Tests: cargo test -p victory-hle --lib dstorage passed (19 passed; 0 failed; 2 ignored, including the new batches_of_many_threads_complete_in_order concurrency test) with 0 compiler warnings.

Clean per-queue lock separation reducing contention between enqueueing threads and busy DirectStorage workers. LGTM.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6096963241 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/934#pullrequestreview-6096963241). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-28T09:28:31Z. **Review:** APPROVED --- Tested locally merged onto `main` (`63b902f`). - **Merge & Policy**: Clean merge; `cargo xtask policy` passed (`294 files ok`). - **Tests**: `cargo test -p victory-hle --lib dstorage` passed (`19 passed; 0 failed; 2 ignored`, including the new `batches_of_many_threads_complete_in_order` concurrency test) with 0 compiler warnings. Clean per-queue lock separation reducing contention between enqueueing threads and busy DirectStorage workers. LGTM. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/934:upstream-pr/934'
git switch 'upstream-pr/934'

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 'upstream-pr/934'
git switch 'upstream-pr/934'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/934'
git switch 'upstream-pr/934'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/934'
git switch 'main'
git merge --squash 'upstream-pr/934'
git switch 'main'
git merge --ff-only 'upstream-pr/934'
git switch 'main'
git merge 'upstream-pr/934'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
trustno1-coder/VIctory-NVIDIA!934
No description provided.