Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # profile Profile CPU usage by sampling stack traces at a timed interval. |
| 5 | # For Linux, uses BCC, BPF, perf_events. Embedded C. |
| 6 | # |
| 7 | # This is an efficient profiler, as stack traces are frequency counted in |
| 8 | # kernel context, rather than passing every stack to user space for frequency |
| 9 | # counting there. Only the unique stacks and counts are passed to user space |
| 10 | # at the end of the profile, greatly reducing the kernel<->user transfer. |
| 11 | # |
| 12 | # This uses perf_event_open to setup a timer which is instrumented by BPF, |
| 13 | # and for efficiency it does not initialize the perf ring buffer, so the |
| 14 | # redundant perf samples are not collected. |
| 15 | # |
Brendan Gregg | 715f7e6 | 2016-10-20 22:50:08 -0700 | [diff] [blame] | 16 | # REQUIRES: Linux 4.9+ (BPF_PROG_TYPE_PERF_EVENT support). Under tools/old is |
| 17 | # a version of this tool that may work on Linux 4.6 - 4.8. |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 18 | # |
| 19 | # Copyright 2016 Netflix, Inc. |
| 20 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 21 | # |
Brendan Gregg | 715f7e6 | 2016-10-20 22:50:08 -0700 | [diff] [blame] | 22 | # THANKS: Alexei Starovoitov, who added proper BPF profiling support to Linux; |
| 23 | # Sasha Goldshtein, Andrew Birchall, and Evgeny Vereshchagin, who wrote much |
| 24 | # of the code here, borrowed from tracepoint.py and offcputime.py; and |
| 25 | # Teng Qin, who added perf support in bcc. |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 26 | # |
| 27 | # 15-Jul-2016 Brendan Gregg Created this. |
Brendan Gregg | 715f7e6 | 2016-10-20 22:50:08 -0700 | [diff] [blame] | 28 | # 20-Oct-2016 " " Switched to use the new 4.9 support. |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 29 | |
| 30 | from __future__ import print_function |
Brendan Gregg | 715f7e6 | 2016-10-20 22:50:08 -0700 | [diff] [blame] | 31 | from bcc import BPF, PerfType, PerfSWConfig |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 32 | from sys import stderr |
| 33 | from time import sleep |
| 34 | import argparse |
| 35 | import signal |
| 36 | import os |
| 37 | import errno |
| 38 | import multiprocessing |
| 39 | import ctypes as ct |
| 40 | |
| 41 | # |
| 42 | # Process Arguments |
| 43 | # |
| 44 | |
| 45 | # arg validation |
| 46 | def positive_int(val): |
| 47 | try: |
| 48 | ival = int(val) |
| 49 | except ValueError: |
| 50 | raise argparse.ArgumentTypeError("must be an integer") |
| 51 | |
| 52 | if ival < 0: |
| 53 | raise argparse.ArgumentTypeError("must be positive") |
| 54 | return ival |
| 55 | |
| 56 | def positive_nonzero_int(val): |
| 57 | ival = positive_int(val) |
| 58 | if ival == 0: |
| 59 | raise argparse.ArgumentTypeError("must be nonzero") |
| 60 | return ival |
| 61 | |
Teng Qin | e4db768 | 2018-04-24 16:24:20 -0700 | [diff] [blame] | 62 | def stack_id_err(stack_id): |
| 63 | # -EFAULT in get_stackid normally means the stack-trace is not availible, |
| 64 | # Such as getting kernel stack trace in userspace code |
| 65 | return (stack_id < 0) and (stack_id != -errno.EFAULT) |
| 66 | |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 67 | # arguments |
| 68 | examples = """examples: |
| 69 | ./profile # profile stack traces at 49 Hertz until Ctrl-C |
| 70 | ./profile -F 99 # profile stack traces at 99 Hertz |
Teng Qin | 86df2b8 | 2018-04-23 12:36:51 -0700 | [diff] [blame] | 71 | ./profile -c 1000000 # profile stack traces every 1 in a million events |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 72 | ./profile 5 # profile at 49 Hertz for 5 seconds only |
| 73 | ./profile -f 5 # output in folded format for flame graphs |
| 74 | ./profile -p 185 # only profile threads for PID 185 |
| 75 | ./profile -U # only show user space stacks (no kernel) |
| 76 | ./profile -K # only show kernel space stacks (no user) |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 77 | """ |
| 78 | parser = argparse.ArgumentParser( |
| 79 | description="Profile CPU stack traces at a timed interval", |
| 80 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 81 | epilog=examples) |
| 82 | thread_group = parser.add_mutually_exclusive_group() |
| 83 | thread_group.add_argument("-p", "--pid", type=positive_int, |
| 84 | help="profile this PID only") |
| 85 | # TODO: add options for user/kernel threads only |
| 86 | stack_group = parser.add_mutually_exclusive_group() |
| 87 | stack_group.add_argument("-U", "--user-stacks-only", action="store_true", |
| 88 | help="show stacks from user space only (no kernel space stacks)") |
| 89 | stack_group.add_argument("-K", "--kernel-stacks-only", action="store_true", |
| 90 | help="show stacks from kernel space only (no user space stacks)") |
Teng Qin | 86df2b8 | 2018-04-23 12:36:51 -0700 | [diff] [blame] | 91 | sample_group = parser.add_mutually_exclusive_group() |
| 92 | sample_group.add_argument("-F", "--frequency", type=positive_int, |
| 93 | help="sample frequency, Hertz") |
| 94 | sample_group.add_argument("-c", "--count", type=positive_int, |
| 95 | help="sample period, number of events") |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 96 | parser.add_argument("-d", "--delimited", action="store_true", |
| 97 | help="insert delimiter between kernel/user stacks") |
| 98 | parser.add_argument("-a", "--annotations", action="store_true", |
| 99 | help="add _[k] annotations to kernel frames") |
| 100 | parser.add_argument("-f", "--folded", action="store_true", |
| 101 | help="output folded format, one line per stack (for flame graphs)") |
Tommaso Sardelli | aa4aa52 | 2018-04-30 22:35:29 +0200 | [diff] [blame] | 102 | parser.add_argument("--stack-storage-size", default=16384, |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 103 | type=positive_nonzero_int, |
| 104 | help="the number of unique stack traces that can be stored and " |
Tommaso Sardelli | 718c928 | 2018-04-30 15:36:40 +0200 | [diff] [blame] | 105 | "displayed (default %(default)s)") |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 106 | parser.add_argument("duration", nargs="?", default=99999999, |
| 107 | type=positive_nonzero_int, |
| 108 | help="duration of trace, in seconds") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 109 | parser.add_argument("--ebpf", action="store_true", |
| 110 | help=argparse.SUPPRESS) |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 111 | |
| 112 | # option logic |
| 113 | args = parser.parse_args() |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 114 | pid = int(args.pid) if args.pid is not None else -1 |
| 115 | duration = int(args.duration) |
| 116 | debug = 0 |
| 117 | need_delimiter = args.delimited and not (args.kernel_stacks_only or |
| 118 | args.user_stacks_only) |
| 119 | # TODO: add stack depth, and interval |
| 120 | |
| 121 | # |
| 122 | # Setup BPF |
| 123 | # |
| 124 | |
| 125 | # define BPF program |
| 126 | bpf_text = """ |
| 127 | #include <uapi/linux/ptrace.h> |
Brendan Gregg | 715f7e6 | 2016-10-20 22:50:08 -0700 | [diff] [blame] | 128 | #include <uapi/linux/bpf_perf_event.h> |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 129 | #include <linux/sched.h> |
| 130 | |
| 131 | struct key_t { |
| 132 | u32 pid; |
| 133 | u64 kernel_ip; |
| 134 | u64 kernel_ret_ip; |
| 135 | int user_stack_id; |
| 136 | int kernel_stack_id; |
| 137 | char name[TASK_COMM_LEN]; |
| 138 | }; |
| 139 | BPF_HASH(counts, struct key_t); |
Song Liu | 67ae605 | 2018-02-01 14:59:24 -0800 | [diff] [blame] | 140 | BPF_STACK_TRACE(stack_traces, STACK_STORAGE_SIZE); |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 141 | |
| 142 | // This code gets a bit complex. Probably not suitable for casual hacking. |
| 143 | |
Brendan Gregg | 715f7e6 | 2016-10-20 22:50:08 -0700 | [diff] [blame] | 144 | int do_perf_event(struct bpf_perf_event_data *ctx) { |
Brendan Gregg | 4c9f660 | 2016-11-30 20:26:26 -0800 | [diff] [blame] | 145 | u32 pid = bpf_get_current_pid_tgid() >> 32; |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 146 | if (!(THREAD_FILTER)) |
| 147 | return 0; |
| 148 | |
| 149 | // create map key |
| 150 | u64 zero = 0, *val; |
| 151 | struct key_t key = {.pid = pid}; |
| 152 | bpf_get_current_comm(&key.name, sizeof(key.name)); |
| 153 | |
| 154 | // get stacks |
| 155 | key.user_stack_id = USER_STACK_GET; |
| 156 | key.kernel_stack_id = KERNEL_STACK_GET; |
| 157 | |
| 158 | if (key.kernel_stack_id >= 0) { |
| 159 | // populate extras to fix the kernel stack |
| 160 | struct pt_regs regs = {}; |
Brendan Gregg | 715f7e6 | 2016-10-20 22:50:08 -0700 | [diff] [blame] | 161 | bpf_probe_read(®s, sizeof(regs), (void *)&ctx->regs); |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 162 | u64 ip = PT_REGS_IP(®s); |
Yonghong Song | b5fcb51 | 2018-04-14 22:55:11 -0700 | [diff] [blame] | 163 | u64 page_offset; |
Brendan Gregg | ac297c1 | 2016-10-18 20:17:04 -0700 | [diff] [blame] | 164 | |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 165 | // if ip isn't sane, leave key ips as zero for later checking |
Yonghong Song | b5fcb51 | 2018-04-14 22:55:11 -0700 | [diff] [blame] | 166 | #if defined(CONFIG_X86_64) && defined(__PAGE_OFFSET_BASE) |
| 167 | // x64, 4.16, ..., 4.11, etc., but some earlier kernel didn't have it |
| 168 | page_offset = __PAGE_OFFSET_BASE; |
| 169 | #elif defined(CONFIG_X86_64) && defined(__PAGE_OFFSET_BASE_L4) |
| 170 | // x64, 4.17, and later |
| 171 | #if defined(CONFIG_DYNAMIC_MEMORY_LAYOUT) && defined(CONFIG_X86_5LEVEL) |
| 172 | page_offset = __PAGE_OFFSET_BASE_L5; |
Brendan Gregg | ac297c1 | 2016-10-18 20:17:04 -0700 | [diff] [blame] | 173 | #else |
Yonghong Song | b5fcb51 | 2018-04-14 22:55:11 -0700 | [diff] [blame] | 174 | page_offset = __PAGE_OFFSET_BASE_L4; |
Brendan Gregg | ac297c1 | 2016-10-18 20:17:04 -0700 | [diff] [blame] | 175 | #endif |
Yonghong Song | b5fcb51 | 2018-04-14 22:55:11 -0700 | [diff] [blame] | 176 | #else |
| 177 | // earlier x86_64 kernels, e.g., 4.6, comes here |
| 178 | // arm64, s390, powerpc, x86_32 |
| 179 | page_offset = PAGE_OFFSET; |
| 180 | #endif |
| 181 | |
| 182 | if (ip > page_offset) { |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 183 | key.kernel_ip = ip; |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | val = counts.lookup_or_init(&key, &zero); |
| 188 | (*val)++; |
| 189 | return 0; |
| 190 | } |
| 191 | """ |
| 192 | |
| 193 | # set thread filter |
| 194 | thread_context = "" |
| 195 | perf_filter = "-a" |
| 196 | if args.pid is not None: |
| 197 | thread_context = "PID %s" % args.pid |
| 198 | thread_filter = 'pid == %s' % args.pid |
| 199 | perf_filter = '-p %s' % args.pid |
| 200 | else: |
| 201 | thread_context = "all threads" |
| 202 | thread_filter = '1' |
| 203 | bpf_text = bpf_text.replace('THREAD_FILTER', thread_filter) |
| 204 | |
| 205 | # set stack storage size |
| 206 | bpf_text = bpf_text.replace('STACK_STORAGE_SIZE', str(args.stack_storage_size)) |
| 207 | |
| 208 | # handle stack args |
Teng Qin | e4db768 | 2018-04-24 16:24:20 -0700 | [diff] [blame] | 209 | kernel_stack_get = "stack_traces.get_stackid(&ctx->regs, 0)" |
| 210 | user_stack_get = "stack_traces.get_stackid(&ctx->regs, BPF_F_USER_STACK)" |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 211 | stack_context = "" |
| 212 | if args.user_stacks_only: |
| 213 | stack_context = "user" |
| 214 | kernel_stack_get = "-1" |
| 215 | elif args.kernel_stacks_only: |
| 216 | stack_context = "kernel" |
| 217 | user_stack_get = "-1" |
| 218 | else: |
| 219 | stack_context = "user + kernel" |
| 220 | bpf_text = bpf_text.replace('USER_STACK_GET', user_stack_get) |
| 221 | bpf_text = bpf_text.replace('KERNEL_STACK_GET', kernel_stack_get) |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 222 | |
Teng Qin | 86df2b8 | 2018-04-23 12:36:51 -0700 | [diff] [blame] | 223 | sample_freq = 0 |
| 224 | sample_period = 0 |
| 225 | if args.frequency: |
| 226 | sample_freq = args.frequency |
| 227 | elif args.count: |
| 228 | sample_period = args.count |
| 229 | else: |
| 230 | # If user didn't specify anything, use default 49Hz sampling |
| 231 | sample_freq = 49 |
| 232 | sample_context = "%s%d %s" % (("", sample_freq, "Hertz") if sample_freq |
| 233 | else ("every ", sample_period, "events")) |
| 234 | |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 235 | # header |
| 236 | if not args.folded: |
Teng Qin | 86df2b8 | 2018-04-23 12:36:51 -0700 | [diff] [blame] | 237 | print("Sampling at %s of %s by %s stack" % |
| 238 | (sample_context, thread_context, stack_context), end="") |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 239 | if duration < 99999999: |
| 240 | print(" for %d secs." % duration) |
| 241 | else: |
| 242 | print("... Hit Ctrl-C to end.") |
| 243 | |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 244 | if debug or args.ebpf: |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 245 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 246 | if args.ebpf: |
| 247 | exit() |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 248 | |
Brendan Gregg | 715f7e6 | 2016-10-20 22:50:08 -0700 | [diff] [blame] | 249 | # initialize BPF & perf_events |
| 250 | b = BPF(text=bpf_text) |
| 251 | b.attach_perf_event(ev_type=PerfType.SOFTWARE, |
| 252 | ev_config=PerfSWConfig.CPU_CLOCK, fn_name="do_perf_event", |
Teng Qin | 86df2b8 | 2018-04-23 12:36:51 -0700 | [diff] [blame] | 253 | sample_period=sample_period, sample_freq=sample_freq) |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 254 | |
| 255 | # signal handler |
| 256 | def signal_ignore(signal, frame): |
| 257 | print() |
| 258 | |
| 259 | # |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 260 | # Output Report |
| 261 | # |
| 262 | |
| 263 | # collect samples |
| 264 | try: |
| 265 | sleep(duration) |
| 266 | except KeyboardInterrupt: |
| 267 | # as cleanup can take some time, trap Ctrl-C: |
| 268 | signal.signal(signal.SIGINT, signal_ignore) |
| 269 | |
| 270 | if not args.folded: |
| 271 | print() |
| 272 | |
| 273 | def aksym(addr): |
| 274 | if args.annotations: |
| 275 | return b.ksym(addr) + "_[k]" |
| 276 | else: |
| 277 | return b.ksym(addr) |
| 278 | |
| 279 | # output stacks |
| 280 | missing_stacks = 0 |
| 281 | has_enomem = False |
| 282 | counts = b.get_table("counts") |
| 283 | stack_traces = b.get_table("stack_traces") |
Teng Qin | e4db768 | 2018-04-24 16:24:20 -0700 | [diff] [blame] | 284 | need_delimiter = args.delimited and not (args.kernel_stacks_only or |
| 285 | args.user_stacks_only) |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 286 | for k, v in sorted(counts.items(), key=lambda counts: counts[1].value): |
Teng Qin | e4db768 | 2018-04-24 16:24:20 -0700 | [diff] [blame] | 287 | # handle get_stackid errors |
| 288 | if not args.user_stacks_only and stack_id_err(k.kernel_stack_id): |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 289 | missing_stacks += 1 |
Teng Qin | e4db768 | 2018-04-24 16:24:20 -0700 | [diff] [blame] | 290 | has_enomem = has_enomem or k.kernel_stack_id == -errno.ENOMEM |
| 291 | if not args.kernel_stacks_only and stack_id_err(k.user_stack_id): |
| 292 | missing_stacks += 1 |
| 293 | has_enomem = has_enomem or k.user_stack_id == -errno.ENOMEM |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 294 | |
| 295 | user_stack = [] if k.user_stack_id < 0 else \ |
| 296 | stack_traces.walk(k.user_stack_id) |
| 297 | kernel_tmp = [] if k.kernel_stack_id < 0 else \ |
| 298 | stack_traces.walk(k.kernel_stack_id) |
| 299 | |
| 300 | # fix kernel stack |
| 301 | kernel_stack = [] |
| 302 | if k.kernel_stack_id >= 0: |
Brendan Gregg | 715f7e6 | 2016-10-20 22:50:08 -0700 | [diff] [blame] | 303 | for addr in kernel_tmp: |
| 304 | kernel_stack.append(addr) |
| 305 | # the later IP checking |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 306 | if k.kernel_ip: |
| 307 | kernel_stack.insert(0, k.kernel_ip) |
| 308 | |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 309 | if args.folded: |
| 310 | # print folded stack output |
| 311 | user_stack = list(user_stack) |
| 312 | kernel_stack = list(kernel_stack) |
Teng Qin | e4db768 | 2018-04-24 16:24:20 -0700 | [diff] [blame] | 313 | line = [k.name.decode()] |
| 314 | # if we failed to get the stack is, such as due to no space (-ENOMEM) or |
| 315 | # hash collision (-EEXIST), we still print a placeholder for consistency |
| 316 | if not args.kernel_stacks_only: |
| 317 | if stack_id_err(k.user_stack_id): |
| 318 | line.append("[Missed User Stack]") |
| 319 | else: |
| 320 | line.extend([b.sym(addr, k.pid) for addr in reversed(user_stack)]) |
| 321 | if not args.user_stacks_only: |
| 322 | line.extend(["-"] if (need_delimiter and k.kernel_stack_id >= 0 and k.user_stack_id >= 0) else []) |
| 323 | if stack_id_err(k.kernel_stack_id): |
| 324 | line.append("[Missed Kernel Stack]") |
| 325 | else: |
| 326 | line.extend([b.ksym(addr) for addr in reversed(kernel_stack)]) |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 327 | print("%s %d" % (";".join(line), v.value)) |
| 328 | else: |
Teng Qin | e4db768 | 2018-04-24 16:24:20 -0700 | [diff] [blame] | 329 | # print default multi-line stack output |
| 330 | if not args.user_stacks_only: |
| 331 | if stack_id_err(k.kernel_stack_id): |
| 332 | print(" [Missed Kernel Stack]") |
| 333 | else: |
| 334 | for addr in kernel_stack: |
| 335 | print(" %s" % aksym(addr)) |
| 336 | if not args.kernel_stacks_only: |
| 337 | if need_delimiter and k.user_stack_id >= 0 and k.kernel_stack_id >= 0: |
| 338 | print(" --") |
| 339 | if stack_id_err(k.user_stack_id): |
| 340 | print(" [Missed User Stack]") |
| 341 | else: |
| 342 | for addr in user_stack: |
| 343 | print(" %s" % b.sym(addr, k.pid)) |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 344 | print(" %-16s %s (%d)" % ("-", k.name.decode(), k.pid)) |
Brendan Gregg | f4bf275 | 2016-07-21 18:13:24 -0700 | [diff] [blame] | 345 | print(" %d\n" % v.value) |
| 346 | |
| 347 | # check missing |
| 348 | if missing_stacks > 0: |
| 349 | enomem_str = "" if not has_enomem else \ |
| 350 | " Consider increasing --stack-storage-size." |
| 351 | print("WARNING: %d stack traces could not be displayed.%s" % |
| 352 | (missing_stacks, enomem_str), |
| 353 | file=stderr) |