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 | |
| 179 | // workaround (rewriter should handle file to d_iname in one step): |
| 180 | struct dentry *de = NULL; |
| 181 | bpf_probe_read(&de, sizeof(de), &valp->fp->f_path.dentry); |
| 182 | |
| 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 | c937a9e | 2016-02-12 02:23:39 -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; |
| 189 | bpf_probe_read(&data.file, sizeof(data.file), de->d_iname); |
| 190 | bpf_get_current_comm(&data.task, sizeof(data.task)); |
| 191 | |
| 192 | events.perf_submit(ctx, &data, sizeof(data)); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | int trace_read_return(struct pt_regs *ctx) |
| 198 | { |
| 199 | return trace_return(ctx, TRACE_READ); |
| 200 | } |
| 201 | |
| 202 | int trace_write_return(struct pt_regs *ctx) |
| 203 | { |
| 204 | return trace_return(ctx, TRACE_WRITE); |
| 205 | } |
| 206 | |
| 207 | int trace_open_return(struct pt_regs *ctx) |
| 208 | { |
| 209 | return trace_return(ctx, TRACE_OPEN); |
| 210 | } |
| 211 | |
| 212 | int trace_fsync_return(struct pt_regs *ctx) |
| 213 | { |
| 214 | return trace_return(ctx, TRACE_FSYNC); |
| 215 | } |
| 216 | |
| 217 | """ |
| 218 | if min_ms == 0: |
| 219 | bpf_text = bpf_text.replace('FILTER_US', '0') |
| 220 | else: |
| 221 | bpf_text = bpf_text.replace('FILTER_US', |
| 222 | 'delta_us <= %s' % str(min_ms * 1000)) |
| 223 | if args.pid: |
| 224 | bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid) |
| 225 | else: |
| 226 | bpf_text = bpf_text.replace('FILTER_PID', '0') |
| 227 | if debug: |
| 228 | print(bpf_text) |
| 229 | |
| 230 | # kernel->user event data: struct data_t |
| 231 | DNAME_INLINE_LEN = 32 # linux/dcache.h |
| 232 | TASK_COMM_LEN = 16 # linux/sched.h |
| 233 | class Data(ct.Structure): |
| 234 | _fields_ = [ |
| 235 | ("ts_us", ct.c_ulonglong), |
| 236 | ("type", ct.c_ulonglong), |
| 237 | ("size", ct.c_ulonglong), |
| 238 | ("offset", ct.c_ulonglong), |
| 239 | ("delta_us", ct.c_ulonglong), |
| 240 | ("pid", ct.c_ulonglong), |
| 241 | ("task", ct.c_char * TASK_COMM_LEN), |
| 242 | ("file", ct.c_char * DNAME_INLINE_LEN) |
| 243 | ] |
| 244 | |
| 245 | # process event |
| 246 | def print_event(cpu, data, size): |
| 247 | event = ct.cast(data, ct.POINTER(Data)).contents |
| 248 | |
| 249 | type = 'R' |
| 250 | if event.type == 1: |
| 251 | type = 'W' |
| 252 | elif event.type == 2: |
| 253 | type = 'O' |
| 254 | elif event.type == 3: |
| 255 | type = 'S' |
| 256 | |
| 257 | if (csv): |
| 258 | print("%d,%s,%d,%s,%d,%d,%d,%s" % ( |
| 259 | event.ts_us, event.task, event.pid, type, event.size, |
| 260 | event.offset, event.delta_us, event.file)) |
| 261 | return |
| 262 | print("%-8s %-14.14s %-6s %1s %-7s %-8d %7.2f %s" % (strftime("%H:%M:%S"), |
| 263 | event.task, event.pid, type, event.size, event.offset / 1024, |
| 264 | float(event.delta_us) / 1000, event.file)) |
| 265 | |
| 266 | # initialize BPF |
| 267 | b = BPF(text=bpf_text) |
| 268 | |
| 269 | # common file functions |
| 270 | b.attach_kprobe(event="xfs_file_read_iter", fn_name="trace_rw_entry") |
| 271 | b.attach_kprobe(event="xfs_file_write_iter", fn_name="trace_rw_entry") |
| 272 | b.attach_kprobe(event="xfs_file_open", fn_name="trace_open_entry") |
| 273 | b.attach_kprobe(event="xfs_file_fsync", fn_name="trace_fsync_entry") |
| 274 | b.attach_kretprobe(event="xfs_file_read_iter", fn_name="trace_read_return") |
| 275 | b.attach_kretprobe(event="xfs_file_write_iter", fn_name="trace_write_return") |
| 276 | b.attach_kretprobe(event="xfs_file_open", fn_name="trace_open_return") |
| 277 | b.attach_kretprobe(event="xfs_file_fsync", fn_name="trace_fsync_return") |
| 278 | |
| 279 | # header |
| 280 | if (csv): |
| 281 | print("ENDTIME_us,TASK,PID,TYPE,BYTES,OFFSET_b,LATENCY_us,FILE") |
| 282 | else: |
| 283 | if min_ms == 0: |
| 284 | print("Tracing XFS operations") |
| 285 | else: |
| 286 | print("Tracing XFS operations slower than %d ms" % min_ms) |
| 287 | print("%-8s %-14s %-6s %1s %-7s %-8s %7s %s" % ("TIME", "COMM", "PID", "T", |
| 288 | "BYTES", "OFF_KB", "LAT(ms)", "FILENAME")) |
| 289 | |
| 290 | # read events |
| 291 | b["events"].open_perf_buffer(print_event) |
| 292 | while 1: |
| 293 | b.kprobe_poll() |