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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
type SpanId = u64;
thread_local! {
pub(crate) static TRACE_LOCAL: std::cell::UnsafeCell<TraceLocal> = std::cell::UnsafeCell::new(TraceLocal {
span_stack: Vec::with_capacity(1024),
enter_stack: Vec::with_capacity(1024),
id_prefix: next_global_id_prefix(),
id_suffix: 0,
cur_collector: None,
});
}
pub(crate) struct TraceLocal {
pub(crate) span_stack: Vec<crate::Span>,
pub(crate) enter_stack: Vec<SpanId>,
pub(crate) id_prefix: u32,
pub(crate) id_suffix: u32,
pub(crate) cur_collector: Option<std::sync::Arc<crate::collector::CollectorInner>>,
}
static GLOBAL_ID_COUNTER: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
#[inline]
fn next_global_id_prefix() -> u32 {
GLOBAL_ID_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
}
pub struct LocalTraceGuard {
collector: std::sync::Arc<crate::collector::CollectorInner>,
trace_local: *mut TraceLocal,
start_index: usize,
create_time_ns: u64,
start_time_ns: u64,
}
impl !Sync for LocalTraceGuard {}
impl !Send for LocalTraceGuard {}
impl LocalTraceGuard {
pub(crate) fn new<T: Into<u32>>(
collector: std::sync::Arc<crate::collector::CollectorInner>,
event: T,
link: crate::Link,
create_time_ns: u64,
start_time_ns: u64,
) -> Option<(Self, SpanId)> {
if collector.closed.load(std::sync::atomic::Ordering::SeqCst) {
return None;
}
let trace_local = TRACE_LOCAL.with(|trace_local| trace_local.get());
let tl = unsafe { &mut *trace_local };
tl.cur_collector = Some(collector.clone());
let id = {
if tl.id_suffix == std::u32::MAX {
tl.id_suffix = 0;
tl.id_prefix = next_global_id_prefix();
} else {
tl.id_suffix += 1;
}
((tl.id_prefix as u64) << 32) | tl.id_suffix as u64
};
tl.enter_stack.push(id);
let start_index = tl.span_stack.len();
tl.span_stack.push(crate::Span {
id,
link,
begin_cycles: crate::time::monotonic_cycles(),
end_cycles: 0,
event: event.into(),
});
Some((
Self {
collector,
trace_local,
start_index,
create_time_ns,
start_time_ns,
},
id,
))
}
}
impl Drop for LocalTraceGuard {
fn drop(&mut self) {
let tl = unsafe { &mut *self.trace_local };
tl.span_stack[self.start_index].end_cycles = crate::time::monotonic_cycles();
let id = tl.span_stack[self.start_index].id;
assert_eq!(tl.enter_stack.pop().unwrap(), id, "corrupted stack");
if !self
.collector
.closed
.load(std::sync::atomic::Ordering::SeqCst)
{
self.collector.queue.push(crate::SpanSet {
create_time_ns: self.create_time_ns,
start_time_ns: self.start_time_ns,
cycles_per_sec: crate::time::cycles_per_sec(),
spans: tl.span_stack[self.start_index..].to_vec(),
});
}
tl.span_stack.truncate(self.start_index);
if tl.span_stack.capacity() > 1024 && tl.span_stack.len() < 512 {
tl.span_stack.shrink_to(1024);
}
if tl.enter_stack.capacity() > 1024 && tl.enter_stack.len() < 512 {
tl.enter_stack.shrink_to(1024);
}
tl.cur_collector = None;
}
}
pub struct SpanGuard {
trace_local: *mut TraceLocal,
index: usize,
}
impl !Sync for SpanGuard {}
impl !Send for SpanGuard {}
impl SpanGuard {
pub(crate) fn new(event: u32) -> Option<Self> {
let trace_local = TRACE_LOCAL.with(|trace_local| trace_local.get());
let tl = unsafe { &mut *trace_local };
if tl.cur_collector.is_none() || tl.enter_stack.is_empty() {
return None;
}
let index = tl.span_stack.len();
let parent = *tl.enter_stack.last().unwrap();
let id = {
if tl.id_suffix == std::u32::MAX {
tl.id_suffix = 0;
tl.id_prefix = next_global_id_prefix();
} else {
tl.id_suffix += 1;
}
((tl.id_prefix as u64) << 32) | tl.id_suffix as u64
};
tl.enter_stack.push(id);
tl.span_stack.push(crate::Span {
id,
link: crate::Link::Parent { id: parent },
begin_cycles: crate::time::monotonic_cycles(),
end_cycles: 0,
event,
});
Some(Self { trace_local, index })
}
}
impl Drop for SpanGuard {
fn drop(&mut self) {
let tl = unsafe { &mut *self.trace_local };
tl.span_stack[self.index].end_cycles = crate::time::monotonic_cycles();
tl.enter_stack.pop();
}
}