Files
metal2vulkan/crates/metal2vulkan-runtime/src/command_buffer/batches.rs
T
Anees Iqbal efcb6851ce The encoder trail: name the encoder a stalled GPU stopped in, and the last one that completed
A GPU hang (a CTX SWITCH TIMEOUT on an RTX 3090) left the runtime with no way to say which
encoder never completed: encoders share batches and grouped submissions, and neither
METAL2VULKAN_LOG_PASSES nor METAL2VULKAN_QUEUE_TIMES logs anything at a stall.

METAL2VULKAN_ENCODER_TRAIL (env_vars.rs), or DeviceOptions::encoder_trail for one device, is a
default-off diagnostic (encoder_trail.rs):

- Each encoder, and each part of one a host step splits off, ends its batch (Batches::alone),
  and no batch waits in a group, so each goes in a vkQueueSubmit of its own that signals its
  queue's timeline. When work goes to the driver changes; what it computes does not.
- Each encoder notes what it was: its kind, its command buffer's object ID, its place, a
  render pass's area, samples, attachments' formats and sizes, instances and rate map, its
  indirect command executions, and the label hash of each state it bound (the first 8 bytes
  of a SHA-256 of the state's label and its functions' names; no name, no shader text). A host
  step adds the states its indirect commands named, as the GPU and the CPU left the records.
- Once no work of the device advanced for 2 s with some pending, completion.rs's stall path
  logs one Info line (id name metal2vulkan-encoder-trail) for each VkQueue whose oldest batch
  is pending: that batch's encoders and the last batch the queue completed, with how long
  before; once a stall.
- A submission a host step makes and waits for itself (a rasterization-rate map's fan-out
  probe, a predicated loop's check run again) signals no timeline; once it ran 2 s, one line
  names it, the encoder whose step made it and the states its commands named.

Off, a device reads the switch once and notes nothing.

Verified on msi (lavapipe, --cfg m2v_cpu_icd), at the branch tip this commit's tree equals:
- tests::stall::the_encoder_trail_names_the_stuck_encoder_and_the_last_one_completed: a cb
  stuck on an event wait before two blit fills; the line names the stuck cb's encoder 0 (not
  its encoder 1) and the earlier completed cb's encoder 0:
  "VkQueue 0's oldest pending batch: Blit encoder 0 of command buffer 3, states []; the last
  batch it completed: Blit encoder 0 of command buffer 2, states [] (2.106823047s before)".
- tests::rate_mapped_fan_out::the_encoder_trail_fans_out_the_same_pixels: with the trail on,
  probes/flagged/fanned [1, 1, 6] as without, no texel differs from the encoder's own draws.
- The host-step families (rate_mapped_fan_out, rate_mapped_pieces,
  indirect_command_buffer_device, host_step_neutrals, ycbcr_in_place_loop) with
  METAL2VULKAN_ENCODER_TRAIL=1: 11 passed.
- encoder_trail's unit test (one state's hash pinned: a reader of the pipeline creation log
  computes it again); the runtime suite 1504 passed; api_ tests 16 passed; clippy all features
  and the runtime under m2v_gpu and m2v_cpu_icd; rustdoc of the runtime and metal2vulkan.

The runtime-sources pin moves (not re-pinned here).
2026-11-12 20:33:17 -08:00

219 lines
8.3 KiB
Rust

