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. |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 14 | |
| 15 | from __future__ import print_function |
| 16 | from bcc import BPF |
| 17 | |
| 18 | # load BPF program |
| 19 | b = BPF(text=""" |
| 20 | #include <uapi/linux/ptrace.h> |
| 21 | #include <linux/blkdev.h> |
| 22 | |
| 23 | struct val_t { |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 24 | u32 pid; |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 25 | char name[TASK_COMM_LEN]; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | BPF_HASH(start, struct request *); |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 29 | BPF_HASH(infobyreq, struct request *, struct val_t); |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 30 | |
| 31 | // cache PID and comm by-req |
| 32 | int trace_pid_start(struct pt_regs *ctx, struct request *req) |
| 33 | { |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 34 | u32 pid; |
| 35 | struct val_t val = {}; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 36 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 37 | pid = bpf_get_current_pid_tgid(); |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 38 | if (bpf_get_current_comm(&val.name, sizeof(val.name)) == 0) { |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 39 | val.pid = pid; |
| 40 | infobyreq.update(&req, &val); |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 41 | } |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 42 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 43 | return 0; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // time block I/O |
| 47 | int trace_req_start(struct pt_regs *ctx, struct request *req) |
| 48 | { |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 49 | u64 ts; |
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 | ts = bpf_ktime_get_ns(); |
| 52 | start.update(&req, &ts); |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 53 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 54 | return 0; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // output |
| 58 | int trace_req_completion(struct pt_regs *ctx, struct request *req) |
| 59 | { |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 60 | u64 *tsp, delta; |
| 61 | u32 *pidp = 0; |
| 62 | struct val_t *valp; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 63 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 64 | // fetch timestamp and calculate delta |
| 65 | tsp = start.lookup(&req); |
| 66 | if (tsp == 0) { |
| 67 | // missed tracing issue |
| 68 | return 0; |
| 69 | } |
| 70 | delta = bpf_ktime_get_ns() - *tsp; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 71 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 72 | // |
| 73 | // Fetch and output issuing pid and comm. |
| 74 | // As bpf_trace_prink() is limited to a maximum of 1 string and 2 |
| 75 | // integers, we'll use more than one to output the data. |
| 76 | // |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 77 | valp = infobyreq.lookup(&req); |
| 78 | if (valp == 0) { |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 79 | bpf_trace_printk("0 0 ? %d\\n", req->__data_len); |
| 80 | } else { |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 81 | bpf_trace_printk("0 %d %s %d\\n", valp->pid, valp->name, |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 82 | req->__data_len); |
| 83 | } |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 84 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 85 | // output remaining details |
| 86 | if (req->cmd_flags & REQ_WRITE) { |
| 87 | bpf_trace_printk("1 W %s %d %d ?\\n", req->rq_disk->disk_name, |
| 88 | req->__sector, delta / 1000); |
| 89 | } else { |
| 90 | bpf_trace_printk("1 R %s %d %d ?\\n", req->rq_disk->disk_name, |
| 91 | req->__sector, delta / 1000); |
| 92 | } |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 93 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 94 | start.delete(&req); |
Brendan Gregg | baebe80 | 2016-02-06 16:45:29 -0800 | [diff] [blame] | 95 | infobyreq.delete(&req); |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 96 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 97 | return 0; |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 98 | } |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 99 | """, debug=0) |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 100 | b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start") |
| 101 | b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start") |
| 102 | b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start") |
| 103 | b.attach_kprobe(event="blk_account_io_completion", |
| 104 | fn_name="trace_req_completion") |
| 105 | |
| 106 | # header |
| 107 | print("%-14s %-14s %-6s %-7s %-2s %-9s %-7s %7s" % ("TIME(s)", "COMM", "PID", |
| 108 | "DISK", "T", "SECTOR", "BYTES", "LAT(ms)")) |
| 109 | |
| 110 | start_ts = 0 |
| 111 | |
| 112 | # format output |
| 113 | while 1: |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 114 | (task, pid, cpu, flags, ts, msg) = b.trace_fields() |
| 115 | args = msg.split(" ") |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 116 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 117 | if start_ts == 0: |
| 118 | start_ts = ts |
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 | if args[0] == "0": |
| 121 | (real_pid, real_comm, bytes_s) = (args[1], args[2], args[3]) |
| 122 | continue |
| 123 | else: |
| 124 | (type_s, disk_s, sector_s, us_s) = (args[1], args[2], args[3], |
| 125 | args[4]) |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 126 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 127 | ms = float(int(us_s, 10)) / 1000 |
Brendan Gregg | 5be5194 | 2015-09-16 15:09:04 -0700 | [diff] [blame] | 128 | |
Alexei Starovoitov | a79da0a | 2016-01-09 12:06:40 -0800 | [diff] [blame] | 129 | print("%-14.9f %-14.14s %-6s %-7s %-2s %-9s %-7s %7.2f" % ( |
| 130 | ts - start_ts, real_comm, real_pid, disk_s, type_s, sector_s, |
| 131 | bytes_s, ms)) |