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