-
Notifications
You must be signed in to change notification settings - Fork 7
/
tracy.zig
529 lines (500 loc) · 18.1 KB
/
tracy.zig
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
const std = @import("std");
const builtin = @import("builtin");
const Src = std.builtin.SourceLocation;
// check for a decl named tracy_enabled in root or build_options
pub const enabled = blk: {
var build_enable: ?bool = null;
var root_enable: ?bool = null;
const root = @import("root");
if (@hasDecl(root, "tracy_enabled")) {
root_enable = @as(bool, root.tracy_enabled);
}
if (!builtin.is_test) {
// Don't try to include build_options in tests.
// Otherwise `zig test` doesn't work.
const options = @import("build_options");
if (@hasDecl(options, "tracy_enabled")) {
build_enable = @as(bool, options.tracy_enabled);
}
}
if (build_enable != null and root_enable != null) {
if (build_enable.? != root_enable.?) {
@compileError("root.tracy_enabled disagrees with build_options.tracy_enabled! Please remove one or make them match.");
}
}
break :blk root_enable orelse (build_enable orelse false);
};
const debug_verify_stack_order = false;
pub usingnamespace if (enabled) tracy_full else tracy_stub;
const tracy_stub = struct {
pub const ZoneCtx = struct {
pub inline fn Text(self: ZoneCtx, text: []const u8) void {
_ = self;
_ = text;
}
pub inline fn Name(self: ZoneCtx, name: []const u8) void {
_ = self;
_ = name;
}
pub inline fn Value(self: ZoneCtx, value: u64) void {
_ = self;
_ = value;
}
pub inline fn End(self: ZoneCtx) void {
_ = self;
}
};
pub inline fn InitThread() void {}
pub inline fn SetThreadName(name: [*:0]const u8) void {
_ = name;
}
pub inline fn Zone(comptime src: Src) ZoneCtx {
_ = src;
return .{};
}
pub inline fn ZoneN(comptime src: Src, name: [*:0]const u8) ZoneCtx {
_ = src;
_ = name;
return .{};
}
pub inline fn ZoneC(comptime src: Src, color: u32) ZoneCtx {
_ = src;
_ = color;
return .{};
}
pub inline fn ZoneNC(comptime src: Src, name: [*:0]const u8, color: u32) ZoneCtx {
_ = src;
_ = name;
_ = color;
return .{};
}
pub inline fn ZoneS(comptime src: Src, depth: i32) ZoneCtx {
_ = src;
_ = depth;
return .{};
}
pub inline fn ZoneNS(comptime src: Src, name: [*:0]const u8, depth: i32) ZoneCtx {
_ = src;
_ = name;
_ = depth;
return .{};
}
pub inline fn ZoneCS(comptime src: Src, color: u32, depth: i32) ZoneCtx {
_ = src;
_ = color;
_ = depth;
return .{};
}
pub inline fn ZoneNCS(comptime src: Src, name: [*:0]const u8, color: u32, depth: i32) ZoneCtx {
_ = src;
_ = name;
_ = color;
_ = depth;
return .{};
}
pub inline fn Alloc(ptr: ?*const anyopaque, size: usize) void {
_ = ptr;
_ = size;
}
pub inline fn Free(ptr: ?*const anyopaque) void {
_ = ptr;
}
pub inline fn SecureAlloc(ptr: ?*const anyopaque, size: usize) void {
_ = ptr;
_ = size;
}
pub inline fn SecureFree(ptr: ?*const anyopaque) void {
_ = ptr;
}
pub inline fn AllocS(ptr: ?*const anyopaque, size: usize, depth: c_int) void {
_ = ptr;
_ = size;
_ = depth;
}
pub inline fn FreeS(ptr: ?*const anyopaque, depth: c_int) void {
_ = ptr;
_ = depth;
}
pub inline fn SecureAllocS(ptr: ?*const anyopaque, size: usize, depth: c_int) void {
_ = ptr;
_ = size;
_ = depth;
}
pub inline fn SecureFreeS(ptr: ?*const anyopaque, depth: c_int) void {
_ = ptr;
_ = depth;
}
pub inline fn AllocN(ptr: ?*const anyopaque, size: usize, name: [*:0]const u8) void {
_ = ptr;
_ = size;
_ = name;
}
pub inline fn FreeN(ptr: ?*const anyopaque, name: [*:0]const u8) void {
_ = ptr;
_ = name;
}
pub inline fn SecureAllocN(ptr: ?*const anyopaque, size: usize, name: [*:0]const u8) void {
_ = ptr;
_ = size;
_ = name;
}
pub inline fn SecureFreeN(ptr: ?*const anyopaque, name: [*:0]const u8) void {
_ = ptr;
_ = name;
}
pub inline fn AllocNS(ptr: ?*const anyopaque, size: usize, depth: c_int, name: [*:0]const u8) void {
_ = ptr;
_ = size;
_ = depth;
_ = name;
}
pub inline fn FreeNS(ptr: ?*const anyopaque, depth: c_int, name: [*:0]const u8) void {
_ = ptr;
_ = depth;
_ = name;
}
pub inline fn SecureAllocNS(ptr: ?*const anyopaque, size: usize, depth: c_int, name: [*:0]const u8) void {
_ = ptr;
_ = size;
_ = depth;
_ = name;
}
pub inline fn SecureFreeNS(ptr: ?*const anyopaque, depth: c_int, name: [*:0]const u8) void {
_ = ptr;
_ = depth;
_ = name;
}
pub inline fn Message(text: []const u8) void {
_ = text;
}
pub inline fn MessageL(text: [*:0]const u8) void {
_ = text;
}
pub inline fn MessageC(text: []const u8, color: u32) void {
_ = text;
_ = color;
}
pub inline fn MessageLC(text: [*:0]const u8, color: u32) void {
_ = text;
_ = color;
}
pub inline fn MessageS(text: []const u8, depth: c_int) void {
_ = text;
_ = depth;
}
pub inline fn MessageLS(text: [*:0]const u8, depth: c_int) void {
_ = text;
_ = depth;
}
pub inline fn MessageCS(text: []const u8, color: u32, depth: c_int) void {
_ = text;
_ = color;
_ = depth;
}
pub inline fn MessageLCS(text: [*:0]const u8, color: u32, depth: c_int) void {
_ = text;
_ = color;
_ = depth;
}
pub inline fn FrameMark() void {}
pub inline fn FrameMarkNamed(name: [*:0]const u8) void {
_ = name;
}
pub inline fn FrameMarkStart(name: [*:0]const u8) void {
_ = name;
}
pub inline fn FrameMarkEnd(name: [*:0]const u8) void {
_ = name;
}
pub inline fn FrameImage(image: ?*const anyopaque, width: u16, height: u16, offset: u8, flip: c_int) void {
_ = image;
_ = width;
_ = height;
_ = offset;
_ = flip;
}
pub inline fn PlotF(name: [*:0]const u8, val: f64) void {
_ = name;
_ = val;
}
pub inline fn PlotU(name: [*:0]const u8, val: u64) void {
_ = name;
_ = val;
}
pub inline fn PlotI(name: [*:0]const u8, val: i64) void {
_ = name;
_ = val;
}
pub inline fn AppInfo(text: []const u8) void {
_ = text;
}
};
const tracy_full = struct {
const c = @cImport({
@cDefine("TRACY_ENABLE", "");
@cInclude("TracyC.h");
});
const has_callstack_support = @hasDecl(c, "TRACY_HAS_CALLSTACK") and @hasDecl(c, "TRACY_CALLSTACK");
const callstack_enabled: c_int = if (has_callstack_support) c.TRACY_CALLSTACK else 0;
threadlocal var stack_depth: if (debug_verify_stack_order) usize else u0 = 0;
pub const ZoneCtx = struct {
_zone: c.___tracy_c_zone_context,
_token: if (debug_verify_stack_order) usize else void,
pub inline fn Text(self: ZoneCtx, text: []const u8) void {
if (debug_verify_stack_order) {
if (stack_depth != self._token) {
std.debug.panic("Error: expected Value() at stack depth {} but was {}\n", .{ self._token, stack_depth });
}
}
c.___tracy_emit_zone_text(self._zone, text.ptr, text.len);
}
pub inline fn Name(self: ZoneCtx, name: []const u8) void {
if (debug_verify_stack_order) {
if (stack_depth != self._token) {
std.debug.panic("Error: expected Value() at stack depth {} but was {}\n", .{ self._token, stack_depth });
}
}
c.___tracy_emit_zone_name(self._zone, name.ptr, name.len);
}
pub inline fn Value(self: ZoneCtx, val: u64) void {
if (debug_verify_stack_order) {
if (stack_depth != self._token) {
std.debug.panic("Error: expected Value() at stack depth {} but was {}\n", .{ self._token, stack_depth });
}
}
c.___tracy_emit_zone_value(self._zone, val);
}
pub inline fn End(self: ZoneCtx) void {
if (debug_verify_stack_order) {
if (stack_depth != self._token) {
std.debug.panic("Error: expected End() at stack depth {} but was {}\n", .{ self._token, stack_depth });
}
stack_depth -= 1;
}
c.___tracy_emit_zone_end(self._zone);
}
};
inline fn initZone(comptime src: Src, name: ?[*:0]const u8, color: u32, depth: c_int) ZoneCtx {
// Tracy uses pointer identity to identify contexts.
// The `src` parameter being comptime ensures that
// each zone gets its own unique global location for this
// struct.
const static = struct {
var loc: c.___tracy_source_location_data = undefined;
};
static.loc = .{
.name = name,
.function = src.fn_name.ptr,
.file = src.file.ptr,
.line = src.line,
.color = color,
};
const zone = if (has_callstack_support)
c.___tracy_emit_zone_begin_callstack(&static.loc, depth, 1)
else
c.___tracy_emit_zone_begin(&static.loc, 1);
if (debug_verify_stack_order) {
stack_depth += 1;
return ZoneCtx{ ._zone = zone, ._token = stack_depth };
} else {
return ZoneCtx{ ._zone = zone, ._token = {} };
}
}
pub inline fn InitThread() void {
c.___tracy_init_thread();
}
pub inline fn SetThreadName(name: [*:0]const u8) void {
c.___tracy_set_thread_name(name);
}
pub inline fn Zone(comptime src: Src) ZoneCtx {
return initZone(src, null, 0, callstack_enabled);
}
pub inline fn ZoneN(comptime src: Src, name: [*:0]const u8) ZoneCtx {
return initZone(src, name, 0, callstack_enabled);
}
pub inline fn ZoneC(comptime src: Src, color: u32) ZoneCtx {
return initZone(src, null, color, callstack_enabled);
}
pub inline fn ZoneNC(comptime src: Src, name: [*:0]const u8, color: u32) ZoneCtx {
return initZone(src, name, color, callstack_enabled);
}
pub inline fn ZoneS(comptime src: Src, depth: i32) ZoneCtx {
return initZone(src, null, 0, depth);
}
pub inline fn ZoneNS(comptime src: Src, name: [*:0]const u8, depth: i32) ZoneCtx {
return initZone(src, name, 0, depth);
}
pub inline fn ZoneCS(comptime src: Src, color: u32, depth: i32) ZoneCtx {
return initZone(src, null, color, depth);
}
pub inline fn ZoneNCS(comptime src: Src, name: [*:0]const u8, color: u32, depth: i32) ZoneCtx {
return initZone(src, name, color, depth);
}
pub inline fn Alloc(ptr: ?*const anyopaque, size: usize) void {
if (has_callstack_support) {
c.___tracy_emit_memory_alloc_callstack(ptr, size, callstack_enabled, 0);
} else {
c.___tracy_emit_memory_alloc(ptr, size, 0);
}
}
pub inline fn Free(ptr: ?*const anyopaque, size: usize) void {
if (has_callstack_support) {
c.___tracy_emit_memory_free_callstack(ptr, callstack_enabled, 0);
} else {
c.___tracy_emit_memory_free(ptr, size, 0);
}
}
pub inline fn SecureAlloc(ptr: ?*const anyopaque, size: usize) void {
if (has_callstack_support) {
c.___tracy_emit_memory_alloc_callstack(ptr, size, callstack_enabled, 1);
} else {
c.___tracy_emit_memory_alloc(ptr, size, 1);
}
}
pub inline fn SecureFree(ptr: ?*const anyopaque, size: usize) void {
if (has_callstack_support) {
c.___tracy_emit_memory_free_callstack(ptr, callstack_enabled, 1);
} else {
c.___tracy_emit_memory_free(ptr, size, 1);
}
}
pub inline fn AllocS(ptr: ?*const anyopaque, size: usize, depth: c_int) void {
if (has_callstack_support) {
c.___tracy_emit_memory_alloc_callstack(ptr, size, depth, 0);
} else {
c.___tracy_emit_memory_alloc(ptr, size, 0);
}
}
pub inline fn FreeS(ptr: ?*const anyopaque, depth: c_int) void {
if (has_callstack_support) {
c.___tracy_emit_memory_free_callstack(ptr, depth, 0);
} else {
c.___tracy_emit_memory_free(ptr, 0);
}
}
pub inline fn SecureAllocS(ptr: ?*const anyopaque, size: usize, depth: c_int) void {
if (has_callstack_support) {
c.___tracy_emit_memory_alloc_callstack(ptr, size, depth, 1);
} else {
c.___tracy_emit_memory_alloc(ptr, size, 1);
}
}
pub inline fn SecureFreeS(ptr: ?*const anyopaque, depth: c_int) void {
if (has_callstack_support) {
c.___tracy_emit_memory_free_callstack(ptr, depth, 1);
} else {
c.___tracy_emit_memory_free(ptr, 1);
}
}
pub inline fn AllocN(ptr: ?*const anyopaque, size: usize, name: [*:0]const u8) void {
if (has_callstack_support) {
c.___tracy_emit_memory_alloc_callstack_named(ptr, size, callstack_enabled, 0, name);
} else {
c.___tracy_emit_memory_alloc_named(ptr, size, 0, name);
}
}
pub inline fn FreeN(ptr: ?*const anyopaque, name: [*:0]const u8) void {
if (has_callstack_support) {
c.___tracy_emit_memory_free_callstack_named(ptr, callstack_enabled, 0, name);
} else {
c.___tracy_emit_memory_free_named(ptr, 0, name);
}
}
pub inline fn SecureAllocN(ptr: ?*const anyopaque, size: usize, name: [*:0]const u8) void {
if (has_callstack_support) {
c.___tracy_emit_memory_alloc_callstack_named(ptr, size, callstack_enabled, 1, name);
} else {
c.___tracy_emit_memory_alloc_named(ptr, size, 1, name);
}
}
pub inline fn SecureFreeN(ptr: ?*const anyopaque, name: [*:0]const u8) void {
if (has_callstack_support) {
c.___tracy_emit_memory_free_callstack_named(ptr, callstack_enabled, 1, name);
} else {
c.___tracy_emit_memory_free_named(ptr, 1, name);
}
}
pub inline fn AllocNS(ptr: ?*const anyopaque, size: usize, depth: c_int, name: [*:0]const u8) void {
if (has_callstack_support) {
c.___tracy_emit_memory_alloc_callstack_named(ptr, size, depth, 0, name);
} else {
c.___tracy_emit_memory_alloc_named(ptr, size, 0, name);
}
}
pub inline fn FreeNS(ptr: ?*const anyopaque, depth: c_int, name: [*:0]const u8) void {
if (has_callstack_support) {
c.___tracy_emit_memory_free_callstack_named(ptr, depth, 0, name);
} else {
c.___tracy_emit_memory_free_named(ptr, 0, name);
}
}
pub inline fn SecureAllocNS(ptr: ?*const anyopaque, size: usize, depth: c_int, name: [*:0]const u8) void {
if (has_callstack_support) {
c.___tracy_emit_memory_alloc_callstack_named(ptr, size, depth, 1, name);
} else {
c.___tracy_emit_memory_alloc_named(ptr, size, 1, name);
}
}
pub inline fn SecureFreeNS(ptr: ?*const anyopaque, depth: c_int, name: [*:0]const u8) void {
if (has_callstack_support) {
c.___tracy_emit_memory_free_callstack_named(ptr, depth, 1, name);
} else {
c.___tracy_emit_memory_free_named(ptr, 1, name);
}
}
pub inline fn Message(text: []const u8) void {
c.___tracy_emit_message(text.ptr, text.len, callstack_enabled);
}
pub inline fn MessageL(text: [*:0]const u8, color: u32) void {
c.___tracy_emit_messageL(text, color, callstack_enabled);
}
pub inline fn MessageC(text: []const u8, color: u32) void {
c.___tracy_emit_messageC(text.ptr, text.len, color, callstack_enabled);
}
pub inline fn MessageLC(text: [*:0]const u8, color: u32) void {
c.___tracy_emit_messageLC(text, color, callstack_enabled);
}
pub inline fn MessageS(text: []const u8, depth: c_int) void {
const inner_depth: c_int = if (has_callstack_support) depth else 0;
c.___tracy_emit_message(text.ptr, text.len, inner_depth);
}
pub inline fn MessageLS(text: [*:0]const u8, depth: c_int) void {
const inner_depth: c_int = if (has_callstack_support) depth else 0;
c.___tracy_emit_messageL(text, inner_depth);
}
pub inline fn MessageCS(text: []const u8, color: u32, depth: c_int) void {
const inner_depth: c_int = if (has_callstack_support) depth else 0;
c.___tracy_emit_messageC(text.ptr, text.len, color, inner_depth);
}
pub inline fn MessageLCS(text: [*:0]const u8, color: u32, depth: c_int) void {
const inner_depth: c_int = if (has_callstack_support) depth else 0;
c.___tracy_emit_messageLC(text, color, inner_depth);
}
pub inline fn FrameMark() void {
c.___tracy_emit_frame_mark(null);
}
pub inline fn FrameMarkNamed(name: [*:0]const u8) void {
c.___tracy_emit_frame_mark(name);
}
pub inline fn FrameMarkStart(name: [*:0]const u8) void {
c.___tracy_emit_frame_mark_start(name);
}
pub inline fn FrameMarkEnd(name: [*:0]const u8) void {
c.___tracy_emit_frame_mark_end(name);
}
pub inline fn FrameImage(image: ?*const anyopaque, width: u16, height: u16, offset: u8, flip: c_int) void {
c.___tracy_emit_frame_image(image, width, height, offset, flip);
}
pub inline fn PlotF(name: [*:0]const u8, val: f64) void {
c.___tracy_emit_plot(name, val);
}
pub inline fn PlotU(name: [*:0]const u8, val: u64) void {
c.___tracy_emit_plot(name, @intToFloat(f64, val));
}
pub inline fn PlotI(name: [*:0]const u8, val: i64) void {
c.___tracy_emit_plot(name, @intToFloat(f64, val));
}
pub inline fn AppInfo(text: []const u8) void {
c.___tracy_emit_message_appinfo(text.ptr, text.len);
}
};