Brendan Gregg | ebdb242 | 2015-08-18 16:53:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 2 | # |
| 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 Coto | fe966bb | 2018-05-10 20:45:45 +0200 | [diff] [blame] | 18 | from __future__ import print_function |
Brenden Blanco | c35989d | 2015-09-02 18:04:07 -0700 | [diff] [blame] | 19 | from bcc import BPF |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 20 | from ctypes import c_ushort, c_int, c_ulonglong |
| 21 | from time import sleep |
| 22 | from sys import argv |
| 23 | |
| 24 | def usage(): |
| 25 | print("USAGE: %s [interval [count]]" % argv[0]) |
| 26 | exit() |
| 27 | |
| 28 | # arguments |
| 29 | interval = 5 |
| 30 | count = -1 |
| 31 | if 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 |
| 42 | b = BPF(src_file = "vfsreadlat.c") |
Brenden Blanco | 5eef65e | 2015-08-19 15:39:19 -0700 | [diff] [blame] | 43 | b.attach_kprobe(event="vfs_read", fn_name="do_entry") |
| 44 | b.attach_kretprobe(event="vfs_read", fn_name="do_return") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 45 | |
| 46 | # header |
| 47 | print("Tracing... Hit Ctrl-C to end.") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 48 | |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 49 | # output |
| 50 | loop = 0 |
| 51 | do_exit = 0 |
| 52 | while (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 Coto | fe966bb | 2018-05-10 20:45:45 +0200 | [diff] [blame] | 62 | print() |
Brendan Gregg | 7bc5b99 | 2015-09-07 14:34:22 -0700 | [diff] [blame] | 63 | b["dist"].print_log2_hist("usecs") |
Brendan Gregg | ef2452d | 2015-08-26 20:15:18 +1000 | [diff] [blame] | 64 | b["dist"].clear() |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 65 | if do_exit: |
| 66 | exit() |