startup: count failed DirectStorage requests by cause in the heartbeat #1774

Merged
MasterDomino merged 2 commits from upstream-pr/1781 into main 2026-11-24 18:45:56 +00:00
MasterDomino commented 2026-11-24 18:33:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-24T18:33:53Z.


What / why

Refs #1198 (scope approved in https://github.com/R0Y00/Xbox2Windows/issues/1198#issuecomment-7140391241). Refs #1758, which collects the first-failure evidence. This PR only adds whole-run totals; it does not show which failure came first.

The DirectStorage heartbeat has only reported one failed= total. That total does not show whether failures come from reads at or past EOF, reads crossing EOF, short destinations or decoding. This PR adds a second heartbeat line that splits the same total by cause. Behavior does not change: request statuses, queue ordering, the existing totals line and every per-request log text (including read failed) stay the same.

Change

  • src/startup_stubs/xcrdapi.cpp:
    • Fixed named counters (enum class StorageFailure, one LONG per cause) are added. count_failure(cause) increments the cause and requests_failed together, so the causes always add up to failed=. It replaces every existing InterlockedIncrement(&requests_failed): in execute_request, the unreadable page array in run_entry, fail_entry and fail_unstarted. The worker path allocates nothing new.
    • Each failure site in execute_request records its cause next to the unchanged outcome text: read failed gets a read cause, and the other sites map as zlib failed → zlib, bcpack failed → bcpack, destination capacity exceeded → destination-capacity and destination not writable → destination-not-writable. Everything else goes to other: unknown option bits, insufficient page grants, missing page tokens, page writes, a page array unreadable at run, an entry that threw and an entry never started.
    • A refused file read is split with GetFileSizeEx on the File object's handle. The call is made only on the failure path. At or past EOF is offset >= size, and crossing EOF is count > size - offset, checked only when offset < size. No end offset is computed, so neither an offset nor a count near 2^64 can wrap. If the size is unknown (closed or non-file object, GetFileSizeEx fails), or the range fits in the file (a short ReadFile), the read is counted as other read.
    • Heartbeat: right after the existing totals line, at the same cadence, it prints
      x2w-dstorage heartbeat <n> s: failed requests by cause read-at-or-past-eof=N read-crossing-eof=N read-other=N destination-capacity=N destination-not-writable=N zlib=N bcpack=N other=N
      The existing line is byte-for-byte unchanged. The new line is safe for the existing parsers:
      • It has no inflated=, options=, Factory::OpenFile, EnqueueRequest, Queue::EnqueueStatus or commit peak= text.
      • tools/startup/live_view.py reaches its heartbeat branch, where heartbeat: (\d+) frames presented does not match, so it prints nothing.
      • None of the x2w-dstorage line regexes in tools/startup/triage_run.py match it.
      • tests/startup_heartbeat_check.py needs commit peak= on the same line, which this line does not have.
  • tests/startup_storage_failure_counts.cpp (new sibling of startup_storage_refusal_order, registered next to it) and CMakeLists.txt.
  • docs/startup-runtime.md: a new Findings entry "DirectStorage failure causes", plus one sentence in "Startup memory accounting", where the heartbeat is described.

How the test reads the counts (no new public knob)

The heartbeat runs every 10 s. Waiting for it, or adding an environment variable or a DLL export only for the test, was avoided. The fixture compiles src/startup_stubs/xcrdapi.cpp into itself, as startup_storage_worker_start already does. It reads the counters directly and formats the heartbeat line through the stand-in's own format_failure_causes, which the heartbeat also uses. xcrdapi.def and the DLL's export surface do not change.

Tests

Fixture: one Submit of 13 requests, each followed by its status entry, closed by a fence. The file is generated by the test (patterned bytes, one valid stored-deflate zlib stream, the same stream with a wrong Adler-32).

