use std::collections::BTreeMap;
use std::time::Duration;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProfileReport {
pub schema_version: u32,
pub durations: BTreeMap<&'static str, Duration>,
pub counters: BTreeMap<&'static str, u64>,
pub live_bytes: u64,
pub peak_live_bytes: u64,
}
impl ProfileReport {
pub const SCHEMA_VERSION: u32 = 1;
pub fn new() -> Self {
Self {
schema_version: Self::SCHEMA_VERSION,
..Self::default()
}
}
pub fn add_duration(&mut self, name: &'static str, duration: Duration) {
*self.durations.entry(name).or_default() += duration;
}
pub fn increment(&mut self, name: &'static str, amount: u64) {
*self.counters.entry(name).or_default() += amount;
}
pub fn allocate_bytes(&mut self, bytes: u64) {
self.live_bytes = self.live_bytes.saturating_add(bytes);
self.peak_live_bytes = self.peak_live_bytes.max(self.live_bytes);
}
pub fn release_bytes(&mut self, bytes: u64) {
self.live_bytes = self.live_bytes.saturating_sub(bytes);
}
pub fn merge(&mut self, other: &Self) {
self.schema_version = self.schema_version.max(other.schema_version);
for (&name, &duration) in &other.durations {
self.add_duration(name, duration);
}
for (&name, &count) in &other.counters {
self.increment(name, count);
}
self.peak_live_bytes = self.peak_live_bytes.max(other.peak_live_bytes);
self.live_bytes = self.live_bytes.saturating_add(other.live_bytes);
}
pub fn write_json_fields(&self, out: &mut String) {
use std::fmt::Write as _;
let _ = write!(out, "\"profile_schema\":{}", self.schema_version);
for (&name, duration) in &self.durations {
let _ = write!(out, ",\"{}\":{}", name, duration.as_nanos());
}
for (&name, count) in &self.counters {
let _ = write!(out, ",\"{}\":{}", name, count);
}
let _ = write!(
out,
",\"live_bytes\":{},\"peak_live_bytes\":{}",
self.live_bytes, self.peak_live_bytes
);
}
}
#[derive(Debug, Clone, Copy)]
pub struct Lap {
last: std::time::Instant,
}
impl Lap {
#[inline]
pub fn start() -> Self {
Self {
last: std::time::Instant::now(),
}
}
#[inline]
pub fn lap_ns(&mut self) -> u64 {
let now = std::time::Instant::now();
let ns = now.duration_since(self.last).as_nanos() as u64;
self.last = now;
ns
}
}
#[inline]
pub fn lap_opt(lap: &mut Option<Lap>) -> u64 {
match lap.as_mut() {
Some(lap) => lap.lap_ns(),
None => 0,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ProfileDetail {
#[default]
Stage,
Fill,
Row,
}
pub fn clock_read_cost_ns() -> u64 {
const BATCH: u32 = 2_000;
let mut samples = [0u64; 9];
for sample in &mut samples {
let start = std::time::Instant::now();
let mut lap = Lap::start();
let mut sink = 0u64;
for _ in 0..BATCH {
sink = sink.wrapping_add(lap.lap_ns());
}
std::hint::black_box(sink);
*sample = start.elapsed().as_nanos() as u64 / u64::from(BATCH);
}
samples.sort_unstable();
samples[samples.len() / 2]
}
impl From<()> for ProfileReport {
fn from(_: ()) -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn accumulates_and_merges() {
let mut a = ProfileReport::new();
a.add_duration("render.execute", Duration::from_nanos(3));
a.increment("image.draws", 2);
a.allocate_bytes(10);
a.release_bytes(4);
let mut b = ProfileReport::new();
b.add_duration("render.execute", Duration::from_nanos(2));
b.increment("image.draws", 1);
b.allocate_bytes(20);
a.merge(&b);
assert_eq!(a.durations["render.execute"], Duration::from_nanos(5));
assert_eq!(a.counters["image.draws"], 3);
assert_eq!(a.peak_live_bytes, 20);
assert_eq!(a.schema_version, ProfileReport::SCHEMA_VERSION);
}
}