Brendan Gregg | 9c1f362 | 2016-02-14 23:14:12 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # zfsdist Summarize ZFS operation latency. |
| 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
| 7 | # USAGE: zfsdist [-h] [-T] [-m] [-p PID] [interval] [count] |
| 8 | # |
| 9 | # Copyright 2016 Netflix, Inc. |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 11 | # |
| 12 | # 14-Feb-2016 Brendan Gregg Created this. |
| 13 | |
| 14 | from __future__ import print_function |
| 15 | from bcc import BPF |
| 16 | from time import sleep, strftime |
| 17 | import argparse |
| 18 | |
| 19 | # arguments |
| 20 | examples = """examples: |
| 21 | ./zfsdist # show operation latency as a histogram |
| 22 | ./zfsdist -p 181 # trace PID 181 only |
| 23 | ./zfsdist 1 10 # print 1 second summaries, 10 times |
| 24 | ./zfsdist -m 5 # 5s summaries, milliseconds |
| 25 | """ |
| 26 | parser = argparse.ArgumentParser( |
| 27 | description="Summarize ZFS operation latency", |
| 28 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 29 | epilog=examples) |
| 30 | parser.add_argument("-T", "--notimestamp", action="store_true", |
| 31 | help="don't include timestamp on interval output") |
| 32 | parser.add_argument("-m", "--milliseconds", action="store_true", |
| 33 | help="output in milliseconds") |
| 34 | parser.add_argument("-p", "--pid", |
| 35 | help="trace this PID only") |
| 36 | parser.add_argument("interval", nargs="?", |
| 37 | help="output interval, in seconds") |
| 38 | parser.add_argument("count", nargs="?", default=99999999, |
| 39 | help="number of outputs") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 40 | parser.add_argument("--ebpf", action="store_true", |
| 41 | help=argparse.SUPPRESS) |
Brendan Gregg | 9c1f362 | 2016-02-14 23:14:12 -0800 | [diff] [blame] | 42 | args = parser.parse_args() |
| 43 | pid = args.pid |
| 44 | countdown = int(args.count) |
| 45 | if args.milliseconds: |
| 46 | factor = 1000000 |
| 47 | label = "msecs" |
| 48 | else: |
| 49 | factor = 1000 |
| 50 | label = "usecs" |
| 51 | if args.interval and int(args.interval) == 0: |
| 52 | print("ERROR: interval 0. Exiting.") |
| 53 | exit() |
| 54 | debug = 0 |
| 55 | |
| 56 | # define BPF program |
| 57 | bpf_text = """ |
| 58 | #include <uapi/linux/ptrace.h> |
| 59 | #include <linux/fs.h> |
| 60 | #include <linux/sched.h> |
| 61 | |
| 62 | #define OP_NAME_LEN 8 |
| 63 | typedef struct dist_key { |
| 64 | char op[OP_NAME_LEN]; |
| 65 | u64 slot; |
| 66 | } dist_key_t; |
| 67 | BPF_HASH(start, u32); |
| 68 | BPF_HISTOGRAM(dist, dist_key_t); |
| 69 | |
| 70 | // time operation |
| 71 | int trace_entry(struct pt_regs *ctx) |
| 72 | { |
| 73 | u32 pid = bpf_get_current_pid_tgid(); |
| 74 | if (FILTER_PID) |
| 75 | return 0; |
| 76 | u64 ts = bpf_ktime_get_ns(); |
| 77 | start.update(&pid, &ts); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int trace_return(struct pt_regs *ctx, const char *op) |
| 82 | { |
| 83 | u64 *tsp; |
| 84 | u32 pid = bpf_get_current_pid_tgid(); |
| 85 | |
| 86 | // fetch timestamp and calculate delta |
| 87 | tsp = start.lookup(&pid); |
| 88 | if (tsp == 0) { |
| 89 | return 0; // missed start or filtered |
| 90 | } |
| 91 | u64 delta = (bpf_ktime_get_ns() - *tsp) / FACTOR; |
| 92 | |
| 93 | // store as histogram |
| 94 | dist_key_t key = {.slot = bpf_log2l(delta)}; |
| 95 | __builtin_memcpy(&key.op, op, sizeof(key.op)); |
| 96 | dist.increment(key); |
| 97 | |
| 98 | start.delete(&pid); |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | int trace_read_return(struct pt_regs *ctx) |
| 103 | { |
| 104 | char *op = "read"; |
| 105 | return trace_return(ctx, op); |
| 106 | } |
| 107 | |
| 108 | int trace_write_return(struct pt_regs *ctx) |
| 109 | { |
| 110 | char *op = "write"; |
| 111 | return trace_return(ctx, op); |
| 112 | } |
| 113 | |
| 114 | int trace_open_return(struct pt_regs *ctx) |
| 115 | { |
| 116 | char *op = "open"; |
| 117 | return trace_return(ctx, op); |
| 118 | } |
| 119 | |
| 120 | int trace_fsync_return(struct pt_regs *ctx) |
| 121 | { |
| 122 | char *op = "fsync"; |
| 123 | return trace_return(ctx, op); |
| 124 | } |
| 125 | """ |
| 126 | bpf_text = bpf_text.replace('FACTOR', str(factor)) |
| 127 | if args.pid: |
| 128 | bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid) |
| 129 | else: |
| 130 | bpf_text = bpf_text.replace('FILTER_PID', '0') |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 131 | if debug or args.ebpf: |
Brendan Gregg | 9c1f362 | 2016-02-14 23:14:12 -0800 | [diff] [blame] | 132 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 133 | if args.ebpf: |
| 134 | exit() |
Brendan Gregg | 9c1f362 | 2016-02-14 23:14:12 -0800 | [diff] [blame] | 135 | |
| 136 | # load BPF program |
| 137 | b = BPF(text=bpf_text) |
| 138 | |
| 139 | # common file functions |
Andreas Gerstmayr | 0d08989 | 2018-05-29 16:06:00 +0200 | [diff] [blame^] | 140 | if BPF.get_kprobe_functions(b'zpl_iter'): |
Marcin Skarbek | 4f444a5 | 2017-08-28 17:39:50 +0200 | [diff] [blame] | 141 | b.attach_kprobe(event="zpl_iter_read", fn_name="trace_entry") |
| 142 | b.attach_kprobe(event="zpl_iter_write", fn_name="trace_entry") |
Andreas Gerstmayr | 0d08989 | 2018-05-29 16:06:00 +0200 | [diff] [blame^] | 143 | elif BPF.get_kprobe_functions(b'zpl_aio'): |
Marcin Skarbek | 4f444a5 | 2017-08-28 17:39:50 +0200 | [diff] [blame] | 144 | b.attach_kprobe(event="zpl_aio_read", fn_name="trace_entry") |
| 145 | b.attach_kprobe(event="zpl_aio_write", fn_name="trace_entry") |
| 146 | else: |
| 147 | b.attach_kprobe(event="zpl_read", fn_name="trace_entry") |
| 148 | b.attach_kprobe(event="zpl_write", fn_name="trace_entry") |
Brendan Gregg | 9c1f362 | 2016-02-14 23:14:12 -0800 | [diff] [blame] | 149 | b.attach_kprobe(event="zpl_open", fn_name="trace_entry") |
| 150 | b.attach_kprobe(event="zpl_fsync", fn_name="trace_entry") |
Andreas Gerstmayr | 0d08989 | 2018-05-29 16:06:00 +0200 | [diff] [blame^] | 151 | if BPF.get_kprobe_functions(b'zpl_iter'): |
Marcin Skarbek | 4f444a5 | 2017-08-28 17:39:50 +0200 | [diff] [blame] | 152 | b.attach_kretprobe(event="zpl_iter_read", fn_name="trace_read_return") |
| 153 | b.attach_kretprobe(event="zpl_iter_write", fn_name="trace_write_return") |
Andreas Gerstmayr | 0d08989 | 2018-05-29 16:06:00 +0200 | [diff] [blame^] | 154 | elif BPF.get_kprobe_functions(b'zpl_aio'): |
Marcin Skarbek | 4f444a5 | 2017-08-28 17:39:50 +0200 | [diff] [blame] | 155 | b.attach_kretprobe(event="zpl_aio_read", fn_name="trace_read_return") |
| 156 | b.attach_kretprobe(event="zpl_aio_write", fn_name="trace_write_return") |
| 157 | else: |
| 158 | b.attach_kretprobe(event="zpl_read", fn_name="trace_read_return") |
| 159 | b.attach_kretprobe(event="zpl_write", fn_name="trace_write_return") |
Brendan Gregg | 9c1f362 | 2016-02-14 23:14:12 -0800 | [diff] [blame] | 160 | b.attach_kretprobe(event="zpl_open", fn_name="trace_open_return") |
| 161 | b.attach_kretprobe(event="zpl_fsync", fn_name="trace_fsync_return") |
| 162 | |
| 163 | print("Tracing ZFS operation latency... Hit Ctrl-C to end.") |
| 164 | |
| 165 | # output |
| 166 | exiting = 0 |
| 167 | dist = b.get_table("dist") |
| 168 | while (1): |
| 169 | try: |
| 170 | if args.interval: |
| 171 | sleep(int(args.interval)) |
| 172 | else: |
| 173 | sleep(99999999) |
| 174 | except KeyboardInterrupt: |
| 175 | exiting = 1 |
| 176 | |
| 177 | print() |
| 178 | if args.interval and (not args.notimestamp): |
| 179 | print(strftime("%H:%M:%S:")) |
| 180 | |
| 181 | dist.print_log2_hist(label, "operation") |
| 182 | dist.clear() |
| 183 | |
| 184 | countdown -= 1 |
| 185 | if exiting or countdown == 0: |
| 186 | exit() |