From 952af9711cc6a4de987a9d9d45e1e36721c5b470 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 12:15:20 -0300 Subject: ztrace perf takes the target's own ring: allocations among the page faults --preload and --alloc on ztrace perf: what the agent says and what the kernel sees are merged by time into one recording (perf.Sorted, as probe merges BPF and perf). test-together runs a list under both. --- README.md | 2 ++ build.zig | 20 +++++++++++++++++++- src/cli/perf.zig | 45 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 029241e..922ab77 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,8 @@ ztrace cc -- clang -O0 targets/instr_list.c -o list ztrace record --by break -b push -b reverse --alloc --preload zig-out/lib/libztrace-agent.so -- ./list ztrace dbg --ring -- ./list # breakpoints and time travel over what the program says of itself ztrace show out.ztrace 40 # the thread's line and calls, the live heap, each variable's bytes +ztrace perf --faults --syscalls --alloc --preload zig-out/lib/libztrace-agent.so -- ./list + # the kernel's view too: each malloc, the brk it makes, the faults of its first touch zig build test-together ``` diff --git a/build.zig b/build.zig index e7dd08a..19fa712 100644 --- a/build.zig +++ b/build.zig @@ -109,6 +109,24 @@ pub fn build(b: *std.Build) void { shown.addArg("show"); shown.addFileArg(recording); inline for (.{ "6 breakpoint", "5 alloc", "7 call", "7 ret", " variable", " line" }) |part| shown.expectStdOutMatch(part); - ctx.areaTest("test-together", "A debugger, the agent and instrumentation on one program").dependOn(&shown.step); + const both = ctx.areaTest("test-together", "The backends on one program, in one recording"); + both.dependOn(&shown.step); + + // ... and the kernel's view with the program's own: a list's + // allocations among the page faults of the heap they grow. + const seen = b.addRunArtifact(exe); + seen.addArgs(&.{ "perf", "--faults", "--alloc", "--preload" }); + seen.addArtifactArg(agent); + seen.addArg("-o"); + const kernel_and_own = seen.addOutputFileArg("perf-agent.ztrace"); + seen.addArg("--"); + seen.addArtifactArg(ctx.cTarget("agent_list", &.{"agent_list.c"})); + seen.addArg("100"); + seen.expectStdErrMatch("(0 lost)"); + const listed = b.addRunArtifact(exe); + listed.addArg("show"); + listed.addFileArg(kernel_and_own); + inline for (.{ " alloc", " free", " fault", " map" }) |part| listed.expectStdOutMatch(part); + both.dependOn(&listed.step); @import("build/kernel.zig").add(ctx); } diff --git a/src/cli/perf.zig b/src/cli/perf.zig index ea211fe..48523ce 100644 --- a/src/cli/perf.zig +++ b/src/cli/perf.zig @@ -40,6 +40,11 @@ pub const usage = \\Moments end where the recorder ends them anyway (a syscall's exit, a \\megabyte of events), so that a profile or a storm of faults stays small. \\ + \\--preload LIB puts the agent into PROGRAM and --alloc has it record + \\allocations: what the program says of itself and what the kernel sees + \\of it are then one recording, in the order they happened (a malloc, + \\the brk it makes, the page faults of its first touch). + \\ ; const Spec = struct { what: []const u8, len: ?u8 = null, mode: zt.perf.Watch.Mode }; @@ -52,6 +57,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { var cut = false; var max_bytes: ?u64 = null; var specs: std.ArrayList(Spec) = .empty; + var producer: util.Producer = .{}; var i: usize = 0; while (i < args.len) : (i += 1) { const arg = args[i]; @@ -59,6 +65,8 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { if (std.mem.eql(u8, arg, "--")) { i += 1; break; + } else if (!std.mem.eql(u8, arg, "--ring") and producer.take(args, &i)) { + // --ring is the kernel's here; the target's comes with --preload. } else if (std.mem.eql(u8, arg, "--callchain")) { options.callchain = true; } else if (std.mem.eql(u8, arg, "--faults")) { @@ -114,13 +122,18 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { const path = try util.resolve(init, args[i]); const argv = try util.targetArgv(arena, args[i..]); // Held at its first instruction until the events are on it. - var proc = try zt.ptrace.Process.spawn(path, argv, init.minimal.environ.block.slice.ptr); + const produced = try producer.start(arena, init.minimal.environ.block.slice); + var proc = try zt.ptrace.Process.spawn(path, argv, if (produced) |p| p.envp.ptr else init.minimal.environ.block.slice.ptr); errdefer proc.close(); var rec = try zt.Recorder.create(init.gpa, init.io, out); if (max_bytes) |bytes| rec.max_bytes = bytes; + // With the target's own ring there are two sources: merged by time. + var sorted: zt.perf.Sorted = .{ .gpa = init.gpa, .to = rec.sink() }; + defer sorted.deinit(); + const sink = if (produced != null) sorted.sink() else rec.sink(); // What changes the state a moment holds (registers, memory) ends one. if (cut) rec.cut |= Kind.fault.bit() | Kind.probe.bit() | Kind.mem_write.bit() | Kind.mem_read.bit(); - var session = zt.perf.Session.open(init.gpa, proc.pid, options, rec.sink()) catch |err| switch (err) { + var session = zt.perf.Session.open(init.gpa, proc.pid, options, sink) catch |err| switch (err) { error.PerfAccess => fail("perf_event_open was refused: kernel.perf_event_paranoid is above 2, or a security module forbids it\n", .{}), error.PerfNoSuchEvent => fail("perf_event_open: this CPU or kernel lacks one of the events asked for (in a VM without a PMU, try -e task-clock; --sample takes at most kernel.perf_event_max_sample_rate)\n", .{}), else => |e| return e, @@ -153,7 +166,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { zt.perf.release(proc); const start = zt.event.now(); - const exit_code: ?u64 = pump(&session, &rec, interval_ms) catch |err| switch (err) { + const exit_code: ?u64 = pump(&session, sink, if (produced) |p| .{ .ring = p.ring, .sorted = &sorted } else null, interval_ms) catch |err| switch (err) { error.RecordingFull => null, else => |e| return e, }; @@ -164,22 +177,38 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { /// Decodes until the target ends; its exit code. `error.RecordingFull` /// when the recording reaches its limit first. -fn pump(session: *zt.perf.Session, rec: *zt.Recorder, interval_ms: u32) !u64 { +fn pump(session: *zt.perf.Session, sink: zt.Sink, own: ?struct { ring: *zt.Ring, sorted: *zt.perf.Sorted }, interval_ms: u32) !u64 { var next_read = zt.event.now(); + // What both sources have reported past: the start of the round before. + var past: u64 = 0; while (true) { - var wait_ms: i32 = 100; + // A target with a ring of its own waits when that is full: look often. + var wait_ms: i32 = if (own != null) 5 else 100; if (interval_ms != 0) { const now = zt.event.now(); if (now >= next_read) { try session.readCounters(); next_read += @as(u64, interval_ms) * std.time.ns_per_ms; } - wait_ms = @intCast((next_read -| now) / std.time.ns_per_ms + 1); + wait_ms = @min(wait_ms, @as(i32, @intCast((next_read -| now) / std.time.ns_per_ms + 1))); + } + const now = zt.event.now(); + const alive = try session.poll(wait_ms); + if (own) |o| { + _ = try o.ring.drain(sink); + try o.sorted.flush(past); + past = now; } - if (!try session.poll(wait_ms)) break; + if (!alive) break; } try session.readCounters(); - return exited(session.pid, rec.sink()); + const code = try exited(session.pid, sink); + if (own) |o| { + // What it emitted on its way out. + _ = try o.ring.drain(sink); + try o.sorted.flush(std.math.maxInt(u64)); + } + return code; } /// Waits for the target's end and records it. The exit code. -- cgit v1.3