Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # btrfsslower Trace slow btrfs operations. |
| 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
| 7 | # USAGE: btrfsslower [-h] [-j] [-p PID] [min_ms] |
| 8 | # |
| 9 | # This script traces common btrfs 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 btrfs 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 | # 15-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 | ee74c37 | 2016-02-15 22:22:19 -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 | # symbols |
| 34 | kallsyms = "/proc/kallsyms" |
| 35 | |
| 36 | # arguments |
| 37 | examples = """examples: |
| 38 | ./btrfsslower # trace operations slower than 10 ms (default) |
| 39 | ./btrfsslower 1 # trace operations slower than 1 ms |
| 40 | ./btrfsslower -j 1 # ... 1 ms, parsable output (csv) |
| 41 | ./btrfsslower 0 # trace all operations (warning: verbose) |
| 42 | ./btrfsslower -p 185 # trace PID 185 only |
| 43 | """ |
| 44 | parser = argparse.ArgumentParser( |
| 45 | description="Trace common btrfs 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 | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 92 | BPF_PERF_OUTPUT(events); |
| 93 | |
| 94 | // |
| 95 | // Store timestamp and size on entry |
| 96 | // |
| 97 | |
| 98 | // The current btrfs (Linux 4.5) uses generic_file_read_iter() instead of it's |
| 99 | // own read function. So we need to trace that and then filter on btrfs, which |
| 100 | // I do by checking file->f_op. |
| 101 | int trace_read_entry(struct pt_regs *ctx, struct kiocb *iocb) |
| 102 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 103 | u64 id = bpf_get_current_pid_tgid(); |
| 104 | u32 pid = id >> 32; // PID is higher part |
| 105 | |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 106 | if (FILTER_PID) |
| 107 | return 0; |
| 108 | |
| 109 | // btrfs filter on file->f_op == btrfs_file_operations |
| 110 | struct file *fp = iocb->ki_filp; |
| 111 | if ((u64)fp->f_op != BTRFS_FILE_OPERATIONS) |
| 112 | return 0; |
| 113 | |
| 114 | // store filep and timestamp by pid |
| 115 | struct val_t val = {}; |
| 116 | val.ts = bpf_ktime_get_ns(); |
| 117 | val.fp = fp; |
| 118 | val.offset = iocb->ki_pos; |
| 119 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 120 | entryinfo.update(&id, &val); |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | // btrfs_file_write_iter(): |
| 126 | int trace_write_entry(struct pt_regs *ctx, struct kiocb *iocb) |
| 127 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 128 | u64 id = bpf_get_current_pid_tgid(); |
| 129 | u32 pid = id >> 32; // PID is higher part |
| 130 | |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 131 | if (FILTER_PID) |
| 132 | return 0; |
| 133 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 134 | // store filep and timestamp by id |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 135 | struct val_t val = {}; |
| 136 | val.ts = bpf_ktime_get_ns(); |
| 137 | val.fp = iocb->ki_filp; |
| 138 | val.offset = iocb->ki_pos; |
| 139 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 140 | entryinfo.update(&id, &val); |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | // The current btrfs (Linux 4.5) uses generic_file_open(), instead of it's own |
| 146 | // function. Same as with reads. Trace the generic path and filter: |
| 147 | int trace_open_entry(struct pt_regs *ctx, struct inode *inode, |
| 148 | struct file *file) |
| 149 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 150 | u64 id = bpf_get_current_pid_tgid(); |
| 151 | u32 pid = id >> 32; // PID is higher part |
| 152 | |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 153 | if (FILTER_PID) |
| 154 | return 0; |
| 155 | |
| 156 | // btrfs filter on file->f_op == btrfs_file_operations |
| 157 | if ((u64)file->f_op != BTRFS_FILE_OPERATIONS) |
| 158 | return 0; |
| 159 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 160 | // store filep and timestamp by id |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 161 | struct val_t val = {}; |
| 162 | val.ts = bpf_ktime_get_ns(); |
| 163 | val.fp = file; |
| 164 | val.offset = 0; |
| 165 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 166 | entryinfo.update(&id, &val); |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | // btrfs_sync_file(): |
| 172 | int trace_fsync_entry(struct pt_regs *ctx, struct file *file) |
| 173 | { |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 174 | u64 id = bpf_get_current_pid_tgid(); |
| 175 | u32 pid = id >> 32; // PID is higher part |
| 176 | |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 177 | if (FILTER_PID) |
| 178 | return 0; |
| 179 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 180 | // store filep and timestamp by id |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 181 | struct val_t val = {}; |
| 182 | val.ts = bpf_ktime_get_ns(); |
| 183 | val.fp = file; |
| 184 | val.offset = 0; |
| 185 | if (val.fp) |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 186 | entryinfo.update(&id, &val); |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | // |
| 192 | // Output |
| 193 | // |
| 194 | |
| 195 | static int trace_return(struct pt_regs *ctx, int type) |
| 196 | { |
| 197 | struct val_t *valp; |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 198 | u64 id = bpf_get_current_pid_tgid(); |
| 199 | u32 pid = id >> 32; // PID is higher part |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 200 | |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 201 | valp = entryinfo.lookup(&id); |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 202 | if (valp == 0) { |
| 203 | // missed tracing issue or filtered |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | // calculate delta |
| 208 | u64 ts = bpf_ktime_get_ns(); |
| 209 | u64 delta_us = (ts - valp->ts) / 1000; |
Dina Goldshtein | c8b9ae3 | 2016-10-18 03:01:05 +0300 | [diff] [blame] | 210 | entryinfo.delete(&id); |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 211 | if (FILTER_US) |
| 212 | return 0; |
| 213 | |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 214 | // populate output struct |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 215 | u32 size = PT_REGS_RC(ctx); |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 216 | struct data_t data = {.type = type, .size = size, .delta_us = delta_us, |
| 217 | .pid = pid}; |
| 218 | data.ts_us = ts / 1000; |
| 219 | data.offset = valp->offset; |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 220 | bpf_get_current_comm(&data.task, sizeof(data.task)); |
| 221 | |
Brendan Gregg | be294db | 2016-10-20 21:46:09 -0700 | [diff] [blame] | 222 | // workaround (rewriter should handle file to d_name in one step): |
| 223 | struct dentry *de = NULL; |
| 224 | struct qstr qs = {}; |
| 225 | bpf_probe_read(&de, sizeof(de), &valp->fp->f_path.dentry); |
| 226 | bpf_probe_read(&qs, sizeof(qs), (void *)&de->d_name); |
| 227 | if (qs.len == 0) |
| 228 | return 0; |
| 229 | bpf_probe_read(&data.file, sizeof(data.file), (void *)qs.name); |
| 230 | |
| 231 | // output |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 232 | events.perf_submit(ctx, &data, sizeof(data)); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | int trace_read_return(struct pt_regs *ctx) |
| 238 | { |
| 239 | return trace_return(ctx, TRACE_READ); |
| 240 | } |
| 241 | |
| 242 | int trace_write_return(struct pt_regs *ctx) |
| 243 | { |
| 244 | return trace_return(ctx, TRACE_WRITE); |
| 245 | } |
| 246 | |
| 247 | int trace_open_return(struct pt_regs *ctx) |
| 248 | { |
| 249 | return trace_return(ctx, TRACE_OPEN); |
| 250 | } |
| 251 | |
| 252 | int trace_fsync_return(struct pt_regs *ctx) |
| 253 | { |
| 254 | return trace_return(ctx, TRACE_FSYNC); |
| 255 | } |
| 256 | |
| 257 | """ |
| 258 | |
| 259 | # code replacements |
| 260 | with open(kallsyms) as syms: |
| 261 | ops = '' |
| 262 | for line in syms: |
| 263 | a = line.rstrip().split() |
| 264 | (addr, name) = (a[0], a[2]) |
ygrek | a5e2ce5 | 2016-06-27 12:54:55 -0700 | [diff] [blame] | 265 | name = name.split("\t")[0] |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 266 | if name == "btrfs_file_operations": |
| 267 | ops = "0x" + addr |
| 268 | break |
| 269 | if ops == '': |
| 270 | print("ERROR: no btrfs_file_operations in /proc/kallsyms. Exiting.") |
| 271 | exit() |
| 272 | bpf_text = bpf_text.replace('BTRFS_FILE_OPERATIONS', ops) |
| 273 | if min_ms == 0: |
| 274 | bpf_text = bpf_text.replace('FILTER_US', '0') |
| 275 | else: |
| 276 | bpf_text = bpf_text.replace('FILTER_US', |
| 277 | 'delta_us <= %s' % str(min_ms * 1000)) |
| 278 | if args.pid: |
| 279 | bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid) |
| 280 | else: |
| 281 | bpf_text = bpf_text.replace('FILTER_PID', '0') |
| 282 | if debug: |
| 283 | print(bpf_text) |
| 284 | |
| 285 | # kernel->user event data: struct data_t |
| 286 | DNAME_INLINE_LEN = 32 # linux/dcache.h |
| 287 | TASK_COMM_LEN = 16 # linux/sched.h |
| 288 | class Data(ct.Structure): |
| 289 | _fields_ = [ |
| 290 | ("ts_us", ct.c_ulonglong), |
| 291 | ("type", ct.c_ulonglong), |
| 292 | ("size", ct.c_ulonglong), |
| 293 | ("offset", ct.c_ulonglong), |
| 294 | ("delta_us", ct.c_ulonglong), |
| 295 | ("pid", ct.c_ulonglong), |
| 296 | ("task", ct.c_char * TASK_COMM_LEN), |
| 297 | ("file", ct.c_char * DNAME_INLINE_LEN) |
| 298 | ] |
| 299 | |
| 300 | # process event |
| 301 | def print_event(cpu, data, size): |
| 302 | event = ct.cast(data, ct.POINTER(Data)).contents |
| 303 | |
| 304 | type = 'R' |
| 305 | if event.type == 1: |
| 306 | type = 'W' |
| 307 | elif event.type == 2: |
| 308 | type = 'O' |
| 309 | elif event.type == 3: |
| 310 | type = 'S' |
| 311 | |
| 312 | if (csv): |
| 313 | print("%d,%s,%d,%s,%d,%d,%d,%s" % ( |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 314 | event.ts_us, event.task.decode(), event.pid, type, event.size, |
| 315 | event.offset, event.delta_us, event.file.decode())) |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 316 | return |
| 317 | print("%-8s %-14.14s %-6s %1s %-7s %-8d %7.2f %s" % (strftime("%H:%M:%S"), |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 318 | event.task.decode(), event.pid, type, event.size, event.offset / 1024, |
| 319 | float(event.delta_us) / 1000, event.file.decode())) |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 320 | |
| 321 | # initialize BPF |
| 322 | b = BPF(text=bpf_text) |
| 323 | |
| 324 | # Common file functions. See earlier comment about generic_*(). |
| 325 | b.attach_kprobe(event="generic_file_read_iter", fn_name="trace_read_entry") |
| 326 | b.attach_kprobe(event="btrfs_file_write_iter", fn_name="trace_write_entry") |
| 327 | b.attach_kprobe(event="generic_file_open", fn_name="trace_open_entry") |
| 328 | b.attach_kprobe(event="btrfs_sync_file", fn_name="trace_fsync_entry") |
| 329 | b.attach_kretprobe(event="generic_file_read_iter", fn_name="trace_read_return") |
| 330 | b.attach_kretprobe(event="btrfs_file_write_iter", fn_name="trace_write_return") |
| 331 | b.attach_kretprobe(event="generic_file_open", fn_name="trace_open_return") |
| 332 | b.attach_kretprobe(event="btrfs_sync_file", fn_name="trace_fsync_return") |
| 333 | |
| 334 | # header |
| 335 | if (csv): |
| 336 | print("ENDTIME_us,TASK,PID,TYPE,BYTES,OFFSET_b,LATENCY_us,FILE") |
| 337 | else: |
| 338 | if min_ms == 0: |
| 339 | print("Tracing btrfs operations") |
| 340 | else: |
| 341 | print("Tracing btrfs operations slower than %d ms" % min_ms) |
| 342 | print("%-8s %-14s %-6s %1s %-7s %-8s %7s %s" % ("TIME", "COMM", "PID", "T", |
| 343 | "BYTES", "OFF_KB", "LAT(ms)", "FILENAME")) |
| 344 | |
| 345 | # read events |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 346 | b["events"].open_perf_buffer(print_event, page_cnt=64) |
Brendan Gregg | ee74c37 | 2016-02-15 22:22:19 -0800 | [diff] [blame] | 347 | while 1: |
| 348 | b.kprobe_poll() |