| author | |
| committer | |
| log | 57975651a2dfee8b4f688b0d0b33344efa824913 |
| tree | 66001ba6be58311a8f1d34d21639c1abba3d7a97 |
| parent | f8f0f93960b5bd5e1d70cc5fde967f19b8646198 |
4 files changed, 145 insertions(+), 0 deletions(-)
src/log.zig created+19| ... | ... | @@ -0,0 +1,19 @@ |
| 1 | const std = @import("std"); | |
| 2 | const tracer = @import("./mod.zig"); | |
| 3 | const log = std.log.scoped(.tracer); | |
| 4 | ||
| 5 | pub fn init() void {} | |
| 6 | ||
| 7 | pub fn deinit() void {} | |
| 8 | ||
| 9 | pub fn init2() void {} | |
| 10 | ||
| 11 | pub fn deinit2() void {} | |
| 12 | ||
| 13 | pub 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 | ||
| 17 | pub inline fn trace_end(ctx: tracer.Ctx) void { | |
| 18 | _ = ctx; | |
| 19 | } |
src/mod.zig+41| ... | ... | @@ -1 +1,42 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const root = @import("root"); | |
| 3 | const impl = std.meta.globalOption("tracer_impl", type) orelse none; | |
| 4 | ||
| 5 | var started = false; | |
| 6 | ||
| 7 | pub const none = @import("./none.zig"); | |
| 8 | pub const log = @import("./log.zig"); | |
| 9 | pub const spall = @import("./spall.zig"); | |
| 10 | ||
| 11 | pub fn init() !void { | |
| 12 | impl.init(); | |
| 13 | } | |
| 14 | ||
| 15 | pub fn deinit() void { | |
| 16 | impl.deinit(); | |
| 17 | } | |
| 18 | ||
| 19 | pub fn init2() !void { | |
| 20 | impl.init2(); | |
| 21 | started = true; | |
| 22 | } | |
| 23 | ||
| 24 | pub fn deinit2() void { | |
| 25 | impl.deinit2(); | |
| 26 | } | |
| 27 | ||
| 28 | pub 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 | ||
| 36 | pub 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 @@ |
| 1 | const std = @import("std"); | |
| 2 | const tracer = @import("./mod.zig"); | |
| 3 | const log = std.log.scoped(.tracer); | |
| 4 | ||
| 5 | pub fn init() void {} | |
| 6 | ||
| 7 | pub fn deinit() void {} | |
| 8 | ||
| 9 | pub fn init2() void {} | |
| 10 | ||
| 11 | pub fn deinit2() void {} | |
| 12 | ||
| 13 | pub inline fn trace_begin(ctx: tracer.Ctx) void { | |
| 14 | _ = ctx; | |
| 15 | } | |
| 16 | ||
| 17 | pub inline fn trace_end(ctx: tracer.Ctx) void { | |
| 18 | _ = ctx; | |
| 19 | } |
src/spall.zig created+66| ... | ... | @@ -0,0 +1,66 @@ |
| 1 | const std = @import("std"); | |
| 2 | const tracer = @import("./mod.zig"); | |
| 3 | const alloc = std.heap.c_allocator; | |
| 4 | const log = std.log.scoped(.tracer); | |
| 5 | const root = @import("root"); | |
| 6 | const trim_count = root.build_options.src_file_trimlen; | |
| 7 | ||
| 8 | var pid: std.os.linux.pid_t = undefined; | |
| 9 | threadlocal var tid: std.os.linux.pid_t = undefined; | |
| 10 | threadlocal var path: []const u8 = undefined; | |
| 11 | threadlocal var file: std.fs.File = undefined; | |
| 12 | ||
| 13 | pub fn init() void { | |
| 14 | pid = std.os.linux.getpid(); | |
| 15 | } | |
| 16 | ||
| 17 | pub fn deinit() void { | |
| 18 | // | |
| 19 | } | |
| 20 | ||
| 21 | pub 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 | ||
| 29 | pub 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 | ||
| 37 | pub 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 | ||
| 54 | pub 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 | } |