blob: 4b8fa9f872c53e3fd6bd31f3e8c9e68136e2dcfb [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 -070016from __future__ import print_function
17from bcc import BPF
mcaleavyaee5f8232016-02-12 20:06:38 +000018import ctypes as ct
19import re
Brendan Gregg5be51942015-09-16 15:09:04 -070020
21# load BPF program
22b = BPF(text="""
23#include <uapi/linux/ptrace.h>
24#include <linux/blkdev.h>
25
26struct val_t {
Brendan Greggbaebe802016-02-06 16:45:29 -080027 u32 pid;
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080028 char name[TASK_COMM_LEN];
Brendan Gregg5be51942015-09-16 15:09:04 -070029};
30
mcaleavya79f3c342016-02-12 23:19:11 +000031struct data_t {
mcaleavyaee5f8232016-02-12 20:06:38 +000032 u32 pid;
33 u64 rwflag;
34 u64 delta;
35 u64 sector;
36 u64 len;
37 u64 ts;
38 char disk_name[DISK_NAME_LEN];
39 char name[TASK_COMM_LEN];
40};
41
Brendan Gregg5be51942015-09-16 15:09:04 -070042BPF_HASH(start, struct request *);
Brendan Greggbaebe802016-02-06 16:45:29 -080043BPF_HASH(infobyreq, struct request *, struct val_t);
mcaleavyaee5f8232016-02-12 20:06:38 +000044BPF_PERF_OUTPUT(events);
Brendan Gregg5be51942015-09-16 15:09:04 -070045
46// cache PID and comm by-req
47int trace_pid_start(struct pt_regs *ctx, struct request *req)
48{
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080049 struct val_t val = {};
Brendan Gregg5be51942015-09-16 15:09:04 -070050
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080051 if (bpf_get_current_comm(&val.name, sizeof(val.name)) == 0) {
Brendan Gregg31cd1042016-02-06 17:19:59 -080052 val.pid = bpf_get_current_pid_tgid();
Brendan Greggbaebe802016-02-06 16:45:29 -080053 infobyreq.update(&req, &val);
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080054 }
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080055 return 0;
Brendan Gregg5be51942015-09-16 15:09:04 -070056}
57
58// time block I/O
59int trace_req_start(struct pt_regs *ctx, struct request *req)
60{
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080061 u64 ts;
Brendan Gregg5be51942015-09-16 15:09:04 -070062
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080063 ts = bpf_ktime_get_ns();
64 start.update(&req, &ts);
Brendan Gregg5be51942015-09-16 15:09:04 -070065
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080066 return 0;
Brendan Gregg5be51942015-09-16 15:09:04 -070067}
68
69// output
70int trace_req_completion(struct pt_regs *ctx, struct request *req)
71{
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080072 u64 *tsp, delta;
73 u32 *pidp = 0;
74 struct val_t *valp;
Brendan Greggaf3302d2016-08-03 15:15:41 -070075 struct data_t data = {};
mcaleavyaee5f8232016-02-12 20:06:38 +000076 u64 ts;
Brendan Gregg5be51942015-09-16 15:09:04 -070077
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080078 // fetch timestamp and calculate delta
79 tsp = start.lookup(&req);
80 if (tsp == 0) {
81 // missed tracing issue
82 return 0;
83 }
Brendan Greggaf3302d2016-08-03 15:15:41 -070084 ts = bpf_ktime_get_ns();
mcaleavya79f3c342016-02-12 23:19:11 +000085 data.delta = ts - *tsp;
86 data.ts = ts / 1000;
Brendan Gregg5be51942015-09-16 15:09:04 -070087
Brendan Greggbaebe802016-02-06 16:45:29 -080088 valp = infobyreq.lookup(&req);
89 if (valp == 0) {
mcaleavya79f3c342016-02-12 23:19:11 +000090 data.len = req->__data_len;
Brendan Greggaf3302d2016-08-03 15:15:41 -070091 strcpy(data.name, "?");
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080092 } else {
mcaleavya79f3c342016-02-12 23:19:11 +000093 data.pid = valp->pid;
94 data.len = req->__data_len;
95 data.sector = req->__sector;
96 bpf_probe_read(&data.name, sizeof(data.name), valp->name);
97 bpf_probe_read(&data.disk_name, sizeof(data.disk_name),
mcaleavyaee5f8232016-02-12 20:06:38 +000098 req->rq_disk->disk_name);
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080099 }
Brendan Gregg5be51942015-09-16 15:09:04 -0700100
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800101 if (req->cmd_flags & REQ_WRITE) {
Brendan Greggaf3302d2016-08-03 15:15:41 -0700102 data.rwflag = 1;
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800103 } else {
Brendan Greggaf3302d2016-08-03 15:15:41 -0700104 data.rwflag = 0;
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800105 }
Brendan Greggaf3302d2016-08-03 15:15:41 -0700106 events.perf_submit(ctx, &data, sizeof(data));
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800107 start.delete(&req);
Brendan Greggbaebe802016-02-06 16:45:29 -0800108 infobyreq.delete(&req);
Brendan Gregg5be51942015-09-16 15:09:04 -0700109
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800110 return 0;
Brendan Gregg5be51942015-09-16 15:09:04 -0700111}
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800112""", debug=0)
Brendan Gregg5be51942015-09-16 15:09:04 -0700113b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start")
114b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start")
115b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start")
116b.attach_kprobe(event="blk_account_io_completion",
117 fn_name="trace_req_completion")
118
mcaleavyaee5f8232016-02-12 20:06:38 +0000119TASK_COMM_LEN = 16 # linux/sched.h
120DISK_NAME_LEN = 32 # linux/genhd.h
121
122class Data(ct.Structure):
123 _fields_ = [
124 ("pid", ct.c_ulonglong),
125 ("rwflag", ct.c_ulonglong),
126 ("delta", ct.c_ulonglong),
127 ("sector", ct.c_ulonglong),
128 ("len", ct.c_ulonglong),
129 ("ts", ct.c_ulonglong),
130 ("disk_name", ct.c_char * DISK_NAME_LEN),
131 ("name", ct.c_char * TASK_COMM_LEN)
132 ]
Brendan Greggaf3302d2016-08-03 15:15:41 -0700133
Brendan Gregg5be51942015-09-16 15:09:04 -0700134# header
135print("%-14s %-14s %-6s %-7s %-2s %-9s %-7s %7s" % ("TIME(s)", "COMM", "PID",
136 "DISK", "T", "SECTOR", "BYTES", "LAT(ms)"))
137
mcaleavyaee5f8232016-02-12 20:06:38 +0000138rwflg = ""
Brendan Gregg5be51942015-09-16 15:09:04 -0700139start_ts = 0
mcaleavyaee5f8232016-02-12 20:06:38 +0000140prev_ts = 0
141delta = 0
mcaleavya62c61522016-02-12 23:43:14 +0000142
mcaleavyaee5f8232016-02-12 20:06:38 +0000143# process event
144def print_event(cpu, data, size):
145 event = ct.cast(data, ct.POINTER(Data)).contents
mcaleavya6e789ab2016-02-12 23:06:53 +0000146
mcaleavyaee5f8232016-02-12 20:06:38 +0000147 val = -1
148 global start_ts
149 global prev_ts
150 global delta
mcaleavya6e789ab2016-02-12 23:06:53 +0000151
mcaleavyaee5f8232016-02-12 20:06:38 +0000152 if event.rwflag == 1:
153 rwflg = "W"
mcaleavya6e789ab2016-02-12 23:06:53 +0000154
mcaleavyaee5f8232016-02-12 20:06:38 +0000155 if event.rwflag == 0:
156 rwflg = "R"
mcaleavya6e789ab2016-02-12 23:06:53 +0000157
Brenden Blancoaa85f332016-03-08 10:07:51 -0800158 if not re.match(b'\?', event.name):
mcaleavyaee5f8232016-02-12 20:06:38 +0000159 val = event.sector
mcaleavya6e789ab2016-02-12 23:06:53 +0000160
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800161 if start_ts == 0:
mcaleavyaee5f8232016-02-12 20:06:38 +0000162 prev_ts = start_ts
mcaleavya6e789ab2016-02-12 23:06:53 +0000163
mcaleavyaee5f8232016-02-12 20:06:38 +0000164 if start_ts == 1:
165 delta = float(delta) + (event.ts - prev_ts)
mcaleavya6e789ab2016-02-12 23:06:53 +0000166
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800167 print("%-14.9f %-14.14s %-6s %-7s %-2s %-9s %-7s %7.2f" % (
mcaleavyaee5f8232016-02-12 20:06:38 +0000168 delta / 1000000, event.name, event.pid, event.disk_name, rwflg, val,
169 event.len, float(event.delta) / 1000000))
mcaleavya6e789ab2016-02-12 23:06:53 +0000170
mcaleavyaee5f8232016-02-12 20:06:38 +0000171 prev_ts = event.ts
172 start_ts = 1
mcaleavya6e789ab2016-02-12 23:06:53 +0000173
mcaleavya62c61522016-02-12 23:43:14 +0000174# loop with callback to print_event
mcaleavyaee5f8232016-02-12 20:06:38 +0000175b["events"].open_perf_buffer(print_event)
176while 1:
177 b.kprobe_poll()