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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use std::alloc;
use std::ffi::CString;
#[doc(hidden)]
pub use tracy_client_sys as sys;
pub struct Span(
sys::TracyCZoneCtx,
std::marker::PhantomData<*mut sys::TracyCZoneCtx>,
);
impl Span {
pub fn new(name: &str, function: &str, file: &str, line: u32, callstack_depth: u16) -> Self {
unsafe {
sys::___tracy_init_thread();
let loc = sys::___tracy_alloc_srcloc_name(
line,
file.as_ptr() as _,
file.len(),
function.as_ptr() as _,
function.len(),
name.as_ptr() as _,
name.len(),
);
if callstack_depth == 0 {
Self(
sys::___tracy_emit_zone_begin_alloc(loc, 1),
std::marker::PhantomData,
)
} else {
Self(
sys::___tracy_emit_zone_begin_alloc_callstack(
loc,
adjust_stack_depth(callstack_depth).into(),
1,
),
std::marker::PhantomData,
)
}
}
}
pub fn emit_value(&self, value: u64) {
unsafe {
sys::___tracy_emit_zone_value(self.0, value);
}
}
pub fn emit_text(&self, text: &str) {
unsafe {
sys::___tracy_emit_zone_text(self.0, text.as_ptr() as _, text.len());
}
}
}
impl Drop for Span {
fn drop(&mut self) {
unsafe {
sys::___tracy_emit_zone_end(self.0);
}
}
}
pub struct ProfiledAllocator<T>(T, u16);
impl<T> ProfiledAllocator<T> {
pub const fn new(inner_allocator: T, callstack_depth: u16) -> Self {
Self(inner_allocator, adjust_stack_depth(callstack_depth))
}
fn emit_alloc(&self, ptr: *mut u8, size: usize) -> *mut u8 {
unsafe {
if self.1 == 0 {
sys::___tracy_emit_memory_alloc(ptr as _, size, 1);
} else {
sys::___tracy_emit_memory_alloc_callstack(ptr as _, size, self.1.into(), 1);
}
}
ptr
}
fn emit_free(&self, ptr: *mut u8) -> *mut u8 {
unsafe {
if self.1 == 0 {
sys::___tracy_emit_memory_free(ptr as _, 1);
} else {
sys::___tracy_emit_memory_free_callstack(ptr as _, self.1.into(), 1);
}
}
ptr
}
}
unsafe impl<T: alloc::GlobalAlloc> alloc::GlobalAlloc for ProfiledAllocator<T> {
unsafe fn alloc(&self, layout: alloc::Layout) -> *mut u8 {
self.emit_alloc(self.0.alloc(layout), layout.size())
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: alloc::Layout) {
self.0.dealloc(self.emit_free(ptr), layout)
}
unsafe fn alloc_zeroed(&self, layout: alloc::Layout) -> *mut u8 {
self.emit_alloc(self.0.alloc_zeroed(layout), layout.size())
}
unsafe fn realloc(&self, ptr: *mut u8, layout: alloc::Layout, new_size: usize) -> *mut u8 {
self.emit_alloc(
self.0.realloc(self.emit_free(ptr), layout, new_size),
new_size,
)
}
}
#[macro_export]
macro_rules! finish_continuous_frame {
() => {
unsafe {
$crate::sys::___tracy_emit_frame_mark(std::ptr::null());
}
};
($name: literal) => {
unsafe {
$crate::sys::___tracy_emit_frame_mark(concat!($name, "\0").as_ptr() as _);
}
};
}
#[macro_export]
macro_rules! start_noncontinuous_frame {
($name: literal) => {
unsafe {
let name = concat!($name, "\0");
$crate::sys::___tracy_emit_frame_mark_start(name.as_ptr() as _);
$crate::Frame::new_unchecked(name)
}
};
}
pub struct Frame(&'static str);
impl Frame {
#[doc(hidden)]
pub const unsafe fn new_unchecked(name: &'static str) -> Self {
Self(name)
}
}
impl Drop for Frame {
fn drop(&mut self) {
unsafe {
sys::___tracy_emit_frame_mark_end(self.0.as_ptr() as _);
}
}
}
pub fn message(message: &str, callstack_depth: u16) {
unsafe {
sys::___tracy_init_thread();
sys::___tracy_emit_message(
message.as_ptr() as _,
message.len(),
adjust_stack_depth(callstack_depth).into(),
)
}
}
pub fn color_message(message: &str, rgba: u32, callstack_depth: u16) {
unsafe {
sys::___tracy_init_thread();
sys::___tracy_emit_messageC(
message.as_ptr() as _,
message.len(),
rgba >> 8,
adjust_stack_depth(callstack_depth).into(),
)
}
}
pub fn set_thread_name(name: &str) {
let name = CString::new(name).unwrap();
unsafe { sys::___tracy_set_thread_name(name.as_ptr() as _) }
}
#[macro_export]
macro_rules! create_plot {
($name: literal) => {
unsafe { $crate::Plot::new_unchecked(concat!($name, "\0")) }
};
}
pub struct Plot(&'static str);
impl Plot {
#[doc(hidden)]
pub const unsafe fn new_unchecked(name: &'static str) -> Self {
Self(name)
}
pub fn point(&self, value: f64) {
unsafe {
sys::___tracy_emit_plot(self.0.as_ptr() as _, value);
}
}
}
#[inline(always)]
#[cfg(windows)]
const fn adjust_stack_depth(depth: u16) -> u16 {
62 ^ ((depth ^ 62) & 0u16.wrapping_sub((depth < 62) as _))
}
#[inline(always)]
#[cfg(not(windows))]
const fn adjust_stack_depth(depth: u16) -> u16 {
depth
}
#[cfg(test)]
mod tests {
use super::*;
#[global_allocator]
static GLOBAL: ProfiledAllocator<alloc::System> = ProfiledAllocator::new(alloc::System, 100);
#[test]
fn zone_values() {
let span = Span::new("test zone values", "zone_values", file!(), line!(), 100);
span.emit_value(42);
span.emit_text("some text");
}
#[test]
fn finish_frameset() {
for _ in 0..10 {
finish_continuous_frame!();
}
}
#[test]
fn finish_secondary_frameset() {
for _ in 0..5 {
finish_continuous_frame!("every two seconds");
}
}
#[test]
fn non_continuous_frameset() {
let _: Frame = start_noncontinuous_frame!("weird frameset");
}
#[test]
fn plot_something() {
static PLOT: Plot = create_plot!("a plot");
for i in 0..10 {
PLOT.point(i as f64);
}
}
}