//! ztrace perf: a program at full speed, the kernel taking notes (perf //! events). No privilege needed, no ptrace once it runs. const std = @import("std"); const linux = std.os.linux; const zt = @import("ztrace"); const util = @import("util.zig"); const fail = util.fail; const Kind = zt.Kind; pub const usage = \\usage: ztrace perf [-o FILE] [OPTIONS] -- PROGRAM [ARGS] \\ \\Runs PROGRAM at full speed while the kernel records it through perf \\events, as any user may on their own processes, into a recording \\(default out.ztrace). Always recorded: its memory maps, the threads it \\starts and ends, its exit. \\ \\ -e NAME,... counters, read when it ends (default instructions,cycles): \\ instructions cycles branches branch-misses \\ cache-references cache-misses page-faults task-clock \\ --interval MS ... and every MS milliseconds \\ --sample HZ a profile: where it is, HZ times a second \\ --callchain ... and who called that (needs frame pointers) \\ --faults every page fault: address, pc, registers \\ --switches every time it leaves or gets a CPU \\ --syscalls every syscall: arguments, result, the path it names \\ --watch WHAT[:LEN][:rw] \\ a debug register on a symbol or an address: every \\ write to its LEN bytes (1, 2, 4, 8; default the symbol's \\ size) with the value found there; with :rw every access \\ --break WHAT ... on code: every time it runs, with the registers \\ --threads also the threads and children it creates \\ --ring BYTES the kernel's buffer, per CPU (a power of two, default 512 KiB) \\ \\There are four debug registers. Only user space is seen: a page the \\kernel touches first (a read() into fresh memory) faults unseen. \\ ; const Spec = struct { what: []const u8, len: ?u8 = null, mode: zt.perf.Watch.Mode }; pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { const arena = init.arena.allocator(); var out: []const u8 = "out.ztrace"; var options: zt.perf.Options = .{ .counters = &.{ .instructions, .cycles } }; var interval_ms: u32 = 0; var specs: std.ArrayList(Spec) = .empty; var i: usize = 0; while (i < args.len) : (i += 1) { const arg = args[i]; const value: ?[]const u8 = if (i + 1 < args.len) args[i + 1] else null; if (std.mem.eql(u8, arg, "--")) { i += 1; break; } else if (std.mem.eql(u8, arg, "--callchain")) { options.callchain = true; } else if (std.mem.eql(u8, arg, "--faults")) { options.faults = true; } else if (std.mem.eql(u8, arg, "--switches")) { options.switches = true; } else if (std.mem.eql(u8, arg, "--syscalls")) { options.syscalls = true; } else if (std.mem.eql(u8, arg, "--threads")) { options.follow = true; } else if (value == null) { fail(usage, .{}); } else if (std.mem.eql(u8, arg, "-o")) { out = value.?; i += 1; } else if (std.mem.eql(u8, arg, "-e")) { var counters: std.ArrayList(zt.perf.Counter) = .empty; var names = std.mem.tokenizeScalar(u8, value.?, ','); while (names.next()) |name| try counters.append(arena, std.meta.stringToEnum(zt.perf.Counter, name) orelse fail("-e {s}: no such counter\n", .{name})); options.counters = counters.items; i += 1; } else if (std.mem.eql(u8, arg, "--interval")) { interval_ms = std.fmt.parseInt(u32, value.?, 0) catch fail("--interval needs milliseconds\n", .{}); i += 1; } else if (std.mem.eql(u8, arg, "--sample")) { options.sample_hz = std.fmt.parseInt(u32, value.?, 0) catch fail("--sample needs a rate\n", .{}); i += 1; } else if (std.mem.eql(u8, arg, "--ring")) { options.ring_bytes = std.fmt.parseInt(usize, value.?, 0) catch 0; if (!std.math.isPowerOfTwo(options.ring_bytes) or options.ring_bytes < 4096) fail("--ring needs a power of two, 4096 or more\n", .{}); i += 1; } else if (std.mem.eql(u8, arg, "--break")) { try specs.append(arena, .{ .what = value.?, .mode = .exec }); i += 1; } else if (std.mem.eql(u8, arg, "--watch")) { var spec: Spec = .{ .what = value.?, .mode = .write }; var parts = std.mem.splitScalar(u8, value.?, ':'); spec.what = parts.first(); while (parts.next()) |part| { if (std.mem.eql(u8, part, "rw")) spec.mode = .access else spec.len = std.fmt.parseInt(u8, part, 0) catch fail("--watch {s}: {s} is neither a length nor rw\n", .{ value.?, part }); } try specs.append(arena, spec); i += 1; } else fail(usage, .{}); } if (i >= args.len) fail(usage, .{}); 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); errdefer proc.kill(); var rec = try zt.Recorder.create(init.gpa, init.io, out); // What changes the state a moment holds (registers, memory) ends one. 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) { 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, }; defer session.close(); var at_entry = false; for (specs.items) |spec| { const sym: zt.perf.Symbol = if (std.fmt.parseInt(u64, spec.what, 0)) |addr| .{ .addr = addr, .size = 8 } else |_| found: { if (try zt.perf.lookup(init.gpa, proc.pid, spec.what)) |sym| break :found sym; // A library's symbol: there once the loader has mapped it. if (!at_entry) try runToEntry(&proc); at_entry = true; break :found try zt.perf.lookup(init.gpa, proc.pid, spec.what) orelse fail("{s}: no such symbol in what the program has mapped\n", .{spec.what}); }; const len = spec.len orelse if (std.mem.indexOfScalar(u64, &.{ 1, 2, 4 }, sym.size)) |_| @as(u8, @intCast(sym.size)) else 8; session.watch(.{ .addr = sym.addr, .len = len, .mode = spec.mode, .name = spec.what }) catch |err| switch (err) { error.NoFreeBreakpoint => fail("{s}: no debug register left, there are four\n", .{spec.what}), error.PerfNoSuchEvent => fail("{s}: a watchpoint covers 1, 2, 4 or 8 bytes at an address aligned to that, not {d} at {x}\n", .{ spec.what, len, sym.addr }), else => |e| return e, }; } zt.perf.release(proc); const start = zt.event.now(); var next_read = start; while (true) { var wait_ms: i32 = 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); } if (!try session.poll(wait_ms)) break; } try session.readCounters(); var status: u32 = 0; _ = linux.waitpid(proc.pid, @ptrCast(&status), 0); const exit_code: u64 = if (linux.W.IFEXITED(status)) linux.W.EXITSTATUS(status) else 128 + @as(u64, @backingInt(linux.W.TERMSIG(status))); const ns = zt.event.now() - start; try rec.emit(.{ .ts = zt.event.now(), .tid = @intCast(proc.pid), .kind = .exit, .a = .{ exit_code, 0, 0, 0, 0, 0 } }, ""); const events = rec.count; const moments = rec.moments + @intFromBool(rec.delta.items.len > 0); try rec.close(); const size = (try std.Io.Dir.cwd().statFile(init.io, out, .{})).size; for (options.counters, session.counts) |c, count| std.debug.print("{d:>16} {t}\n", .{ count, c }); std.debug.print("{s}: {d} moments, {d} events ({d} lost), {d} bytes, {d:.1} ms, {d:.0} events/s, exit {d}\n", .{ out, moments, events, session.lost, size, @as(f64, @floatFromInt(ns)) / 1e6, @as(f64, @floatFromInt(events)) / (@as(f64, @floatFromInt(ns)) / 1e9), exit_code, }); } /// Runs the held target up to its own entry point, where the dynamic /// loader is done and its libraries are mapped. fn runToEntry(proc: *zt.ptrace.Process) !void { var path_buf: [64]u8 = undefined; const fd = linux.open(try std.fmt.bufPrintSentinel(&path_buf, "/proc/{d}/auxv", .{proc.pid}, 0), .{}, 0); if (linux.errno(fd) != .SUCCESS) return error.NoSuchProcess; defer _ = linux.close(@intCast(fd)); var auxv: [64][2]u64 = undefined; const n = linux.read(@intCast(fd), @ptrCast(&auxv), @sizeOf(@TypeOf(auxv))); if (linux.errno(n) != .SUCCESS) return error.NoSuchProcess; const entry = for (auxv[0 .. n / 16]) |pair| { if (pair[0] == std.elf.AT.ENTRY) break pair[1]; } else return error.NoEntryPoint; var saved: [1]u8 = undefined; if (proc.read(entry, &saved) != 1) return error.BadAddress; try proc.write(entry, &.{0xcc}); while (true) switch (try proc.cont()) { .trap => break, .exited, .killed => fail("the program ended before its entry point\n", .{}), else => {}, }; try proc.write(entry, &saved); var regs = try proc.regs(); regs.rip = entry; try proc.setRegs(®s); }