blob: b2c4156eb0827cf91adbdf35111a76002e42da4c [file] [log] [blame]
Brendan Greggebdb2422015-08-18 16:53:41 -07001#!/usr/bin/python
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07002#
3# vfsreadlat.py VFS read latency distribution.
4# For Linux, uses BCC, eBPF. See .c file.
5#
6# Written as a basic example of a function latency distribution histogram.
7#
8# USAGE: vfsreadlat.py [interval [count]]
9#
10# The default interval is 5 seconds. A Ctrl-C will print the partially
11# gathered histogram then exit.
12#
13# Copyright (c) 2015 Brendan Gregg.
14# Licensed under the Apache License, Version 2.0 (the "License")
15#
16# 15-Aug-2015 Brendan Gregg Created this.
17
Javier Honduvilla Cotofe966bb2018-05-10 20:45:45 +020018from __future__ import print_function
Brenden Blancoc35989d2015-09-02 18:04:07 -070019from bcc import BPF
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070020from ctypes import c_ushort, c_int, c_ulonglong
21from time import sleep
22from sys import argv
23
24def usage():
25 print("USAGE: %s [interval [count]]" % argv[0])
26 exit()
27
28# arguments
29interval = 5
30count = -1
31if len(argv) > 1:
32 try:
33 interval = int(argv[1])
34 if interval == 0:
35 raise
36 if len(argv) > 2:
37 count = int(argv[2])
38 except: # also catches -h, --help
39 usage()
40
41# load BPF program
42b = BPF(src_file = "vfsreadlat.c")
Brenden Blanco5eef65e2015-08-19 15:39:19 -070043b.attach_kprobe(event="vfs_read", fn_name="do_entry")
44b.attach_kretprobe(event="vfs_read", fn_name="do_return")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070045
46# header
47print("Tracing... Hit Ctrl-C to end.")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070048
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070049# output
50loop = 0
51do_exit = 0
52while (1):
53 if count > 0:
54 loop += 1
55 if loop > count:
56 exit()
57 try:
58 sleep(interval)
59 except KeyboardInterrupt:
60 pass; do_exit = 1
61
Javier Honduvilla Cotofe966bb2018-05-10 20:45:45 +020062 print()
Brendan Gregg7bc5b992015-09-07 14:34:22 -070063 b["dist"].print_log2_hist("usecs")
Brendan Greggef2452d2015-08-26 20:15:18 +100064 b["dist"].clear()
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070065 if do_exit:
66 exit()