Request Expected cause
valid raw read; valid zlib stream not failed (S_OK; done=1, inflated=1)
raw read at offset == file size; zlib read at offset 2^64 − 8 (offset + size wraps) read-at-or-past-eof = 2
raw and zlib reads of size 0x98cabbec at offset 0; raw 16-byte read 8 bytes before EOF read-crossing-eof = 3
read through a File object that was closed read-other = 1
valid 4096-byte zlib stream into a 2048-byte destination (followed by a raw request, so it is not a chunk head) destination-capacity = 1
valid raw read into a PAGE_READONLY page destination-not-writable = 1
zlib stream with a wrong Adler-32 zlib = 1
BC7 BCPack request whose destination is not whole 16-byte blocks bcpack = 1
request with an unknown option bit (1 << 40) other = 1

The test checks:

  • every status (S_OK or E_FAIL);
  • the exact count of every cause;
  • that the causes add up to requests_failed (11);
  • done=1 and inflated=1;
  • the exact heartbeat line;
  • that the six refused reads still log exactly read failed;
  • nine direct calls of the classifier on edge ranges, including count = 2^64 − 1 at offset 1 and size = 2^64 − 1 with offset 2^64 − 2, count 2, where a naive offset + count wraps to 0.

Environment: Windows 11 (10.0.26300), MSVC x64, dev preset. Branch startup/storage-failure-counters from upstream/main 787f284f.

cmake --preset dev
cmake --build --preset dev
ctest --preset dev --no-tests=error -R '^startup_storage_failure_counts' -V

2/2 passed (startup_storage_failure_counts, startup_storage_failure_counts_one_bucket_control). The passing run prints:
x2w-dstorage heartbeat 20 s: failed requests by cause read-at-or-past-eof=2 read-crossing-eof=3 read-other=1 destination-capacity=1 destination-not-writable=1 zlib=1 bcpack=1 other=1

ctest --preset dev --no-tests=error -R '(^startup_storage|dstorage)'

13/13 passed. This includes startup_storage_refusal_order, startup_storage_worker_start, startup_storage_alloc_failure, startup_dstorage_destination, startup_dstorage_file_read, startup_storage_directory_batch and cpu_native_dstorage.

ctest --preset dev -R '^startup_' -j 4

207 tests: 203 passed, 0 failed, 4 capability skips (startup_view_heap_hardware, startup_view_heap_warp, startup_logical_view_hardware, startup_logical_view_warp). startup_heartbeat_commit, which parses the live heartbeat from the DLL, passed with the new line present. So did startup_triage_run.

python -B tests/docs_links_check.py     # Checked 17 Markdown files and 366 links.
python -B tests/bidi_controls_check.py  # no bidirectional controls

A live check loaded the built XCRDAPI.dll, called DStorageGetFactory and waited for the heartbeat. The log showed the unchanged totals line, followed by x2w-dstorage heartbeat 10 s: failed requests by cause read-at-or-past-eof=0 ... other=0.

Negative controls

  • Registered: startup_storage_failure_counts_one_bucket_control (--mutate one-bucket, WILL_FAIL) holds the expectations of a counter that books every failure under one cause. It fails as required:
    FAIL: cause read-at-or-past-eof counted 2, expected 0
    FAIL: cause read-crossing-eof counted 3, expected 0
    ...
    FAIL: cause other counted 1, expected 11
    FAIL: heartbeat line differs
    
  • Manual, one-bucket implementation: count_failure was changed to always increment other, then reverted. The real test fails:
    FAIL: cause read-at-or-past-eof counted 0, expected 2
    FAIL: cause read-crossing-eof counted 0, expected 3
    FAIL: cause read-other counted 0, expected 1
    FAIL: cause destination-capacity counted 0, expected 1
    FAIL: cause destination-not-writable counted 0, expected 1
    FAIL: cause zlib counted 0, expected 1
    FAIL: cause bcpack counted 0, expected 1
    FAIL: cause other counted 11, expected 1
    FAIL: heartbeat line differs
    
  • Manual, at-EOF and crossing-EOF swapped in read_failure_cause, then reverted. The test fails:
    FAIL: cause read-at-or-past-eof counted 3, expected 2
    FAIL: cause read-crossing-eof counted 2, expected 3
    FAIL: heartbeat line differs
    FAIL: edge range (offset == size) is read-crossing-eof, expected read-at-or-past-eof
    FAIL: edge range (one byte past EOF) is read-at-or-past-eof, expected read-crossing-eof
    ... (7 edge-range failures in total)
    
  • Manual, wrapping comparison (offset + count > size in place of count > size - offset), then reverted. The test fails:
    FAIL: edge range (count 2^64 - 1, offset + count wraps to 0) is read-other, expected read-crossing-eof
    FAIL: edge range (size 2^64 - 1, offset + count wraps to 0) is read-other, expected read-crossing-eof
    

