blob: 633ab6121767deccc4f2227c830463a08476477c [file] [log] [blame]
Brendan Gregg5be51942015-09-16 15:09:04 -07001#!/usr/bin/python
2#
3# biosnoop Trace block device I/O and print details including issuing PID.
4# For Linux, uses BCC, eBPF.
5#
6# This uses in-kernel eBPF maps to cache process details (PID and comm) by I/O
7# request, as well as a starting timestamp for calculating I/O latency.
8#
9# Copyright (c) 2015 Brendan Gregg.
10# Licensed under the Apache License, Version 2.0 (the "License")
11#
12# 16-Sep-2015 Brendan Gregg Created this.
13
14from __future__ import print_function
15from bcc import BPF
16
17# load BPF program
18b = BPF(text="""
19#include <uapi/linux/ptrace.h>
20#include <linux/blkdev.h>
21
22struct val_t {
23 char name[TASK_COMM_LEN];
24};
25
26BPF_HASH(start, struct request *);
27BPF_HASH(pidbyreq, struct request *, u32);
28BPF_HASH(commbyreq, struct request *, struct val_t);
29
30// cache PID and comm by-req
31int trace_pid_start(struct pt_regs *ctx, struct request *req)
32{
33 u32 pid;
34 struct val_t val = {};
35
36 pid = bpf_get_current_pid_tgid();
37 pidbyreq.update(&req, &pid);
38 if (bpf_get_current_comm(&val.name, sizeof(val.name)) == 0) {
39 commbyreq.update(&req, &val);
40 }
41
42 return 0;
43}
44
45// time block I/O
46int trace_req_start(struct pt_regs *ctx, struct request *req)
47{
48 u64 ts;
49
50 ts = bpf_ktime_get_ns();
51 start.update(&req, &ts);
52
53 return 0;
54}
55
56// output
57int trace_req_completion(struct pt_regs *ctx, struct request *req)
58{
59 u64 *tsp, delta;
60 u32 *pidp = 0;
61 struct val_t *valp;
62
63 // fetch timestamp and calculate delta
64 tsp = start.lookup(&req);
65 if (tsp == 0) {
66 // missed tracing issue
67 return 0;
68 }
69 delta = bpf_ktime_get_ns() - *tsp;
70
71 //
72 // Fetch and output issuing pid and comm.
73 // As bpf_trace_prink() is limited to a maximum of 1 string and 2
74 // integers, we'll use more than one to output the data.
75 //
76 valp = commbyreq.lookup(&req);
77 pidp = pidbyreq.lookup(&req);
78 if (pidp == 0 || valp == 0) {
79 bpf_trace_printk("0 0 ? %d\\n", req->__data_len);
80 } else {
81 bpf_trace_printk("0 %d %s %d\\n", *pidp, valp->name,
82 req->__data_len);
83 }
84
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 }
93
94 start.delete(&req);
95 pidbyreq.delete(&req);
96 commbyreq.delete(&req);
97
98 return 0;
99}
100""")
Brendan Gregg5be51942015-09-16 15:09:04 -0700101b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start")
102b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start")
103b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start")
104b.attach_kprobe(event="blk_account_io_completion",
105 fn_name="trace_req_completion")
106
107# header
108print("%-14s %-14s %-6s %-7s %-2s %-9s %-7s %7s" % ("TIME(s)", "COMM", "PID",
109 "DISK", "T", "SECTOR", "BYTES", "LAT(ms)"))
110
111start_ts = 0
112
113# format output
114while 1:
115 (task, pid, cpu, flags, ts, msg) = b.trace_fields()
116 args = msg.split(" ")
117
118 if start_ts == 0:
119 start_ts = ts
120
121 if args[0] == "0":
122 (real_pid, real_comm, bytes_s) = (args[1], args[2], args[3])
123 continue
124 else:
125 (type_s, disk_s, sector_s, us_s) = (args[1], args[2], args[3],
126 args[4])
127
128 ms = float(int(us_s, 10)) / 1000
129
130 print("%-14.9f %-14.14s %-6s %-7s %-2s %-9s %-7s %7.2f" % (
131 ts - start_ts, real_comm, real_pid, disk_s, type_s, sector_s,
132 bytes_s, ms))