Files
reims-vgpu/crates
Anees IqbalandClaude 6783a94576 ✨ A drain of one channel stamps one slot, so it submits one command buffer and not eleven
Pathway: x86 macOS / Linux Vulkan, drain worker. Behavior change on the child
FIFO's completion-stamp path, switched by `REIMS_VGPU_STAMP_COALESCE`.

THE MEASUREMENT THAT ASKED FOR THIS

`cb718e5f` split the drain residue's `regs` span three ways. Two driven macos-13
`blur=40` boots (`pin-split`), medians over 74 and 75 census windows:

    tailrd   0.49 / 0.48 ms/s      59 /  58 ns per op
    headwr   1.00 / 0.99 ms/s     128 / 127 ns per op
    stamp   92.29 / 98.59 ms/s   12266 / 13334 ns per op

    sum vs regs_us = 1.00 on every window

The two accesses that really are four-byte guest word accesses cost **1.5 ms a
second between them**. The stamp is **98.4 % of the span at 12.3 microseconds a
packet**, because `write_stamp` is not a word write: on the Vulkan arm it takes
the engine lock, binds the guest RAM reference, records a barrier and a
`cmd_update_buffer`, reserves a timeline value, and **submits a command buffer**.

`engine_delta` says the same thing from the other side, and this is the part that
generalizes past the stamp:

    batch_opens=6812  batch_joins=3934  batch_flushes=6812  batch_flush_draws=10746

**`batch_flushes` equals `batch_opens` exactly, at 1.58 draws a flush.** The draw
batch exists to amortize submits across draws and was never allowed to
accumulate any, because `write_stamp_after_guest_writes` appends to the open
batch and then flushes it. Every packet's stamp flushed whatever draws had
gathered.

WHAT THE CONTRACT LICENSES

Two facts, neither of them a heuristic:

1. **A stamp wait is `>=`, not `==`.** `StampWait::satisfied_by` is
   `current.wrapping_sub(self.value) as i32 >= 0`, so a wait on V is discharged
   by any value at or past V in wrapping-signed order. Intermediate values need
   never appear.
2. **A drain of one channel stamps one slot.** `stamp_index` is read once from
   the channel's register block *before* the loop, so every packet in a
   `drain_child_fifo` call writes the same slot. A run of writes to one slot is
   observable only through its greatest value unless the guest samples between
   them.

So `drain_child_fifo` now latches each packet's completion stamp into a
`PendingStamp` and writes once, after the loop. A driven boot averages **11.6
packets per drain call** (`packets` ~6400/s over `setup_calls` ~550-600/s), so
this removes roughly ten submissions in eleven.

THE TWO HAZARDS IT HAD TO DISPOSE OF, AND HOW

**A wait on the slot the drain is holding.** `note_packet_stamp_waits` reads the
word out of guest RAM, which is now stale by exactly the stamps this drain has
latched. A later packet in the same drain waiting on that slot would read the old
value, return `Hold`, and park the channel against a stamp this device is itself
sitting on — a deadlock introduced by the coalescing rather than by the guest. It
now consults the latch first, and answering from it is not a shortcut: packets
are processed in order on this thread, so a latched stamp is work that finished
before the waiting packet was decoded. Counted as `packet_stamp_wait_met_pending`
so the reliance is measurable rather than assumed.

**A latched stamp that never lands.** Every `break` in that loop falls through to
one site after it, which is why one flush is enough — a drain that stops on a
held stamp, a deferred translation, a decode fault or a host-action yield owes
what it latched exactly as one that ran the ring dry does. Leaving one unwritten
is a guest waiting on a word that never arrives, and its own deadline is a
second, so it would present as a stall rather than a hang.

`PendingStamp::latch` keeps the **maximum in wrapping-signed order**, not the
last value seen. For a well-formed guest those are the same value; taking the
maximum means this device cannot publish a slot going backwards even if a
regressing stamp arrives, which would unsatisfy a wait the guest had already been
told was met.

The main FIFO is unchanged and passes `None`: it stamps per packet and its slot
is not fixed across a drain, so neither fact above holds there. It also executed
zero times on this workload.

SWITCH

`REIMS_VGPU_STAMP_COALESCE=off` restores the per-packet arm on the same binary,
because a change to *when* a completion becomes visible fails as a stall or a
stutter rather than as a decline, and both arms have to be reachable for one A/B.
Off is a refusal and never a permission — one write per packet is strictly more
work than one per drain, and the coalesced arm needs no capability the per-packet
arm does not.

VERIFIED

- 1881 tests pass (+2), `--test-threads=1`, backend-vulkan,host-window.
- Both new tests fail without the change. `a_coalesced_stamp_keeps_the_latest_
  value_across_the_u32_wrap` was probed by stubbing `latch` to last-write-wins
  and fails on the regressing-stamp assertion; the file was restored by copying
  it back, not with git.
- Clippy `-D warnings` clean on all three arms.
- `scripts/feature-matrix`: all supported arms compile, warnings=0, lib count
  1881 (was 1879).

NOT VERIFIED

- **No boot.** Nothing here is measured on a guest yet: not the saving, not the
  frame rate, and not that the desktop still composites. That is the next step
  and it is an A/B against `=off` on one binary.
- Whether `batch_flushes` actually falls and whether the draw batch then
  accumulates. The prediction is that it does and that the win is larger than the
  92 ms/s the stamp span itself carries, but a prediction is not a reading.
- The arm64/Metal pathway. The coalescing is backend-independent but `write_stamp`'s
  GPU rail is `backend-vulkan`-gated, so the saving is expected to differ there
  and no Apple host has booted it.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-11 16:38:49 +03:00
..