tracing-calltree 0.1.2

Always-on hierarchical profiling for Rust tracing spans with rolling latency statistics.
Documentation
use crate::display::SnapshotDisplay;
use crate::sample::TimingSample;
use crate::stats::TimingStats;

#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct CallTreeSnapshot {
    pub roots: Vec<NodeSnapshot>,
}

impl CallTreeSnapshot {
    pub fn display(&self) -> SnapshotDisplay<'_> {
        SnapshotDisplay(self)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct NodeSnapshot {
    pub name: String,
    pub target: String,
    pub module_path: Option<String>,
    pub line: Option<u32>,
    pub total_calls: u64,
    pub wall: TimingStats,
    pub active: TimingStats,
    pub suspended: TimingStats,
    pub children: Vec<NodeSnapshot>,
}

impl NodeSnapshot {
    pub(crate) fn from_samples(
        name: String,
        target: String,
        module_path: Option<String>,
        line: Option<u32>,
        total_calls: u64,
        samples: &[TimingSample],
        children: Vec<NodeSnapshot>,
    ) -> Self {
        Self {
            name,
            target,
            module_path,
            line,
            total_calls,
            wall: TimingStats::from_nanos(samples.iter().map(|sample| sample.wall_ns)),
            active: TimingStats::from_nanos(samples.iter().map(|sample| sample.active_ns)),
            suspended: TimingStats::from_nanos(samples.iter().map(|sample| sample.suspended_ns())),
            children,
        }
    }
}