blob: 37ee3f9cb40111ce07ee4f6045d1003a02a15010 [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.
Brendan Gregg5be51942015-09-16 15:09:04 -070014
15from __future__ import print_function
16from bcc import BPF
17
18# load BPF program
19b = BPF(text="""
20#include <uapi/linux/ptrace.h>
21#include <linux/blkdev.h>
22
23struct val_t {
Brendan Greggbaebe802016-02-06 16:45:29 -080024 u32 pid;
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080025 char name[TASK_COMM_LEN];
Brendan Gregg5be51942015-09-16 15:09:04 -070026};
27
28BPF_HASH(start, struct request *);
Brendan Greggbaebe802016-02-06 16:45:29 -080029BPF_HASH(infobyreq, struct request *, struct val_t);
Brendan Gregg5be51942015-09-16 15:09:04 -070030
31// cache PID and comm by-req
32int trace_pid_start(struct pt_regs *ctx, struct request *req)
33{
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080034 struct val_t val = {};
Brendan Gregg5be51942015-09-16 15:09:04 -070035
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080036 if (bpf_get_current_comm(&val.name, sizeof(val.name)) == 0) {
Brendan Gregg31cd1042016-02-06 17:19:59 -080037 val.pid = bpf_get_current_pid_tgid();
Brendan Greggbaebe802016-02-06 16:45:29 -080038 infobyreq.update(&req, &val);
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080039 }
Brendan Gregg5be51942015-09-16 15:09:04 -070040
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080041 return 0;
Brendan Gregg5be51942015-09-16 15:09:04 -070042}
43
44// time block I/O
45int trace_req_start(struct pt_regs *ctx, struct request *req)
46{
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080047 u64 ts;
Brendan Gregg5be51942015-09-16 15:09:04 -070048
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080049 ts = bpf_ktime_get_ns();
50 start.update(&req, &ts);
Brendan Gregg5be51942015-09-16 15:09:04 -070051
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080052 return 0;
Brendan Gregg5be51942015-09-16 15:09:04 -070053}
54
55// output
56int trace_req_completion(struct pt_regs *ctx, struct request *req)
57{
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080058 u64 *tsp, delta;
59 u32 *pidp = 0;
60 struct val_t *valp;
Brendan Gregg5be51942015-09-16 15:09:04 -070061
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080062 // fetch timestamp and calculate delta
63 tsp = start.lookup(&req);
64 if (tsp == 0) {
65 // missed tracing issue
66 return 0;
67 }
68 delta = bpf_ktime_get_ns() - *tsp;
Brendan Gregg5be51942015-09-16 15:09:04 -070069
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080070 //
71 // Fetch and output issuing pid and comm.
72 // As bpf_trace_prink() is limited to a maximum of 1 string and 2
73 // integers, we'll use more than one to output the data.
74 //
Brendan Greggbaebe802016-02-06 16:45:29 -080075 valp = infobyreq.lookup(&req);
76 if (valp == 0) {
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080077 bpf_trace_printk("0 0 ? %d\\n", req->__data_len);
78 } else {
Brendan Greggbaebe802016-02-06 16:45:29 -080079 bpf_trace_printk("0 %d %s %d\\n", valp->pid, valp->name,
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080080 req->__data_len);
81 }
Brendan Gregg5be51942015-09-16 15:09:04 -070082
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080083 // output remaining details
84 if (req->cmd_flags & REQ_WRITE) {
85 bpf_trace_printk("1 W %s %d %d ?\\n", req->rq_disk->disk_name,
86 req->__sector, delta / 1000);
87 } else {
88 bpf_trace_printk("1 R %s %d %d ?\\n", req->rq_disk->disk_name,
89 req->__sector, delta / 1000);
90 }
Brendan Gregg5be51942015-09-16 15:09:04 -070091
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080092 start.delete(&req);
Brendan Greggbaebe802016-02-06 16:45:29 -080093 infobyreq.delete(&req);
Brendan Gregg5be51942015-09-16 15:09:04 -070094
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080095 return 0;
Brendan Gregg5be51942015-09-16 15:09:04 -070096}
Alexei Starovoitova79da0a2016-01-09 12:06:40 -080097""", debug=0)
Brendan Gregg5be51942015-09-16 15:09:04 -070098b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start")
99b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start")
100b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start")
101b.attach_kprobe(event="blk_account_io_completion",
102 fn_name="trace_req_completion")
103
104# header
105print("%-14s %-14s %-6s %-7s %-2s %-9s %-7s %7s" % ("TIME(s)", "COMM", "PID",
106 "DISK", "T", "SECTOR", "BYTES", "LAT(ms)"))
107
108start_ts = 0
109
110# format output
111while 1:
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800112 (task, pid, cpu, flags, ts, msg) = b.trace_fields()
113 args = msg.split(" ")
Brendan Gregg5be51942015-09-16 15:09:04 -0700114
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800115 if start_ts == 0:
116 start_ts = ts
Brendan Gregg5be51942015-09-16 15:09:04 -0700117
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800118 if args[0] == "0":
119 (real_pid, real_comm, bytes_s) = (args[1], args[2], args[3])
120 continue
121 else:
122 (type_s, disk_s, sector_s, us_s) = (args[1], args[2], args[3],
123 args[4])
Brendan Gregg5be51942015-09-16 15:09:04 -0700124
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800125 ms = float(int(us_s, 10)) / 1000
Brendan Gregg5be51942015-09-16 15:09:04 -0700126
Alexei Starovoitova79da0a2016-01-09 12:06:40 -0800127 print("%-14.9f %-14.14s %-6s %-7s %-2s %-9s %-7s %7.2f" % (
128 ts - start_ts, real_comm, real_pid, disk_s, type_s, sector_s,
129 bytes_s, ms))