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 |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 19 | b = BPF(text=""" |
| 20 | #include <uapi/linux/ptrace.h> |
| 21 | |
| 22 | struct key_t { |
| 23 | u64 ip; |
| 24 | }; |
| 25 | |
Teng Qin | 7a3e5bc | 2017-03-29 13:39:17 -0700 | [diff] [blame] | 26 | BPF_HASH(counts, struct key_t, u64, 256); |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 27 | |
Nan Xiao | 92d86bb | 2017-08-22 09:59:26 +0800 | [diff] [blame] | 28 | int do_count(struct pt_regs *ctx) { |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 29 | struct key_t key = {}; |
| 30 | u64 zero = 0, *val; |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 31 | key.ip = PT_REGS_IP(ctx); |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 32 | val = counts.lookup_or_init(&key, &zero); |
| 33 | (*val)++; |
| 34 | return 0; |
| 35 | } |
| 36 | """) |
Brendan Gregg | 4cbdef7 | 2015-08-29 14:53:22 +1000 | [diff] [blame] | 37 | b.attach_kprobe(event_re="^vfs_.*", fn_name="do_count") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 38 | |
| 39 | # header |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 40 | print("Tracing... Ctrl-C to end.") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 41 | |
| 42 | # output |
| 43 | try: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 44 | sleep(99999999) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 45 | except KeyboardInterrupt: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 46 | pass |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 47 | |
Brendan Gregg | 4cbdef7 | 2015-08-29 14:53:22 +1000 | [diff] [blame] | 48 | print("\n%-16s %-26s %8s" % ("ADDR", "FUNC", "COUNT")) |
Brendan Gregg | 057b078 | 2015-08-20 12:40:37 -0700 | [diff] [blame] | 49 | counts = b.get_table("counts") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 50 | 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] | 51 | print("%-16x %-26s %8d" % (k.ip, b.ksym(k.ip), v.value)) |