blob: 2b1e77d526429b5bae4203f784d707d4f2abae3e [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);
Paul Chaignonf86f7e82018-06-14 02:20:03 +020097 struct gendisk *rq_disk = req->rq_disk;
mcaleavya79f3c342016-02-12 23:19:11 +000098 bpf_probe_read(&data.disk_name, sizeof(data.disk_name),
Paul Chaignonf86f7e82018-06-14 02:20:03 +020099 rq_disk->disk_name);
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800100 }
Brendan Gregg5be51942015-09-16 15:09:04 -0700101
Brendan Gregg0d4d0bf2016-08-24 15:10:05 -0700102/*
103 * The following deals with a kernel version change (in mainline 4.7, although
104 * it may be backported to earlier kernels) with how block request write flags
105 * are tested. We handle both pre- and post-change versions here. Please avoid
106 * kernel version tests like this as much as possible: they inflate the code,
107 * test, and maintenance burden.
108 */
109#ifdef REQ_WRITE
Ryan Learybc43a292017-01-07 15:34:31 -0500110 data.rwflag = !!(req->cmd_flags & REQ_WRITE);
111#elif defined(REQ_OP_SHIFT)
112 data.rwflag = !!((req->cmd_flags >> REQ_OP_SHIFT) == REQ_OP_WRITE);
Brendan Gregg0d4d0bf2016-08-24 15:10:05 -0700113#else
Ryan Learybc43a292017-01-07 15:34:31 -0500114 data.rwflag = !!((req->cmd_flags & REQ_OP_MASK) == REQ_OP_WRITE);
Brendan Gregg0d4d0bf2016-08-24 15:10:05 -0700115#endif
Brendan Gregg0d4d0bf2016-08-24 15:10:05 -0700116
Brendan Greggaf3302d2016-08-03 15:15:41 -0700117 events.perf_submit(ctx, &data, sizeof(data));
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800118 start.delete(&req);
Brendan Greggbaebe802016-02-06 16:45:29 -0800119 infobyreq.delete(&req);
Brendan Gregg5be51942015-09-16 15:09:04 -0700120
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800121 return 0;
Brendan Gregg5be51942015-09-16 15:09:04 -0700122}
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800123""", debug=0)
Brendan Gregg5be51942015-09-16 15:09:04 -0700124b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start")
125b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start")
126b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start")
127b.attach_kprobe(event="blk_account_io_completion",
128 fn_name="trace_req_completion")
129
mcaleavyaee5f8232016-02-12 20:06:38 +0000130TASK_COMM_LEN = 16 # linux/sched.h
131DISK_NAME_LEN = 32 # linux/genhd.h
132
133class Data(ct.Structure):
134 _fields_ = [
135 ("pid", ct.c_ulonglong),
136 ("rwflag", ct.c_ulonglong),
137 ("delta", ct.c_ulonglong),
138 ("sector", ct.c_ulonglong),
139 ("len", ct.c_ulonglong),
140 ("ts", ct.c_ulonglong),
141 ("disk_name", ct.c_char * DISK_NAME_LEN),
142 ("name", ct.c_char * TASK_COMM_LEN)
143 ]
Brendan Greggaf3302d2016-08-03 15:15:41 -0700144
Brendan Gregg5be51942015-09-16 15:09:04 -0700145# header
146print("%-14s %-14s %-6s %-7s %-2s %-9s %-7s %7s" % ("TIME(s)", "COMM", "PID",
147 "DISK", "T", "SECTOR", "BYTES", "LAT(ms)"))
148
mcaleavyaee5f8232016-02-12 20:06:38 +0000149rwflg = ""
Brendan Gregg5be51942015-09-16 15:09:04 -0700150start_ts = 0
mcaleavyaee5f8232016-02-12 20:06:38 +0000151prev_ts = 0
152delta = 0
mcaleavya62c61522016-02-12 23:43:14 +0000153
mcaleavyaee5f8232016-02-12 20:06:38 +0000154# process event
155def print_event(cpu, data, size):
156 event = ct.cast(data, ct.POINTER(Data)).contents
mcaleavya6e789ab2016-02-12 23:06:53 +0000157
mcaleavyaee5f8232016-02-12 20:06:38 +0000158 val = -1
159 global start_ts
160 global prev_ts
161 global delta
mcaleavya6e789ab2016-02-12 23:06:53 +0000162
mcaleavyaee5f8232016-02-12 20:06:38 +0000163 if event.rwflag == 1:
164 rwflg = "W"
mcaleavya6e789ab2016-02-12 23:06:53 +0000165
mcaleavyaee5f8232016-02-12 20:06:38 +0000166 if event.rwflag == 0:
167 rwflg = "R"
mcaleavya6e789ab2016-02-12 23:06:53 +0000168
Brenden Blancoaa85f332016-03-08 10:07:51 -0800169 if not re.match(b'\?', event.name):
mcaleavyaee5f8232016-02-12 20:06:38 +0000170 val = event.sector
mcaleavya6e789ab2016-02-12 23:06:53 +0000171
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800172 if start_ts == 0:
mcaleavyaee5f8232016-02-12 20:06:38 +0000173 prev_ts = start_ts
mcaleavya6e789ab2016-02-12 23:06:53 +0000174
mcaleavyaee5f8232016-02-12 20:06:38 +0000175 if start_ts == 1:
176 delta = float(delta) + (event.ts - prev_ts)
mcaleavya6e789ab2016-02-12 23:06:53 +0000177
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800178 print("%-14.9f %-14.14s %-6s %-7s %-2s %-9s %-7s %7.2f" % (
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200179 delta / 1000000, event.name.decode('utf-8', 'replace'), event.pid,
180 event.disk_name.decode('utf-8', 'replace'), rwflg, val,
mcaleavyaee5f8232016-02-12 20:06:38 +0000181 event.len, float(event.delta) / 1000000))
mcaleavya6e789ab2016-02-12 23:06:53 +0000182
mcaleavyaee5f8232016-02-12 20:06:38 +0000183 prev_ts = event.ts
184 start_ts = 1
mcaleavya6e789ab2016-02-12 23:06:53 +0000185
mcaleavya62c61522016-02-12 23:43:14 +0000186# loop with callback to print_event
Mark Drayton5f5687e2017-02-20 18:13:03 +0000187b["events"].open_perf_buffer(print_event, page_cnt=64)
mcaleavyaee5f8232016-02-12 20:06:38 +0000188while 1:
Teng Qindbf00292018-02-28 21:47:50 -0800189 b.perf_buffer_poll()