Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [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 | # |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 12 | # The pattern is a string with optional '*' wildcards, similar to file |
| 13 | # globbing. 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 | # |
chenyue.zhou | 0cf8166 | 2021-04-14 13:37:07 -0400 | [diff] [blame] | 15 | # Without the '-l' option, only the innermost calls will be recorded. |
| 16 | # Use '-l LEVEL' to record the outermost n levels of nested/recursive functions. |
| 17 | # |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 18 | # Copyright (c) 2015 Brendan Gregg. |
chenyue.zhou | 0cf8166 | 2021-04-14 13:37:07 -0400 | [diff] [blame] | 19 | # Copyright (c) 2021 Chenyue Zhou. |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 20 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 21 | # |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 22 | # 20-Sep-2015 Brendan Gregg Created this. |
| 23 | # 06-Oct-2016 Sasha Goldshtein Added user function support. |
chenyue.zhou | 0cf8166 | 2021-04-14 13:37:07 -0400 | [diff] [blame] | 24 | # 14-Apr-2021 Chenyue Zhou Added nested or recursive 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 |
Akilesh Kailash | 8996719 | 2018-05-18 13:36:54 -0700 | [diff] [blame] | 38 | ./funclatency -i 2 -d 10 c:open # output every 2 seconds, for duration 10s |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 39 | ./funclatency -mTi 5 vfs_read # output every 5 seconds, with timestamps |
| 40 | ./funclatency -p 181 vfs_read # time process 181 only |
| 41 | ./funclatency 'vfs_fstat*' # time both vfs_fstat() and vfs_fstatat() |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 42 | ./funclatency 'c:*printf' # time the *printf family of functions |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 43 | ./funclatency -F 'vfs_r*' # show one histogram per matched function |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 44 | """ |
| 45 | parser = argparse.ArgumentParser( |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 46 | description="Time functions and print latency as a histogram", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 47 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 48 | epilog=examples) |
htbegin | 5ac5d6e | 2017-05-24 22:53:17 +0800 | [diff] [blame] | 49 | parser.add_argument("-p", "--pid", type=int, |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 50 | help="trace this PID only") |
Akilesh Kailash | 8996719 | 2018-05-18 13:36:54 -0700 | [diff] [blame] | 51 | parser.add_argument("-i", "--interval", type=int, |
| 52 | help="summary interval, in seconds") |
| 53 | parser.add_argument("-d", "--duration", type=int, |
| 54 | help="total duration of trace, in seconds") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 55 | parser.add_argument("-T", "--timestamp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 56 | help="include timestamp on output") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 57 | parser.add_argument("-u", "--microseconds", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 58 | help="microsecond histogram") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 59 | parser.add_argument("-m", "--milliseconds", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 60 | help="millisecond histogram") |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 61 | parser.add_argument("-F", "--function", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 62 | help="show a separate histogram per function") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 63 | parser.add_argument("-r", "--regexp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 64 | help="use regular expressions. Default is \"*\" wildcards only.") |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 65 | parser.add_argument("-l", "--level", type=int, |
| 66 | help="set the level of nested or recursive functions") |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 67 | parser.add_argument("-v", "--verbose", action="store_true", |
| 68 | help="print the BPF program (for debugging purposes)") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 69 | parser.add_argument("pattern", |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 70 | help="search expression for functions") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 71 | parser.add_argument("--ebpf", action="store_true", |
| 72 | help=argparse.SUPPRESS) |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 73 | args = parser.parse_args() |
Akilesh Kailash | 8996719 | 2018-05-18 13:36:54 -0700 | [diff] [blame] | 74 | if args.duration and not args.interval: |
| 75 | args.interval = args.duration |
| 76 | if not args.interval: |
| 77 | args.interval = 99999999 |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 78 | |
| 79 | def bail(error): |
| 80 | print("Error: " + error) |
| 81 | exit(1) |
| 82 | |
| 83 | parts = args.pattern.split(':') |
| 84 | if len(parts) == 1: |
| 85 | library = None |
| 86 | pattern = args.pattern |
| 87 | elif len(parts) == 2: |
| 88 | library = parts[0] |
| 89 | libpath = BPF.find_library(library) or BPF.find_exe(library) |
| 90 | if not libpath: |
| 91 | bail("can't resolve library %s" % library) |
| 92 | library = libpath |
| 93 | pattern = parts[1] |
| 94 | else: |
| 95 | bail("unrecognized pattern format '%s'" % pattern) |
| 96 | |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 97 | if not args.regexp: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 98 | pattern = pattern.replace('*', '.*') |
| 99 | pattern = '^' + pattern + '$' |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 100 | |
| 101 | # define BPF program |
| 102 | bpf_text = """ |
| 103 | #include <uapi/linux/ptrace.h> |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 104 | |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 105 | typedef struct ip_pid { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 106 | u64 ip; |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 107 | u64 pid; |
| 108 | } ip_pid_t; |
| 109 | |
| 110 | typedef struct hist_key { |
| 111 | ip_pid_t key; |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 112 | u64 slot; |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 113 | } hist_key_t; |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 114 | |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 115 | TYPEDEF |
| 116 | |
Hao | af90bd4 | 2020-09-29 02:01:36 +0800 | [diff] [blame] | 117 | BPF_ARRAY(avg, u64, 2); |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 118 | STORAGE |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 119 | FUNCTION |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 120 | |
| 121 | int trace_func_entry(struct pt_regs *ctx) |
| 122 | { |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 123 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 124 | u32 pid = pid_tgid; |
| 125 | u32 tgid = pid_tgid >> 32; |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 126 | u64 ts = bpf_ktime_get_ns(); |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 127 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 128 | FILTER |
| 129 | ENTRYSTORE |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 130 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 131 | return 0; |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int trace_func_return(struct pt_regs *ctx) |
| 135 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 136 | u64 *tsp, delta; |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 137 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 138 | u32 pid = pid_tgid; |
| 139 | u32 tgid = pid_tgid >> 32; |
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 | // calculate delta time |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 142 | CALCULATE |
Hao | af90bd4 | 2020-09-29 02:01:36 +0800 | [diff] [blame] | 143 | |
| 144 | u32 lat = 0; |
| 145 | u32 cnt = 1; |
chenyuezhou | 08a2203 | 2021-06-21 10:07:30 -0400 | [diff] [blame] | 146 | avg.atomic_increment(lat, delta); |
| 147 | avg.atomic_increment(cnt); |
Hao | af90bd4 | 2020-09-29 02:01:36 +0800 | [diff] [blame] | 148 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 149 | FACTOR |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 150 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 151 | // store as histogram |
| 152 | STORE |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 153 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 154 | return 0; |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 155 | } |
| 156 | """ |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 157 | |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 158 | # do we need to store the IP and pid for each invocation? |
| 159 | need_key = args.function or (library and not args.pid) |
| 160 | |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 161 | # code substitutions |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 162 | if args.pid: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 163 | bpf_text = bpf_text.replace('FILTER', |
htbegin | 5ac5d6e | 2017-05-24 22:53:17 +0800 | [diff] [blame] | 164 | 'if (tgid != %d) { return 0; }' % args.pid) |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 165 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 166 | bpf_text = bpf_text.replace('FILTER', '') |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 167 | if args.milliseconds: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 168 | bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000000;') |
| 169 | label = "msecs" |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 170 | elif args.microseconds: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 171 | bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000;') |
| 172 | label = "usecs" |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 173 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 174 | bpf_text = bpf_text.replace('FACTOR', '') |
| 175 | label = "nsecs" |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 176 | if need_key: |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 177 | pid = '-1' if not library else 'tgid' |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 178 | |
chenyue.zhou | 0cf8166 | 2021-04-14 13:37:07 -0400 | [diff] [blame] | 179 | if args.level and args.level > 1: |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 180 | bpf_text = bpf_text.replace('TYPEDEF', |
| 181 | """ |
| 182 | #define STACK_DEPTH %s |
| 183 | |
| 184 | typedef struct { |
| 185 | u64 ip; |
| 186 | u64 start_ts; |
| 187 | } func_cache_t; |
| 188 | |
| 189 | /* LIFO */ |
| 190 | typedef struct { |
| 191 | u32 head; |
| 192 | func_cache_t cache[STACK_DEPTH]; |
| 193 | } func_stack_t; |
| 194 | """ % args.level) |
| 195 | |
| 196 | bpf_text = bpf_text.replace('STORAGE', |
| 197 | """ |
| 198 | BPF_HASH(func_stack, u32, func_stack_t); |
| 199 | BPF_HISTOGRAM(dist, hist_key_t); |
| 200 | """) |
| 201 | |
| 202 | bpf_text = bpf_text.replace('FUNCTION', |
| 203 | """ |
| 204 | static inline int stack_pop(func_stack_t *stack, func_cache_t *cache) { |
| 205 | if (stack->head <= 0) { |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | u32 index = --stack->head; |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 210 | if (index < STACK_DEPTH) { |
| 211 | /* bound check */ |
| 212 | cache->ip = stack->cache[index].ip; |
| 213 | cache->start_ts = stack->cache[index].start_ts; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static inline int stack_push(func_stack_t *stack, func_cache_t *cache) { |
| 220 | u32 index = stack->head; |
| 221 | |
| 222 | if (index > STACK_DEPTH - 1) { |
| 223 | /* bound check */ |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | stack->head++; |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 228 | stack->cache[index].ip = cache->ip; |
| 229 | stack->cache[index].start_ts = cache->start_ts; |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | """) |
| 234 | |
| 235 | bpf_text = bpf_text.replace('ENTRYSTORE', |
| 236 | """ |
| 237 | u64 ip = PT_REGS_IP(ctx); |
| 238 | func_cache_t cache = { |
| 239 | .ip = ip, |
| 240 | .start_ts = ts, |
| 241 | }; |
| 242 | |
| 243 | func_stack_t *stack = func_stack.lookup(&pid); |
| 244 | if (!stack) { |
| 245 | func_stack_t new_stack = { |
| 246 | .head = 0, |
| 247 | }; |
| 248 | |
| 249 | if (!stack_push(&new_stack, &cache)) { |
| 250 | func_stack.update(&pid, &new_stack); |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | if (!stack_push(stack, &cache)) { |
| 257 | func_stack.update(&pid, stack); |
| 258 | } |
| 259 | """) |
| 260 | |
| 261 | bpf_text = bpf_text.replace('CALCULATE', |
| 262 | """ |
| 263 | u64 ip, start_ts; |
| 264 | func_stack_t *stack = func_stack.lookup(&pid); |
| 265 | if (!stack) { |
| 266 | /* miss start */ |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | func_cache_t cache = {}; |
| 271 | if (stack_pop(stack, &cache)) { |
| 272 | func_stack.delete(&pid); |
| 273 | |
| 274 | return 0; |
| 275 | } |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 276 | ip = cache.ip; |
| 277 | start_ts = cache.start_ts; |
| 278 | delta = bpf_ktime_get_ns() - start_ts; |
| 279 | """) |
| 280 | |
| 281 | bpf_text = bpf_text.replace('STORE', |
| 282 | """ |
| 283 | hist_key_t key; |
| 284 | key.key.ip = ip; |
| 285 | key.key.pid = %s; |
| 286 | key.slot = bpf_log2l(delta); |
zcy | 80242fb | 2021-07-02 00:12:32 +0800 | [diff] [blame] | 287 | dist.atomic_increment(key); |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 288 | |
| 289 | if (stack->head == 0) { |
| 290 | /* empty */ |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 291 | func_stack.delete(&pid); |
| 292 | } |
| 293 | """ % pid) |
| 294 | |
| 295 | else: |
| 296 | bpf_text = bpf_text.replace('STORAGE', 'BPF_HASH(ipaddr, u32);\n'\ |
| 297 | 'BPF_HISTOGRAM(dist, hist_key_t);\n'\ |
| 298 | 'BPF_HASH(start, u32);') |
| 299 | # stash the IP on entry, as on return it's kretprobe_trampoline: |
| 300 | bpf_text = bpf_text.replace('ENTRYSTORE', |
| 301 | 'u64 ip = PT_REGS_IP(ctx); ipaddr.update(&pid, &ip);'\ |
| 302 | ' start.update(&pid, &ts);') |
| 303 | bpf_text = bpf_text.replace('STORE', |
| 304 | """ |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 305 | u64 ip, *ipp = ipaddr.lookup(&pid); |
| 306 | if (ipp) { |
| 307 | ip = *ipp; |
| 308 | hist_key_t key; |
| 309 | key.key.ip = ip; |
| 310 | key.key.pid = %s; |
| 311 | key.slot = bpf_log2l(delta); |
zcy | 80242fb | 2021-07-02 00:12:32 +0800 | [diff] [blame] | 312 | dist.atomic_increment(key); |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 313 | ipaddr.delete(&pid); |
| 314 | } |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 315 | """ % pid) |
Brendan Gregg | 50bbca4 | 2015-09-25 12:47:53 -0700 | [diff] [blame] | 316 | else: |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 317 | bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);\n'\ |
| 318 | 'BPF_HASH(start, u32);') |
| 319 | bpf_text = bpf_text.replace('ENTRYSTORE', 'start.update(&pid, &ts);') |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 320 | bpf_text = bpf_text.replace('STORE', |
zcy | 80242fb | 2021-07-02 00:12:32 +0800 | [diff] [blame] | 321 | 'dist.atomic_increment(bpf_log2l(delta));') |
chenyue.zhou | c76cd92 | 2021-04-06 16:55:48 -0400 | [diff] [blame] | 322 | |
| 323 | bpf_text = bpf_text.replace('TYPEDEF', '') |
| 324 | bpf_text = bpf_text.replace('FUNCTION', '') |
| 325 | bpf_text = bpf_text.replace('CALCULATE', |
| 326 | """ |
| 327 | tsp = start.lookup(&pid); |
| 328 | if (tsp == 0) { |
| 329 | return 0; // missed start |
| 330 | } |
| 331 | delta = bpf_ktime_get_ns() - *tsp; |
| 332 | start.delete(&pid); |
| 333 | """) |
| 334 | |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 335 | if args.verbose or args.ebpf: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 336 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 337 | if args.ebpf: |
| 338 | exit() |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 339 | |
| 340 | # signal handler |
| 341 | def signal_ignore(signal, frame): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 342 | print() |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 343 | |
| 344 | # load BPF program |
| 345 | b = BPF(text=bpf_text) |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 346 | |
| 347 | # attach probes |
| 348 | if not library: |
| 349 | b.attach_kprobe(event_re=pattern, fn_name="trace_func_entry") |
| 350 | b.attach_kretprobe(event_re=pattern, fn_name="trace_func_return") |
| 351 | matched = b.num_open_kprobes() |
| 352 | else: |
Paul Chaignon | d73c58f | 2017-01-21 14:25:41 +0100 | [diff] [blame] | 353 | b.attach_uprobe(name=library, sym_re=pattern, fn_name="trace_func_entry", |
| 354 | pid=args.pid or -1) |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 355 | b.attach_uretprobe(name=library, sym_re=pattern, |
Paul Chaignon | d73c58f | 2017-01-21 14:25:41 +0100 | [diff] [blame] | 356 | fn_name="trace_func_return", pid=args.pid or -1) |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 357 | matched = b.num_open_uprobes() |
| 358 | |
Brendan Gregg | 6b8add0 | 2015-09-25 11:16:33 -0700 | [diff] [blame] | 359 | if matched == 0: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 360 | print("0 functions matched by \"%s\". Exiting." % args.pattern) |
| 361 | exit() |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 362 | |
| 363 | # header |
Brendan Gregg | 6b8add0 | 2015-09-25 11:16:33 -0700 | [diff] [blame] | 364 | print("Tracing %d functions for \"%s\"... Hit Ctrl-C to end." % |
| 365 | (matched / 2, args.pattern)) |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 366 | |
| 367 | # output |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 368 | def print_section(key): |
| 369 | if not library: |
| 370 | return BPF.sym(key[0], -1) |
| 371 | else: |
| 372 | return "%s [%d]" % (BPF.sym(key[0], key[1]), key[1]) |
| 373 | |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 374 | exiting = 0 if args.interval else 1 |
Akilesh Kailash | 8996719 | 2018-05-18 13:36:54 -0700 | [diff] [blame] | 375 | seconds = 0 |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 376 | dist = b.get_table("dist") |
| 377 | while (1): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 378 | try: |
Akilesh Kailash | 8996719 | 2018-05-18 13:36:54 -0700 | [diff] [blame] | 379 | sleep(args.interval) |
| 380 | seconds += args.interval |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 381 | except KeyboardInterrupt: |
| 382 | exiting = 1 |
| 383 | # as cleanup can take many seconds, trap Ctrl-C: |
| 384 | signal.signal(signal.SIGINT, signal_ignore) |
Akilesh Kailash | 8996719 | 2018-05-18 13:36:54 -0700 | [diff] [blame] | 385 | if args.duration and seconds >= args.duration: |
| 386 | exiting = 1 |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 387 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 388 | print() |
| 389 | if args.timestamp: |
| 390 | print("%-8s\n" % strftime("%H:%M:%S"), end="") |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 391 | |
Sasha Goldshtein | a466c46 | 2016-10-06 21:17:59 +0300 | [diff] [blame] | 392 | if need_key: |
| 393 | dist.print_log2_hist(label, "Function", section_print_fn=print_section, |
| 394 | bucket_fn=lambda k: (k.ip, k.pid)) |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 395 | else: |
| 396 | dist.print_log2_hist(label) |
Brendan Gregg | 74016c3 | 2015-09-21 15:49:21 -0700 | [diff] [blame] | 397 | |
Hao | af90bd4 | 2020-09-29 02:01:36 +0800 | [diff] [blame] | 398 | total = b['avg'][0].value |
| 399 | counts = b['avg'][1].value |
| 400 | if counts > 0: |
| 401 | if label == 'msecs': |
| 402 | total /= 1000000 |
| 403 | elif label == 'usecs': |
| 404 | total /= 1000 |
| 405 | avg = total/counts |
| 406 | print("\navg = %ld %s, total: %ld %s, count: %ld\n" %(total/counts, label, total, label, counts)) |
| 407 | |
netedwardwu | e0c8c10 | 2021-04-28 12:53:26 +0800 | [diff] [blame] | 408 | dist.clear() |
| 409 | b['avg'].clear() |
| 410 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 411 | if exiting: |
| 412 | print("Detaching...") |
| 413 | exit() |