diff --git a/src/chrome.zig b/src/chrome.zig new file mode 100644 index 0000000000000000000000000000000000000000..b134b778eecc19c326c4a976dc451a344578fad3 --- /dev/null +++ b/src/chrome.zig @@ -0,0 +1,70 @@ +const std = @import("std"); +const tracer = @import("./mod.zig"); +const alloc = std.heap.c_allocator; +const log = std.log.scoped(.tracer); +const root = @import("root"); +const trim_count = root.build_options.src_file_trimlen; + +var pid: std.os.linux.pid_t = undefined; +threadlocal var tid: std.os.linux.pid_t = undefined; +threadlocal var path: []const u8 = undefined; +threadlocal var file: std.fs.File = undefined; +threadlocal var buffered_writer: std.io.BufferedWriter(4096, std.fs.File.Writer) = undefined; + +pub fn init() !void { + pid = std.os.linux.getpid(); +} + +pub fn deinit() void { + // +} + +pub fn init_thread() !void { + tid = std.os.linux.gettid(); + + path = try std.fmt.allocPrint(alloc, "/data/trace.{d}.{d}.spall.json", .{ pid, tid }); + file = try std.fs.cwd().createFile(path, .{}); + buffered_writer = std.io.bufferedWriter(file.writer()); + + try buffered_writer.writer().writeAll("[\n"); +} + +pub fn deinit_thread() void { + defer alloc.free(path); + defer file.close(); + + buffered_writer.writer().writeAll("]\n") catch {}; + buffered_writer.flush() catch {}; + log.debug("{s}", .{path}); +} + +pub inline fn trace_begin(ctx: tracer.Ctx) void { + buffered_writer.writer().print( + \\{{"cat":"function", "name":"{s}:{d}:{d} ({s})", "ph": "B", "pid": {d}, "tid": {d}, "ts": {d}}}, + \\ + , + .{ + if (ctx.src.file[0] == '/') ctx.src.file[trim_count..] else ctx.src.file, + ctx.src.line, + ctx.src.column, + ctx.src.fn_name, + pid, + tid, + std.time.microTimestamp(), + }, + ) catch {}; +} + +pub inline fn trace_end(ctx: tracer.Ctx) void { + _ = ctx; + buffered_writer.writer().print( + \\{{"cat":"function", "ph": "E", "pid": {d}, "tid": {d}, "ts": {d}}}, + \\ + , + .{ + pid, + tid, + std.time.microTimestamp(), + }, + ) catch {}; +} diff --git a/src/mod.zig b/src/mod.zig index 44be1c7f550879530459ad1ac551183c4e7a4a32..5c01653771ef36f610c4ae893df1ec345ced8f27 100644 --- a/src/mod.zig +++ b/src/mod.zig @@ -6,7 +6,7 @@ var started = false; pub const none = @import("./none.zig"); pub const log = @import("./log.zig"); -pub const spall = @import("./spall.zig"); +pub const chrome = @import("./chrome.zig"); pub fn init() !void { try impl.init(); diff --git a/src/spall.zig b/src/spall.zig deleted file mode 100644 index b134b778eecc19c326c4a976dc451a344578fad3..0000000000000000000000000000000000000000 --- a/src/spall.zig +++ /dev/null @@ -1,70 +0,0 @@ -const std = @import("std"); -const tracer = @import("./mod.zig"); -const alloc = std.heap.c_allocator; -const log = std.log.scoped(.tracer); -const root = @import("root"); -const trim_count = root.build_options.src_file_trimlen; - -var pid: std.os.linux.pid_t = undefined; -threadlocal var tid: std.os.linux.pid_t = undefined; -threadlocal var path: []const u8 = undefined; -threadlocal var file: std.fs.File = undefined; -threadlocal var buffered_writer: std.io.BufferedWriter(4096, std.fs.File.Writer) = undefined; - -pub fn init() !void { - pid = std.os.linux.getpid(); -} - -pub fn deinit() void { - // -} - -pub fn init_thread() !void { - tid = std.os.linux.gettid(); - - path = try std.fmt.allocPrint(alloc, "/data/trace.{d}.{d}.spall.json", .{ pid, tid }); - file = try std.fs.cwd().createFile(path, .{}); - buffered_writer = std.io.bufferedWriter(file.writer()); - - try buffered_writer.writer().writeAll("[\n"); -} - -pub fn deinit_thread() void { - defer alloc.free(path); - defer file.close(); - - buffered_writer.writer().writeAll("]\n") catch {}; - buffered_writer.flush() catch {}; - log.debug("{s}", .{path}); -} - -pub inline fn trace_begin(ctx: tracer.Ctx) void { - buffered_writer.writer().print( - \\{{"cat":"function", "name":"{s}:{d}:{d} ({s})", "ph": "B", "pid": {d}, "tid": {d}, "ts": {d}}}, - \\ - , - .{ - if (ctx.src.file[0] == '/') ctx.src.file[trim_count..] else ctx.src.file, - ctx.src.line, - ctx.src.column, - ctx.src.fn_name, - pid, - tid, - std.time.microTimestamp(), - }, - ) catch {}; -} - -pub inline fn trace_end(ctx: tracer.Ctx) void { - _ = ctx; - buffered_writer.writer().print( - \\{{"cat":"function", "ph": "E", "pid": {d}, "tid": {d}, "ts": {d}}}, - \\ - , - .{ - pid, - tid, - std.time.microTimestamp(), - }, - ) catch {}; -}