Brendan Gregg | 3e55ae2 | 2015-09-10 12:11:35 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # funccount Count kernel function calls. |
| 4 | # For Linux, uses BCC, eBPF. See .c file. |
| 5 | # |
| 6 | # USAGE: funccount [-h] [-p PID] [-i INTERVAL] [-T] [-r] pattern |
| 7 | # |
| 8 | # The pattern is a string with optional '*' wildcards, similar to file globbing. |
| 9 | # If you'd prefer to use regular expressions, use the -r option. |
| 10 | # |
| 11 | # Copyright (c) 2015 Brendan Gregg. |
| 12 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 13 | # |
| 14 | # 09-Sep-2015 Brendan Gregg Created this. |
| 15 | |
| 16 | from __future__ import print_function |
| 17 | from bcc import BPF |
| 18 | from time import sleep, strftime |
| 19 | import argparse |
Brendan Gregg | 3e55ae2 | 2015-09-10 12:11:35 -0700 | [diff] [blame] | 20 | import signal |
| 21 | |
| 22 | # arguments |
| 23 | examples = """examples: |
| 24 | ./funccount 'vfs_*' # count kernel functions starting with "vfs" |
| 25 | ./funccount 'tcp_send*' # count kernel funcs starting with "tcp_send" |
| 26 | ./funccount -r '^vfs.*' # same as above, using regular expressions |
| 27 | ./funccount -Ti 5 'vfs_*' # output every 5 seconds, with timestamps |
| 28 | ./funccount -p 185 'vfs_*' # count vfs calls for PID 181 only |
| 29 | """ |
| 30 | parser = argparse.ArgumentParser( |
| 31 | description="Count kernel function calls", |
| 32 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 33 | epilog=examples) |
| 34 | parser.add_argument("-p", "--pid", |
| 35 | help="trace this PID only") |
| 36 | parser.add_argument("-i", "--interval", default=99999999, |
| 37 | help="summary interval, seconds") |
| 38 | parser.add_argument("-T", "--timestamp", action="store_true", |
| 39 | help="include timestamp on output") |
| 40 | parser.add_argument("-r", "--regexp", action="store_true", |
| 41 | help="use regular expressions. Default is \"*\" wildcards only.") |
| 42 | parser.add_argument("pattern", |
| 43 | help="search expression for kernel functions") |
| 44 | args = parser.parse_args() |
| 45 | pattern = args.pattern |
| 46 | if not args.regexp: |
| 47 | pattern = pattern.replace('*', '.*') |
| 48 | pattern = '^' + pattern + '$' |
| 49 | debug = 0 |
| 50 | |
| 51 | # signal handler |
| 52 | def signal_ignore(signal, frame): |
| 53 | print() |
| 54 | |
| 55 | # load BPF program |
| 56 | bpf_text = """ |
| 57 | #include <uapi/linux/ptrace.h> |
| 58 | |
| 59 | struct key_t { |
| 60 | u64 ip; |
| 61 | }; |
| 62 | BPF_HASH(counts, struct key_t); |
| 63 | |
| 64 | int trace_count(struct pt_regs *ctx) { |
Brendan Gregg | 91734d3 | 2015-09-21 11:58:16 -0700 | [diff] [blame^] | 65 | FILTER |
Brendan Gregg | 3e55ae2 | 2015-09-10 12:11:35 -0700 | [diff] [blame] | 66 | struct key_t key = {}; |
| 67 | u64 zero = 0, *val; |
| 68 | key.ip = ctx->ip; |
| 69 | val = counts.lookup_or_init(&key, &zero); |
| 70 | (*val)++; |
Brendan Gregg | 3e55ae2 | 2015-09-10 12:11:35 -0700 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | """ |
| 74 | if args.pid: |
Brendan Gregg | 91734d3 | 2015-09-21 11:58:16 -0700 | [diff] [blame^] | 75 | bpf_text = bpf_text.replace('FILTER', |
Brendan Gregg | 3e55ae2 | 2015-09-10 12:11:35 -0700 | [diff] [blame] | 76 | ('u32 pid; pid = bpf_get_current_pid_tgid(); ' + |
Brendan Gregg | 91734d3 | 2015-09-21 11:58:16 -0700 | [diff] [blame^] | 77 | 'if (pid != %s) { return 0; }') % (args.pid)) |
Brendan Gregg | 3e55ae2 | 2015-09-10 12:11:35 -0700 | [diff] [blame] | 78 | else: |
Brendan Gregg | 91734d3 | 2015-09-21 11:58:16 -0700 | [diff] [blame^] | 79 | bpf_text = bpf_text.replace('FILTER', '') |
Brendan Gregg | 3e55ae2 | 2015-09-10 12:11:35 -0700 | [diff] [blame] | 80 | if debug: |
| 81 | print(bpf_text) |
| 82 | b = BPF(text=bpf_text) |
| 83 | b.attach_kprobe(event_re=pattern, fn_name="trace_count") |
| 84 | |
| 85 | # header |
| 86 | print("Tracing... Ctrl-C to end.") |
| 87 | |
| 88 | # output |
| 89 | exiting = 0 if args.interval else 1 |
| 90 | while (1): |
| 91 | try: |
| 92 | sleep(int(args.interval)) |
| 93 | except KeyboardInterrupt: |
| 94 | exiting=1 |
| 95 | # as cleanup can take many seconds, trap Ctrl-C: |
| 96 | signal.signal(signal.SIGINT, signal_ignore) |
| 97 | |
| 98 | print() |
| 99 | if args.timestamp: |
| 100 | print("%-8s\n" % strftime("%H:%M:%S"), end="") |
| 101 | |
| 102 | print("%-16s %-26s %8s" % ("ADDR", "FUNC", "COUNT")) |
| 103 | counts = b.get_table("counts") |
| 104 | for k, v in sorted(counts.items(), key=lambda counts: counts[1].value): |
| 105 | print("%-16x %-26s %8d" % (k.ip, b.ksym(k.ip), v.value)) |
| 106 | counts.clear() |
| 107 | |
| 108 | if exiting: |
| 109 | print("Detaching...") |
| 110 | exit() |