✨ The third of the drain worker named by nothing gets a per-packet split, because the residue scales with draws and every packet pays two ring reads and a decode

Pathway: x86 macOS / Linux Vulkan, drain worker. Instrument only — no behaviour
change.

`tail_us`/`boundary_us` closed the two spans after `Device::drain` returns and
found them near-empty (1.5 ms/s and 0), which localized the residue to inside
`runtime::drain::drain_pending` and no further. There is no `Instant::now`
anywhere in either FIFO function, so nothing inside it could be ranked.

Three driven macos-13 sustained-animation boots at 71462a94 put the residue at:

| boot | class | duty | unnamed in drain |
|---|---|---|---|
| v1 | ~60 Hz | 0.42 | 128 ms/s |
| v2 | ~109 Hz | 0.67 | 236 ms/s |
| v3 | ~60 Hz | 0.39 | 110 ms/s |

It roughly doubles with the frame rate, which is the shape of a per-packet cost
rather than a per-opcode one — so the two spans every packet pays are where to
look first.

WHAT IS COUNTED

`ring_us`/`ring_reads` and `decode_us`/`packets` on the `drain_duty` line.
Timed at `read_ring_bytes` and `decode_packet` themselves rather than at their
call sites, so both FIFOs and both reads per packet are covered and a fifth
call site cannot be added without being measured. Each keeps its own count, so
the per-call figure is available and not inferred from a draw count.

Nanoseconds, not microseconds. These fire tens of thousands of times a second
and one costs well under a microsecond, so `as_micros()` would truncate most
samples to zero and report a rail that runs constantly as free. Divided to
microseconds at the report, so the line's units stay uniform.

The analyzer reads them as their own family rather than adding them to the
existing `tail_us` request: `rows()` drops a whole family when any requested key
is missing, so asking for these alongside `tail_us` would silently retire the
residue block on every log captured before this commit. It also prints the pair
as a share of the residue they sit inside, which is the number that decides
whether they are worth optimizing at all.

WHAT THIS DOES NOT CLAIM

Nothing about where the residue is. A previous session's handoff asserted
`read_ring_bytes` "copies the ring byte by byte"; it does not — it chunks
through `HostMemory::read_gpa` in at most two iterations. What it does do is
allocate and zero a fresh `Vec` per call and cross into QEMU's address space
per call, twice per packet, with the second read a superset of the first. That
is a hypothesis and this commit exists to test it rather than to act on it.

VERIFIED

- 1879 tests pass, `--test-threads=1`, backend-vulkan,host-window.
- Clippy `-D warnings` clean on the vulkan and metal arms.
- The three baseline boots above also confirm the two layout repairs
  (25051457, 71462a94) are a no-op on macos-13: no guest panic, exactly one
  boot per log, and zero firings of `frag_neutral_texture_substituted`,
  `compute_neutral_sampled_image_unbound` or `used_binding_absent_from_layout`.
  That is the expected result — the kb note measured that population on rails
  14, 15 and 26, not on 13.

NOT VERIFIED

- The instrument's own cost. Two `Instant::now` per ring read and two per
  decode, at tens of thousands a second, is order 1-2 ms/s against a 110-236
  ms/s residue; it has not been A/B'd and the residue figure should be read as
  including it.
- Nothing on the rails that fire the layout repairs.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Anees IqbalandClaude committed 2026-08-11 16:06:43 +03:00
1 parent 71462a94c8
commit 1937db0c36
2 files changed
+78

No files matched your search

