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