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 | |
| 17 | from __future__ import print_function |
| 18 | from bcc import BPF |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 19 | import ctypes as ct |
| 20 | import re |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 21 | |
| 22 | # load BPF program |
| 23 | b = BPF(text=""" |
| 24 | #include <uapi/linux/ptrace.h> |
| 25 | #include <linux/blkdev.h> |
| 26 | |
| 27 | struct val_t { |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 28 | u32 pid; |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 29 | char name[TASK_COMM_LEN]; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 32 | struct key_t { |
| 33 | u32 pid; |
| 34 | u64 rwflag; |
| 35 | u64 delta; |
| 36 | u64 sector; |
| 37 | u64 len; |
| 38 | u64 ts; |
| 39 | char disk_name[DISK_NAME_LEN]; |
| 40 | char name[TASK_COMM_LEN]; |
| 41 | }; |
| 42 | |
| 43 | |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 44 | BPF_HASH(start, struct request *); |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 45 | BPF_HASH(infobyreq, struct request *, struct val_t); |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 46 | BPF_PERF_OUTPUT(events); |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 47 | |
| 48 | // cache PID and comm by-req |
| 49 | int trace_pid_start(struct pt_regs *ctx, struct request *req) |
| 50 | { |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 51 | struct val_t val = {}; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 52 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 53 | if (bpf_get_current_comm(&val.name, sizeof(val.name)) == 0) { |
Brendan Gregg | 31cd104 | 2016-02-06 17:19:59 -0800 | [diff] [blame] | 54 | val.pid = bpf_get_current_pid_tgid(); |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 55 | infobyreq.update(&req, &val); |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 56 | } |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 57 | return 0; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // time block I/O |
| 61 | int trace_req_start(struct pt_regs *ctx, struct request *req) |
| 62 | { |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 63 | u64 ts; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 64 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 65 | ts = bpf_ktime_get_ns(); |
| 66 | start.update(&req, &ts); |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 67 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 68 | return 0; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // output |
| 72 | int trace_req_completion(struct pt_regs *ctx, struct request *req) |
| 73 | { |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 74 | u64 *tsp, delta; |
| 75 | u32 *pidp = 0; |
| 76 | struct val_t *valp; |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 77 | struct key_t key ={}; |
| 78 | u64 ts; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 79 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 80 | // fetch timestamp and calculate delta |
| 81 | tsp = start.lookup(&req); |
| 82 | if (tsp == 0) { |
| 83 | // missed tracing issue |
| 84 | return 0; |
| 85 | } |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 86 | ts = bpf_ktime_get_ns(); |
| 87 | key.delta = ts - *tsp; |
| 88 | key.ts = ts / 1000; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 89 | |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 90 | valp = infobyreq.lookup(&req); |
| 91 | if (valp == 0) { |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 92 | key.len = req->__data_len; |
| 93 | strcpy(key.name,"?"); |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 94 | } else { |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 95 | key.pid = valp->pid; |
| 96 | key.len = req->__data_len; |
| 97 | key.sector = req->__sector; |
| 98 | bpf_probe_read(&key.name, sizeof(key.name), valp->name); |
| 99 | bpf_probe_read(&key.disk_name, sizeof(key.disk_name), |
| 100 | req->rq_disk->disk_name); |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 101 | } |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 102 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 103 | if (req->cmd_flags & REQ_WRITE) { |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 104 | key.rwflag=1; |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 105 | } else { |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 106 | key.rwflag=0; |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 107 | } |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 108 | events.perf_submit(ctx,&key,sizeof(key)); |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 109 | start.delete(&req); |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 110 | infobyreq.delete(&req); |
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 | return 0; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 113 | } |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 114 | """, debug=0) |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 115 | b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start") |
| 116 | b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start") |
| 117 | b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start") |
| 118 | b.attach_kprobe(event="blk_account_io_completion", |
| 119 | fn_name="trace_req_completion") |
| 120 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 121 | |
| 122 | TASK_COMM_LEN = 16 # linux/sched.h |
| 123 | DISK_NAME_LEN = 32 # linux/genhd.h |
| 124 | |
| 125 | class Data(ct.Structure): |
| 126 | _fields_ = [ |
| 127 | ("pid", ct.c_ulonglong), |
| 128 | ("rwflag", ct.c_ulonglong), |
| 129 | ("delta", ct.c_ulonglong), |
| 130 | ("sector", ct.c_ulonglong), |
| 131 | ("len", ct.c_ulonglong), |
| 132 | ("ts", ct.c_ulonglong), |
| 133 | ("disk_name", ct.c_char * DISK_NAME_LEN), |
| 134 | ("name", ct.c_char * TASK_COMM_LEN) |
| 135 | ] |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 136 | # header |
| 137 | print("%-14s %-14s %-6s %-7s %-2s %-9s %-7s %7s" % ("TIME(s)", "COMM", "PID", |
| 138 | "DISK", "T", "SECTOR", "BYTES", "LAT(ms)")) |
| 139 | |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 140 | rwflg = "" |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 141 | start_ts = 0 |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 142 | prev_ts = 0 |
| 143 | delta = 0 |
| 144 | # process event |
| 145 | def print_event(cpu, data, size): |
| 146 | event = ct.cast(data, ct.POINTER(Data)).contents |
| 147 | val = -1 |
| 148 | global start_ts |
| 149 | global prev_ts |
| 150 | global delta |
| 151 | if event.rwflag == 1: |
| 152 | rwflg = "W" |
| 153 | if event.rwflag == 0: |
| 154 | rwflg = "R" |
| 155 | if not re.match('\?', event.name): |
| 156 | val = event.sector |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 157 | if start_ts == 0: |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 158 | prev_ts = start_ts |
| 159 | if start_ts == 1: |
| 160 | delta = float(delta) + (event.ts - prev_ts) |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 161 | print("%-14.9f %-14.14s %-6s %-7s %-2s %-9s %-7s %7.2f" % ( |
mcaleavya | ee5f823 | 2016-02-12 20:06:38 +0000 | [diff] [blame] | 162 | delta / 1000000, event.name, event.pid, event.disk_name, rwflg, val, |
| 163 | event.len, float(event.delta) / 1000000)) |
| 164 | prev_ts = event.ts |
| 165 | start_ts = 1 |
| 166 | b["events"].open_perf_buffer(print_event) |
| 167 | while 1: |
| 168 | b.kprobe_poll() |