@@ -901,6 +901,21 @@ pub(crate) struct DrainDutyCensus {
/// probe, so it is not a property of one workload.
tail_us: std::sync::atomic::AtomicU64,
boundary_us: std::sync::atomic::AtomicU64,
/// The per-packet halves of that same residue: the ring snapshot reads and
/// the decode. Both run for every packet either FIFO drains, both sit
/// inside `drain_us`, and neither is inside any [`DrainPhase`] — so if the
/// third of the drain worker named by nothing is per-packet rather than
/// per-opcode, it is here.
///
/// **Nanoseconds, not microseconds, and that is not a style choice.** These
/// fire tens of thousands of times a second and a single one costs well
/// under a microsecond, so `as_micros()` would truncate most samples to
/// zero and report a rail that runs constantly as free. `tail_us` above can
/// afford microseconds because it is sampled once per tranche.
ring_ns: std::sync::atomic::AtomicU64,
ring_reads: std::sync::atomic::AtomicU64,
decode_ns: std::sync::atomic::AtomicU64,
packets: std::sync::atomic::AtomicU64,
max_tranche_us: std::sync::atomic::AtomicU64,
/// Longest single Flush in the window. `flush_us/flushes` is a mean, and a
/// mean cannot tell "every flush costs 7.7 ms" from "most are free and one
@@ -1072,6 +1087,22 @@ impl DrainDutyCensus {
self.boundary_us.fetch_add(boundary_us, Relaxed);
}
/// One ring snapshot read, in nanoseconds. Twice per packet — the header,
/// then the packet the header sized.
pub(crate) fn note_ring(&self, ns: u64) {
use std::sync::atomic::Ordering::Relaxed;
self.ring_ns.fetch_add(ns, Relaxed);
self.ring_reads.fetch_add(1, Relaxed);
}
/// One packet decode, in nanoseconds. The count is the packet count for
/// both FIFOs, which is what every per-packet figure is normalized by.
pub(crate) fn note_decode(&self, ns: u64) {
use std::sync::atomic::Ordering::Relaxed;
self.decode_ns.fetch_add(ns, Relaxed);
self.packets.fetch_add(1, Relaxed);
}
/// Accumulate one completed tranche and return the line when a report is
/// due. Returns the line rather than emitting it so the reporting rule is
/// testable without a log sink: that the window resets on report (so the
@@ -1114,6 +1145,12 @@ impl DrainDutyCensus {
let max_flush = self.max_flush_us.swap(0, Relaxed);
let tail = self.tail_us.swap(0, Relaxed);
let boundary = self.boundary_us.swap(0, Relaxed);
// Reported in microseconds like every other span on this line, but
// accumulated in nanoseconds — see the field docs.
let ring = self.ring_ns.swap(0, Relaxed) / 1000;
let ring_reads = self.ring_reads.swap(0, Relaxed);
let decode = self.decode_ns.swap(0, Relaxed) / 1000;
let packets = self.packets.swap(0, Relaxed);
let slow = self.slow_tranches.swap(0, Relaxed);
let busy = drain.saturating_add(publish);
let duty = busy as f64 / (win_ms as f64 * 1000.0);
@@ -1123,6 +1160,7 @@ impl DrainDutyCensus {
draw_us={draw} draws={draws} compute_us={compute} computes={computes} \
flush_us={flush} flushes={flushes} max_flush_us={max_flush} \
tail_us={tail} boundary_us={boundary} \
ring_us={ring} ring_reads={ring_reads} decode_us={decode} packets={packets} \
slow_tranches={slow}/{tranches} slow_us={DRAIN_TRANCHE_SLOW_US}"
))
}
@@ -1611,6 +1649,16 @@ pub fn note_drain_tail(tail_us: u64, boundary_us: u64) {
DRAIN_DUTY.note_tail(tail_us, boundary_us);
}
/// Attribute one ring snapshot read, in nanoseconds.
pub fn note_drain_ring(ns: u64) {
DRAIN_DUTY.note_ring(ns);
}
/// Attribute one packet decode, in nanoseconds.
pub fn note_drain_decode(ns: u64) {
DRAIN_DUTY.note_decode(ns);
}
/// Accumulate one completed drain tranche; emits at most once per second.
pub fn note_drain_tranche(drain_us: u64, publish_us: u64) {
if let Some(line) = DRAIN_DUTY.note(drain_us, publish_us, crate::observe::elapsed_ms() as u64) {
@@ -1225,11 +1225,25 @@ fn note_packet_stamp_waits<H: HostMemory + HostOps>(
/// must stay quiet — and reading the second as the first put a
/// `packet_bad_size` line on the always-on channel for a healthy producer
/// mid-write, while making `Incomplete` unreachable.
/// Timed at the function for the same reason [`read_ring_bytes`] is: the count
/// this keeps is the packet count both FIFOs are normalized by.
fn decode_packet(
bytes: &[u8],
head: u32,
available: u32,
ring_capacity: u32,
) -> Result<Packet, PacketError> {
let started = std::time::Instant::now();
let out = decode_packet_inner(bytes, head, available, ring_capacity);
census::note_drain_decode(started.elapsed().as_nanos() as u64);
out
}
fn decode_packet_inner(
bytes: &[u8],
head: u32,
available: u32,
ring_capacity: u32,
) -> Result<Packet, PacketError> {
if available < PACKET_HEADER_LEN {
return Err(PacketError::ShortHeader);
@@ -1286,12 +1300,28 @@ fn decode_packet(
})
}
/// Timed at the function rather than at its four call sites, so both FIFOs and
/// both reads per packet are counted and a fifth call site cannot be added
/// without being measured.
fn read_ring_bytes<M: HostMemory>(
mem: &M,
base_gpa: u64,
ring_size: u32,
absolute: u32,
len: u32,
) -> Result<Vec<u8>, MemError> {
let started = std::time::Instant::now();
let out = read_ring_bytes_inner(mem, base_gpa, ring_size, absolute, len);
census::note_drain_ring(started.elapsed().as_nanos() as u64);
out
}
fn read_ring_bytes_inner<M: HostMemory>(
mem: &M,
base_gpa: u64,
ring_size: u32,
absolute: u32,
len: u32,
) -> Result<Vec<u8>, MemError> {
let mut out = vec![0u8; len as usize];
if ring_size == 0 || len == 0 {