File size: 4,473 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use std::{
    borrow::Cow,
    fmt::{Display, Formatter},
};

use serde::{Deserialize, Serialize};

/// A raw trace line.
#[derive(Debug, Serialize, Deserialize)]
pub enum TraceRow<'a> {
    /// A new span has been started, but not entered yet.
    Start {
        /// Timestamp
        ts: u64,
        /// Unique id for this span.
        id: u64,
        /// Id of the parent span, if any.
        parent: Option<u64>,
        /// The name of the span.
        #[serde(borrow)]
        name: Cow<'a, str>,
        /// The target of the span.
        #[serde(borrow)]
        target: Cow<'a, str>,
        /// A list of key-value pairs for all attributes of the span.
        #[serde(borrow)]
        values: Vec<(Cow<'a, str>, TraceValue<'a>)>,
    },
    /// A span has ended. The id might be reused in future.
    End {
        /// Timestamp
        ts: u64,
        /// Unique id for this span. Must be created by a `Start` event before.
        id: u64,
    },
    /// A span has been entered. This means it is spending CPU time now.
    Enter {
        /// Timestamp
        ts: u64,
        /// Unique id for this span. Must be created by a `Start` event before.
        id: u64,
        /// The thread id of the thread that entered the span.
        thread_id: u64,
    },
    /// A span has been exited. This means it is not spending CPU time anymore.
    Exit {
        /// Timestamp
        ts: u64,
        /// Unique id for this span. Must be entered by a `Enter` event before.
        id: u64,
        /// The thread id of the thread that exits the span.
        thread_id: u64,
    },
    /// A event has happened for some span.
    Event {
        /// Timestamp
        ts: u64,
        /// Id of the parent span, if any.
        parent: Option<u64>,
        /// A list of key-value pairs for all attributes of the event.
        #[serde(borrow)]
        values: Vec<(Cow<'a, str>, TraceValue<'a>)>,
    },
    /// Additional fields for a span
    Record {
        /// Unique id for this span. Must be created by a `Start` event before.
        id: u64,
        /// A list of key-value pairs for all attributes of the span.
        #[serde(borrow)]
        values: Vec<(Cow<'a, str>, TraceValue<'a>)>,
    },
    /// Data about (de)allocations that happened
    Allocation {
        /// Timestamp
        ts: u64,
        /// The thread id of the thread where allocations happened.
        thread_id: u64,
        /// Allocations
        allocations: u64,
        /// Allocation count
        allocation_count: u64,
        /// Deallocations
        deallocations: u64,
        /// Deallocation count
        deallocation_count: u64,
    },
    /// Data about (de)allocations per thread counters. Actual allocations can
    /// be computed from the difference.
    AllocationCounters {
        /// Timestamp
        ts: u64,
        /// The thread id of the thread where allocations happened.
        thread_id: u64,
        /// Allocations
        allocations: u64,
        /// Allocation count
        allocation_count: u64,
        /// Deallocations
        deallocations: u64,
        /// Deallocation count
        deallocation_count: u64,
    },
}

#[derive(Debug, Serialize, Deserialize)]
pub enum TraceValue<'a> {
    String(#[serde(borrow)] Cow<'a, str>),
    Bool(bool),
    UInt(u64),
    Int(i64),
    Float(f64),
}

impl Display for TraceValue<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            TraceValue::String(s) => write!(f, "{s}"),
            TraceValue::Bool(b) => write!(f, "{b}"),
            TraceValue::UInt(u) => write!(f, "{u}"),
            TraceValue::Int(i) => write!(f, "{i}"),
            TraceValue::Float(fl) => write!(f, "{fl}"),
        }
    }
}

impl TraceValue<'_> {
    pub fn as_u64(&self) -> Option<u64> {
        match self {
            TraceValue::UInt(u) => Some(*u),
            _ => None,
        }
    }

    pub fn as_str(&self) -> Option<&str> {
        match self {
            TraceValue::String(s) => Some(s),
            _ => None,
        }
    }

    pub fn into_static(self) -> TraceValue<'static> {
        match self {
            TraceValue::String(s) => TraceValue::String(s.into_owned().into()),
            TraceValue::Bool(b) => TraceValue::Bool(b),
            TraceValue::UInt(u) => TraceValue::UInt(u),
            TraceValue::Int(i) => TraceValue::Int(i),
            TraceValue::Float(fl) => TraceValue::Float(fl),
        }
    }
}