Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -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 | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 3 | # |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 4 | # funclatency Time functions and print latency as a histogram. |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 5 | # For Linux, uses BCC, eBPF. |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 6 | # |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 7 | # USAGE: funclatency [-h] [-p PID] [-i INTERVAL] [-T] [-u] [-m] [-F] [-r] [-v] |
| 8 | # pattern |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 9 | # |
| 10 | # Run "funclatency -h" for full usage. |
| 11 | # |
| 12 | # The pattern is a string with optional '*' wildcards, similar to file globbing. |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 13 | # If you'd prefer to use regular expressions, use the -r option. |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 14 | # |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 15 | # Currently nested or recursive functions are not supported properly, and |
| 16 | # timestamps will be overwritten, creating dubious output. Try to match single |
| 17 | # functions, or groups of functions that run at the same stack layer, and |
| 18 | # don't ultimately call each other. |
| 19 | # |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 20 | # Copyright (c) 2015 Brendan Gregg. |
| 21 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 22 | # |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 23 | # 20-Sep-2015 Brendan Gregg Created this. |
| 24 | # 06-Oct-2016 Sasha Goldshtein Added user function support. |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 25 | |
| 26 | from __future__ import print_function |
| 27 | from bcc import BPF |
| 28 | from time import sleep, strftime |
| 29 | import argparse |
| 30 | import signal |
| 31 | |
| 32 | # arguments |
| 33 | examples = """examples: |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 34 | ./funclatency do_sys_open # time the do_sys_open() kernel function |
| 35 | ./funclatency c:read # time the read() C library function |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 36 | ./funclatency -u vfs_read # time vfs_read(), in microseconds |
| 37 | ./funclatency -m do_nanosleep # time do_nanosleep(), in milliseconds |
| 38 | ./funclatency -mTi 5 vfs_read # output every 5 seconds, with timestamps |
| 39 | ./funclatency -p 181 vfs_read # time process 181 only |
| 40 | ./funclatency 'vfs_fstat*' # time both vfs_fstat() and vfs_fstatat() |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 41 | ./funclatency 'c:*printf' # time the *printf family of functions |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 42 | ./funclatency -F 'vfs_r*' # show one histogram per matched function |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 43 | """ |
| 44 | parser = argparse.ArgumentParser( |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 45 | description="Time functions and print latency as a histogram", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 46 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 47 | epilog=examples) |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 48 | parser.add_argument("-p", "--pid", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 49 | help="trace this PID only") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 50 | parser.add_argument("-i", "--interval", default=99999999, |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 51 | help="summary interval, seconds") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 52 | parser.add_argument("-T", "--timestamp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 53 | help="include timestamp on output") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 54 | parser.add_argument("-u", "--microseconds", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 55 | help="microsecond histogram") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 56 | parser.add_argument("-m", "--milliseconds", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 57 | help="millisecond histogram") |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 58 | parser.add_argument("-F", "--function", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 59 | help="show a separate histogram per function") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 60 | parser.add_argument("-r", "--regexp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 61 | help="use regular expressions. Default is \"*\" wildcards only.") |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 62 | parser.add_argument("-v", "--verbose", action="store_true", |
| 63 | help="print the BPF program (for debugging purposes)") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 64 | parser.add_argument("pattern", |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 65 | help="search expression for functions") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 66 | args = parser.parse_args() |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 67 | |
| 68 | def bail(error): |
| 69 | print("Error: " + error) |
| 70 | exit(1) |
| 71 | |
| 72 | parts = args.pattern.split(':') |
| 73 | if len(parts) == 1: |
| 74 | library = None |
| 75 | pattern = args.pattern |
| 76 | elif len(parts) == 2: |
| 77 | library = parts[0] |
| 78 | libpath = BPF.find_library(library) or BPF.find_exe(library) |
| 79 | if not libpath: |
| 80 | bail("can't resolve library %s" % library) |
| 81 | library = libpath |
| 82 | pattern = parts[1] |
| 83 | else: |
| 84 | bail("unrecognized pattern format '%s'" % pattern) |
| 85 | |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 86 | if not args.regexp: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 87 | pattern = pattern.replace('*', '.*') |
| 88 | pattern = '^' + pattern + '$' |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 89 | |
| 90 | # define BPF program |
| 91 | bpf_text = """ |
| 92 | #include <uapi/linux/ptrace.h> |
| 93 | #include <linux/blkdev.h> |
| 94 | |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 95 | typedef struct ip_pid { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 96 | u64 ip; |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 97 | u64 pid; |
| 98 | } ip_pid_t; |
| 99 | |
| 100 | typedef struct hist_key { |
| 101 | ip_pid_t key; |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 102 | u64 slot; |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 103 | } hist_key_t; |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 104 | |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 105 | BPF_HASH(start, u32); |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 106 | STORAGE |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 107 | |
| 108 | int trace_func_entry(struct pt_regs *ctx) |
| 109 | { |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 110 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 111 | u32 pid = pid_tgid; |
| 112 | u32 tgid = pid_tgid >> 32; |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 113 | u64 ts = bpf_ktime_get_ns(); |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 114 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 115 | FILTER |
| 116 | ENTRYSTORE |
| 117 | start.update(&pid, &ts); |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 118 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 119 | return 0; |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | int trace_func_return(struct pt_regs *ctx) |
| 123 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 124 | u64 *tsp, delta; |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 125 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 126 | u32 pid = pid_tgid; |
| 127 | u32 tgid = pid_tgid >> 32; |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 128 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 129 | // calculate delta time |
| 130 | tsp = start.lookup(&pid); |
| 131 | if (tsp == 0) { |
| 132 | return 0; // missed start |
| 133 | } |
| 134 | delta = bpf_ktime_get_ns() - *tsp; |
| 135 | start.delete(&pid); |
| 136 | FACTOR |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 137 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 138 | // store as histogram |
| 139 | STORE |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 140 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 141 | return 0; |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 142 | } |
| 143 | """ |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 144 | |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 145 | # do we need to store the IP and pid for each invocation? |
| 146 | need_key = args.function or (library and not args.pid) |
| 147 | |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 148 | # code substitutions |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 149 | if args.pid: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 150 | bpf_text = bpf_text.replace('FILTER', |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 151 | 'if (tgid != %s) { return 0; }' % args.pid) |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 152 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 153 | bpf_text = bpf_text.replace('FILTER', '') |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 154 | if args.milliseconds: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 155 | bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000000;') |
| 156 | label = "msecs" |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 157 | elif args.microseconds: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 158 | bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000;') |
| 159 | label = "usecs" |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 160 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 161 | bpf_text = bpf_text.replace('FACTOR', '') |
| 162 | label = "nsecs" |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 163 | if need_key: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 164 | bpf_text = bpf_text.replace('STORAGE', 'BPF_HASH(ipaddr, u32);\n' + |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 165 | 'BPF_HISTOGRAM(dist, hist_key_t);') |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 166 | # stash the IP on entry, as on return it's kretprobe_trampoline: |
| 167 | bpf_text = bpf_text.replace('ENTRYSTORE', |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 168 | 'u64 ip = PT_REGS_IP(ctx); ipaddr.update(&pid, &ip);') |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 169 | pid = '-1' if not library else 'tgid' |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 170 | bpf_text = bpf_text.replace('STORE', |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 171 | """ |
| 172 | u64 ip, *ipp = ipaddr.lookup(&pid); |
| 173 | if (ipp) { |
| 174 | ip = *ipp; |
| 175 | hist_key_t key; |
| 176 | key.key.ip = ip; |
| 177 | key.key.pid = %s; |
| 178 | key.slot = bpf_log2l(delta); |
| 179 | dist.increment(key); |
| 180 | ipaddr.delete(&pid); |
| 181 | } |
| 182 | """ % pid) |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 183 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 184 | bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);') |
| 185 | bpf_text = bpf_text.replace('ENTRYSTORE', '') |
| 186 | bpf_text = bpf_text.replace('STORE', |
| 187 | 'dist.increment(bpf_log2l(delta));') |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 188 | if args.verbose: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 189 | print(bpf_text) |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 190 | |
| 191 | # signal handler |
| 192 | def signal_ignore(signal, frame): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 193 | print() |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 194 | |
| 195 | # load BPF program |
| 196 | b = BPF(text=bpf_text) |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 197 | |
| 198 | # attach probes |
| 199 | if not library: |
| 200 | b.attach_kprobe(event_re=pattern, fn_name="trace_func_entry") |
| 201 | b.attach_kretprobe(event_re=pattern, fn_name="trace_func_return") |
| 202 | matched = b.num_open_kprobes() |
| 203 | else: |
| 204 | b.attach_uprobe(name=library, sym_re=pattern, fn_name="trace_func_entry") |
| 205 | b.attach_uretprobe(name=library, sym_re=pattern, fn_name="trace_func_return") |
| 206 | matched = b.num_open_uprobes() |
| 207 | |
Brendan Gregg | 6b8add0 | 2015-09-25 11:16:33 -0700 | [diff] [blame] | 208 | if matched == 0: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 209 | print("0 functions matched by \"%s\". Exiting." % args.pattern) |
| 210 | exit() |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 211 | |
| 212 | # header |
Brendan Gregg | 6b8add0 | 2015-09-25 11:16:33 -0700 | [diff] [blame] | 213 | print("Tracing %d functions for \"%s\"... Hit Ctrl-C to end." % |
| 214 | (matched / 2, args.pattern)) |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 215 | |
| 216 | # output |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 217 | def print_section(key): |
| 218 | if not library: |
| 219 | return BPF.sym(key[0], -1) |
| 220 | else: |
| 221 | return "%s [%d]" % (BPF.sym(key[0], key[1]), key[1]) |
| 222 | |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 223 | exiting = 0 if args.interval else 1 |
| 224 | dist = b.get_table("dist") |
| 225 | while (1): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 226 | try: |
| 227 | sleep(int(args.interval)) |
| 228 | except KeyboardInterrupt: |
| 229 | exiting = 1 |
| 230 | # as cleanup can take many seconds, trap Ctrl-C: |
| 231 | signal.signal(signal.SIGINT, signal_ignore) |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 232 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 233 | print() |
| 234 | if args.timestamp: |
| 235 | print("%-8s\n" % strftime("%H:%M:%S"), end="") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 236 | |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame^] | 237 | if need_key: |
| 238 | dist.print_log2_hist(label, "Function", section_print_fn=print_section, |
| 239 | bucket_fn=lambda k: (k.ip, k.pid)) |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 240 | else: |
| 241 | dist.print_log2_hist(label) |
| 242 | dist.clear() |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 243 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 244 | if exiting: |
| 245 | print("Detaching...") |
| 246 | exit() |