submit_path: the committing threads' own CPU

The threads mode's committing threads ended before the process-wide
reading after the run, and their per-thread CPU went with them, so the CPU
that commit_and_submit moves onto them was not counted. Each thread now reads
its own CPU (CLOCK_THREAD_CPUTIME_ID) before it ends, and the mode prints
their sum per command buffer. The main thread is grouped by the name's prefix
too, since a copied binary runs under another name.

Verified: clippy -D warnings on the example; run on the 5080 for the M1 and
wake numbers.

(cherry picked from commit 7822ca3293633c6da2fa4a1a734e137dc792ddbc)
(cherry picked from commit 951eb7841e4274bce6b276d36ae89245ad554ba4)
(cherry picked from commit 9e2d82e104f88da4f98cc14073a48f3eaaff97d8)
This commit is contained in:
Anees Iqbal committed 2026-11-03 01:40:29 -08:00
1 parent 431ebb90ad
commit 65c0371cbf
1 file changed
+24 -9
@@ -188,7 +188,7 @@ fn group_of(comm: &str) -> &'static str {
c if c.starts_with("metal2vulkan-qu") => "queue-waiters",
c if c.starts_with("metal2vulkan-ha") => "handlers",
c if c.starts_with("metal2vulkan") => "other-m2v",
"submit_path" => "committer+main",
c if c.starts_with("submit_path") => "committer+main",
_ => "driver/other",
}
}
@@ -295,6 +295,17 @@ fn cpu_by_group() -> Vec<(String, u64)> {
groups
}
/// This thread's CPU so far, in nanoseconds.
fn thread_cpu_nanos() -> u64 {
// SAFETY: a plain call with an out-parameter of the right type.
let spec = unsafe {
let mut spec: libc::timespec = std::mem::zeroed();
libc::clock_gettime(libc::CLOCK_THREAD_CPUTIME_ID, &mut spec);
spec
};
spec.tv_sec as u64 * 1_000_000_000 + spec.tv_nsec as u64
}
/// The CPU each group spent between `before` and `after`, per command buffer, in microseconds.
fn print_cpu(label: &str, before: &[(String, u64)], after: &[(String, u64)], count: usize) {
let mut line = format!("{label}:");
@@ -400,29 +411,33 @@ fn threads(device: &Device, threads: usize) {
std::thread::sleep(Duration::from_millis(200));
let before = cpu_by_group();
let start = Instant::now();
let count: usize = std::thread::scope(|scope| {
// Each committing thread's own CPU, read before it ends (its schedstat goes with it).
let (count, committers): (usize, u64) = std::thread::scope(|scope| {
let running: Vec<_> = (queues.iter())
.map(|(queue, target)| {
scope.spawn(move || {
let mut count = 0;
let (mut count, began) = (0, thread_cpu_nanos());
while start.elapsed().as_secs_f64() < RATE_SECONDS {
commit(&tiny(queue, target, (Callback::Completed, true), |_| {}));
count += 1;
}
count
(count, thread_cpu_nanos() - began)
})
})
.collect();
running
.into_iter()
(running.into_iter())
.map(|thread| thread.join().unwrap())
.sum()
.fold((0, 0), |(count, nanos), (more, spent)| {
(count + more, nanos + spent)
})
});
let elapsed = start.elapsed().as_secs_f64();
let after = cpu_by_group();
println!(
"threads {threads}: {:.0} command buffers a second ({count} in {elapsed:.2} s)",
count as f64 / elapsed
"threads {threads}: {:.0} command buffers a second ({count} in {elapsed:.2} s), \
committing threads {:.2} us of CPU per command buffer",
count as f64 / elapsed,
committers as f64 / 1e3 / count as f64
);
print_cpu(&format!("threads {threads}"), &before, &after, count);
for (queue, _) in &queues {