Files
leleka/crates/cuda-model/src/command_profile.rs
T

42 lines
1.8 KiB
Rust

//! `LELEKA_BRINGUP_TRACE` + `LELEKA_PREFILL_PROFILE_SYNC`: per-command wall time of the batched prompt
//! producers (each command drained on its own; attribution only, the drains slow the run). The runtime's
//! `PREFILL-PROFILE` takes and prints the totals once per chunk.
use std::sync::Mutex;
use crate::env_switch::{EnvSwitch, rules};
use std::time::Duration;
static TOTALS: Mutex<Vec<(&'static str, usize, &'static str, Duration, u32)>> = Mutex::new(Vec::new());
/// Whether per-command timing is on (both switches set).
#[inline]
pub fn enabled() -> bool {
static PROFILE_SYNC: EnvSwitch = EnvSwitch::new("LELEKA_PREFILL_PROFILE_SYNC", rules::is_set);
crate::bringup_trace_enabled() && PROFILE_SYNC.get()
}
pub fn add(producer: &'static str, index: usize, label: &'static str, elapsed: Duration) {
if let Ok(mut totals) = TOTALS.lock() {
match totals.iter_mut().find(|e| e.0 == producer && e.1 == index) {
Some(entry) => {
entry.3 += elapsed;
entry.4 += 1;
}
None => totals.push((producer, index, label, elapsed, 1)),
}
}
}
/// The same accumulation for host-side stages of the runtime (`LELEKA_PREFILL_PROFILE_SYNC`).
pub fn record(producer: &'static str, index: usize, label: &'static str, elapsed: Duration) {
add(producer, index, label, elapsed)
}
/// The accumulated totals as `producer#index:label=ms(xcount)` and reset.
pub fn take_report() -> String {
let Ok(mut totals) = TOTALS.lock() else { return String::new() };
let mut entries = std::mem::take(&mut *totals);
entries.sort_by_key(|e| (e.0, e.1));
entries
.iter()
.map(|(p, i, l, d, n)| format!("{p}#{i}:{l}={:.1}(x{n})", d.as_secs_f64() * 1e3))
.collect::<Vec<_>>()
.join(" ")
}