use std::{ num::NonZeroUsize, sync::{Arc, OnceLock}, }; use hashbrown::HashMap; use crate::timestamp::Timestamp; pub type SpanIndex = NonZeroUsize; pub struct Span { // These values won't change after creation: pub parent: Option, pub depth: u32, pub start: Timestamp, pub category: String, pub name: String, pub args: Vec<(String, String)>, // This might change during writing: pub events: Vec, pub is_complete: bool, // These values are computed automatically: pub self_allocations: u64, pub self_allocation_count: u64, pub self_deallocations: u64, pub self_deallocation_count: u64, // These values are computed when accessed (and maybe deleted during writing): pub max_depth: OnceLock, pub total_allocations: OnceLock, pub total_deallocations: OnceLock, pub total_persistent_allocations: OnceLock, pub total_span_count: OnceLock, pub total_allocation_count: OnceLock, // More nested fields, but memory lazily allocated pub time_data: OnceLock>, pub extra: OnceLock>, pub names: OnceLock>, } #[derive(Default)] pub struct SpanTimeData { // These values won't change after creation: pub ignore_self_time: bool, // This might change during writing: pub self_end: Timestamp, // These values are computed automatically: pub self_time: Timestamp, // These values are computed when accessed (and maybe deleted during writing): pub end: OnceLock, pub total_time: OnceLock, pub corrected_self_time: OnceLock, pub corrected_total_time: OnceLock, } #[derive(Default)] pub struct SpanExtra { pub graph: OnceLock>, pub bottom_up: OnceLock>>, pub search_index: OnceLock>>, } #[derive(Default)] pub struct SpanNames { // These values are computed when accessed (and maybe deleted during writing): pub nice_name: OnceLock<(String, String)>, pub group_name: OnceLock<(String, String)>, } impl Span { pub fn time_data(&self) -> &SpanTimeData { self.time_data.get_or_init(|| { Box::new(SpanTimeData { self_end: self.start, ignore_self_time: &self.name == "thread", ..Default::default() }) }) } pub fn time_data_mut(&mut self) -> &mut SpanTimeData { self.time_data(); self.time_data.get_mut().unwrap() } pub fn extra(&self) -> &SpanExtra { self.extra.get_or_init(Default::default) } pub fn names(&self) -> &SpanNames { self.names.get_or_init(Default::default) } } #[derive(Copy, Clone, PartialEq, Eq)] pub enum SpanEvent { SelfTime { start: Timestamp, end: Timestamp }, Child { index: SpanIndex }, } #[derive(Clone)] pub enum SpanGraphEvent { // TODO(sokra) use events instead of children for visualizing span graphs #[allow(dead_code)] SelfTime { duration: Timestamp, }, Child { child: Arc, }, } pub struct SpanGraph { // These values won't change after creation: pub root_spans: Vec, pub recursive_spans: Vec, // These values are computed when accessed: pub max_depth: OnceLock, pub events: OnceLock>, pub self_time: OnceLock, pub self_allocations: OnceLock, pub self_deallocations: OnceLock, pub self_persistent_allocations: OnceLock, pub self_allocation_count: OnceLock, pub total_time: OnceLock, pub total_allocations: OnceLock, pub total_deallocations: OnceLock, pub total_persistent_allocations: OnceLock, pub total_allocation_count: OnceLock, pub total_span_count: OnceLock, pub corrected_self_time: OnceLock, pub corrected_total_time: OnceLock, pub bottom_up: OnceLock>>, } pub struct SpanBottomUp { // These values won't change after creation: pub self_spans: Vec, pub children: Vec>, pub example_span: SpanIndex, // These values are computed when accessed: pub max_depth: OnceLock, pub events: OnceLock>, pub self_time: OnceLock, pub corrected_self_time: OnceLock, pub self_allocations: OnceLock, pub self_deallocations: OnceLock, pub self_persistent_allocations: OnceLock, pub self_allocation_count: OnceLock, } impl SpanBottomUp { pub fn new( self_spans: Vec, example_span: SpanIndex, children: Vec>, ) -> Self { Self { self_spans, children, example_span, max_depth: OnceLock::new(), events: OnceLock::new(), self_time: OnceLock::new(), corrected_self_time: OnceLock::new(), self_allocations: OnceLock::new(), self_deallocations: OnceLock::new(), self_persistent_allocations: OnceLock::new(), self_allocation_count: OnceLock::new(), } } }