blob: f65e524320d4e380991f34e1b4ed1c436ea69051 [file] [log] [blame]
Brendan Greggebdb2422015-08-18 16:53:41 -07001#!/usr/bin/python
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07002#
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 Gregg762b1b42015-08-18 16:49:48 -070013from __future__ import print_function
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070014from bpf import BPF
15import sys
16
17REQ_WRITE = 1 # from include/linux/blk_types.h
18
19# load BPF program
20b = BPF(src_file="disksnoop.c")
Brenden Blanco5eef65e2015-08-19 15:39:19 -070021b.attach_kprobe(event="blk_start_request", fn_name="do_request")
22b.attach_kprobe(event="blk_update_request", fn_name="do_completion")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070023
24# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070025print("%-18s %-2s %-7s %8s" % ("TIME(s)", "T", "BYTES", "LAT(ms)"))
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070026
27# open trace pipe
28try:
29 trace = open("/sys/kernel/debug/tracing/trace_pipe", "r")
30except:
Brendan Gregg762b1b42015-08-18 16:49:48 -070031 print("ERROR: opening trace_pipe", file=sys.stderr)
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070032 exit(1)
33
34# format output
35while 1:
Brendan Gregg39e13732015-08-26 20:16:29 +100036 (task, pid, cpu, flags, ts, msg) = b.trace_readline_fields()
37 (bytes_s, bflags_s, us_s) = msg.split()
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070038
Brendan Gregg39e13732015-08-26 20:16:29 +100039 if int(bflags_s, 16) & REQ_WRITE:
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070040 type_s = "W"
41 elif bytes_s == "0": # see blk_fill_rwbs() for logic
42 type_s = "M"
43 else:
44 type_s = "R"
45 ms = float(int(us_s, 10)) / 1000
46
Brendan Gregg39e13732015-08-26 20:16:29 +100047 print("%-18.9f %-2s %-7s %8.2f" % (ts, type_s, bytes_s, ms))