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; |
mcaleavya | 79f3c34 | 2016-02-12 23:19:11 +0000 | [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 | } |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [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; |
| 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 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 101 | if (req->cmd_flags & REQ_WRITE) { |
mcaleavya | 79f3c34 | 2016-02-12 23:19:11 +0000 | [diff] [blame] | 102 | data.rwflag=1; |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 103 | } else { |
mcaleavya | 79f3c34 | 2016-02-12 23:19:11 +0000 | [diff] [blame] | 104 | data.rwflag=0; |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 105 | } |
mcaleavya | 79f3c34 | 2016-02-12 23:19:11 +0000 | [diff] [blame] | 106 | events.perf_submit(ctx,&data,sizeof(data)); |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 107 | start.delete(&req); |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 108 | infobyreq.delete(&req); |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 109 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 110 | return 0; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 111 | } |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 112 | """, debug=0) |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 113 | b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start") |
| 114 | b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start") |
| 115 | b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start") |
| 116 | b.attach_kprobe(event="blk_account_io_completion", |
| 117 | fn_name="trace_req_completion") |
| 118 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 119 | TASK_COMM_LEN = 16 # linux/sched.h |
| 120 | DISK_NAME_LEN = 32 # linux/genhd.h |
| 121 | |
| 122 | class Data(ct.Structure): |
| 123 | _fields_ = [ |
| 124 | ("pid", ct.c_ulonglong), |
| 125 | ("rwflag", ct.c_ulonglong), |
| 126 | ("delta", ct.c_ulonglong), |
| 127 | ("sector", ct.c_ulonglong), |
| 128 | ("len", ct.c_ulonglong), |
| 129 | ("ts", ct.c_ulonglong), |
| 130 | ("disk_name", ct.c_char * DISK_NAME_LEN), |
| 131 | ("name", ct.c_char * TASK_COMM_LEN) |
| 132 | ] |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 133 | # header |
| 134 | print("%-14s %-14s %-6s %-7s %-2s %-9s %-7s %7s" % ("TIME(s)", "COMM", "PID", |
| 135 | "DISK", "T", "SECTOR", "BYTES", "LAT(ms)")) |
| 136 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 137 | rwflg = "" |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 138 | start_ts = 0 |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 139 | prev_ts = 0 |
| 140 | delta = 0 |
mcaleavya | 62c6152 | 2016-02-12 23:43:14 +0000 | [diff] [blame] | 141 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 142 | # process event |
| 143 | def print_event(cpu, data, size): |
| 144 | event = ct.cast(data, ct.POINTER(Data)).contents |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 145 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 146 | val = -1 |
| 147 | global start_ts |
| 148 | global prev_ts |
| 149 | global delta |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 150 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 151 | if event.rwflag == 1: |
| 152 | rwflg = "W" |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 153 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 154 | if event.rwflag == 0: |
| 155 | rwflg = "R" |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 156 | |
Brenden Blanco | aa85f33 | 2016-03-08 10:07:51 -0800 | [diff] [blame] | 157 | if not re.match(b'\?', event.name): |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 158 | val = event.sector |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 159 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 160 | if start_ts == 0: |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 161 | prev_ts = start_ts |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 162 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 163 | if start_ts == 1: |
| 164 | delta = float(delta) + (event.ts - prev_ts) |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 165 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 166 | print("%-14.9f %-14.14s %-6s %-7s %-2s %-9s %-7s %7.2f" % ( |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 167 | delta / 1000000, event.name, event.pid, event.disk_name, rwflg, val, |
| 168 | event.len, float(event.delta) / 1000000)) |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 169 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 170 | prev_ts = event.ts |
| 171 | start_ts = 1 |
mcaleavya | 6e789ab | 2016-02-12 23:06:53 +0000 | [diff] [blame] | 172 | |
mcaleavya | 62c6152 | 2016-02-12 23:43:14 +0000 | [diff] [blame] | 173 | # loop with callback to print_event |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 174 | b["events"].open_perf_buffer(print_event) |
| 175 | while 1: |
| 176 | b.kprobe_poll() |