Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # funclatency Time kernel funcitons and print latency as a histogram. |
| 4 | # For Linux, uses BCC, eBPF. |
| 5 | # |
| 6 | # USAGE: funclatency [-h] [-p PID] [-i INTERVAL] [-T] [-u] [-m] [-r] pattern |
| 7 | # |
| 8 | # Run "funclatency -h" for full usage. |
| 9 | # |
| 10 | # The pattern is a string with optional '*' wildcards, similar to file globbing. |
| 11 | # If you'd prefer to use regular expressions, use the -r option. Matching |
| 12 | # multiple functions is of limited use, since the output has one histogram for |
| 13 | # everything. Future versions should split the output histogram by the function. |
| 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 | # |
| 23 | # 20-Sep-2015 Brendan Gregg Created this. |
| 24 | |
| 25 | from __future__ import print_function |
| 26 | from bcc import BPF |
| 27 | from time import sleep, strftime |
| 28 | import argparse |
| 29 | import signal |
| 30 | |
| 31 | # arguments |
| 32 | examples = """examples: |
| 33 | ./funclatency do_sys_open # time the do_sys_open() kenel function |
| 34 | ./funclatency -u vfs_read # time vfs_read(), in microseconds |
| 35 | ./funclatency -m do_nanosleep # time do_nanosleep(), in milliseconds |
| 36 | ./funclatency -mTi 5 vfs_read # output every 5 seconds, with timestamps |
| 37 | ./funclatency -p 181 vfs_read # time process 181 only |
| 38 | ./funclatency 'vfs_fstat*' # time both vfs_fstat() and vfs_fstatat() |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame^] | 39 | ./funclatency -F 'vfs_r*' # show one histogram per matched function |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 40 | """ |
| 41 | parser = argparse.ArgumentParser( |
| 42 | description="Time kernel funcitons and print latency as a histogram", |
| 43 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 44 | epilog=examples) |
| 45 | parser.add_argument("-p", "--pid", |
| 46 | help="trace this PID only") |
| 47 | parser.add_argument("-i", "--interval", default=99999999, |
| 48 | help="summary interval, seconds") |
| 49 | parser.add_argument("-T", "--timestamp", action="store_true", |
| 50 | help="include timestamp on output") |
| 51 | parser.add_argument("-u", "--microseconds", action="store_true", |
| 52 | help="microsecond histogram") |
| 53 | parser.add_argument("-m", "--milliseconds", action="store_true", |
| 54 | help="millisecond histogram") |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame^] | 55 | parser.add_argument("-F", "--function", action="store_true", |
| 56 | help="show a separate histogram per function") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 57 | parser.add_argument("-r", "--regexp", action="store_true", |
| 58 | help="use regular expressions. Default is \"*\" wildcards only.") |
| 59 | parser.add_argument("pattern", |
| 60 | help="search expression for kernel functions") |
| 61 | args = parser.parse_args() |
| 62 | pattern = args.pattern |
| 63 | if not args.regexp: |
| 64 | pattern = pattern.replace('*', '.*') |
| 65 | pattern = '^' + pattern + '$' |
| 66 | debug = 0 |
| 67 | |
| 68 | # define BPF program |
| 69 | bpf_text = """ |
| 70 | #include <uapi/linux/ptrace.h> |
| 71 | #include <linux/blkdev.h> |
| 72 | |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame^] | 73 | typedef struct ip_key { |
| 74 | u64 ip; |
| 75 | u64 slot; |
| 76 | } ip_key_t; |
| 77 | |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 78 | BPF_HASH(start, u32); |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame^] | 79 | STORAGE |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 80 | |
| 81 | int trace_func_entry(struct pt_regs *ctx) |
| 82 | { |
| 83 | u32 pid = bpf_get_current_pid_tgid(); |
| 84 | u64 ts = bpf_ktime_get_ns(); |
| 85 | |
| 86 | FILTER |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame^] | 87 | ENTRYSTORE |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 88 | start.update(&pid, &ts); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | int trace_func_return(struct pt_regs *ctx) |
| 94 | { |
| 95 | u64 *tsp, delta; |
| 96 | u32 pid = bpf_get_current_pid_tgid(); |
| 97 | |
| 98 | // calculate delta time |
| 99 | tsp = start.lookup(&pid); |
| 100 | if (tsp == 0) { |
| 101 | return 0; // missed start |
| 102 | } |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 103 | delta = bpf_ktime_get_ns() - *tsp; |
Brendan Gregg | 3736885 | 2015-09-25 11:17:00 -0700 | [diff] [blame] | 104 | start.delete(&pid); |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 105 | FACTOR |
| 106 | |
| 107 | // store as histogram |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame^] | 108 | STORE |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | """ |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame^] | 113 | |
| 114 | # code substitutions |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 115 | if args.pid: |
| 116 | bpf_text = bpf_text.replace('FILTER', |
| 117 | 'if (pid != %s) { return 0; }' % args.pid) |
| 118 | else: |
| 119 | bpf_text = bpf_text.replace('FILTER', '') |
| 120 | if args.milliseconds: |
| 121 | bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000000;') |
| 122 | label = "msecs" |
| 123 | elif args.microseconds: |
| 124 | bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000;') |
| 125 | label = "usecs" |
| 126 | else: |
| 127 | bpf_text = bpf_text.replace('FACTOR', '') |
| 128 | label = "nsecs" |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame^] | 129 | if args.function: |
| 130 | bpf_text = bpf_text.replace('STORAGE', 'BPF_HASH(ipaddr, u32);\n' + |
| 131 | 'BPF_HISTOGRAM(dist, ip_key_t);') |
| 132 | # stash the IP on entry, as on return it's kretprobe_trampoline: |
| 133 | bpf_text = bpf_text.replace('ENTRYSTORE', |
| 134 | 'u64 ip = ctx->ip; ipaddr.update(&pid, &ip);') |
| 135 | bpf_text = bpf_text.replace('STORE', |
| 136 | 'u64 ip, *ipp = ipaddr.lookup(&pid); if (ipp) { ip = *ipp; ' + |
| 137 | 'dist.increment((ip_key_t){ip, bpf_log2l(delta)}); ' + |
| 138 | 'ipaddr.delete(&pid); }') |
| 139 | else: |
| 140 | bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);') |
| 141 | bpf_text = bpf_text.replace('ENTRYSTORE', '') |
| 142 | bpf_text = bpf_text.replace('STORE', |
| 143 | 'dist.increment(bpf_log2l(delta));') |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 144 | if debug: |
| 145 | print(bpf_text) |
| 146 | |
| 147 | # signal handler |
| 148 | def signal_ignore(signal, frame): |
| 149 | print() |
| 150 | |
| 151 | # load BPF program |
| 152 | b = BPF(text=bpf_text) |
| 153 | b.attach_kprobe(event_re=pattern, fn_name="trace_func_entry") |
| 154 | b.attach_kretprobe(event_re=pattern, fn_name="trace_func_return") |
Brendan Gregg | 6b8add0 | 2015-09-25 11:16:33 -0700 | [diff] [blame] | 155 | matched = b.num_open_kprobes() |
| 156 | if matched == 0: |
| 157 | print("0 functions matched by \"%s\". Exiting." % args.pattern) |
| 158 | exit() |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 159 | |
| 160 | # header |
Brendan Gregg | 6b8add0 | 2015-09-25 11:16:33 -0700 | [diff] [blame] | 161 | print("Tracing %d functions for \"%s\"... Hit Ctrl-C to end." % |
| 162 | (matched / 2, args.pattern)) |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 163 | |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame^] | 164 | # custom output (from __init__.py) |
| 165 | def print_log2_hist_byfunc(self, val_type="value"): |
| 166 | tmp = {} |
| 167 | f1 = self.Key._fields_[0][0] |
| 168 | f2 = self.Key._fields_[1][0] |
| 169 | for k, v in self.items(): |
| 170 | bucket = getattr(k, f1) |
| 171 | vals = tmp[bucket] = tmp.get(bucket, [0] * 65) |
| 172 | slot = getattr(k, f2) |
| 173 | vals[slot] = v.value |
| 174 | for bucket, vals in tmp.items(): |
| 175 | print("\nFunction = %s" % BPF.ksym(bucket)) |
| 176 | self._print_log2_hist(vals, val_type, 0) |
| 177 | |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 178 | # output |
| 179 | exiting = 0 if args.interval else 1 |
| 180 | dist = b.get_table("dist") |
| 181 | while (1): |
| 182 | try: |
| 183 | sleep(int(args.interval)) |
| 184 | except KeyboardInterrupt: |
| 185 | exiting=1 |
| 186 | # as cleanup can take many seconds, trap Ctrl-C: |
| 187 | signal.signal(signal.SIGINT, signal_ignore) |
| 188 | |
| 189 | print() |
| 190 | if args.timestamp: |
| 191 | print("%-8s\n" % strftime("%H:%M:%S"), end="") |
| 192 | |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame^] | 193 | if args.function: |
| 194 | print_log2_hist_byfunc(dist, label) |
| 195 | else: |
| 196 | dist.print_log2_hist(label) |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 197 | dist.clear() |
| 198 | |
| 199 | if exiting: |
| 200 | print("Detaching...") |
| 201 | exit() |