git clone https://garage44.eu/jeroen/pinnacle.git.. /vulkan/src/stats.rs
//! What the driver spends its time on, said on stderr every five seconds
//! when the program's environment has `PINNACLE_VULKAN_STATS`: how often it
//! sent to the host and how long the sending took, and per command that
//! waits for a reply, how often and how long it waited.
use std::collections::HashMap;
use std::time::{Duration, Instant};
const EVERY: Duration = Duration::from_secs(5);
pub(crate) struct Stats {
on: bool,
since: Instant,
/// Messages sent to the host, their bytes, and the time the sends took.
sends: u64,
send_bytes: u64,
send_time: Duration,
/// Streams too long for a message, run from shared memory.
long_streams: u64,
/// Per command waited for: how often, and how long in all.
calls: HashMap<u32, (u64, Duration)>,
/// Swapchain images presented, and the time presenting took.
presents: u64,
present_time: Duration,
}
impl Stats {
pub(crate) fn new() -> Stats {
Stats {
on: std::env::var_os("PINNACLE_VULKAN_STATS").is_some(),
since: Instant::now(),
sends: 0,
send_bytes: 0,
send_time: Duration::ZERO,
long_streams: 0,
calls: HashMap::new(),
presents: 0,
present_time: Duration::ZERO,
}
}
pub(crate) fn on(&self) -> bool {
self.on
}
pub(crate) fn sent(&mut self, bytes: usize, took: Duration, long: bool) {
self.sends += 1;
self.send_bytes += bytes as u64;
self.send_time += took;
self.long_streams += u64::from(long);
self.say();
}
pub(crate) fn called(&mut self, kind: u32, took: Duration) {
let entry = self.calls.entry(kind).or_default();
entry.0 += 1;
entry.1 += took;
}
pub(crate) fn presented(&mut self, took: Duration) {
self.presents += 1;
self.present_time += took;
}
/// Says what the last five seconds held, once they are up.
fn say(&mut self) {
let elapsed = self.since.elapsed();
if !self.on || elapsed < EVERY {
return;
}
let waited: Duration = self.calls.values().map(|c| c.1).sum();
let mut calls: Vec<_> = self.calls.iter().collect();
calls.sort_by(|a, b| b.1.1.cmp(&a.1.1));
let top: Vec<String> = calls
.iter()
.take(6)
.map(|(kind, (n, t))| format!("{kind}:{n}x/{}ms", t.as_millis()))
.collect();
eprintln!(
"pinnacle-vulkan stats {:.1}s: {} sends ({} KiB, {} long) taking {} ms; {} replies waited {} ms [{}]; {} presents taking {} ms",
elapsed.as_secs_f32(),
self.sends,
self.send_bytes >> 10,
self.long_streams,
self.send_time.as_millis(),
calls.iter().map(|c| c.1.0).sum::<u64>(),
waited.as_millis(),
top.join(" "),
self.presents,
self.present_time.as_millis(),
);
*self = Stats {
on: true,
..Stats::new()
};
}
}