Brendan Gregg | ebdb242 | 2015-08-18 16:53:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame^] | 2 | # @lint-avoid-python-3-compatibility-imports |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame^] | 4 | # vfscount Count VFS calls ("vfs_*"). |
| 5 | # For Linux, uses BCC, eBPF. See .c file. |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 6 | # |
| 7 | # Written as a basic example of counting functions. |
| 8 | # |
| 9 | # Copyright (c) 2015 Brendan Gregg. |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 11 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame^] | 12 | # 14-Aug-2015 Brendan Gregg Created this. |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 13 | |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 14 | from __future__ import print_function |
Brenden Blanco | c35989d | 2015-09-02 18:04:07 -0700 | [diff] [blame] | 15 | from bcc import BPF |
Brendan Gregg | d21af64 | 2015-09-04 17:42:51 -0700 | [diff] [blame] | 16 | from time import sleep |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 17 | |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 18 | # load BPF program |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame^] | 19 | b = BPF(src_file="vfscount.c") |
Brendan Gregg | 4cbdef7 | 2015-08-29 14:53:22 +1000 | [diff] [blame] | 20 | b.attach_kprobe(event_re="^vfs_.*", fn_name="do_count") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 21 | |
| 22 | # header |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 23 | print("Tracing... Ctrl-C to end.") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 24 | |
| 25 | # output |
| 26 | try: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame^] | 27 | sleep(99999999) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 28 | except KeyboardInterrupt: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame^] | 29 | pass |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 30 | |
Brendan Gregg | 4cbdef7 | 2015-08-29 14:53:22 +1000 | [diff] [blame] | 31 | print("\n%-16s %-26s %8s" % ("ADDR", "FUNC", "COUNT")) |
Brendan Gregg | 057b078 | 2015-08-20 12:40:37 -0700 | [diff] [blame] | 32 | counts = b.get_table("counts") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 33 | for k, v in sorted(counts.items(), key=lambda counts: counts[1].value): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame^] | 34 | print("%-16x %-26s %8d" % (k.ip, b.ksym(k.ip), v.value)) |