Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 4 | # biosnoop Trace block device I/O and print details including issuing PID. |
| 5 | # For Linux, uses BCC, eBPF. |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 6 | # |
| 7 | # This uses in-kernel eBPF maps to cache process details (PID and comm) by I/O |
| 8 | # request, as well as a starting timestamp for calculating I/O latency. |
| 9 | # |
| 10 | # Copyright (c) 2015 Brendan Gregg. |
| 11 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 12 | # |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 13 | # 16-Sep-2015 Brendan Gregg Created this. |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 14 | # 11-Feb-2016 Allan McAleavy updated for BPF_PERF_OUTPUT |
| 15 | |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
| 17 | from bcc import BPF |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 18 | import ctypes as ct |
| 19 | import re |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 20 | |
| 21 | # load BPF program |
| 22 | b = BPF(text=""" |
| 23 | #include <uapi/linux/ptrace.h> |
| 24 | #include <linux/blkdev.h> |
| 25 | |
| 26 | struct val_t { |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 27 | u32 pid; |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 28 | char name[TASK_COMM_LEN]; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
mcaleavya | 79f3c34 | 2016-02-12 23:19:11 +0000 | [diff] [blame] | 31 | struct data_t { |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 32 | u32 pid; |
| 33 | u64 rwflag; |
| 34 | u64 delta; |
| 35 | u64 sector; |
| 36 | u64 len; |
| 37 | u64 ts; |
| 38 | char disk_name[DISK_NAME_LEN]; |
| 39 | char name[TASK_COMM_LEN]; |
| 40 | }; |
| 41 | |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 42 | BPF_HASH(start, struct request *); |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 43 | BPF_HASH(infobyreq, struct request *, struct val_t); |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 44 | BPF_PERF_OUTPUT(events); |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 45 | |
| 46 | // cache PID and comm by-req |
| 47 | int trace_pid_start(struct pt_regs *ctx, struct request *req) |
| 48 | { |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 49 | struct val_t val = {}; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 50 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 51 | if (bpf_get_current_comm(&val.name, sizeof(val.name)) == 0) { |
Brendan Gregg | 31cd104 | 2016-02-06 17:19:59 -0800 | [diff] [blame] | 52 | val.pid = bpf_get_current_pid_tgid(); |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 53 | infobyreq.update(&req, &val); |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 54 | } |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 55 | return 0; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // time block I/O |
| 59 | int trace_req_start(struct pt_regs *ctx, struct request *req) |
| 60 | { |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 61 | u64 ts; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 62 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 63 | ts = bpf_ktime_get_ns(); |
| 64 | start.update(&req, &ts); |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 65 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 66 | return 0; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // output |
| 70 | int trace_req_completion(struct pt_regs *ctx, struct request *req) |
| 71 | { |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 72 | u64 *tsp, delta; |
| 73 | u32 *pidp = 0; |
| 74 | struct val_t *valp; |
Brendan Gregg | af3302d | 2016-08-03 15:15:41 -0700 | [diff] [blame] | 75 | struct data_t data = {}; |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 76 | u64 ts; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 77 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 78 | // fetch timestamp and calculate delta |
| 79 | tsp = start.lookup(&req); |
| 80 | if (tsp == 0) { |
| 81 | // missed tracing issue |
| 82 | return 0; |
| 83 | } |
Brendan Gregg | af3302d | 2016-08-03 15:15:41 -0700 | [diff] [blame] | 84 | ts = bpf_ktime_get_ns(); |
mcaleavya | 79f3c34 | 2016-02-12 23:19:11 +0000 | [diff] [blame] | 85 | data.delta = ts - *tsp; |
| 86 | data.ts = ts / 1000; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 87 | |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 88 | valp = infobyreq.lookup(&req); |
| 89 | if (valp == 0) { |
mcaleavya | 79f3c34 | 2016-02-12 23:19:11 +0000 | [diff] [blame] | 90 | data.len = req->__data_len; |
Brendan Gregg | af3302d | 2016-08-03 15:15:41 -0700 | [diff] [blame] | 91 | strcpy(data.name, "?"); |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 92 | } else { |
mcaleavya | 79f3c34 | 2016-02-12 23:19:11 +0000 | [diff] [blame] | 93 | data.pid = valp->pid; |
| 94 | data.len = req->__data_len; |
| 95 | data.sector = req->__sector; |
| 96 | bpf_probe_read(&data.name, sizeof(data.name), valp->name); |
| 97 | bpf_probe_read(&data.disk_name, sizeof(data.disk_name), |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 98 | req->rq_disk->disk_name); |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 99 | } |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 100 | |
Brendan Gregg | 0d4d0bf | 2016-08-24 15:10:05 -0700 | [diff] [blame] | 101 | /* |
| 102 | * The following deals with a kernel version change (in mainline 4.7, although |
| 103 | * it may be backported to earlier kernels) with how block request write flags |
| 104 | * are tested. We handle both pre- and post-change versions here. Please avoid |
| 105 | * kernel version tests like this as much as possible: they inflate the code, |
| 106 | * test, and maintenance burden. |
| 107 | */ |
| 108 | #ifdef REQ_WRITE |
Ryan Leary | bc43a29 | 2017-01-07 15:34:31 -0500 | [diff] [blame] | 109 | data.rwflag = !!(req->cmd_flags & REQ_WRITE); |
| 110 | #elif defined(REQ_OP_SHIFT) |
| 111 | data.rwflag = !!((req->cmd_flags >> REQ_OP_SHIFT) == REQ_OP_WRITE); |
Brendan Gregg | 0d4d0bf | 2016-08-24 15:10:05 -0700 | [diff] [blame] | 112 | #else |
Ryan Leary | bc43a29 | 2017-01-07 15:34:31 -0500 | [diff] [blame] | 113 | data.rwflag = !!((req->cmd_flags & REQ_OP_MASK) == REQ_OP_WRITE); |
Brendan Gregg | 0d4d0bf | 2016-08-24 15:10:05 -0700 | [diff] [blame] | 114 | #endif |
Brendan Gregg | 0d4d0bf | 2016-08-24 15:10:05 -0700 | [diff] [blame] | 115 | |
Brendan Gregg | af3302d | 2016-08-03 15:15:41 -0700 | [diff] [blame] | 116 | events.perf_submit(ctx, &data, sizeof(data)); |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 117 | start.delete(&req); |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 118 | infobyreq.delete(&req); |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 119 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 120 | return 0; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 121 | } |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 122 | """, debug=0) |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 123 | b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start") |
| 124 | b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start") |
| 125 | b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start") |
| 126 | b.attach_kprobe(event="blk_account_io_completion", |
| 127 | fn_name="trace_req_completion") |
| 128 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 129 | TASK_COMM_LEN = 16 # linux/sched.h |
| 130 | DISK_NAME_LEN = 32 # linux/genhd.h |
| 131 | |
| 132 | class Data(ct.Structure): |
| 133 | _fields_ = [ |
| 134 | ("pid", ct.c_ulonglong), |
| 135 | ("rwflag", ct.c_ulonglong), |
| 136 | ("delta", ct.c_ulonglong), |
| 137 | ("sector", ct.c_ulonglong), |
| 138 | ("len", ct.c_ulonglong), |
| 139 | ("ts", ct.c_ulonglong), |
| 140 | ("disk_name", ct.c_char * DISK_NAME_LEN), |
| 141 | ("name", ct.c_char * TASK_COMM_LEN) |
| 142 | ] |
Brendan Gregg | af3302d | 2016-08-03 15:15:41 -0700 | [diff] [blame] | 143 | |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 144 | # header |
| 145 | print("%-14s %-14s %-6s %-7s %-2s %-9s %-7s %7s" % ("TIME(s)", "COMM", "PID", |
| 146 | "DISK", "T", "SECTOR", "BYTES", "LAT(ms)")) |
| 147 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 148 | rwflg = "" |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 149 | start_ts = 0 |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 150 | prev_ts = 0 |
| 151 | delta = 0 |
mcaleavya | 62c6152 | 2016-02-12 23:43:14 +0000 | [diff] [blame] | 152 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 153 | # process event |
| 154 | def print_event(cpu, data, size): |
| 155 | event = ct.cast(data, ct.POINTER(Data)).contents |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 156 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 157 | val = -1 |
| 158 | global start_ts |
| 159 | global prev_ts |
| 160 | global delta |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 161 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 162 | if event.rwflag == 1: |
| 163 | rwflg = "W" |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 164 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 165 | if event.rwflag == 0: |
| 166 | rwflg = "R" |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 167 | |
Brenden Blanco | aa85f33 | 2016-03-08 10:07:51 -0800 | [diff] [blame] | 168 | if not re.match(b'\?', event.name): |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 169 | val = event.sector |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 170 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 171 | if start_ts == 0: |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 172 | prev_ts = start_ts |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 173 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 174 | if start_ts == 1: |
| 175 | delta = float(delta) + (event.ts - prev_ts) |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 176 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 177 | print("%-14.9f %-14.14s %-6s %-7s %-2s %-9s %-7s %7.2f" % ( |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 178 | delta / 1000000, event.name.decode(), event.pid, |
| 179 | event.disk_name.decode(), rwflg, val, |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 180 | event.len, float(event.delta) / 1000000)) |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 181 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 182 | prev_ts = event.ts |
| 183 | start_ts = 1 |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 184 | |
mcaleavya | 62c6152 | 2016-02-12 23:43:14 +0000 | [diff] [blame] | 185 | # loop with callback to print_event |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 186 | b["events"].open_perf_buffer(print_event, page_cnt=64) |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 187 | while 1: |
| 188 | b.kprobe_poll() |