summaryrefslogtreecommitdiff
path: root/src/tracy.zig
blob: 3b0b862152d0c96c0a62aef3dc9c12a8525cb345 (plain) (blame)
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
//! Minimal Tracy client (the Zig compiler's pattern): `zone(@src(), "name")`
//! ... `.end()`, plus `frameMark()`. Everything compiles to a no-op unless the
//! build sets -Dtracy=<path-to-tracy-checkout>, which also compiles and links
//! TracyClient.cpp. Capture with tracy-capture, inspect with tracy-csvexport.
const std = @import("std");
const builtin = @import("builtin");

pub const enable = if (builtin.is_test) false else @import("pardes_config").enable_tracy;

const SourceLocation = extern struct {
    name: ?[*:0]const u8,
    function: [*:0]const u8,
    file: [*:0]const u8,
    line: u32,
    color: u32,
};

pub const Ctx = if (enable) extern struct {
    id: u32,
    active: i32,

    pub inline fn end(c: @This()) void {
        ___tracy_emit_zone_end(c);
    }
} else struct {
    pub inline fn end(_: @This()) void {}
};

pub inline fn zone(comptime src: std.builtin.SourceLocation, comptime name: [:0]const u8) Ctx {
    if (!enable) return .{};
    const static = struct {
        const loc: SourceLocation = .{
            .name = name.ptr,
            .function = src.fn_name.ptr,
            .file = src.file.ptr,
            .line = src.line,
            .color = 0,
        };
    };
    return ___tracy_emit_zone_begin(&static.loc, 1);
}

pub inline fn frameMark() void {
    if (!enable) return;
    ___tracy_emit_frame_mark(null);
}

extern fn ___tracy_emit_zone_begin(srcloc: *const SourceLocation, active: c_int) Ctx;
extern fn ___tracy_emit_zone_end(ctx: Ctx) void;
extern fn ___tracy_emit_frame_mark(name: ?[*:0]const u8) void;