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)") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 54 | parser.add_argument("--ebpf", action="store_true", |
| 55 | help=argparse.SUPPRESS) |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 56 | args = parser.parse_args() |
| 57 | min_ms = int(args.min_ms) |
| 58 | pid = args.pid |
| 59 | csv = args.csv |
| 60 | debug = 0 |
| 61 | |
| 62 | # define BPF program |
| 63 | bpf_text = """ |
| 64 | #include <uapi/linux/ptrace.h> |
| 65 | #include <linux/fs.h> |
| 66 | #include <linux/sched.h> |
| 67 | #include <linux/dcache.h> |
| 68 | |
| 69 | // XXX: switch these to char's when supported |
| 70 | #define TRACE_READ 0 |
| 71 | #define TRACE_WRITE 1 |
| 72 | #define TRACE_OPEN 2 |
| 73 | #define TRACE_FSYNC 3 |
| 74 | |
| 75 | struct val_t { |
| 76 | u64 ts; |
| 77 | u64 offset; |
| 78 | struct file *fp; |
| 79 | }; |
| 80 | |
| 81 | struct data_t { |
| 82 | // XXX: switch some to u32's when supported |
| 83 | u64 ts_us; |
| 84 | u64 type; |
| 85 | u64 size; |
| 86 | u64 offset; |
| 87 | u64 delta_us; |
| 88 | u64 pid; |
| 89 | char task[TASK_COMM_LEN]; |
| 90 | char file[DNAME_INLINE_LEN]; |
| 91 | }; |
| 92 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 93 | BPF_HASH(entryinfo, u64, struct val_t); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 94 | BPF_PERF_OUTPUT(events); |
| 95 | |
| 96 | // |
| 97 | // Store timestamp and size on entry |
| 98 | // |
| 99 | |
| 100 | // zpl_read(), zpl_write(): |
| 101 | int trace_rw_entry(struct pt_regs *ctx, struct file *filp, char __user *buf, |
| 102 | size_t len, loff_t *ppos) |
| 103 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 104 | u64 id = bpf_get_current_pid_tgid(); |
| 105 | u32 pid = id >> 32; // PID is higher part |
| 106 | |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 107 | if (FILTER_PID) |
| 108 | return 0; |
| 109 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 110 | // store filep and timestamp by id |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 111 | struct val_t val = {}; |
| 112 | val.ts = bpf_ktime_get_ns(); |
| 113 | val.fp = filp; |
| 114 | val.offset = *ppos; |
| 115 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 116 | entryinfo.update(&id, &val); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | // zpl_open(): |
| 122 | int trace_open_entry(struct pt_regs *ctx, struct inode *inode, |
| 123 | struct file *filp) |
| 124 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 125 | u64 id = bpf_get_current_pid_tgid(); |
| 126 | u32 pid = id >> 32; // PID is higher part |
| 127 | |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 128 | if (FILTER_PID) |
| 129 | return 0; |
| 130 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 131 | // store filep and timestamp by id |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 132 | struct val_t val = {}; |
| 133 | val.ts = bpf_ktime_get_ns(); |
| 134 | val.fp = filp; |
| 135 | val.offset = 0; |
| 136 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 137 | entryinfo.update(&id, &val); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | // zpl_fsync(): |
| 143 | int trace_fsync_entry(struct pt_regs *ctx, struct file *filp) |
| 144 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 145 | u64 id = bpf_get_current_pid_tgid(); |
| 146 | u32 pid = id >> 32; // PID is higher part |
| 147 | |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 148 | if (FILTER_PID) |
| 149 | return 0; |
| 150 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 151 | // store filp and timestamp by id |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 152 | struct val_t val = {}; |
| 153 | val.ts = bpf_ktime_get_ns(); |
| 154 | val.fp = filp; |
| 155 | val.offset = 0; |
| 156 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 157 | entryinfo.update(&id, &val); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | // |
| 163 | // Output |
| 164 | // |
| 165 | |
| 166 | static int trace_return(struct pt_regs *ctx, int type) |
| 167 | { |
| 168 | struct val_t *valp; |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 169 | u64 id = bpf_get_current_pid_tgid(); |
| 170 | u32 pid = id >> 32; // PID is higher part |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 171 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 172 | valp = entryinfo.lookup(&id); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 173 | if (valp == 0) { |
| 174 | // missed tracing issue or filtered |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | // calculate delta |
| 179 | u64 ts = bpf_ktime_get_ns(); |
| 180 | u64 delta_us = (ts - valp->ts) / 1000; |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 181 | entryinfo.delete(&id); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 182 | if (FILTER_US) |
| 183 | return 0; |
| 184 | |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 185 | // populate output struct |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 186 | u32 size = PT_REGS_RC(ctx); |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 187 | struct data_t data = {.type = type, .size = size, .delta_us = delta_us, |
| 188 | .pid = pid}; |
| 189 | data.ts_us = ts / 1000; |
| 190 | data.offset = valp->offset; |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 191 | bpf_get_current_comm(&data.task, sizeof(data.task)); |
| 192 | |
Brendan Gregg | be294db | 2016-10-20 21:46:09 -0700 | [diff] [blame] | 193 | // workaround (rewriter should handle file to d_name in one step): |
| 194 | struct dentry *de = NULL; |
| 195 | struct qstr qs = {}; |
| 196 | bpf_probe_read(&de, sizeof(de), &valp->fp->f_path.dentry); |
| 197 | bpf_probe_read(&qs, sizeof(qs), (void *)&de->d_name); |
| 198 | if (qs.len == 0) |
| 199 | return 0; |
| 200 | bpf_probe_read(&data.file, sizeof(data.file), (void *)qs.name); |
| 201 | |
| 202 | // output |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 203 | events.perf_submit(ctx, &data, sizeof(data)); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | int trace_read_return(struct pt_regs *ctx) |
| 209 | { |
| 210 | return trace_return(ctx, TRACE_READ); |
| 211 | } |
| 212 | |
| 213 | int trace_write_return(struct pt_regs *ctx) |
| 214 | { |
| 215 | return trace_return(ctx, TRACE_WRITE); |
| 216 | } |
| 217 | |
| 218 | int trace_open_return(struct pt_regs *ctx) |
| 219 | { |
| 220 | return trace_return(ctx, TRACE_OPEN); |
| 221 | } |
| 222 | |
| 223 | int trace_fsync_return(struct pt_regs *ctx) |
| 224 | { |
| 225 | return trace_return(ctx, TRACE_FSYNC); |
| 226 | } |
| 227 | |
| 228 | """ |
| 229 | if min_ms == 0: |
| 230 | bpf_text = bpf_text.replace('FILTER_US', '0') |
| 231 | else: |
| 232 | bpf_text = bpf_text.replace('FILTER_US', |
| 233 | 'delta_us <= %s' % str(min_ms * 1000)) |
| 234 | if args.pid: |
| 235 | bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid) |
| 236 | else: |
| 237 | bpf_text = bpf_text.replace('FILTER_PID', '0') |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 238 | if debug or args.ebpf: |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 239 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 240 | if args.ebpf: |
| 241 | exit() |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 242 | |
| 243 | # kernel->user event data: struct data_t |
| 244 | DNAME_INLINE_LEN = 32 # linux/dcache.h |
| 245 | TASK_COMM_LEN = 16 # linux/sched.h |
| 246 | class Data(ct.Structure): |
| 247 | _fields_ = [ |
| 248 | ("ts_us", ct.c_ulonglong), |
| 249 | ("type", ct.c_ulonglong), |
| 250 | ("size", ct.c_ulonglong), |
| 251 | ("offset", ct.c_ulonglong), |
| 252 | ("delta_us", ct.c_ulonglong), |
| 253 | ("pid", ct.c_ulonglong), |
| 254 | ("task", ct.c_char * TASK_COMM_LEN), |
| 255 | ("file", ct.c_char * DNAME_INLINE_LEN) |
| 256 | ] |
| 257 | |
| 258 | # process event |
| 259 | def print_event(cpu, data, size): |
| 260 | event = ct.cast(data, ct.POINTER(Data)).contents |
| 261 | |
| 262 | type = 'R' |
| 263 | if event.type == 1: |
| 264 | type = 'W' |
| 265 | elif event.type == 2: |
| 266 | type = 'O' |
| 267 | elif event.type == 3: |
| 268 | type = 'S' |
| 269 | |
| 270 | if (csv): |
| 271 | print("%d,%s,%d,%s,%d,%d,%d,%s" % ( |
| 272 | event.ts_us, event.task, event.pid, type, event.size, |
| 273 | event.offset, event.delta_us, event.file)) |
| 274 | return |
| 275 | print("%-8s %-14.14s %-6s %1s %-7s %-8d %7.2f %s" % (strftime("%H:%M:%S"), |
| 276 | event.task, event.pid, type, event.size, event.offset / 1024, |
| 277 | float(event.delta_us) / 1000, event.file)) |
| 278 | |
| 279 | # initialize BPF |
| 280 | b = BPF(text=bpf_text) |
| 281 | |
| 282 | # common file functions |
Marcin Skarbek | 4f444a5 | 2017-08-28 17:39:50 +0200 | [diff] [blame] | 283 | if BPF.get_kprobe_functions('zpl_iter'): |
| 284 | b.attach_kprobe(event="zpl_iter_read", fn_name="trace_rw_entry") |
| 285 | b.attach_kprobe(event="zpl_iter_write", fn_name="trace_rw_entry") |
| 286 | elif BPF.get_kprobe_functions('zpl_aio'): |
| 287 | b.attach_kprobe(event="zpl_aio_read", fn_name="trace_rw_entry") |
| 288 | b.attach_kprobe(event="zpl_aio_write", fn_name="trace_rw_entry") |
| 289 | else: |
| 290 | b.attach_kprobe(event="zpl_read", fn_name="trace_rw_entry") |
| 291 | b.attach_kprobe(event="zpl_write", fn_name="trace_rw_entry") |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 292 | b.attach_kprobe(event="zpl_open", fn_name="trace_open_entry") |
| 293 | b.attach_kprobe(event="zpl_fsync", fn_name="trace_fsync_entry") |
Marcin Skarbek | 4f444a5 | 2017-08-28 17:39:50 +0200 | [diff] [blame] | 294 | if BPF.get_kprobe_functions('zpl_iter'): |
| 295 | b.attach_kretprobe(event="zpl_iter_read", fn_name="trace_read_return") |
| 296 | b.attach_kretprobe(event="zpl_iter_write", fn_name="trace_write_return") |
| 297 | elif BPF.get_kprobe_functions('zpl_aio'): |
| 298 | b.attach_kretprobe(event="zpl_aio_read", fn_name="trace_read_return") |
| 299 | b.attach_kretprobe(event="zpl_aio_write", fn_name="trace_write_return") |
| 300 | else: |
| 301 | b.attach_kretprobe(event="zpl_read", fn_name="trace_read_return") |
| 302 | b.attach_kretprobe(event="zpl_write", fn_name="trace_write_return") |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 303 | b.attach_kretprobe(event="zpl_open", fn_name="trace_open_return") |
| 304 | b.attach_kretprobe(event="zpl_fsync", fn_name="trace_fsync_return") |
| 305 | |
| 306 | # header |
| 307 | if (csv): |
| 308 | print("ENDTIME_us,TASK,PID,TYPE,BYTES,OFFSET_b,LATENCY_us,FILE") |
| 309 | else: |
| 310 | if min_ms == 0: |
| 311 | print("Tracing ZFS operations") |
| 312 | else: |
| 313 | print("Tracing ZFS operations slower than %d ms" % min_ms) |
| 314 | print("%-8s %-14s %-6s %1s %-7s %-8s %7s %s" % ("TIME", "COMM", "PID", "T", |
| 315 | "BYTES", "OFF_KB", "LAT(ms)", "FILENAME")) |
| 316 | |
| 317 | # read events |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 318 | b["events"].open_perf_buffer(print_event, page_cnt=64) |
Brendan Gregg | bc54bb6 | 2016-02-14 23:13:13 -0800 | [diff] [blame] | 319 | while 1: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 320 | b.perf_buffer_poll() |