blob: 21f74c58b18e8c2c311c398fc21440d06333fffe [file] [log] [blame]
Brendan Gregg5be51942015-09-16 15:09:04 -07001#!/usr/bin/python
Alexei Starovoitova79da0a2016-01-09 12:06:40 -08002# @lint-avoid-python-3-compatibility-imports
Brendan Gregg5be51942015-09-16 15:09:04 -07003#
Alexei Starovoitova79da0a2016-01-09 12:06:40 -08004# biosnoop Trace block device I/O and print details including issuing PID.
5# For Linux, uses BCC, eBPF.
Brendan Gregg5be51942015-09-16 15:09:04 -07006#
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 Starovoitova79da0a2016-01-09 12:06:40 -080013# 16-Sep-2015 Brendan Gregg Created this.
mcaleavyaee5f8232016-02-12 20:06:38 +000014# 11-Feb-2016 Allan McAleavy updated for BPF_PERF_OUTPUT
15
Brendan Gregg5be51942015-09-16 15:09:04 -070016
17from __future__ import print_function
18from bcc import BPF
mcaleavyaee5f8232016-02-12 20:06:38 +000019import ctypes as ct
20import re
Brendan Gregg5be51942015-09-16 15:09:04 -070021
22# load BPF program
23b = BPF(text="""
24#include <uapi/linux/ptrace.h>
25#include <linux/blkdev.h>
26
27struct val_t {
Brendan Greggbaebe802016-02-06 16:45:29 -080028 u32 pid;
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080029 char name[TASK_COMM_LEN];
Brendan Gregg5be51942015-09-16 15:09:04 -070030};
31
mcaleavyaee5f8232016-02-12 20:06:38 +000032struct 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 Gregg5be51942015-09-16 15:09:04 -070044BPF_HASH(start, struct request *);
Brendan Greggbaebe802016-02-06 16:45:29 -080045BPF_HASH(infobyreq, struct request *, struct val_t);
mcaleavyaee5f8232016-02-12 20:06:38 +000046BPF_PERF_OUTPUT(events);
Brendan Gregg5be51942015-09-16 15:09:04 -070047
48// cache PID and comm by-req
49int trace_pid_start(struct pt_regs *ctx, struct request *req)
50{
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080051 struct val_t val = {};
Brendan Gregg5be51942015-09-16 15:09:04 -070052
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080053 if (bpf_get_current_comm(&val.name, sizeof(val.name)) == 0) {
Brendan Gregg31cd1042016-02-06 17:19:59 -080054 val.pid = bpf_get_current_pid_tgid();
Brendan Greggbaebe802016-02-06 16:45:29 -080055 infobyreq.update(&req, &val);
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080056 }
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080057 return 0;
Brendan Gregg5be51942015-09-16 15:09:04 -070058}
59
60// time block I/O
61int trace_req_start(struct pt_regs *ctx, struct request *req)
62{
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080063 u64 ts;
Brendan Gregg5be51942015-09-16 15:09:04 -070064
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080065 ts = bpf_ktime_get_ns();
66 start.update(&req, &ts);
Brendan Gregg5be51942015-09-16 15:09:04 -070067
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080068 return 0;
Brendan Gregg5be51942015-09-16 15:09:04 -070069}
70
71// output
72int trace_req_completion(struct pt_regs *ctx, struct request *req)
73{
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080074 u64 *tsp, delta;
75 u32 *pidp = 0;
76 struct val_t *valp;
mcaleavyaee5f8232016-02-12 20:06:38 +000077 struct key_t key ={};
78 u64 ts;
Brendan Gregg5be51942015-09-16 15:09:04 -070079
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080080 // fetch timestamp and calculate delta
81 tsp = start.lookup(&req);
82 if (tsp == 0) {
83 // missed tracing issue
84 return 0;
85 }
mcaleavyaee5f8232016-02-12 20:06:38 +000086 ts = bpf_ktime_get_ns();
87 key.delta = ts - *tsp;
88 key.ts = ts / 1000;
Brendan Gregg5be51942015-09-16 15:09:04 -070089
Brendan Greggbaebe802016-02-06 16:45:29 -080090 valp = infobyreq.lookup(&req);
91 if (valp == 0) {
mcaleavyaee5f8232016-02-12 20:06:38 +000092 key.len = req->__data_len;
93 strcpy(key.name,"?");
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080094 } else {
mcaleavyaee5f8232016-02-12 20:06:38 +000095 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 Starovoitova79da0a2016-01-09 12:06:40 -0800101 }
Brendan Gregg5be51942015-09-16 15:09:04 -0700102
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800103 if (req->cmd_flags & REQ_WRITE) {
mcaleavyaee5f8232016-02-12 20:06:38 +0000104 key.rwflag=1;
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800105 } else {
mcaleavyaee5f8232016-02-12 20:06:38 +0000106 key.rwflag=0;
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800107 }
mcaleavyaee5f8232016-02-12 20:06:38 +0000108 events.perf_submit(ctx,&key,sizeof(key));
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800109 start.delete(&req);
Brendan Greggbaebe802016-02-06 16:45:29 -0800110 infobyreq.delete(&req);
Brendan Gregg5be51942015-09-16 15:09:04 -0700111
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800112 return 0;
Brendan Gregg5be51942015-09-16 15:09:04 -0700113}
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800114""", debug=0)
Brendan Gregg5be51942015-09-16 15:09:04 -0700115b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start")
116b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start")
117b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start")
118b.attach_kprobe(event="blk_account_io_completion",
119 fn_name="trace_req_completion")
120
mcaleavyaee5f8232016-02-12 20:06:38 +0000121
122TASK_COMM_LEN = 16 # linux/sched.h
123DISK_NAME_LEN = 32 # linux/genhd.h
124
125class 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 Gregg5be51942015-09-16 15:09:04 -0700136# header
137print("%-14s %-14s %-6s %-7s %-2s %-9s %-7s %7s" % ("TIME(s)", "COMM", "PID",
138 "DISK", "T", "SECTOR", "BYTES", "LAT(ms)"))
139
mcaleavyaee5f8232016-02-12 20:06:38 +0000140rwflg = ""
Brendan Gregg5be51942015-09-16 15:09:04 -0700141start_ts = 0
mcaleavyaee5f8232016-02-12 20:06:38 +0000142prev_ts = 0
143delta = 0
144# process event
145def 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 Starovoitova79da0a2016-01-09 12:06:40 -0800157 if start_ts == 0:
mcaleavyaee5f8232016-02-12 20:06:38 +0000158 prev_ts = start_ts
159 if start_ts == 1:
160 delta = float(delta) + (event.ts - prev_ts)
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800161 print("%-14.9f %-14.14s %-6s %-7s %-2s %-9s %-7s %7.2f" % (
mcaleavyaee5f8232016-02-12 20:06:38 +0000162 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
166b["events"].open_perf_buffer(print_event)
167while 1:
168 b.kprobe_poll()