blob: 352a170a3d542ed75692ff27b69b0432d7df626a [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
Brenden Blancoc35989d2015-09-02 18:04:07 -070014from bcc import BPF
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070015
16REQ_WRITE = 1 # from include/linux/blk_types.h
17
18# load BPF program
19b = BPF(src_file="disksnoop.c")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070020
21# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070022print("%-18s %-2s %-7s %8s" % ("TIME(s)", "T", "BYTES", "LAT(ms)"))
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070023
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070024# format output
25while 1:
Brenden Blanco819334e2015-09-04 21:20:59 -070026 (task, pid, cpu, flags, ts, msg) = b.trace_fields()
Brendan Gregg39e13732015-08-26 20:16:29 +100027 (bytes_s, bflags_s, us_s) = msg.split()
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070028
Brendan Gregg39e13732015-08-26 20:16:29 +100029 if int(bflags_s, 16) & REQ_WRITE:
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070030 type_s = "W"
31 elif bytes_s == "0": # see blk_fill_rwbs() for logic
32 type_s = "M"
33 else:
34 type_s = "R"
35 ms = float(int(us_s, 10)) / 1000
36
Brendan Gregg39e13732015-08-26 20:16:29 +100037 print("%-18.9f %-2s %-7s %8.2f" % (ts, type_s, bytes_s, ms))