Not covered

  • Page-shape counts and the destination-prefix experiment are deliberately left out, as the scope requires. Page-shape counts can follow separately.
  • other is not split further (page grants, page writes, an unreadable page array, a thrown entry, an entry never started, unknown option bits). The fixture covers only its unknown-option case.
  • A short or failed ReadFile inside the file's range is counted as read-other. The fixture reaches that bucket through a closed File object, not through a real I/O error.
  • The causes and the total are separate interlocked counters, so a heartbeat taken while a request is being counted can be off by that request. Each count is exact once the queue is idle.
  • Counts cover the whole run. They do not show which failure came first (#1758) or what a title expects for a refused request. No title or full-game startup was run for this PR.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6588755239 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1781). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-24T18:33:53Z. --- ## What / why Refs #1198 (scope approved in https://github.com/R0Y00/Xbox2Windows/issues/1198#issuecomment-7140391241). Refs #1758, which collects the first-failure evidence. This PR only adds whole-run totals; it does not show which failure came first. The DirectStorage heartbeat has only reported one `failed=` total. That total does not show whether failures come from reads at or past EOF, reads crossing EOF, short destinations or decoding. This PR adds a second heartbeat line that splits the same total by cause. Behavior does not change: request statuses, queue ordering, the existing totals line and every per-request log text (including `read failed`) stay the same. ## Change - `src/startup_stubs/xcrdapi.cpp`: - Fixed named counters (`enum class StorageFailure`, one `LONG` per cause) are added. `count_failure(cause)` increments the cause and `requests_failed` together, so the causes always add up to `failed=`. It replaces every existing `InterlockedIncrement(&requests_failed)`: in `execute_request`, the unreadable page array in `run_entry`, `fail_entry` and `fail_unstarted`. The worker path allocates nothing new. - Each failure site in `execute_request` records its cause next to the unchanged `outcome` text: `read failed` gets a read cause, and the other sites map as `zlib failed` → zlib, `bcpack failed` → bcpack, `destination capacity exceeded` → destination-capacity and `destination not writable` → destination-not-writable. Everything else goes to `other`: unknown option bits, insufficient page grants, missing page tokens, page writes, a page array unreadable at run, an entry that threw and an entry never started. - A refused file read is split with `GetFileSizeEx` on the File object's handle. The call is made only on the failure path. **At or past EOF** is `offset >= size`, and **crossing EOF** is `count > size - offset`, checked only when `offset < size`. No end offset is computed, so neither an offset nor a count near 2^64 can wrap. If the size is unknown (closed or non-file object, `GetFileSizeEx` fails), or the range fits in the file (a short `ReadFile`), the read is counted as **other read**. - Heartbeat: right after the existing totals line, at the same cadence, it prints `x2w-dstorage heartbeat <n> s: failed requests by cause read-at-or-past-eof=N read-crossing-eof=N read-other=N destination-capacity=N destination-not-writable=N zlib=N bcpack=N other=N` The existing line is byte-for-byte unchanged. The new line is safe for the existing parsers: - It has no `inflated=`, `options=`, `Factory::OpenFile`, `EnqueueRequest`, `Queue::EnqueueStatus` or `commit peak=` text. - `tools/startup/live_view.py` reaches its `heartbeat` branch, where `heartbeat: (\d+) frames presented` does not match, so it prints nothing. - None of the `x2w-dstorage` line regexes in `tools/startup/triage_run.py` match it. - `tests/startup_heartbeat_check.py` needs `commit peak=` on the same line, which this line does not have. - `tests/startup_storage_failure_counts.cpp` (new sibling of `startup_storage_refusal_order`, registered next to it) and `CMakeLists.txt`. - `docs/startup-runtime.md`: a new Findings entry "DirectStorage failure causes", plus one sentence in "Startup memory accounting", where the heartbeat is described. ### How the test reads the counts (no new public knob) The heartbeat runs every 10 s. Waiting for it, or adding an environment variable or a DLL export only for the test, was avoided. The fixture compiles `src/startup_stubs/xcrdapi.cpp` into itself, as `startup_storage_worker_start` already does. It reads the counters directly and formats the heartbeat line through the stand-in's own `format_failure_causes`, which the heartbeat also uses. `xcrdapi.def` and the DLL's export surface do not change. ## Tests Fixture: one Submit of 13 requests, each followed by its status entry, closed by a fence. The file is generated by the test (patterned bytes, one valid stored-deflate zlib stream, the same stream with a wrong Adler-32). | Request | Expected cause | |---|---| | valid raw read; valid zlib stream | not failed (S_OK; done=1, inflated=1) | | raw read at offset == file size; zlib read at offset 2^64 − 8 (offset + size wraps) | read-at-or-past-eof = 2 | | raw and zlib reads of size `0x98cabbec` at offset 0; raw 16-byte read 8 bytes before EOF | read-crossing-eof = 3 | | read through a File object that was closed | read-other = 1 | | valid 4096-byte zlib stream into a 2048-byte destination (followed by a raw request, so it is not a chunk head) | destination-capacity = 1 | | valid raw read into a `PAGE_READONLY` page | destination-not-writable = 1 | | zlib stream with a wrong Adler-32 | zlib = 1 | | BC7 BCPack request whose destination is not whole 16-byte blocks | bcpack = 1 | | request with an unknown option bit (1 << 40) | other = 1 | The test checks: - every status (S_OK or E_FAIL); - the exact count of every cause; - that the causes add up to `requests_failed` (11); - done=1 and inflated=1; - the exact heartbeat line; - that the six refused reads still log exactly `read failed`; - nine direct calls of the classifier on edge ranges, including `count = 2^64 − 1` at offset 1 and `size = 2^64 − 1` with offset `2^64 − 2`, count 2, where a naive `offset + count` wraps to 0. Environment: Windows 11 (10.0.26300), MSVC x64, `dev` preset. Branch `startup/storage-failure-counters` from `upstream/main` 787f284f. ```powershell cmake --preset dev cmake --build --preset dev ctest --preset dev --no-tests=error -R '^startup_storage_failure_counts' -V ``` 2/2 passed (`startup_storage_failure_counts`, `startup_storage_failure_counts_one_bucket_control`). The passing run prints: `x2w-dstorage heartbeat 20 s: failed requests by cause read-at-or-past-eof=2 read-crossing-eof=3 read-other=1 destination-capacity=1 destination-not-writable=1 zlib=1 bcpack=1 other=1` ```powershell ctest --preset dev --no-tests=error -R '(^startup_storage|dstorage)' ``` 13/13 passed. This includes `startup_storage_refusal_order`, `startup_storage_worker_start`, `startup_storage_alloc_failure`, `startup_dstorage_destination`, `startup_dstorage_file_read`, `startup_storage_directory_batch` and `cpu_native_dstorage`. ```powershell ctest --preset dev -R '^startup_' -j 4 ``` 207 tests: 203 passed, 0 failed, 4 capability skips (`startup_view_heap_hardware`, `startup_view_heap_warp`, `startup_logical_view_hardware`, `startup_logical_view_warp`). `startup_heartbeat_commit`, which parses the live heartbeat from the DLL, passed with the new line present. So did `startup_triage_run`. ```powershell python -B tests/docs_links_check.py # Checked 17 Markdown files and 366 links. python -B tests/bidi_controls_check.py # no bidirectional controls ``` A live check loaded the built `XCRDAPI.dll`, called `DStorageGetFactory` and waited for the heartbeat. The log showed the unchanged totals line, followed by `x2w-dstorage heartbeat 10 s: failed requests by cause read-at-or-past-eof=0 ... other=0`. ## Negative controls - **Registered:** `startup_storage_failure_counts_one_bucket_control` (`--mutate one-bucket`, `WILL_FAIL`) holds the expectations of a counter that books every failure under one cause. It fails as required: ``` FAIL: cause read-at-or-past-eof counted 2, expected 0 FAIL: cause read-crossing-eof counted 3, expected 0 ... FAIL: cause other counted 1, expected 11 FAIL: heartbeat line differs ``` - **Manual, one-bucket implementation:** `count_failure` was changed to always increment `other`, then reverted. The real test fails: ``` FAIL: cause read-at-or-past-eof counted 0, expected 2 FAIL: cause read-crossing-eof counted 0, expected 3 FAIL: cause read-other counted 0, expected 1 FAIL: cause destination-capacity counted 0, expected 1 FAIL: cause destination-not-writable counted 0, expected 1 FAIL: cause zlib counted 0, expected 1 FAIL: cause bcpack counted 0, expected 1 FAIL: cause other counted 11, expected 1 FAIL: heartbeat line differs ``` - **Manual, at-EOF and crossing-EOF swapped** in `read_failure_cause`, then reverted. The test fails: ``` FAIL: cause read-at-or-past-eof counted 3, expected 2 FAIL: cause read-crossing-eof counted 2, expected 3 FAIL: heartbeat line differs FAIL: edge range (offset == size) is read-crossing-eof, expected read-at-or-past-eof FAIL: edge range (one byte past EOF) is read-at-or-past-eof, expected read-crossing-eof ... (7 edge-range failures in total) ``` - **Manual, wrapping comparison** (`offset + count > size` in place of `count > size - offset`), then reverted. The test fails: ``` FAIL: edge range (count 2^64 - 1, offset + count wraps to 0) is read-other, expected read-crossing-eof FAIL: edge range (size 2^64 - 1, offset + count wraps to 0) is read-other, expected read-crossing-eof ``` ## Not covered - Page-shape counts and the destination-prefix experiment are deliberately left out, as the scope requires. Page-shape counts can follow separately. - `other` is not split further (page grants, page writes, an unreadable page array, a thrown entry, an entry never started, unknown option bits). The fixture covers only its unknown-option case. - A short or failed `ReadFile` inside the file's range is counted as `read-other`. The fixture reaches that bucket through a closed File object, not through a real I/O error. - The causes and the total are separate interlocked counters, so a heartbeat taken while a request is being counted can be off by that request. Each count is exact once the queue is idle. - Counts cover the whole run. They do not show which failure came first (#1758) or what a title expects for a refused request. No title or full-game startup was run for this PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 19:01:29 +00:00
R0Y00 commented 2026-11-24 19:01:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T18:41:45Z.


The two heartbeat lines now share a single sample of the failure buckets, so a worker reporting another failure between the lines cannot make the printed total disagree with their sum. The fixture publishes a new count after taking the sample and checks that the formatted breakdown still matches the recorded values. Windows Release refusal-order, exact counts, and the one-bucket control passed 3/3.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7141017339 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1781#issuecomment-7141017339). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T18:41:45Z. --- The two heartbeat lines now share a single sample of the failure buckets, so a worker reporting another failure between the lines cannot make the printed total disagree with their sum. The fixture publishes a new count after taking the sample and checks that the formatted breakdown still matches the recorded values. Windows Release refusal-order, exact counts, and the one-bucket control passed 3/3.
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
R0Y00/Xbox2Windows!1774
No description provided.