Brendan Gregg | ebdb242 | 2015-08-18 16:53:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 2 | # |
| 3 | # disksnoop.py Trace block device I/O: basic version of iosnoop. |
Brendan Gregg | 9894e3e | 2016-07-24 13:37:20 -0700 | [diff] [blame] | 4 | # For Linux, uses BCC, eBPF. Embedded C. |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 5 | # |
| 6 | # Written as a basic example of tracing latency. |
| 7 | # |
| 8 | # Copyright (c) 2015 Brendan Gregg. |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 10 | # |
| 11 | # 11-Aug-2015 Brendan Gregg Created this. |
| 12 | |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 13 | from __future__ import print_function |
Brenden Blanco | c35989d | 2015-09-02 18:04:07 -0700 | [diff] [blame] | 14 | from bcc import BPF |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 15 | |
| 16 | REQ_WRITE = 1 # from include/linux/blk_types.h |
| 17 | |
| 18 | # load BPF program |
Brendan Gregg | 9894e3e | 2016-07-24 13:37:20 -0700 | [diff] [blame] | 19 | b = BPF(text=""" |
| 20 | #include <uapi/linux/ptrace.h> |
| 21 | #include <linux/blkdev.h> |
| 22 | |
| 23 | BPF_HASH(start, struct request *); |
| 24 | |
| 25 | void trace_start(struct pt_regs *ctx, struct request *req) { |
| 26 | // stash start timestamp by request ptr |
| 27 | u64 ts = bpf_ktime_get_ns(); |
| 28 | |
| 29 | start.update(&req, &ts); |
| 30 | } |
| 31 | |
| 32 | void trace_completion(struct pt_regs *ctx, struct request *req) { |
| 33 | u64 *tsp, delta; |
| 34 | |
| 35 | tsp = start.lookup(&req); |
| 36 | if (tsp != 0) { |
| 37 | delta = bpf_ktime_get_ns() - *tsp; |
| 38 | bpf_trace_printk("%d %x %d\\n", req->__data_len, |
| 39 | req->cmd_flags, delta / 1000); |
| 40 | start.delete(&req); |
| 41 | } |
| 42 | } |
| 43 | """) |
| 44 | |
Brendan Gregg | 6ed4a49 | 2015-09-16 15:12:55 -0700 | [diff] [blame] | 45 | b.attach_kprobe(event="blk_start_request", fn_name="trace_start") |
| 46 | b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_start") |
| 47 | b.attach_kprobe(event="blk_account_io_completion", fn_name="trace_completion") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 48 | |
| 49 | # header |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 50 | print("%-18s %-2s %-7s %8s" % ("TIME(s)", "T", "BYTES", "LAT(ms)")) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 51 | |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 52 | # format output |
| 53 | while 1: |
Brenden Blanco | 819334e | 2015-09-04 21:20:59 -0700 | [diff] [blame] | 54 | (task, pid, cpu, flags, ts, msg) = b.trace_fields() |
Brendan Gregg | 39e1373 | 2015-08-26 20:16:29 +1000 | [diff] [blame] | 55 | (bytes_s, bflags_s, us_s) = msg.split() |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 56 | |
Brendan Gregg | 39e1373 | 2015-08-26 20:16:29 +1000 | [diff] [blame] | 57 | if int(bflags_s, 16) & REQ_WRITE: |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 58 | type_s = "W" |
| 59 | elif bytes_s == "0": # see blk_fill_rwbs() for logic |
| 60 | type_s = "M" |
| 61 | else: |
| 62 | type_s = "R" |
| 63 | ms = float(int(us_s, 10)) / 1000 |
| 64 | |
Brendan Gregg | 39e1373 | 2015-08-26 20:16:29 +1000 | [diff] [blame] | 65 | print("%-18.9f %-2s %-7s %8.2f" % (ts, type_s, bytes_s, ms)) |