summaryrefslogtreecommitdiff
path: root/src/cli/perf.zig
blob: 57ad11ac57e2ce89bb5d8aa9b0d9a271a6356508 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! 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(&regs);
}