blob: bd16dd501ceab5a8f9744744e6257e2be23fd460 [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
Brenden Blancoc35989d2015-09-02 18:04:07 -070018from bcc import BPF
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070019from ctypes import c_ushort, c_int, c_ulonglong
20from time import sleep
21from sys import argv
22
23def usage():
24 print("USAGE: %s [interval [count]]" % argv[0])
25 exit()
26
27# arguments
28interval = 5
29count = -1
30if len(argv) > 1:
31 try:
32 interval = int(argv[1])
33 if interval == 0:
34 raise
35 if len(argv) > 2:
36 count = int(argv[2])
37 except: # also catches -h, --help
38 usage()
39
40# load BPF program
41b = BPF(src_file = "vfsreadlat.c")
Brenden Blanco5eef65e2015-08-19 15:39:19 -070042b.attach_kprobe(event="vfs_read", fn_name="do_entry")
43b.attach_kretprobe(event="vfs_read", fn_name="do_return")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070044
45# header
46print("Tracing... Hit Ctrl-C to end.")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070047
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070048# output
49loop = 0
50do_exit = 0
51while (1):
52 if count > 0:
53 loop += 1
54 if loop > count:
55 exit()
56 try:
57 sleep(interval)
58 except KeyboardInterrupt:
59 pass; do_exit = 1
60
61 print
Brendan Gregg7bc5b992015-09-07 14:34:22 -070062 b["dist"].print_log2_hist("usecs")
Brendan Greggef2452d2015-08-26 20:15:18 +100063 b["dist"].clear()
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070064 if do_exit:
65 exit()