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