Skip to main content

turbopack_trace_utils/
tracing.rs

1use std::{
2    borrow::Cow,
3    fmt::{Display, Formatter},
4};
5
6use serde::{Deserialize, Serialize};
7
8/// A raw trace line.
9#[derive(Debug, Serialize, Deserialize)]
10pub enum TraceRow<'a> {
11    /// A new span has been started, but not entered yet.
12    Start {
13        /// Timestamp
14        ts: u64,
15        /// Unique id for this span.
16        id: u64,
17        /// Id of the parent span, if any.
18        parent: Option<u64>,
19        /// The name of the span.
20        #[serde(borrow)]
21        name: Cow<'a, str>,
22        /// The target of the span.
23        #[serde(borrow)]
24        target: Cow<'a, str>,
25        /// A list of key-value pairs for all attributes of the span.
26        #[serde(borrow)]
27        values: Vec<(Cow<'a, str>, TraceValue<'a>)>,
28    },
29    /// A span has ended. The id might be reused in future.
30    End {
31        /// Timestamp
32        ts: u64,
33        /// Unique id for this span. Must be created by a `Start` event before.
34        id: u64,
35    },
36    /// A span has been entered. This means it is spending CPU time now.
37    Enter {
38        /// Timestamp
39        ts: u64,
40        /// Unique id for this span. Must be created by a `Start` event before.
41        id: u64,
42        /// The thread id of the thread that entered the span.
43        thread_id: u64,
44    },
45    /// A span has been exited. This means it is not spending CPU time anymore.
46    Exit {
47        /// Timestamp
48        ts: u64,
49        /// Unique id for this span. Must be entered by a `Enter` event before.
50        id: u64,
51        /// The thread id of the thread that exits the span.
52        thread_id: u64,
53    },
54    /// A event has happened for some span.
55    Event {
56        /// Timestamp
57        ts: u64,
58        /// Id of the parent span, if any.
59        parent: Option<u64>,
60        /// A list of key-value pairs for all attributes of the event.
61        #[serde(borrow)]
62        values: Vec<(Cow<'a, str>, TraceValue<'a>)>,
63    },
64    /// Additional fields for a span
65    Record {
66        /// Unique id for this span. Must be created by a `Start` event before.
67        id: u64,
68        /// A list of key-value pairs for all attributes of the span.
69        #[serde(borrow)]
70        values: Vec<(Cow<'a, str>, TraceValue<'a>)>,
71    },
72    /// Data about (de)allocations that happened
73    Allocation {
74        /// Timestamp
75        ts: u64,
76        /// The thread id of the thread where allocations happened.
77        thread_id: u64,
78        /// Allocations
79        allocations: u64,
80        /// Allocation count
81        allocation_count: u64,
82        /// Deallocations
83        deallocations: u64,
84        /// Deallocation count
85        deallocation_count: u64,
86    },
87    /// Data about (de)allocations per thread counters. Actual allocations can
88    /// be computed from the difference.
89    AllocationCounters {
90        /// Timestamp
91        ts: u64,
92        /// The thread id of the thread where allocations happened.
93        thread_id: u64,
94        /// Allocations
95        allocations: u64,
96        /// Allocation count
97        allocation_count: u64,
98        /// Deallocations
99        deallocations: u64,
100        /// Deallocation count
101        deallocation_count: u64,
102    },
103    /// A snapshot of the process memory usage at a point in time.
104    MemorySample {
105        /// Timestamp
106        ts: u64,
107        /// Memory usage in bytes (from TurboMalloc::memory_usage())
108        memory: u64,
109    },
110}
111
112#[derive(Debug, Serialize, Deserialize)]
113pub enum TraceValue<'a> {
114    String(#[serde(borrow)] Cow<'a, str>),
115    Bool(bool),
116    UInt(u64),
117    Int(i64),
118    Float(f64),
119}
120
121impl Display for TraceValue<'_> {
122    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
123        match self {
124            TraceValue::String(s) => write!(f, "{s}"),
125            TraceValue::Bool(b) => write!(f, "{b}"),
126            TraceValue::UInt(u) => write!(f, "{u}"),
127            TraceValue::Int(i) => write!(f, "{i}"),
128            TraceValue::Float(fl) => write!(f, "{fl}"),
129        }
130    }
131}
132
133impl TraceValue<'_> {
134    pub fn as_u64(&self) -> Option<u64> {
135        match self {
136            TraceValue::UInt(u) => Some(*u),
137            _ => None,
138        }
139    }
140
141    pub fn as_str(&self) -> Option<&str> {
142        match self {
143            TraceValue::String(s) => Some(s),
144            _ => None,
145        }
146    }
147
148    pub fn into_static(self) -> TraceValue<'static> {
149        match self {
150            TraceValue::String(s) => TraceValue::String(s.into_owned().into()),
151            TraceValue::Bool(b) => TraceValue::Bool(b),
152            TraceValue::UInt(u) => TraceValue::UInt(u),
153            TraceValue::Int(i) => TraceValue::Int(i),
154            TraceValue::Float(fl) => TraceValue::Float(fl),
155        }
156    }
157}