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