authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-09-20 15:52:49 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-09-20 15:52:49 -07:00
log57975651a2dfee8b4f688b0d0b33344efa824913
tree66001ba6be58311a8f1d34d21639c1abba3d7a97
parentf8f0f93960b5bd5e1d70cc5fde967f19b8646198

add implementation


4 files changed, 145 insertions(+), 0 deletions(-)

src/log.zig created+19
...@@ -0,0 +1,19 @@
1const std = @import("std");
2const tracer = @import("./mod.zig");
3const log = std.log.scoped(.tracer);
4
5pub fn init() void {}
6
7pub fn deinit() void {}
8
9pub fn init2() void {}
10
11pub fn deinit2() void {}
12
13pub inline fn trace_begin(ctx: tracer.Ctx) void {
14 log.debug("{s}:{d}:{d} ({s})", .{ ctx.src.file, ctx.src.line, ctx.src.column, ctx.src.fn_name });
15}
16
17pub inline fn trace_end(ctx: tracer.Ctx) void {
18 _ = ctx;
19}
src/mod.zig+41
...@@ -1 +1,42 @@...@@ -1 +1,42 @@
1const std = @import("std");1const std = @import("std");
2const root = @import("root");
3const impl = std.meta.globalOption("tracer_impl", type) orelse none;
4
5var started = false;
6
7pub const none = @import("./none.zig");
8pub const log = @import("./log.zig");
9pub const spall = @import("./spall.zig");
10
11pub fn init() !void {
12 impl.init();
13}
14
15pub fn deinit() void {
16 impl.deinit();
17}
18
19pub fn init2() !void {
20 impl.init2();
21 started = true;
22}
23
24pub fn deinit2() void {
25 impl.deinit2();
26}
27
28pub inline fn trace(src: std.builtin.SourceLocation) Ctx {
29 const ctx = Ctx{
30 .src = src,
31 };
32 if (started) impl.trace_begin(ctx);
33 return ctx;
34}
35
36pub const Ctx = struct {
37 src: std.builtin.SourceLocation,
38
39 pub inline fn end(self: Ctx) void {
40 if (started) impl.trace_end(self);
41 }
42};
src/none.zig created+19
...@@ -0,0 +1,19 @@
1const std = @import("std");
2const tracer = @import("./mod.zig");
3const log = std.log.scoped(.tracer);
4
5pub fn init() void {}
6
7pub fn deinit() void {}
8
9pub fn init2() void {}
10
11pub fn deinit2() void {}
12
13pub inline fn trace_begin(ctx: tracer.Ctx) void {
14 _ = ctx;
15}
16
17pub inline fn trace_end(ctx: tracer.Ctx) void {
18 _ = ctx;
19}
src/spall.zig created+66
...@@ -0,0 +1,66 @@
1const std = @import("std");
2const tracer = @import("./mod.zig");
3const alloc = std.heap.c_allocator;
4const log = std.log.scoped(.tracer);
5const root = @import("root");
6const trim_count = root.build_options.src_file_trimlen;
7
8var pid: std.os.linux.pid_t = undefined;
9threadlocal var tid: std.os.linux.pid_t = undefined;
10threadlocal var path: []const u8 = undefined;
11threadlocal var file: std.fs.File = undefined;
12
13pub fn init() void {
14 pid = std.os.linux.getpid();
15}
16
17pub fn deinit() void {
18 //
19}
20
21pub fn init2() void {
22 tid = std.os.linux.gettid();
23
24 path = std.fmt.allocPrint(alloc, "/data/trace.{d}.{d}.spall.jsonl", .{ pid, tid }) catch @panic("oom");
25 file = std.fs.cwd().createFile(path, .{}) catch @panic("create fail");
26 file.writer().writeAll("[\n") catch @panic("[");
27}
28
29pub fn deinit2() void {
30 defer alloc.free(path);
31 defer file.close();
32
33 file.writer().writeAll("]\n") catch {};
34 log.debug("{s}", .{path});
35}
36
37pub inline fn trace_begin(ctx: tracer.Ctx) void {
38 file.writer().print(
39 \\{{"cat":"function", "name":"{s}:{d}:{d} ({s})", "ph": "B", "pid": {d}, "tid": {d}, "ts": {d}}},
40 \\
41 ,
42 .{
43 if (ctx.src.file[0] == '/') ctx.src.file[trim_count..] else ctx.src.file,
44 ctx.src.line,
45 ctx.src.column,
46 ctx.src.fn_name,
47 pid,
48 tid,
49 std.time.microTimestamp(),
50 },
51 ) catch {};
52}
53
54pub inline fn trace_end(ctx: tracer.Ctx) void {
55 _ = ctx;
56 file.writer().print(
57 \\{{"cat":"function", "ph": "E", "pid": {d}, "tid": {d}, "ts": {d}}},
58 \\
59 ,
60 .{
61 pid,
62 tid,
63 std.time.microTimestamp(),
64 },
65 ) catch {};
66}