Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # xfsslower Trace slow XFS operations. |
| 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
| 7 | # USAGE: xfsslower [-h] [-j] [-p PID] [min_ms] |
| 8 | # |
| 9 | # This script traces common XFS 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 XFS 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 | # By default, a minimum millisecond threshold of 10 is used. |
| 20 | # |
| 21 | # Copyright 2016 Netflix, Inc. |
| 22 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 23 | # |
| 24 | # 11-Feb-2016 Brendan Gregg Created this. |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 25 | # 16-Oct-2016 Dina Goldshtein -p to filter by process ID. |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 26 | |
| 27 | from __future__ import print_function |
| 28 | from bcc import BPF |
| 29 | import argparse |
| 30 | from time import strftime |
| 31 | import ctypes as ct |
| 32 | |
| 33 | # arguments |
| 34 | examples = """examples: |
| 35 | ./xfsslower # trace operations slower than 10 ms (default) |
| 36 | ./xfsslower 1 # trace operations slower than 1 ms |
| 37 | ./xfsslower -j 1 # ... 1 ms, parsable output (csv) |
| 38 | ./xfsslower 0 # trace all operations (warning: verbose) |
| 39 | ./xfsslower -p 185 # trace PID 185 only |
| 40 | """ |
| 41 | parser = argparse.ArgumentParser( |
| 42 | description="Trace common XFS file operations slower than a threshold", |
| 43 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 44 | epilog=examples) |
| 45 | parser.add_argument("-j", "--csv", action="store_true", |
| 46 | help="just print fields: comma-separated values") |
| 47 | parser.add_argument("-p", "--pid", |
| 48 | help="trace this PID only") |
| 49 | parser.add_argument("min_ms", nargs="?", default='10', |
| 50 | help="minimum I/O duration to trace, in ms (default 10)") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 51 | parser.add_argument("--ebpf", action="store_true", |
| 52 | help=argparse.SUPPRESS) |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 53 | args = parser.parse_args() |
| 54 | min_ms = int(args.min_ms) |
| 55 | pid = args.pid |
| 56 | csv = args.csv |
| 57 | debug = 0 |
| 58 | |
| 59 | # define BPF program |
| 60 | bpf_text = """ |
| 61 | #include <uapi/linux/ptrace.h> |
| 62 | #include <linux/fs.h> |
| 63 | #include <linux/sched.h> |
| 64 | #include <linux/dcache.h> |
| 65 | |
| 66 | // XXX: switch these to char's when supported |
| 67 | #define TRACE_READ 0 |
| 68 | #define TRACE_WRITE 1 |
| 69 | #define TRACE_OPEN 2 |
| 70 | #define TRACE_FSYNC 3 |
| 71 | |
| 72 | struct val_t { |
| 73 | u64 ts; |
| 74 | u64 offset; |
| 75 | struct file *fp; |
| 76 | }; |
| 77 | |
| 78 | struct data_t { |
| 79 | // XXX: switch some to u32's when supported |
| 80 | u64 ts_us; |
| 81 | u64 type; |
| 82 | u64 size; |
| 83 | u64 offset; |
| 84 | u64 delta_us; |
| 85 | u64 pid; |
| 86 | char task[TASK_COMM_LEN]; |
| 87 | char file[DNAME_INLINE_LEN]; |
| 88 | }; |
| 89 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 90 | BPF_HASH(entryinfo, u64, struct val_t); |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 91 | BPF_PERF_OUTPUT(events); |
| 92 | |
| 93 | // |
| 94 | // Store timestamp and size on entry |
| 95 | // |
| 96 | |
| 97 | // xfs_file_read_iter(), xfs_file_write_iter(): |
| 98 | int trace_rw_entry(struct pt_regs *ctx, struct kiocb *iocb) |
| 99 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 100 | u64 id = bpf_get_current_pid_tgid(); |
| 101 | u32 pid = id >> 32; // PID is higher part |
| 102 | |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 103 | if (FILTER_PID) |
| 104 | return 0; |
| 105 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 106 | // store filep and timestamp by id |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 107 | struct val_t val = {}; |
| 108 | val.ts = bpf_ktime_get_ns(); |
| 109 | val.fp = iocb->ki_filp; |
| 110 | val.offset = iocb->ki_pos; |
| 111 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 112 | entryinfo.update(&id, &val); |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | // xfs_file_open(): |
| 118 | int trace_open_entry(struct pt_regs *ctx, struct inode *inode, |
| 119 | struct file *file) |
| 120 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 121 | u64 id = bpf_get_current_pid_tgid(); |
| 122 | u32 pid = id >> 32; // PID is higher part |
| 123 | |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 124 | if (FILTER_PID) |
| 125 | return 0; |
| 126 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 127 | // store filep and timestamp by id |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 128 | struct val_t val = {}; |
| 129 | val.ts = bpf_ktime_get_ns(); |
| 130 | val.fp = file; |
| 131 | val.offset = 0; |
| 132 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 133 | entryinfo.update(&id, &val); |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | // xfs_file_fsync(): |
| 139 | int trace_fsync_entry(struct pt_regs *ctx, struct file *file) |
| 140 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 141 | u64 id = bpf_get_current_pid_tgid(); |
| 142 | u32 pid = id >> 32; // PID is higher part |
| 143 | |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 144 | if (FILTER_PID) |
| 145 | return 0; |
| 146 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 147 | // store filep and timestamp by id |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 148 | struct val_t val = {}; |
| 149 | val.ts = bpf_ktime_get_ns(); |
| 150 | val.fp = file; |
| 151 | val.offset = 0; |
| 152 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 153 | entryinfo.update(&id, &val); |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | // |
| 159 | // Output |
| 160 | // |
| 161 | |
| 162 | static int trace_return(struct pt_regs *ctx, int type) |
| 163 | { |
| 164 | struct val_t *valp; |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 165 | u64 id = bpf_get_current_pid_tgid(); |
| 166 | u32 pid = id >> 32; // PID is higher part |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 167 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 168 | valp = entryinfo.lookup(&id); |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 169 | if (valp == 0) { |
| 170 | // missed tracing issue or filtered |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | // calculate delta |
| 175 | u64 ts = bpf_ktime_get_ns(); |
| 176 | u64 delta_us = (ts - valp->ts) / 1000; |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 177 | entryinfo.delete(&id); |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 178 | if (FILTER_US) |
| 179 | return 0; |
| 180 | |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 181 | // populate output struct |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 182 | u32 size = PT_REGS_RC(ctx); |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 183 | struct data_t data = {.type = type, .size = size, .delta_us = delta_us, |
| 184 | .pid = pid}; |
| 185 | data.ts_us = ts / 1000; |
| 186 | data.offset = valp->offset; |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 187 | bpf_get_current_comm(&data.task, sizeof(data.task)); |
| 188 | |
Brendan Gregg | be294db | 2016-10-20 21:46:09 -0700 | [diff] [blame] | 189 | // workaround (rewriter should handle file to d_name in one step): |
| 190 | struct dentry *de = NULL; |
| 191 | struct qstr qs = {}; |
| 192 | bpf_probe_read(&de, sizeof(de), &valp->fp->f_path.dentry); |
| 193 | bpf_probe_read(&qs, sizeof(qs), (void *)&de->d_name); |
| 194 | if (qs.len == 0) |
| 195 | return 0; |
| 196 | bpf_probe_read(&data.file, sizeof(data.file), (void *)qs.name); |
| 197 | |
| 198 | // output |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 199 | events.perf_submit(ctx, &data, sizeof(data)); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | int trace_read_return(struct pt_regs *ctx) |
| 205 | { |
| 206 | return trace_return(ctx, TRACE_READ); |
| 207 | } |
| 208 | |
| 209 | int trace_write_return(struct pt_regs *ctx) |
| 210 | { |
| 211 | return trace_return(ctx, TRACE_WRITE); |
| 212 | } |
| 213 | |
| 214 | int trace_open_return(struct pt_regs *ctx) |
| 215 | { |
| 216 | return trace_return(ctx, TRACE_OPEN); |
| 217 | } |
| 218 | |
| 219 | int trace_fsync_return(struct pt_regs *ctx) |
| 220 | { |
| 221 | return trace_return(ctx, TRACE_FSYNC); |
| 222 | } |
| 223 | |
| 224 | """ |
| 225 | if min_ms == 0: |
| 226 | bpf_text = bpf_text.replace('FILTER_US', '0') |
| 227 | else: |
| 228 | bpf_text = bpf_text.replace('FILTER_US', |
| 229 | 'delta_us <= %s' % str(min_ms * 1000)) |
| 230 | if args.pid: |
| 231 | bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid) |
| 232 | else: |
| 233 | bpf_text = bpf_text.replace('FILTER_PID', '0') |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 234 | if debug or args.ebpf: |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 235 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 236 | if args.ebpf: |
| 237 | exit() |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 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="xfs_file_read_iter", fn_name="trace_rw_entry") |
| 280 | b.attach_kprobe(event="xfs_file_write_iter", fn_name="trace_rw_entry") |
| 281 | b.attach_kprobe(event="xfs_file_open", fn_name="trace_open_entry") |
| 282 | b.attach_kprobe(event="xfs_file_fsync", fn_name="trace_fsync_entry") |
| 283 | b.attach_kretprobe(event="xfs_file_read_iter", fn_name="trace_read_return") |
| 284 | b.attach_kretprobe(event="xfs_file_write_iter", fn_name="trace_write_return") |
| 285 | b.attach_kretprobe(event="xfs_file_open", fn_name="trace_open_return") |
| 286 | b.attach_kretprobe(event="xfs_file_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 XFS operations") |
| 294 | else: |
| 295 | print("Tracing XFS 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 |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 300 | b["events"].open_perf_buffer(print_event, page_cnt=64) |
Brendan Gregg | c937a9e | 2016-02-12 02:23:39 -0800 | [diff] [blame] | 301 | while 1: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 302 | b.perf_buffer_poll() |