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        /// OS memory pressure in `0..=100` (from
110        /// `TurboMalloc::memory_pressure()`). `0` is used when the current
111        /// platform does not report a pressure value.
112        memory_pressure: u8,
113    },
114}
115
116#[derive(Debug, Serialize, Deserialize)]
117pub enum TraceValue<'a> {
118    String(#[serde(borrow)] Cow<'a, str>),
119    Bool(bool),
120    UInt(u64),
121    Int(i64),
122    Float(f64),
123}
124
125impl Display for TraceValue<'_> {
126    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
127        match self {
128            TraceValue::String(s) => write!(f, "{s}"),
129            TraceValue::Bool(b) => write!(f, "{b}"),
130            TraceValue::UInt(u) => write!(f, "{u}"),
131            TraceValue::Int(i) => write!(f, "{i}"),
132            TraceValue::Float(fl) => write!(f, "{fl}"),
133        }
134    }
135}
136
137impl TraceValue<'_> {
138    pub fn as_u64(&self) -> Option<u64> {
139        match self {
140            TraceValue::UInt(u) => Some(*u),
141            _ => None,
142        }
143    }
144
145    pub fn as_str(&self) -> Option<&str> {
146        match self {
147            TraceValue::String(s) => Some(s),
148            _ => None,
149        }
150    }
151
152    pub fn into_static(self) -> TraceValue<'static> {
153        match self {
154            TraceValue::String(s) => TraceValue::String(s.into_owned().into()),
155            TraceValue::Bool(b) => TraceValue::Bool(b),
156            TraceValue::UInt(u) => TraceValue::UInt(u),
157            TraceValue::Int(i) => TraceValue::Int(i),
158            TraceValue::Float(fl) => TraceValue::Float(fl),
159        }
160    }
161}