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 happend.
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 happend.
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}
104
105#[derive(Debug, Serialize, Deserialize)]
106pub enum TraceValue<'a> {
107 String(#[serde(borrow)] Cow<'a, str>),
108 Bool(bool),
109 UInt(u64),
110 Int(i64),
111 Float(f64),
112}
113
114impl Display for TraceValue<'_> {
115 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
116 match self {
117 TraceValue::String(s) => write!(f, "{s}"),
118 TraceValue::Bool(b) => write!(f, "{b}"),
119 TraceValue::UInt(u) => write!(f, "{u}"),
120 TraceValue::Int(i) => write!(f, "{i}"),
121 TraceValue::Float(fl) => write!(f, "{fl}"),
122 }
123 }
124}
125
126impl TraceValue<'_> {
127 pub fn as_u64(&self) -> Option<u64> {
128 match self {
129 TraceValue::UInt(u) => Some(*u),
130 _ => None,
131 }
132 }
133
134 pub fn as_str(&self) -> Option<&str> {
135 match self {
136 TraceValue::String(s) => Some(s),
137 _ => None,
138 }
139 }
140
141 pub fn into_static(self) -> TraceValue<'static> {
142 match self {
143 TraceValue::String(s) => TraceValue::String(s.into_owned().into()),
144 TraceValue::Bool(b) => TraceValue::Bool(b),
145 TraceValue::UInt(u) => TraceValue::UInt(u),
146 TraceValue::Int(i) => TraceValue::Int(i),
147 TraceValue::Float(fl) => TraceValue::Float(fl),
148 }
149 }
150}