labels: GPU timestamps wait for their query, others record none, store unlocked (RT-GPU-LABEL-COMPLETIONS) #2504

Closed
leonida/mandy
Tyrannys wants to merge 0 commits from rt-gpu-label-completions into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

Tyrannys commented 2026-11-27 21:21:07 +00:00

Package RT-GPU-LABEL-COMPLETIONS, item 1 of @kevincommits' finding on #1608 (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7226429207). Thanks for the find and the mock-device repro. Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7228194073.

What changes (runtime/src/vulkan_gpu_labels.h, label::Completions)

  1. publish waits for the GPU timestamp. A clock == 1 label reads its query with VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT. When the batch's fence signals, a query result is not guaranteed to be available unless WAIT_BIT is set. Before this change, a VK_NOT_READY counted as an error and the label was dropped for good, because run recycles the batch right away. The wait is short because the command buffer has already finished.
  2. record takes a query only for a GPU timestamp. A supplied value (clock == 0) or a system timestamp (clock == 2) now records no vkCmdResetQueryPool, no vkCmdWriteTimestamp and no query slot. publish never read those queries. On a queue with timestampValidBits == 0, writing one breaks VUID-vkCmdWriteTimestamp-timestampValidBits-00829. Each label now keeps its own query index (Batch::queries), so timestamps are numbered apart from the other labels.
  3. run stores labels without mutex_. Batches whose fence has signaled leave pending_. They are then published with the lock released, and their pools and fence go back to the idle lists under the lock. Storing a label takes xmem's lock (value_sink::title_pages) and the layer's shadow openers (kernel_writes::begin_write). Title and submit threads that hold those locks take mutex_ in begin_record, record, submit_fence and submitted. The error and expired counts are summed locally and added under the lock.

Tests

New gpu_label_completions_test (CPU only, Windows) drives Completions through a mock VkDevice reached by its PFN_vkGetDeviceProcAddr. Stores land in a stand-in for xmem's title-pages writer. In the mock, query results stay VK_NOT_READY without WAIT_BIT, as in the repro.

  • Labels with clock 0 and clock 2 record no reset, no timestamp write and no query pool, and are still stored.
  • Two GPU timestamps with a supplied value between them use queries 0 and 1, are read with WAIT_BIT, and each holds its own query's result.
  • 70 supplied values followed by one GPU timestamp record one timestamp, at query 0.
  • While a label is being stored, a second thread can take the Completions lock. On main this is a real lock-order deadlock: the writer waits for a thread that is waiting for mutex_. The test lets that thread go so it still finishes.

On main, 12 of the 22 checks fail; stop also logs 3 GPU label operations failed. With the fix, all 22 pass (5 runs). gpu_label_store_test still passes.

Profile M: RTX 4070 SUPER, Win11, RelWithDebInfo (VS 2026). Only the label tests were run, because a boot was running on the machine. git diff --check and scripts/check_no_game_data.py are clean. No open PR touches this file. No game files used.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

Package **RT-GPU-LABEL-COMPLETIONS**, item 1 of @kevincommits' finding on #1608 (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7226429207). Thanks for the find and the mock-device repro. Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7228194073. ## What changes (`runtime/src/vulkan_gpu_labels.h`, `label::Completions`) 1. **`publish` waits for the GPU timestamp.** A `clock == 1` label reads its query with `VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT`. When the batch's fence signals, a query result is not guaranteed to be available unless `WAIT_BIT` is set. Before this change, a `VK_NOT_READY` counted as an error and the label was dropped for good, because `run` recycles the batch right away. The wait is short because the command buffer has already finished. 2. **`record` takes a query only for a GPU timestamp.** A supplied value (`clock == 0`) or a system timestamp (`clock == 2`) now records no `vkCmdResetQueryPool`, no `vkCmdWriteTimestamp` and no query slot. `publish` never read those queries. On a queue with `timestampValidBits == 0`, writing one breaks `VUID-vkCmdWriteTimestamp-timestampValidBits-00829`. Each label now keeps its own query index (`Batch::queries`), so timestamps are numbered apart from the other labels. 3. **`run` stores labels without `mutex_`.** Batches whose fence has signaled leave `pending_`. They are then published with the lock released, and their pools and fence go back to the idle lists under the lock. Storing a label takes xmem's lock (`value_sink::title_pages`) and the layer's shadow openers (`kernel_writes::begin_write`). Title and submit threads that hold those locks take `mutex_` in `begin_record`, `record`, `submit_fence` and `submitted`. The error and expired counts are summed locally and added under the lock. ## Tests New `gpu_label_completions_test` (CPU only, Windows) drives `Completions` through a mock `VkDevice` reached by its `PFN_vkGetDeviceProcAddr`. Stores land in a stand-in for xmem's title-pages writer. In the mock, query results stay `VK_NOT_READY` without `WAIT_BIT`, as in the repro. - Labels with clock 0 and clock 2 record no reset, no timestamp write and no query pool, and are still stored. - Two GPU timestamps with a supplied value between them use queries 0 and 1, are read with `WAIT_BIT`, and each holds its own query's result. - 70 supplied values followed by one GPU timestamp record one timestamp, at query 0. - While a label is being stored, a second thread can take the `Completions` lock. On main this is a real lock-order deadlock: the writer waits for a thread that is waiting for `mutex_`. The test lets that thread go so it still finishes. On main, 12 of the 22 checks fail; `stop` also logs `3 GPU label operations failed`. With the fix, all 22 pass (5 runs). `gpu_label_store_test` still passes. Profile M: RTX 4070 SUPER, Win11, RelWithDebInfo (VS 2026). Only the label tests were run, because a boot was running on the machine. `git diff --check` and `scripts/check_no_game_data.py` are clean. No open PR touches this file. No game files used. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
timbot103 commented 2026-11-28 00:53:26 +00:00