//! A command buffer's batches: its encoders, split where it waits for events or for the caller's
//! binary semaphores, and before the part of an encoder that executes commands the GPU may write,
//! which runs once everything before it completed.
//!
//! A batch is its waits, then its encoders, then its signals, and the scheduler submits each batch
//! once its waits are satisfied. A wait after an encoder or a signal starts a new batch, since
//! everything encoded before it must be able to run, and to signal, while it waits (measured on
//! Metal). An encoder after a signal starts one too, so the signal happens once the encoders before
//! it completed, not the ones after. Within one batch, two waits for one event are the wait for the
//! larger value, and two signals of one event the signal of the larger value, which is what Metal's
//! maximum makes of them. A binary semaphore is waited for or signaled at most once per batch: each
//! wait consumes one signal, so a second wait or signal of the same semaphore starts a new batch,
//! in order. Every command buffer has at least one batch, so even one with nothing encoded
//! completes in its queue's order.
/// One batch: waits, a run of encoders, and signals, with events of type `E` and binary
/// semaphores of type `S`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Batch<E, S> {
/// The events waited for, each once, with the value.
pub(crate) waits: Vec<(E, u64)>,
/// The binary semaphores waited for, each once.
pub(crate) binary_waits: Vec<S>,
/// The number of the command buffer's encoders before this batch's end.
pub(crate) end: usize,
/// The events signaled, each once, with the value.
pub(crate) signals: Vec<(E, u64)>,
/// The binary semaphores signaled, each once.
pub(crate) binary_signals: Vec<S>,
}
impl<E, S> Batch<E, S> {
fn new(end: usize) -> Batch<E, S> {
Batch {
waits: Vec::new(),
binary_waits: Vec::new(),
end,
signals: Vec::new(),
binary_signals: Vec::new(),
}
}
/// Whether the batch signals anything.
fn signals_any(&self) -> bool {
!self.signals.is_empty() || !self.binary_signals.is_empty()
}
}
/// The batches of a command buffer being encoded: the last one is the current one.
#[derive(Debug)]
pub(crate) struct Batches<E, S> {
batches: Vec<Batch<E, S>>,
/// The encoders of the current batch.
encoders: usize,
/// The resource state encoders the current batch begins with.
binds: usize,
/// The event waits encoded so far (`encodeWaitForEvent:value:`), each call counted.
event_waits: usize,
/// The event signals encoded so far (`encodeSignalEvent:value:`), each call counted.
event_signals: usize,
}
impl<E, S> Default for Batches<E, S> {
fn default() -> Batches<E, S> {
Batches {
batches: vec![Batch::new(0)],
encoders: 0,
binds: 0,
event_waits: 0,
event_signals: 0,
}
}
}
impl<E, S> Batches<E, S> {
/// An encoder ended.
pub(crate) fn encoder(&mut self) {
if self.current().signals_any() {
self.split();
}
self.encoders += 1;
self.current().end += 1;
}
/// The next encoder, the part of one that executes commands the GPU may write, starts a
/// batch of its own: the scheduler submits it once everything before it completed and its
/// host step recorded the commands.
pub(crate) fn host_step(&mut self) {
self.split();
}
/// The next encoder goes in a batch of its own, where the current one holds an encoder
/// already: the encoder trail's (`encoder_trail.rs`), whose batches hold one encoder each.
/// The current batch's waits stay with it, and so does the encoder where it holds none.
pub(crate) fn alone(&mut self) {
if self.encoders > 0 {
self.split();
}
}
/// The next encoder is a resource state encoder: its updates bind before its batch's work, so
/// it starts a batch unless the current one holds only such encoders and no signal.
pub(crate) fn sparse(&mut self) {
if self.encoders > self.binds || self.current().signals_any() {
self.split();
}
self.binds += 1;
}
/// The batches, in order: at least one.
pub(crate) fn all(&self) -> &[Batch<E, S>] {
&self.batches
}
/// Whether the current batch signals, so the next encoder starts a batch of its own
/// ([`encoder`](Self::encoder)).
pub(crate) fn signals_pending(&self) -> bool {
self.batches.last().is_some_and(Batch::signals_any)
}
/// The event waits and the event signals encoded so far, each call counted, binary
/// semaphores' none: an encoder that ends now has them all before it
/// (`CommandBuffer::ended_encoders`).
pub(crate) fn events(&self) -> (usize, usize) {
(self.event_waits, self.event_signals)
}
/// Signal no event (`CommandBuffer::commit_failed` without signals): every batch keeps its
/// waits, encoders and binary semaphore signals.
pub(crate) fn drop_signals(&mut self) {
for batch in &mut self.batches {
batch.signals.clear();
}
}
/// Signal no event after the first `parts` encoders (or parts of them): a command buffer
/// Metal fails from an encoder on signals what was encoded before it. The batches that end
/// past them keep their waits, encoders and binary semaphore signals.
pub(crate) fn drop_signals_after(&mut self, parts: usize) {
for batch in self.batches.iter_mut().filter(|batch| batch.end > parts) {
batch.signals.clear();
}
}
fn current(&mut self) -> &mut Batch<E, S> {
let last = self.batches.len() - 1;
&mut self.batches[last]
}
fn split(&mut self) {
let end = self.current().end;
self.batches.push(Batch::new(end));
self.encoders = 0;
self.binds = 0;
}
/// Start a new batch where a wait cannot join the current one: it has encoders or signals.
fn before_wait(&mut self) {
if self.encoders > 0 || self.current().signals_any() {
self.split();
}
}
}
impl<E: PartialEq, S: PartialEq> Batches<E, S> {
/// `encodeWaitForEvent:value:`.
pub(crate) fn wait(&mut self, event: E, value: u64) {
self.event_waits += 1;
self.before_wait();
add_max(&mut self.current().waits, event, value);
}
/// The runtime's own wait for `event` to reach `value`, which no `encodeWaitForEvent:value:`
/// made: it splits the batches as one does, and is not counted among the event waits
/// ([`events`](Self::events)).
pub(crate) fn hold(&mut self, event: E, value: u64) {
self.before_wait();
add_max(&mut self.current().waits, event, value);
}
/// The runtime's own signal of `event` to `value`, which no `encodeSignalEvent:value:` made:
/// it ends the batch as one does, and is not counted among the event signals
/// ([`events`](Self::events)).
pub(crate) fn hold_signal(&mut self, event: E, value: u64) {
add_max(&mut self.current().signals, event, value);
}
/// `encodeSignalEvent:value:`.
pub(crate) fn signal(&mut self, event: E, value: u64) {
self.event_signals += 1;
add_max(&mut self.current().signals, event, value);
}
/// A wait for binary semaphore `semaphore` (`encode_wait_for_vk_semaphore`).
pub(crate) fn binary_wait(&mut self, semaphore: S) {
self.before_wait();
if self.current().binary_waits.contains(&semaphore) {
self.split();
}
self.current().binary_waits.push(semaphore);
}
/// A signal of binary semaphore `semaphore` (`encode_signal_vk_semaphore`).
pub(crate) fn binary_signal(&mut self, semaphore: S) {
if self.current().binary_signals.contains(&semaphore) {
self.split();
}
self.current().binary_signals.push(semaphore);
}
}
/// Add `event` at `value` to `list`, which holds each event once at its largest value.
fn add_max<E: PartialEq>(list: &mut Vec<(E, u64)>, event: E, value: u64) {
match list.iter_mut().find(|(known, _)| *known == event) {
Some((_, known)) => *known = (*known).max(value),
None => list.push((event, value)),
}
}