blob: 32d83e5e91d7c84201e3720db8a2b59065644813 [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
Brendan Gregg0d4d0bf2016-08-24 15:10:05 -0700101/*
102 * The following deals with a kernel version change (in mainline 4.7, although
103 * it may be backported to earlier kernels) with how block request write flags
104 * are tested. We handle both pre- and post-change versions here. Please avoid
105 * kernel version tests like this as much as possible: they inflate the code,
106 * test, and maintenance burden.
107 */
108#ifdef REQ_WRITE
Ryan Learybc43a292017-01-07 15:34:31 -0500109 data.rwflag = !!(req->cmd_flags & REQ_WRITE);
110#elif defined(REQ_OP_SHIFT)
111 data.rwflag = !!((req->cmd_flags >> REQ_OP_SHIFT) == REQ_OP_WRITE);
Brendan Gregg0d4d0bf2016-08-24 15:10:05 -0700112#else
Ryan Learybc43a292017-01-07 15:34:31 -0500113 data.rwflag = !!((req->cmd_flags & REQ_OP_MASK) == REQ_OP_WRITE);
Brendan Gregg0d4d0bf2016-08-24 15:10:05 -0700114#endif
Brendan Gregg0d4d0bf2016-08-24 15:10:05 -0700115
Brendan Greggaf3302d2016-08-03 15:15:41 -0700116 events.perf_submit(ctx, &data, sizeof(data));
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800117 start.delete(&req);
Brendan Greggbaebe802016-02-06 16:45:29 -0800118 infobyreq.delete(&req);
Brendan Gregg5be51942015-09-16 15:09:04 -0700119
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800120 return 0;
Brendan Gregg5be51942015-09-16 15:09:04 -0700121}
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800122""", debug=0)
Brendan Gregg5be51942015-09-16 15:09:04 -0700123b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start")
124b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start")
125b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start")
126b.attach_kprobe(event="blk_account_io_completion",
127 fn_name="trace_req_completion")
128
mcaleavyaee5f8232016-02-12 20:06:38 +0000129TASK_COMM_LEN = 16 # linux/sched.h
130DISK_NAME_LEN = 32 # linux/genhd.h
131
132class Data(ct.Structure):
133 _fields_ = [
134 ("pid", ct.c_ulonglong),
135 ("rwflag", ct.c_ulonglong),
136 ("delta", ct.c_ulonglong),
137 ("sector", ct.c_ulonglong),
138 ("len", ct.c_ulonglong),
139 ("ts", ct.c_ulonglong),
140 ("disk_name", ct.c_char * DISK_NAME_LEN),
141 ("name", ct.c_char * TASK_COMM_LEN)
142 ]
Brendan Greggaf3302d2016-08-03 15:15:41 -0700143
Brendan Gregg5be51942015-09-16 15:09:04 -0700144# header
145print("%-14s %-14s %-6s %-7s %-2s %-9s %-7s %7s" % ("TIME(s)", "COMM", "PID",
146 "DISK", "T", "SECTOR", "BYTES", "LAT(ms)"))
147
mcaleavyaee5f8232016-02-12 20:06:38 +0000148rwflg = ""
Brendan Gregg5be51942015-09-16 15:09:04 -0700149start_ts = 0
mcaleavyaee5f8232016-02-12 20:06:38 +0000150prev_ts = 0
151delta = 0
mcaleavya62c61522016-02-12 23:43:14 +0000152
mcaleavyaee5f8232016-02-12 20:06:38 +0000153# process event
154def print_event(cpu, data, size):
155 event = ct.cast(data, ct.POINTER(Data)).contents
mcaleavya6e789ab2016-02-12 23:06:53 +0000156
mcaleavyaee5f8232016-02-12 20:06:38 +0000157 val = -1
158 global start_ts
159 global prev_ts
160 global delta
mcaleavya6e789ab2016-02-12 23:06:53 +0000161
mcaleavyaee5f8232016-02-12 20:06:38 +0000162 if event.rwflag == 1:
163 rwflg = "W"
mcaleavya6e789ab2016-02-12 23:06:53 +0000164
mcaleavyaee5f8232016-02-12 20:06:38 +0000165 if event.rwflag == 0:
166 rwflg = "R"
mcaleavya6e789ab2016-02-12 23:06:53 +0000167
Brenden Blancoaa85f332016-03-08 10:07:51 -0800168 if not re.match(b'\?', event.name):
mcaleavyaee5f8232016-02-12 20:06:38 +0000169 val = event.sector
mcaleavya6e789ab2016-02-12 23:06:53 +0000170
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800171 if start_ts == 0:
mcaleavyaee5f8232016-02-12 20:06:38 +0000172 prev_ts = start_ts
mcaleavya6e789ab2016-02-12 23:06:53 +0000173
mcaleavyaee5f8232016-02-12 20:06:38 +0000174 if start_ts == 1:
175 delta = float(delta) + (event.ts - prev_ts)
mcaleavya6e789ab2016-02-12 23:06:53 +0000176
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800177 print("%-14.9f %-14.14s %-6s %-7s %-2s %-9s %-7s %7.2f" % (
Rafael F78948e42017-03-26 14:54:25 +0200178 delta / 1000000, event.name.decode(), event.pid,
179 event.disk_name.decode(), rwflg, val,
mcaleavyaee5f8232016-02-12 20:06:38 +0000180 event.len, float(event.delta) / 1000000))
mcaleavya6e789ab2016-02-12 23:06:53 +0000181
mcaleavyaee5f8232016-02-12 20:06:38 +0000182 prev_ts = event.ts
183 start_ts = 1
mcaleavya6e789ab2016-02-12 23:06:53 +0000184
mcaleavya62c61522016-02-12 23:43:14 +0000185# loop with callback to print_event
Mark Drayton5f5687e2017-02-20 18:13:03 +0000186b["events"].open_perf_buffer(print_event, page_cnt=64)
mcaleavyaee5f8232016-02-12 20:06:38 +0000187while 1:
Teng Qindbf00292018-02-28 21:47:50 -0800188 b.perf_buffer_poll()