Brendan Gregg | 6f075b9 | 2016-02-07 00:46:34 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # biotop block device (disk) I/O by process. |
| 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
Nathan Scott | f5fb9af | 2018-01-17 09:39:59 +1100 | [diff] [blame] | 7 | # USAGE: biotop.py [-h] [-C] [-r MAXROWS] [interval] [count] |
Brendan Gregg | 6f075b9 | 2016-02-07 00:46:34 -0800 | [diff] [blame] | 8 | # |
| 9 | # This uses in-kernel eBPF maps to cache process details (PID and comm) by I/O |
| 10 | # request, as well as a starting timestamp for calculating I/O latency. |
| 11 | # |
| 12 | # Copyright 2016 Netflix, Inc. |
| 13 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 14 | # |
| 15 | # 06-Feb-2016 Brendan Gregg Created this. |
| 16 | |
| 17 | from __future__ import print_function |
| 18 | from bcc import BPF |
| 19 | from time import sleep, strftime |
| 20 | import argparse |
| 21 | import signal |
| 22 | from subprocess import call |
| 23 | |
| 24 | # arguments |
| 25 | examples = """examples: |
| 26 | ./biotop # block device I/O top, 1 second refresh |
| 27 | ./biotop -C # don't clear the screen |
| 28 | ./biotop 5 # 5 second summaries |
| 29 | ./biotop 5 10 # 5 second summaries, 10 times only |
| 30 | """ |
| 31 | parser = argparse.ArgumentParser( |
| 32 | description="Block device (disk) I/O by process", |
| 33 | formatter_class=argparse.RawDescriptionHelpFormatter, |
Nathan Scott | 1a197db | 2018-01-21 09:14:27 +1100 | [diff] [blame] | 34 | epilog=examples) |
Brendan Gregg | 6f075b9 | 2016-02-07 00:46:34 -0800 | [diff] [blame] | 35 | parser.add_argument("-C", "--noclear", action="store_true", |
| 36 | help="don't clear the screen") |
| 37 | parser.add_argument("-r", "--maxrows", default=20, |
| 38 | help="maximum rows to print, default 20") |
| 39 | parser.add_argument("interval", nargs="?", default=1, |
| 40 | help="output interval, in seconds") |
| 41 | parser.add_argument("count", nargs="?", default=99999999, |
| 42 | help="number of outputs") |
Nathan Scott | f5fb9af | 2018-01-17 09:39:59 +1100 | [diff] [blame] | 43 | parser.add_argument("--ebpf", action="store_true", |
| 44 | help=argparse.SUPPRESS) |
Brendan Gregg | 6f075b9 | 2016-02-07 00:46:34 -0800 | [diff] [blame] | 45 | args = parser.parse_args() |
| 46 | interval = int(args.interval) |
| 47 | countdown = int(args.count) |
| 48 | maxrows = int(args.maxrows) |
| 49 | clear = not int(args.noclear) |
| 50 | |
| 51 | # linux stats |
| 52 | loadavg = "/proc/loadavg" |
| 53 | diskstats = "/proc/diskstats" |
| 54 | |
| 55 | # signal handler |
| 56 | def signal_ignore(signal, frame): |
| 57 | print() |
| 58 | |
| 59 | # load BPF program |
Nathan Scott | ca4ba55 | 2018-01-16 11:02:58 +1100 | [diff] [blame] | 60 | bpf_text = """ |
Brendan Gregg | 6f075b9 | 2016-02-07 00:46:34 -0800 | [diff] [blame] | 61 | #include <uapi/linux/ptrace.h> |
| 62 | #include <linux/blkdev.h> |
| 63 | |
Brendan Gregg | 6321d00 | 2016-02-07 00:54:44 -0800 | [diff] [blame] | 64 | // for saving process info by request |
Brendan Gregg | 6f075b9 | 2016-02-07 00:46:34 -0800 | [diff] [blame] | 65 | struct who_t { |
| 66 | u32 pid; |
| 67 | char name[TASK_COMM_LEN]; |
| 68 | }; |
| 69 | |
Brendan Gregg | 6321d00 | 2016-02-07 00:54:44 -0800 | [diff] [blame] | 70 | // the key for the output summary |
Brendan Gregg | 6f075b9 | 2016-02-07 00:46:34 -0800 | [diff] [blame] | 71 | struct info_t { |
| 72 | u32 pid; |
Brendan Gregg | 51add78 | 2016-11-29 11:11:31 -0800 | [diff] [blame] | 73 | int rwflag; |
Brendan Gregg | 6f075b9 | 2016-02-07 00:46:34 -0800 | [diff] [blame] | 74 | int major; |
| 75 | int minor; |
| 76 | char name[TASK_COMM_LEN]; |
| 77 | }; |
| 78 | |
Brendan Gregg | 6321d00 | 2016-02-07 00:54:44 -0800 | [diff] [blame] | 79 | // the value of the output summary |
Brendan Gregg | 6f075b9 | 2016-02-07 00:46:34 -0800 | [diff] [blame] | 80 | struct val_t { |
| 81 | u64 bytes; |
| 82 | u64 us; |
| 83 | u32 io; |
| 84 | }; |
| 85 | |
| 86 | BPF_HASH(start, struct request *); |
| 87 | BPF_HASH(whobyreq, struct request *, struct who_t); |
| 88 | BPF_HASH(counts, struct info_t, struct val_t); |
| 89 | |
| 90 | // cache PID and comm by-req |
| 91 | int trace_pid_start(struct pt_regs *ctx, struct request *req) |
| 92 | { |
| 93 | struct who_t who = {}; |
| 94 | |
| 95 | if (bpf_get_current_comm(&who.name, sizeof(who.name)) == 0) { |
| 96 | who.pid = bpf_get_current_pid_tgid(); |
| 97 | whobyreq.update(&req, &who); |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | // time block I/O |
| 104 | int trace_req_start(struct pt_regs *ctx, struct request *req) |
| 105 | { |
| 106 | u64 ts; |
| 107 | |
| 108 | ts = bpf_ktime_get_ns(); |
| 109 | start.update(&req, &ts); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | // output |
| 115 | int trace_req_completion(struct pt_regs *ctx, struct request *req) |
| 116 | { |
| 117 | u64 *tsp; |
| 118 | |
| 119 | // fetch timestamp and calculate delta |
| 120 | tsp = start.lookup(&req); |
| 121 | if (tsp == 0) { |
| 122 | return 0; // missed tracing issue |
| 123 | } |
| 124 | |
| 125 | struct who_t *whop; |
| 126 | struct val_t *valp, zero = {}; |
| 127 | u64 delta_us = (bpf_ktime_get_ns() - *tsp) / 1000; |
| 128 | |
| 129 | // setup info_t key |
| 130 | struct info_t info = {}; |
| 131 | info.major = req->rq_disk->major; |
| 132 | info.minor = req->rq_disk->first_minor; |
Brendan Gregg | 51add78 | 2016-11-29 11:11:31 -0800 | [diff] [blame] | 133 | /* |
| 134 | * The following deals with a kernel version change (in mainline 4.7, although |
| 135 | * it may be backported to earlier kernels) with how block request write flags |
| 136 | * are tested. We handle both pre- and post-change versions here. Please avoid |
| 137 | * kernel version tests like this as much as possible: they inflate the code, |
| 138 | * test, and maintenance burden. |
| 139 | */ |
| 140 | #ifdef REQ_WRITE |
| 141 | info.rwflag = !!(req->cmd_flags & REQ_WRITE); |
Ryan Leary | bc43a29 | 2017-01-07 15:34:31 -0500 | [diff] [blame] | 142 | #elif defined(REQ_OP_SHIFT) |
Brendan Gregg | 51add78 | 2016-11-29 11:11:31 -0800 | [diff] [blame] | 143 | info.rwflag = !!((req->cmd_flags >> REQ_OP_SHIFT) == REQ_OP_WRITE); |
Ryan Leary | bc43a29 | 2017-01-07 15:34:31 -0500 | [diff] [blame] | 144 | #else |
| 145 | info.rwflag = !!((req->cmd_flags & REQ_OP_MASK) == REQ_OP_WRITE); |
Brendan Gregg | 51add78 | 2016-11-29 11:11:31 -0800 | [diff] [blame] | 146 | #endif |
| 147 | |
Brendan Gregg | 6f075b9 | 2016-02-07 00:46:34 -0800 | [diff] [blame] | 148 | whop = whobyreq.lookup(&req); |
| 149 | if (whop == 0) { |
| 150 | // missed pid who, save stats as pid 0 |
| 151 | valp = counts.lookup_or_init(&info, &zero); |
| 152 | } else { |
| 153 | info.pid = whop->pid; |
| 154 | __builtin_memcpy(&info.name, whop->name, sizeof(info.name)); |
| 155 | valp = counts.lookup_or_init(&info, &zero); |
| 156 | } |
| 157 | |
| 158 | // save stats |
| 159 | valp->us += delta_us; |
| 160 | valp->bytes += req->__data_len; |
| 161 | valp->io++; |
| 162 | |
| 163 | start.delete(&req); |
| 164 | whobyreq.delete(&req); |
| 165 | |
| 166 | return 0; |
| 167 | } |
Nathan Scott | ca4ba55 | 2018-01-16 11:02:58 +1100 | [diff] [blame] | 168 | """ |
| 169 | |
| 170 | if args.ebpf: |
| 171 | print(bpf_text) |
| 172 | exit() |
| 173 | |
| 174 | b = BPF(text=bpf_text) |
Brendan Gregg | 6f075b9 | 2016-02-07 00:46:34 -0800 | [diff] [blame] | 175 | b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start") |
| 176 | b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start") |
| 177 | b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start") |
| 178 | b.attach_kprobe(event="blk_account_io_completion", |
| 179 | fn_name="trace_req_completion") |
| 180 | |
| 181 | print('Tracing... Output every %d secs. Hit Ctrl-C to end' % interval) |
| 182 | |
| 183 | # cache disk major,minor -> diskname |
| 184 | disklookup = {} |
| 185 | with open(diskstats) as stats: |
| 186 | for line in stats: |
| 187 | a = line.split() |
| 188 | disklookup[a[0] + "," + a[1]] = a[2] |
| 189 | |
| 190 | # output |
| 191 | exiting = 0 |
| 192 | while 1: |
| 193 | try: |
| 194 | sleep(interval) |
| 195 | except KeyboardInterrupt: |
| 196 | exiting = 1 |
| 197 | |
| 198 | # header |
| 199 | if clear: |
| 200 | call("clear") |
| 201 | else: |
| 202 | print() |
| 203 | with open(loadavg) as stats: |
| 204 | print("%-8s loadavg: %s" % (strftime("%H:%M:%S"), stats.read())) |
| 205 | print("%-6s %-16s %1s %-3s %-3s %-8s %5s %7s %6s" % ("PID", "COMM", |
| 206 | "D", "MAJ", "MIN", "DISK", "I/O", "Kbytes", "AVGms")) |
| 207 | |
| 208 | # by-PID output |
| 209 | counts = b.get_table("counts") |
| 210 | line = 0 |
| 211 | for k, v in reversed(sorted(counts.items(), |
| 212 | key=lambda counts: counts[1].bytes)): |
| 213 | |
| 214 | # lookup disk |
| 215 | disk = str(k.major) + "," + str(k.minor) |
| 216 | if disk in disklookup: |
| 217 | diskname = disklookup[disk] |
| 218 | else: |
| 219 | diskname = "?" |
| 220 | |
| 221 | # print line |
| 222 | avg_ms = (float(v.us) / 1000) / v.io |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 223 | print("%-6d %-16s %1s %-3d %-3d %-8s %5s %7s %6.2f" % (k.pid, |
| 224 | k.name.decode(), "W" if k.rwflag else "R", k.major, k.minor, |
| 225 | diskname, v.io, v.bytes / 1024, avg_ms)) |
Brendan Gregg | 6f075b9 | 2016-02-07 00:46:34 -0800 | [diff] [blame] | 226 | |
| 227 | line += 1 |
| 228 | if line >= maxrows: |
| 229 | break |
| 230 | counts.clear() |
| 231 | |
| 232 | countdown -= 1 |
| 233 | if exiting or countdown == 0: |
| 234 | print("Detaching...") |
| 235 | exit() |