Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # zfsslower Trace slow ZFS operations. |
| 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
| 7 | # USAGE: zfsslower [-h] [-j] [-p PID] [min_ms] |
| 8 | # |
| 9 | # This script traces common ZFS file operations: reads, writes, opens, and |
| 10 | # syncs. It measures the time spent in these operations, and prints details |
| 11 | # for each that exceeded a threshold. |
| 12 | # |
| 13 | # WARNING: This adds low-overhead instrumentation to these ZFS operations, |
| 14 | # including reads and writes from the file system cache. Such reads and writes |
| 15 | # can be very frequent (depending on the workload; eg, 1M/sec), at which |
| 16 | # point the overhead of this tool (even if it prints no "slower" events) can |
| 17 | # begin to become significant. |
| 18 | # |
| 19 | # This works by using kernel dynamic tracing of the ZPL interface, and will |
| 20 | # need updates to match any changes to this interface. |
| 21 | # |
| 22 | # By default, a minimum millisecond threshold of 10 is used. |
| 23 | # |
| 24 | # Copyright 2016 Netflix, Inc. |
| 25 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 26 | # |
| 27 | # 14-Feb-2016 Brendan Gregg Created this. |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 28 | # 16-Oct-2016 Dina Goldshtein -p to filter by process ID. |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 29 | |
| 30 | from __future__ import print_function |
| 31 | from bcc import BPF |
| 32 | import argparse |
| 33 | from time import strftime |
| 34 | import ctypes as ct |
| 35 | |
| 36 | # arguments |
| 37 | examples = """examples: |
| 38 | ./zfsslower # trace operations slower than 10 ms (default) |
| 39 | ./zfsslower 1 # trace operations slower than 1 ms |
| 40 | ./zfsslower -j 1 # ... 1 ms, parsable output (csv) |
| 41 | ./zfsslower 0 # trace all operations (warning: verbose) |
| 42 | ./zfsslower -p 185 # trace PID 185 only |
| 43 | """ |
| 44 | parser = argparse.ArgumentParser( |
| 45 | description="Trace common ZFS file operations slower than a threshold", |
| 46 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 47 | epilog=examples) |
| 48 | parser.add_argument("-j", "--csv", action="store_true", |
| 49 | help="just print fields: comma-separated values") |
| 50 | parser.add_argument("-p", "--pid", |
| 51 | help="trace this PID only") |
| 52 | parser.add_argument("min_ms", nargs="?", default='10', |
| 53 | help="minimum I/O duration to trace, in ms (default 10)") |
| 54 | args = parser.parse_args() |
| 55 | min_ms = int(args.min_ms) |
| 56 | pid = args.pid |
| 57 | csv = args.csv |
| 58 | debug = 0 |
| 59 | |
| 60 | # define BPF program |
| 61 | bpf_text = """ |
| 62 | #include <uapi/linux/ptrace.h> |
| 63 | #include <linux/fs.h> |
| 64 | #include <linux/sched.h> |
| 65 | #include <linux/dcache.h> |
| 66 | |
| 67 | // XXX: switch these to char's when supported |
| 68 | #define TRACE_READ 0 |
| 69 | #define TRACE_WRITE 1 |
| 70 | #define TRACE_OPEN 2 |
| 71 | #define TRACE_FSYNC 3 |
| 72 | |
| 73 | struct val_t { |
| 74 | u64 ts; |
| 75 | u64 offset; |
| 76 | struct file *fp; |
| 77 | }; |
| 78 | |
| 79 | struct data_t { |
| 80 | // XXX: switch some to u32's when supported |
| 81 | u64 ts_us; |
| 82 | u64 type; |
| 83 | u64 size; |
| 84 | u64 offset; |
| 85 | u64 delta_us; |
| 86 | u64 pid; |
| 87 | char task[TASK_COMM_LEN]; |
| 88 | char file[DNAME_INLINE_LEN]; |
| 89 | }; |
| 90 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 91 | BPF_HASH(entryinfo, u64, struct val_t); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 92 | BPF_PERF_OUTPUT(events); |
| 93 | |
| 94 | // |
| 95 | // Store timestamp and size on entry |
| 96 | // |
| 97 | |
| 98 | // zpl_read(), zpl_write(): |
| 99 | int trace_rw_entry(struct pt_regs *ctx, struct file *filp, char __user *buf, |
| 100 | size_t len, loff_t *ppos) |
| 101 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 102 | u64 id = bpf_get_current_pid_tgid(); |
| 103 | u32 pid = id >> 32; // PID is higher part |
| 104 | |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 105 | if (FILTER_PID) |
| 106 | return 0; |
| 107 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 108 | // store filep and timestamp by id |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 109 | struct val_t val = {}; |
| 110 | val.ts = bpf_ktime_get_ns(); |
| 111 | val.fp = filp; |
| 112 | val.offset = *ppos; |
| 113 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 114 | entryinfo.update(&id, &val); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | // zpl_open(): |
| 120 | int trace_open_entry(struct pt_regs *ctx, struct inode *inode, |
| 121 | struct file *filp) |
| 122 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 123 | u64 id = bpf_get_current_pid_tgid(); |
| 124 | u32 pid = id >> 32; // PID is higher part |
| 125 | |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 126 | if (FILTER_PID) |
| 127 | return 0; |
| 128 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 129 | // store filep and timestamp by id |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 130 | struct val_t val = {}; |
| 131 | val.ts = bpf_ktime_get_ns(); |
| 132 | val.fp = filp; |
| 133 | val.offset = 0; |
| 134 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 135 | entryinfo.update(&id, &val); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | // zpl_fsync(): |
| 141 | int trace_fsync_entry(struct pt_regs *ctx, struct file *filp) |
| 142 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 143 | u64 id = bpf_get_current_pid_tgid(); |
| 144 | u32 pid = id >> 32; // PID is higher part |
| 145 | |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 146 | if (FILTER_PID) |
| 147 | return 0; |
| 148 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 149 | // store filp and timestamp by id |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 150 | struct val_t val = {}; |
| 151 | val.ts = bpf_ktime_get_ns(); |
| 152 | val.fp = filp; |
| 153 | val.offset = 0; |
| 154 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 155 | entryinfo.update(&id, &val); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | // |
| 161 | // Output |
| 162 | // |
| 163 | |
| 164 | static int trace_return(struct pt_regs *ctx, int type) |
| 165 | { |
| 166 | struct val_t *valp; |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 167 | u64 id = bpf_get_current_pid_tgid(); |
| 168 | u32 pid = id >> 32; // PID is higher part |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 169 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 170 | valp = entryinfo.lookup(&id); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 171 | if (valp == 0) { |
| 172 | // missed tracing issue or filtered |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | // calculate delta |
| 177 | u64 ts = bpf_ktime_get_ns(); |
| 178 | u64 delta_us = (ts - valp->ts) / 1000; |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 179 | entryinfo.delete(&id); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 180 | if (FILTER_US) |
| 181 | return 0; |
| 182 | |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 183 | // populate output struct |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 184 | u32 size = PT_REGS_RC(ctx); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 185 | struct data_t data = {.type = type, .size = size, .delta_us = delta_us, |
| 186 | .pid = pid}; |
| 187 | data.ts_us = ts / 1000; |
| 188 | data.offset = valp->offset; |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 189 | bpf_get_current_comm(&data.task, sizeof(data.task)); |
| 190 | |
Brendan Gregg | be294db | 2016-10-20 21:46:09 -0700 | [diff] [blame^] | 191 | // workaround (rewriter should handle file to d_name in one step): |
| 192 | struct dentry *de = NULL; |
| 193 | struct qstr qs = {}; |
| 194 | bpf_probe_read(&de, sizeof(de), &valp->fp->f_path.dentry); |
| 195 | bpf_probe_read(&qs, sizeof(qs), (void *)&de->d_name); |
| 196 | if (qs.len == 0) |
| 197 | return 0; |
| 198 | bpf_probe_read(&data.file, sizeof(data.file), (void *)qs.name); |
| 199 | |
| 200 | // output |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 201 | events.perf_submit(ctx, &data, sizeof(data)); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | int trace_read_return(struct pt_regs *ctx) |
| 207 | { |
| 208 | return trace_return(ctx, TRACE_READ); |
| 209 | } |
| 210 | |
| 211 | int trace_write_return(struct pt_regs *ctx) |
| 212 | { |
| 213 | return trace_return(ctx, TRACE_WRITE); |
| 214 | } |
| 215 | |
| 216 | int trace_open_return(struct pt_regs *ctx) |
| 217 | { |
| 218 | return trace_return(ctx, TRACE_OPEN); |
| 219 | } |
| 220 | |
| 221 | int trace_fsync_return(struct pt_regs *ctx) |
| 222 | { |
| 223 | return trace_return(ctx, TRACE_FSYNC); |
| 224 | } |
| 225 | |
| 226 | """ |
| 227 | if min_ms == 0: |
| 228 | bpf_text = bpf_text.replace('FILTER_US', '0') |
| 229 | else: |
| 230 | bpf_text = bpf_text.replace('FILTER_US', |
| 231 | 'delta_us <= %s' % str(min_ms * 1000)) |
| 232 | if args.pid: |
| 233 | bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid) |
| 234 | else: |
| 235 | bpf_text = bpf_text.replace('FILTER_PID', '0') |
| 236 | if debug: |
| 237 | print(bpf_text) |
| 238 | |
| 239 | # kernel->user event data: struct data_t |
| 240 | DNAME_INLINE_LEN = 32 # linux/dcache.h |
| 241 | TASK_COMM_LEN = 16 # linux/sched.h |
| 242 | class Data(ct.Structure): |
| 243 | _fields_ = [ |
| 244 | ("ts_us", ct.c_ulonglong), |
| 245 | ("type", ct.c_ulonglong), |
| 246 | ("size", ct.c_ulonglong), |
| 247 | ("offset", ct.c_ulonglong), |
| 248 | ("delta_us", ct.c_ulonglong), |
| 249 | ("pid", ct.c_ulonglong), |
| 250 | ("task", ct.c_char * TASK_COMM_LEN), |
| 251 | ("file", ct.c_char * DNAME_INLINE_LEN) |
| 252 | ] |
| 253 | |
| 254 | # process event |
| 255 | def print_event(cpu, data, size): |
| 256 | event = ct.cast(data, ct.POINTER(Data)).contents |
| 257 | |
| 258 | type = 'R' |
| 259 | if event.type == 1: |
| 260 | type = 'W' |
| 261 | elif event.type == 2: |
| 262 | type = 'O' |
| 263 | elif event.type == 3: |
| 264 | type = 'S' |
| 265 | |
| 266 | if (csv): |
| 267 | print("%d,%s,%d,%s,%d,%d,%d,%s" % ( |
| 268 | event.ts_us, event.task, event.pid, type, event.size, |
| 269 | event.offset, event.delta_us, event.file)) |
| 270 | return |
| 271 | print("%-8s %-14.14s %-6s %1s %-7s %-8d %7.2f %s" % (strftime("%H:%M:%S"), |
| 272 | event.task, event.pid, type, event.size, event.offset / 1024, |
| 273 | float(event.delta_us) / 1000, event.file)) |
| 274 | |
| 275 | # initialize BPF |
| 276 | b = BPF(text=bpf_text) |
| 277 | |
| 278 | # common file functions |
| 279 | b.attach_kprobe(event="zpl_read", fn_name="trace_rw_entry") |
| 280 | b.attach_kprobe(event="zpl_write", fn_name="trace_rw_entry") |
| 281 | b.attach_kprobe(event="zpl_open", fn_name="trace_open_entry") |
| 282 | b.attach_kprobe(event="zpl_fsync", fn_name="trace_fsync_entry") |
| 283 | b.attach_kretprobe(event="zpl_read", fn_name="trace_read_return") |
| 284 | b.attach_kretprobe(event="zpl_write", fn_name="trace_write_return") |
| 285 | b.attach_kretprobe(event="zpl_open", fn_name="trace_open_return") |
| 286 | b.attach_kretprobe(event="zpl_fsync", fn_name="trace_fsync_return") |
| 287 | |
| 288 | # header |
| 289 | if (csv): |
| 290 | print("ENDTIME_us,TASK,PID,TYPE,BYTES,OFFSET_b,LATENCY_us,FILE") |
| 291 | else: |
| 292 | if min_ms == 0: |
| 293 | print("Tracing ZFS operations") |
| 294 | else: |
| 295 | print("Tracing ZFS operations slower than %d ms" % min_ms) |
| 296 | print("%-8s %-14s %-6s %1s %-7s %-8s %7s %s" % ("TIME", "COMM", "PID", "T", |
| 297 | "BYTES", "OFF_KB", "LAT(ms)", "FILENAME")) |
| 298 | |
| 299 | # read events |
| 300 | b["events"].open_perf_buffer(print_event) |
| 301 | while 1: |
| 302 | b.kprobe_poll() |