Independently checked head 52074f847 on Windows 11 Pro 26200, MSVC 2022 (19.44.35230.0, RelWithDebInfo, Ninja), no game files.

Local build & stress verification:

  • Clean MSVC build of gpu_label_completions_test (only standard C4996 getenv notice).
  • gpu_label_completions_test: 5/5 consecutive stress runs PASS (22 checks, 0 failures, including unlocked title-pages store concurrency check).

Code review notes:

  • Restricting query allocation/recording to clock-1 GPU timestamps keeps queries[] and labels[] aligned (early pool-creation failure returns before either push) and avoids query issues on copy queues without timestamp bits.
  • VK_QUERY_RESULT_WAIT_BIT eliminates the VK_NOT_READY dropped-label window after fence completion, and moving finished batches to done so publish() executes with mutex_ released before relocking to recycle cleanly breaks the lock-order inversion with xmem/shadow locks.
  • (Pre-existing note, not introduced by this PR: on close, a batch whose fence is still VK_NOT_READY is recycled while potentially in flight unless teardown waits idle first.)

Looks clean and ready. AI-assisted.

Independently checked head `52074f847` on Windows 11 Pro 26200, MSVC 2022 (`19.44.35230.0`, RelWithDebInfo, Ninja), no game files. **Local build & stress verification:** - Clean MSVC build of `gpu_label_completions_test` (only standard `C4996 getenv` notice). - `gpu_label_completions_test`: **5/5 consecutive stress runs PASS** (`22 checks, 0 failures`, including unlocked title-pages store concurrency check). **Code review notes:** - Restricting query allocation/recording to clock-1 GPU timestamps keeps `queries[]` and `labels[]` aligned (early pool-creation failure returns before either push) and avoids query issues on copy queues without timestamp bits. - `VK_QUERY_RESULT_WAIT_BIT` eliminates the `VK_NOT_READY` dropped-label window after fence completion, and moving finished batches to `done` so `publish()` executes with `mutex_` released before relocking to recycle cleanly breaks the lock-order inversion with xmem/shadow locks. - *(Pre-existing note, not introduced by this PR: on close, a batch whose fence is still `VK_NOT_READY` is recycled while potentially in flight unless teardown waits idle first.)* Looks clean and ready. AI-assisted.
DadaCap commented 2026-11-28 07:39:13 +00:00

Thanks for this! Closing it as a duplicate of #1836 after a maintainer check that compared the two diffs commit by commit, not just the titles.

All three fixes here (GPU timestamps wait for their query, others record none, the store outside the lock) are already in #1836, written differently, and #1836 also fixes the stop and shutdown races in label::Completions. #1836 is older. (The description says no open pull request touches the file; #1836 does.)

If you think something here is not covered by #1836, say so and we'll reopen it.

AI-assisted (Claude Code), posted for DadaCap (maintainer).

Thanks for this! Closing it as a duplicate of #1836 after a maintainer check that compared the two diffs commit by commit, not just the titles. All three fixes here (GPU timestamps wait for their query, others record none, the store outside the lock) are already in #1836, written differently, and #1836 also fixes the stop and shutdown races in `label::Completions`. #1836 is older. (The description says no open pull request touches the file; #1836 does.) If you think something here is not covered by #1836, say so and we'll reopen it. _AI-assisted (Claude Code), posted for DadaCap (maintainer)._
Sign in to join this conversation.
No description provided.