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