Trace the completion turn's steps in the latency lane

finish() marks, in test builds only, where its events were reached, its fences
settled and its GPU times placed, so the lane splits the time from the turn
to the completion-seen handler.

(cherry picked from commit 66ecc26c9ce04f92e7747c8f873e04fcbca18641)
This commit is contained in:
Anees Iqbal committed 2026-11-05 17:12:25 -08:00
1 parent 587d155705
commit 02ca2a2c20
2 files changed
+60 -8

No files matched your search

@@ -666,6 +666,8 @@ fn finish(watch: &Watch, lists: &mut FenceLists, mut finished: Vec<(usize, Submi
}
// A batch held at its gate, or for a GPU signal to land, may now go (`scheduler`).
device.shared().scheduler.completions(&device);
#[cfg(test)]
trace::step("events reached");
// Every finished batch's fence is waited for: where a queue's failures are relayed each
// batch has one (`device/relay.rs`), signaled after the driver reported its failure.
let mut ending = Vec::new();
@@ -688,6 +690,8 @@ fn finish(watch: &Watch, lists: &mut FenceLists, mut finished: Vec<(usize, Submi
}
}
let settled = settle(&device, lists);
#[cfg(test)]
trace::step("fences settled");
// Their GPU times' timestamps are read, and their slots given back, and their GPU times
// placed among their queues' in order, before their transitions are posted: a completed
// handler reads the times. A section, as each poll.
@@ -701,6 +705,8 @@ fn finish(watch: &Watch, lists: &mut FenceLists, mut finished: Vec<(usize, Submi
buffer.place_gpu_times();
}
}
#[cfg(test)]
trace::step("times placed");
drop(device);
match settled {
Settled::Signaled => {
@@ -1239,6 +1245,8 @@ pub(crate) mod trace {
/// A turn finished the batch of value `value` of `queue` that ends its command buffer;
/// `seen` is the command buffer's completion time (`finish`).
Finished { queue: usize, value: u64, seen: u64 },
/// The turn that finished the batches reached step `what` of `finish`.
Step { what: &'static str, at: u64 },
}
static ON: AtomicBool = AtomicBool::new(false);
@@ -1256,6 +1264,14 @@ pub(crate) mod trace {
STEPS.lock().unwrap().drain(..).collect()
}
/// Keep the turn's step `what`, now.
pub(super) fn step(what: &'static str) {
record(|| Traced::Step {
what,
at: crate::gpu_time::host_nanos(),
});
}
/// Keep the step `step` makes, while a test asks.
pub(super) fn record(step: impl FnOnce() -> Traced) {
if ON.load(Ordering::Relaxed) {
@@ -820,13 +820,21 @@ fn runtime_completion_latency() {
.unwrap();
}
let steps = trace::stop();
let finished: Vec<(usize, u64, u64)> = steps
.iter()
.filter_map(|step| match *step {
Traced::Finished { queue, value, seen } => Some((queue, value, seen)),
_ => None,
})
.collect();
// Each turn's batch, and the steps of `finish` that follow it, up to the next turn's.
let mut finished: Vec<(usize, u64, u64, Vec<(&'static str, u64)>)> = Vec::new();
for step in &steps {
match *step {
Traced::Finished { queue, value, seen } => {
finished.push((queue, value, seen, Vec::new()));
}
Traced::Step { what, at } => {
if let Some((.., turn)) = finished.last_mut() {
turn.push((what, at));
}
}
_ => {}
}
}
assert_eq!(
finished.len(),
RUNS,
@@ -861,6 +869,8 @@ fn runtime_completion_latency() {
entered: u64,
returned: u64,
turn: u64,
/// The turn's steps of `finish` (`trace::step`), in order.
turn_steps: Vec<(&'static str, u64)>,
seen: u64,
completed: u64,
gpu_end: Option<u64>,
@@ -868,7 +878,8 @@ fn runtime_completion_latency() {
let times: Vec<Times> = committed
.iter()
.zip(&finished)
.map(|(each, &(queue, value, turn))| {
.map(|(each, (queue, value, turn, turn_steps))| {
let (queue, value, turn) = (*queue, *value, *turn);
let (entered, returned) = waited(queue, value, turn).expect("a wait before the turn");
let gpu_end = calibrated
.then(|| (each.command_buffer.gpu_end_time().unwrap() * 1e9).round() as u64);
@@ -878,6 +889,7 @@ fn runtime_completion_latency() {
entered,
returned,
turn,
turn_steps: turn_steps.clone(),
seen: each
.seen
.lock()
@@ -911,4 +923,28 @@ fn runtime_completion_latency() {
of(&|t| us(t.seen, t.completed)),
of(&|t| us(t.commit, t.seen)),
);
// The turn's steps, each from the one before it (the turn first) and the last to the
// completion-seen handler.
let names: Vec<&str> = times[0].turn_steps.iter().map(|&(what, _)| what).collect();
assert!(
(times.iter()).all(|t| t.turn_steps.iter().map(|&(what, _)| what).eq(names.clone())),
"every turn took the same steps"
);
let mut split = String::new();
for at in 0..=names.len() {
let from = |t: &Times| match at {
0 => t.turn,
at => t.turn_steps[at - 1].1,
};
let to = |t: &Times| t.turn_steps.get(at).map_or(t.seen, |&(_, to)| to);
let (from_name, to_name) = (
at.checked_sub(1).map_or("turn", |at| names[at]),
names.get(at).copied().unwrap_or("seen handler"),
);
split.push_str(&format!(
"; {from_name} -> {to_name} {} us",
of(&|t| us(from(t), to(t)))
));
}
eprintln!("COMPLETION-LATENCY runtime turn ({name}){split}");
}