blob: 3030e19b4a5d67277e087204905d91922819beb2 [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;
mcaleavya79f3c342016-02-12 23:19:11 +000075 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 }
mcaleavyaee5f8232016-02-12 20:06:38 +000084 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;
91 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) {
mcaleavya79f3c342016-02-12 23:19:11 +0000102 data.rwflag=1;
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800103 } else {
mcaleavya79f3c342016-02-12 23:19:11 +0000104 data.rwflag=0;
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800105 }
mcaleavya79f3c342016-02-12 23:19:11 +0000106 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 Gregg5be51942015-09-16 15:09:04 -0700133# header
134print("%-14s %-14s %-6s %-7s %-2s %-9s %-7s %7s" % ("TIME(s)", "COMM", "PID",
135 "DISK", "T", "SECTOR", "BYTES", "LAT(ms)"))
136
mcaleavyaee5f8232016-02-12 20:06:38 +0000137rwflg = ""
Brendan Gregg5be51942015-09-16 15:09:04 -0700138start_ts = 0
mcaleavyaee5f8232016-02-12 20:06:38 +0000139prev_ts = 0
140delta = 0
mcaleavya62c61522016-02-12 23:43:14 +0000141
mcaleavyaee5f8232016-02-12 20:06:38 +0000142# process event
143def print_event(cpu, data, size):
144 event = ct.cast(data, ct.POINTER(Data)).contents
mcaleavya6e789ab2016-02-12 23:06:53 +0000145
mcaleavyaee5f8232016-02-12 20:06:38 +0000146 val = -1
147 global start_ts
148 global prev_ts
149 global delta
mcaleavya6e789ab2016-02-12 23:06:53 +0000150
mcaleavyaee5f8232016-02-12 20:06:38 +0000151 if event.rwflag == 1:
152 rwflg = "W"
mcaleavya6e789ab2016-02-12 23:06:53 +0000153
mcaleavyaee5f8232016-02-12 20:06:38 +0000154 if event.rwflag == 0:
155 rwflg = "R"
mcaleavya6e789ab2016-02-12 23:06:53 +0000156
Brenden Blancoaa85f332016-03-08 10:07:51 -0800157 if not re.match(b'\?', event.name):
mcaleavyaee5f8232016-02-12 20:06:38 +0000158 val = event.sector
mcaleavya6e789ab2016-02-12 23:06:53 +0000159
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800160 if start_ts == 0:
mcaleavyaee5f8232016-02-12 20:06:38 +0000161 prev_ts = start_ts
mcaleavya6e789ab2016-02-12 23:06:53 +0000162
mcaleavyaee5f8232016-02-12 20:06:38 +0000163 if start_ts == 1:
164 delta = float(delta) + (event.ts - prev_ts)
mcaleavya6e789ab2016-02-12 23:06:53 +0000165
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800166 print("%-14.9f %-14.14s %-6s %-7s %-2s %-9s %-7s %7.2f" % (
mcaleavyaee5f8232016-02-12 20:06:38 +0000167 delta / 1000000, event.name, event.pid, event.disk_name, rwflg, val,
168 event.len, float(event.delta) / 1000000))
mcaleavya6e789ab2016-02-12 23:06:53 +0000169
mcaleavyaee5f8232016-02-12 20:06:38 +0000170 prev_ts = event.ts
171 start_ts = 1
mcaleavya6e789ab2016-02-12 23:06:53 +0000172
mcaleavya62c61522016-02-12 23:43:14 +0000173# loop with callback to print_event
mcaleavyaee5f8232016-02-12 20:06:38 +0000174b["events"].open_perf_buffer(print_event)
175while 1:
176 b.kprobe_poll()