diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 11:49:22 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 12:19:06 -0300 |
| commit | 297943b01e18bb3d1dd41a8d3e220811de25228b (patch) | |
| tree | 4938e643abada811d3ab28b14c0e31dc94ebda62 /src/trace/perf.zig | |
| parent | db923fa823354270217362db06356f2b1a41315e (diff) | |
| download | computeranim-297943b01e18bb3d1dd41a8d3e220811de25228b.tar.gz computeranim-297943b01e18bb3d1dd41a8d3e220811de25228b.zip | |
ztrace probe: BPF on syscalls, scheduling, kprobes, uprobes and USDT
Programs built as instruction arrays for one process (no compiler, no
libbpf), writing Events into a BPF ring buffer; a drop counter in a map;
task_struct offsets from the kernel's BTF; a USDT probe's arguments read
in the kernel at the probe. Without CAP_BPF and CAP_PERFMON it says so;
--dry-run builds and lists everything. Tested offline by running the
programs in a small BPF machine, and for real by test-kernel-vm, which
boots this kernel in QEMU where we are root.
ztrace perf: moments are no longer cut per event unless --cut;
--max-size, and a full recording stops the target and says so.
perf.Sorted merges two backends' events by time.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'src/trace/perf.zig')
| -rw-r--r-- | src/trace/perf.zig | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/trace/perf.zig b/src/trace/perf.zig index 7c9ef43..c251741 100644 --- a/src/trace/perf.zig +++ b/src/trace/perf.zig @@ -513,6 +513,58 @@ fn userData(raw: []const u8) []const u8 { return ""; } +/// A sink in front of another for when two backends record the same run +/// (this one and BPF, or the agent's ring): each emits in time order, both +/// with the same clock, and here they become one sequence. Events are held +/// until `flush` passes on, oldest first, those up to a time that every +/// backend has reported past. +pub const Sorted = struct { + gpa: std.mem.Allocator, + to: event.Sink, + held: std.ArrayList(Held) = .empty, + payloads: std.ArrayList(u8) = .empty, + + const Held = struct { ev: Event, at: usize, len: usize }; + + pub fn sink(s: *Sorted) event.Sink { + return .{ .ctx = s, .emitFn = hold }; + } + + fn hold(ctx: *anyopaque, ev: *const Event, payload: []const u8) anyerror!void { + const s: *Sorted = @ptrCast(@alignCast(ctx)); + try s.held.append(s.gpa, .{ .ev = ev.*, .at = s.payloads.items.len, .len = payload.len }); + try s.payloads.appendSlice(s.gpa, payload); + } + + pub fn flush(s: *Sorted, until: u64) !void { + // Stable: events of one instant stay in the order their backend gave them. + std.mem.sort(Held, s.held.items, {}, struct { + fn older(_: void, a: Held, b: Held) bool { + return a.ev.ts < b.ev.ts; + } + }.older); + var done: usize = 0; + while (done < s.held.items.len and s.held.items[done].ev.ts <= until) : (done += 1) { + const h = s.held.items[done]; + try s.to.emit(h.ev, s.payloads.items[h.at..][0..h.len]); + } + // What stays moves to the front, its payloads with it. + var kept: std.ArrayList(u8) = .empty; + for (s.held.items[done..]) |*h| { + try kept.appendSlice(s.gpa, s.payloads.items[h.at..][0..h.len]); + h.at = kept.items.len - h.len; + } + s.held.replaceRangeAssumeCapacity(0, done, &.{}); + s.payloads.deinit(s.gpa); + s.payloads = kept; + } + + pub fn deinit(s: *Sorted) void { + s.held.deinit(s.gpa); + s.payloads.deinit(s.gpa); + } +}; + /// Lets a target held by `ptrace.Process.spawn` run on its own: once the /// events are attached, ptrace has done its part. pub fn release(p: ptrace.Process) void { @@ -596,6 +648,31 @@ fn perfOpen(attr: *linux.perf_event_attr, pid: linux.pid_t, cpu: i32) !linux.fd_ }; } +test "two backends' events come out as one sequence in time" { + const Seen = struct { + order: [8]u8 = undefined, + len: usize = 0, + fn emit(ctx: *anyopaque, ev: *const Event, payload: []const u8) anyerror!void { + const seen: *@This() = @ptrCast(@alignCast(ctx)); + try std.testing.expectEqual(ev.a[0], payload.len); + seen.order[seen.len] = payload[0]; + seen.len += 1; + } + }; + var seen: Seen = .{}; + var sorted: Sorted = .{ .gpa = std.testing.allocator, .to = .{ .ctx = &seen, .emitFn = Seen.emit } }; + defer sorted.deinit(); + // One backend, then the other, each in its own order. + for ([_]struct { u64, []const u8 }{ .{ 10, "a" }, .{ 30, "ccc" }, .{ 50, "eeeee" }, .{ 20, "bb" }, .{ 30, "dddd" }, .{ 60, "ffffff" } }) |e| { + try sorted.sink().emit(.{ .ts = e[0], .kind = .mark, .a = .{ e[1].len, 0, 0, 0, 0, 0 } }, e[1]); + } + try sorted.flush(30); + try std.testing.expectEqualStrings("abcd", seen.order[0..seen.len]); + try sorted.sink().emit(.{ .ts = 55, .kind = .mark, .a = .{ 1, 0, 0, 0, 0, 0 } }, "x"); + try sorted.flush(std.math.maxInt(u64)); + try std.testing.expectEqualStrings("abcdexf", seen.order[0..seen.len]); +} + test "a maps line; the user data of a syscall record" { const m = parseMap("7f80b4224000-7f80b439f000 r-xp 00024000 08:02 1234 /usr/lib/libc.so.6").?; try std.testing.expectEqual(@as(u64, 0x7f80b4224000), m.start); |
