authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-09-21 00:12:47 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-09-21 00:12:47 -07:00
log3e61b8f598cfd209c142911db15077fb3e0ff1b7
tree804f26b8e8315028eb5ab2d515fb31d3aa2967a4
parent16783577405277a465ac95f2b524fc5f5e959533

backend previously named spall is actually chrome://tracing json format


3 files changed, 71 insertions(+), 71 deletions(-)

src/chrome.zig created+70
......@@ -0,0 +1,70 @@
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;
12threadlocal var buffered_writer: std.io.BufferedWriter(4096, std.fs.File.Writer) = undefined;
13
14pub fn init() !void {
15 pid = std.os.linux.getpid();
16}
17
18pub fn deinit() void {
19 //
20}
21
22pub fn init_thread() !void {
23 tid = std.os.linux.gettid();
24
25 path = try std.fmt.allocPrint(alloc, "/data/trace.{d}.{d}.spall.json", .{ pid, tid });
26 file = try std.fs.cwd().createFile(path, .{});
27 buffered_writer = std.io.bufferedWriter(file.writer());
28
29 try buffered_writer.writer().writeAll("[\n");
30}
31
32pub fn deinit_thread() void {
33 defer alloc.free(path);
34 defer file.close();
35
36 buffered_writer.writer().writeAll("]\n") catch {};
37 buffered_writer.flush() catch {};
38 log.debug("{s}", .{path});
39}
40
41pub inline fn trace_begin(ctx: tracer.Ctx) void {
42 buffered_writer.writer().print(
43 \\{{"cat":"function", "name":"{s}:{d}:{d} ({s})", "ph": "B", "pid": {d}, "tid": {d}, "ts": {d}}},
44 \\
45 ,
46 .{
47 if (ctx.src.file[0] == '/') ctx.src.file[trim_count..] else ctx.src.file,
48 ctx.src.line,
49 ctx.src.column,
50 ctx.src.fn_name,
51 pid,
52 tid,
53 std.time.microTimestamp(),
54 },
55 ) catch {};
56}
57
58pub inline fn trace_end(ctx: tracer.Ctx) void {
59 _ = ctx;
60 buffered_writer.writer().print(
61 \\{{"cat":"function", "ph": "E", "pid": {d}, "tid": {d}, "ts": {d}}},
62 \\
63 ,
64 .{
65 pid,
66 tid,
67 std.time.microTimestamp(),
68 },
69 ) catch {};
70}
src/mod.zig+1-1
......@@ -6,7 +6,7 @@ var started = false;
66
77pub const none = @import("./none.zig");
88pub const log = @import("./log.zig");
9pub const spall = @import("./spall.zig");
9pub const chrome = @import("./chrome.zig");
1010
1111pub fn init() !void {
1212 try impl.init();
src/spall.zig deleted-70
......@@ -1,70 +0,0 @@
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;
12threadlocal var buffered_writer: std.io.BufferedWriter(4096, std.fs.File.Writer) = undefined;
13
14pub fn init() !void {
15 pid = std.os.linux.getpid();
16}
17
18pub fn deinit() void {
19 //
20}
21
22pub fn init_thread() !void {
23 tid = std.os.linux.gettid();
24
25 path = try std.fmt.allocPrint(alloc, "/data/trace.{d}.{d}.spall.json", .{ pid, tid });
26 file = try std.fs.cwd().createFile(path, .{});
27 buffered_writer = std.io.bufferedWriter(file.writer());
28
29 try buffered_writer.writer().writeAll("[\n");
30}
31
32pub fn deinit_thread() void {
33 defer alloc.free(path);
34 defer file.close();
35
36 buffered_writer.writer().writeAll("]\n") catch {};
37 buffered_writer.flush() catch {};
38 log.debug("{s}", .{path});
39}
40
41pub inline fn trace_begin(ctx: tracer.Ctx) void {
42 buffered_writer.writer().print(
43 \\{{"cat":"function", "name":"{s}:{d}:{d} ({s})", "ph": "B", "pid": {d}, "tid": {d}, "ts": {d}}},
44 \\
45 ,
46 .{
47 if (ctx.src.file[0] == '/') ctx.src.file[trim_count..] else ctx.src.file,
48 ctx.src.line,
49 ctx.src.column,
50 ctx.src.fn_name,
51 pid,
52 tid,
53 std.time.microTimestamp(),
54 },
55 ) catch {};
56}
57
58pub inline fn trace_end(ctx: tracer.Ctx) void {
59 _ = ctx;
60 buffered_writer.writer().print(
61 \\{{"cat":"function", "ph": "E", "pid": {d}, "tid": {d}, "ts": {d}}},
62 \\
63 ,
64 .{
65 pid,
66 tid,
67 std.time.microTimestamp(),
68 },
69 ) catch {};
70}