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. |
| 4 | # For Linux, uses BCC, eBPF. See .c file. |
| 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 |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 14 | from bpf import BPF |
| 15 | import sys |
| 16 | |
| 17 | REQ_WRITE = 1 # from include/linux/blk_types.h |
| 18 | |
| 19 | # load BPF program |
| 20 | b = BPF(src_file="disksnoop.c") |
Brenden Blanco | 5eef65e | 2015-08-19 15:39:19 -0700 | [diff] [blame] | 21 | b.attach_kprobe(event="blk_start_request", fn_name="do_request") |
| 22 | b.attach_kprobe(event="blk_update_request", fn_name="do_completion") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 23 | |
| 24 | # header |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 25 | print("%-18s %-2s %-7s %8s" % ("TIME(s)", "T", "BYTES", "LAT(ms)")) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 26 | |
| 27 | # open trace pipe |
| 28 | try: |
| 29 | trace = open("/sys/kernel/debug/tracing/trace_pipe", "r") |
| 30 | except: |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 31 | print("ERROR: opening trace_pipe", file=sys.stderr) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 32 | exit(1) |
| 33 | |
| 34 | # format output |
| 35 | while 1: |
| 36 | try: |
| 37 | line = trace.readline().rstrip() |
| 38 | except KeyboardInterrupt: |
| 39 | pass; exit() |
| 40 | prolog, time_s, colon, bytes_s, flags_s, us_s = \ |
| 41 | line.rsplit(" ", 5) |
| 42 | |
| 43 | time_s = time_s[:-1] # strip trailing ":" |
| 44 | flags = int(flags_s, 16) |
| 45 | if flags & REQ_WRITE: |
| 46 | type_s = "W" |
| 47 | elif bytes_s == "0": # see blk_fill_rwbs() for logic |
| 48 | type_s = "M" |
| 49 | else: |
| 50 | type_s = "R" |
| 51 | ms = float(int(us_s, 10)) / 1000 |
| 52 | |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 53 | print("%-18s %-2s %-7s %8.2f" % (time_s, type_s, bytes_s, ms)) |