blob: 3040a482d3991d7223000bcf29c138ae229e5165 [file] [log] [blame]
Brendan Greggf4bf2752016-07-21 18:13:24 -07001#!/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#
Brendan Gregg715f7e62016-10-20 22:50:08 -070012# REQUIRES: Linux 4.9+ (BPF_PROG_TYPE_PERF_EVENT support). Under tools/old is
13# a version of this tool that may work on Linux 4.6 - 4.8.
Brendan Greggf4bf2752016-07-21 18:13:24 -070014#
15# Copyright 2016 Netflix, Inc.
16# Licensed under the Apache License, Version 2.0 (the "License")
17#
Brendan Gregg715f7e62016-10-20 22:50:08 -070018# THANKS: Alexei Starovoitov, who added proper BPF profiling support to Linux;
19# Sasha Goldshtein, Andrew Birchall, and Evgeny Vereshchagin, who wrote much
20# of the code here, borrowed from tracepoint.py and offcputime.py; and
21# Teng Qin, who added perf support in bcc.
Brendan Greggf4bf2752016-07-21 18:13:24 -070022#
23# 15-Jul-2016 Brendan Gregg Created this.
Brendan Gregg715f7e62016-10-20 22:50:08 -070024# 20-Oct-2016 " " Switched to use the new 4.9 support.
Brendan Greggf4bf2752016-07-21 18:13:24 -070025
26from __future__ import print_function
Brendan Gregg715f7e62016-10-20 22:50:08 -070027from bcc import BPF, PerfType, PerfSWConfig
Brendan Greggf4bf2752016-07-21 18:13:24 -070028from sys import stderr
29from time import sleep
30import argparse
31import signal
32import os
33import errno
34import multiprocessing
35import ctypes as ct
36
37#
38# Process Arguments
39#
40
41# arg validation
42def positive_int(val):
43 try:
44 ival = int(val)
45 except ValueError:
46 raise argparse.ArgumentTypeError("must be an integer")
47
48 if ival < 0:
49 raise argparse.ArgumentTypeError("must be positive")
50 return ival
51
52def positive_nonzero_int(val):
53 ival = positive_int(val)
54 if ival == 0:
55 raise argparse.ArgumentTypeError("must be nonzero")
56 return ival
57
Teng Qine4db7682018-04-24 16:24:20 -070058def stack_id_err(stack_id):
59 # -EFAULT in get_stackid normally means the stack-trace is not availible,
60 # Such as getting kernel stack trace in userspace code
61 return (stack_id < 0) and (stack_id != -errno.EFAULT)
62
Brendan Greggf4bf2752016-07-21 18:13:24 -070063# arguments
64examples = """examples:
65 ./profile # profile stack traces at 49 Hertz until Ctrl-C
66 ./profile -F 99 # profile stack traces at 99 Hertz
Teng Qin86df2b82018-04-23 12:36:51 -070067 ./profile -c 1000000 # profile stack traces every 1 in a million events
Brendan Greggf4bf2752016-07-21 18:13:24 -070068 ./profile 5 # profile at 49 Hertz for 5 seconds only
69 ./profile -f 5 # output in folded format for flame graphs
70 ./profile -p 185 # only profile threads for PID 185
71 ./profile -U # only show user space stacks (no kernel)
72 ./profile -K # only show kernel space stacks (no user)
Brendan Greggf4bf2752016-07-21 18:13:24 -070073"""
74parser = argparse.ArgumentParser(
75 description="Profile CPU stack traces at a timed interval",
76 formatter_class=argparse.RawDescriptionHelpFormatter,
77 epilog=examples)
78thread_group = parser.add_mutually_exclusive_group()
79thread_group.add_argument("-p", "--pid", type=positive_int,
80 help="profile this PID only")
81# TODO: add options for user/kernel threads only
82stack_group = parser.add_mutually_exclusive_group()
83stack_group.add_argument("-U", "--user-stacks-only", action="store_true",
84 help="show stacks from user space only (no kernel space stacks)")
85stack_group.add_argument("-K", "--kernel-stacks-only", action="store_true",
86 help="show stacks from kernel space only (no user space stacks)")
Teng Qin86df2b82018-04-23 12:36:51 -070087sample_group = parser.add_mutually_exclusive_group()
88sample_group.add_argument("-F", "--frequency", type=positive_int,
89 help="sample frequency, Hertz")
90sample_group.add_argument("-c", "--count", type=positive_int,
91 help="sample period, number of events")
Brendan Greggf4bf2752016-07-21 18:13:24 -070092parser.add_argument("-d", "--delimited", action="store_true",
93 help="insert delimiter between kernel/user stacks")
94parser.add_argument("-a", "--annotations", action="store_true",
95 help="add _[k] annotations to kernel frames")
96parser.add_argument("-f", "--folded", action="store_true",
97 help="output folded format, one line per stack (for flame graphs)")
Tommaso Sardelliaa4aa522018-04-30 22:35:29 +020098parser.add_argument("--stack-storage-size", default=16384,
Brendan Greggf4bf2752016-07-21 18:13:24 -070099 type=positive_nonzero_int,
100 help="the number of unique stack traces that can be stored and "
Tommaso Sardelli718c9282018-04-30 15:36:40 +0200101 "displayed (default %(default)s)")
Brendan Greggf4bf2752016-07-21 18:13:24 -0700102parser.add_argument("duration", nargs="?", default=99999999,
103 type=positive_nonzero_int,
104 help="duration of trace, in seconds")
Nathan Scottcf0792f2018-02-02 16:56:50 +1100105parser.add_argument("--ebpf", action="store_true",
106 help=argparse.SUPPRESS)
Brendan Greggf4bf2752016-07-21 18:13:24 -0700107
108# option logic
109args = parser.parse_args()
Brendan Greggf4bf2752016-07-21 18:13:24 -0700110pid = int(args.pid) if args.pid is not None else -1
111duration = int(args.duration)
112debug = 0
113need_delimiter = args.delimited and not (args.kernel_stacks_only or
114 args.user_stacks_only)
115# TODO: add stack depth, and interval
116
117#
118# Setup BPF
119#
120
121# define BPF program
122bpf_text = """
123#include <uapi/linux/ptrace.h>
Brendan Gregg715f7e62016-10-20 22:50:08 -0700124#include <uapi/linux/bpf_perf_event.h>
Brendan Greggf4bf2752016-07-21 18:13:24 -0700125#include <linux/sched.h>
126
127struct key_t {
128 u32 pid;
129 u64 kernel_ip;
130 u64 kernel_ret_ip;
131 int user_stack_id;
132 int kernel_stack_id;
133 char name[TASK_COMM_LEN];
134};
135BPF_HASH(counts, struct key_t);
Song Liu67ae6052018-02-01 14:59:24 -0800136BPF_STACK_TRACE(stack_traces, STACK_STORAGE_SIZE);
Brendan Greggf4bf2752016-07-21 18:13:24 -0700137
138// This code gets a bit complex. Probably not suitable for casual hacking.
139
Brendan Gregg715f7e62016-10-20 22:50:08 -0700140int do_perf_event(struct bpf_perf_event_data *ctx) {
Brendan Gregg4c9f6602016-11-30 20:26:26 -0800141 u32 pid = bpf_get_current_pid_tgid() >> 32;
Brendan Greggf4bf2752016-07-21 18:13:24 -0700142 if (!(THREAD_FILTER))
143 return 0;
144
145 // create map key
146 u64 zero = 0, *val;
147 struct key_t key = {.pid = pid};
148 bpf_get_current_comm(&key.name, sizeof(key.name));
149
150 // get stacks
151 key.user_stack_id = USER_STACK_GET;
152 key.kernel_stack_id = KERNEL_STACK_GET;
153
154 if (key.kernel_stack_id >= 0) {
155 // populate extras to fix the kernel stack
156 struct pt_regs regs = {};
Brendan Gregg715f7e62016-10-20 22:50:08 -0700157 bpf_probe_read(&regs, sizeof(regs), (void *)&ctx->regs);
Brendan Greggf4bf2752016-07-21 18:13:24 -0700158 u64 ip = PT_REGS_IP(&regs);
Yonghong Songb5fcb512018-04-14 22:55:11 -0700159 u64 page_offset;
Brendan Greggac297c12016-10-18 20:17:04 -0700160
Brendan Greggf4bf2752016-07-21 18:13:24 -0700161 // if ip isn't sane, leave key ips as zero for later checking
Yonghong Songb5fcb512018-04-14 22:55:11 -0700162#if defined(CONFIG_X86_64) && defined(__PAGE_OFFSET_BASE)
163 // x64, 4.16, ..., 4.11, etc., but some earlier kernel didn't have it
164 page_offset = __PAGE_OFFSET_BASE;
165#elif defined(CONFIG_X86_64) && defined(__PAGE_OFFSET_BASE_L4)
166 // x64, 4.17, and later
167#if defined(CONFIG_DYNAMIC_MEMORY_LAYOUT) && defined(CONFIG_X86_5LEVEL)
168 page_offset = __PAGE_OFFSET_BASE_L5;
Brendan Greggac297c12016-10-18 20:17:04 -0700169#else
Yonghong Songb5fcb512018-04-14 22:55:11 -0700170 page_offset = __PAGE_OFFSET_BASE_L4;
Brendan Greggac297c12016-10-18 20:17:04 -0700171#endif
Yonghong Songb5fcb512018-04-14 22:55:11 -0700172#else
173 // earlier x86_64 kernels, e.g., 4.6, comes here
174 // arm64, s390, powerpc, x86_32
175 page_offset = PAGE_OFFSET;
176#endif
177
178 if (ip > page_offset) {
Brendan Greggf4bf2752016-07-21 18:13:24 -0700179 key.kernel_ip = ip;
Brendan Greggf4bf2752016-07-21 18:13:24 -0700180 }
181 }
182
183 val = counts.lookup_or_init(&key, &zero);
184 (*val)++;
185 return 0;
186}
187"""
188
189# set thread filter
190thread_context = ""
191perf_filter = "-a"
192if args.pid is not None:
193 thread_context = "PID %s" % args.pid
194 thread_filter = 'pid == %s' % args.pid
195 perf_filter = '-p %s' % args.pid
196else:
197 thread_context = "all threads"
198 thread_filter = '1'
199bpf_text = bpf_text.replace('THREAD_FILTER', thread_filter)
200
201# set stack storage size
202bpf_text = bpf_text.replace('STACK_STORAGE_SIZE', str(args.stack_storage_size))
203
204# handle stack args
Teng Qine4db7682018-04-24 16:24:20 -0700205kernel_stack_get = "stack_traces.get_stackid(&ctx->regs, 0)"
206user_stack_get = "stack_traces.get_stackid(&ctx->regs, BPF_F_USER_STACK)"
Brendan Greggf4bf2752016-07-21 18:13:24 -0700207stack_context = ""
208if args.user_stacks_only:
209 stack_context = "user"
210 kernel_stack_get = "-1"
211elif args.kernel_stacks_only:
212 stack_context = "kernel"
213 user_stack_get = "-1"
214else:
215 stack_context = "user + kernel"
216bpf_text = bpf_text.replace('USER_STACK_GET', user_stack_get)
217bpf_text = bpf_text.replace('KERNEL_STACK_GET', kernel_stack_get)
Brendan Greggf4bf2752016-07-21 18:13:24 -0700218
Teng Qin86df2b82018-04-23 12:36:51 -0700219sample_freq = 0
220sample_period = 0
221if args.frequency:
222 sample_freq = args.frequency
223elif args.count:
224 sample_period = args.count
225else:
226 # If user didn't specify anything, use default 49Hz sampling
227 sample_freq = 49
228sample_context = "%s%d %s" % (("", sample_freq, "Hertz") if sample_freq
229 else ("every ", sample_period, "events"))
230
Brendan Greggf4bf2752016-07-21 18:13:24 -0700231# header
232if not args.folded:
Teng Qin86df2b82018-04-23 12:36:51 -0700233 print("Sampling at %s of %s by %s stack" %
234 (sample_context, thread_context, stack_context), end="")
Brendan Greggf4bf2752016-07-21 18:13:24 -0700235 if duration < 99999999:
236 print(" for %d secs." % duration)
237 else:
238 print("... Hit Ctrl-C to end.")
239
Nathan Scottcf0792f2018-02-02 16:56:50 +1100240if debug or args.ebpf:
Brendan Greggf4bf2752016-07-21 18:13:24 -0700241 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100242 if args.ebpf:
243 exit()
Brendan Greggf4bf2752016-07-21 18:13:24 -0700244
Brendan Gregg715f7e62016-10-20 22:50:08 -0700245# initialize BPF & perf_events
246b = BPF(text=bpf_text)
247b.attach_perf_event(ev_type=PerfType.SOFTWARE,
248 ev_config=PerfSWConfig.CPU_CLOCK, fn_name="do_perf_event",
Teng Qin86df2b82018-04-23 12:36:51 -0700249 sample_period=sample_period, sample_freq=sample_freq)
Brendan Greggf4bf2752016-07-21 18:13:24 -0700250
251# signal handler
252def signal_ignore(signal, frame):
253 print()
254
255#
Brendan Greggf4bf2752016-07-21 18:13:24 -0700256# Output Report
257#
258
259# collect samples
260try:
261 sleep(duration)
262except KeyboardInterrupt:
263 # as cleanup can take some time, trap Ctrl-C:
264 signal.signal(signal.SIGINT, signal_ignore)
265
266if not args.folded:
267 print()
268
269def aksym(addr):
270 if args.annotations:
271 return b.ksym(addr) + "_[k]"
272 else:
273 return b.ksym(addr)
274
275# output stacks
276missing_stacks = 0
277has_enomem = False
278counts = b.get_table("counts")
279stack_traces = b.get_table("stack_traces")
Teng Qine4db7682018-04-24 16:24:20 -0700280need_delimiter = args.delimited and not (args.kernel_stacks_only or
281 args.user_stacks_only)
Brendan Greggf4bf2752016-07-21 18:13:24 -0700282for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
Teng Qine4db7682018-04-24 16:24:20 -0700283 # handle get_stackid errors
284 if not args.user_stacks_only and stack_id_err(k.kernel_stack_id):
Brendan Greggf4bf2752016-07-21 18:13:24 -0700285 missing_stacks += 1
Teng Qine4db7682018-04-24 16:24:20 -0700286 has_enomem = has_enomem or k.kernel_stack_id == -errno.ENOMEM
287 if not args.kernel_stacks_only and stack_id_err(k.user_stack_id):
288 missing_stacks += 1
289 has_enomem = has_enomem or k.user_stack_id == -errno.ENOMEM
Brendan Greggf4bf2752016-07-21 18:13:24 -0700290
291 user_stack = [] if k.user_stack_id < 0 else \
292 stack_traces.walk(k.user_stack_id)
293 kernel_tmp = [] if k.kernel_stack_id < 0 else \
294 stack_traces.walk(k.kernel_stack_id)
295
296 # fix kernel stack
297 kernel_stack = []
298 if k.kernel_stack_id >= 0:
Brendan Gregg715f7e62016-10-20 22:50:08 -0700299 for addr in kernel_tmp:
300 kernel_stack.append(addr)
301 # the later IP checking
Brendan Greggf4bf2752016-07-21 18:13:24 -0700302 if k.kernel_ip:
303 kernel_stack.insert(0, k.kernel_ip)
304
Brendan Greggf4bf2752016-07-21 18:13:24 -0700305 if args.folded:
306 # print folded stack output
307 user_stack = list(user_stack)
308 kernel_stack = list(kernel_stack)
Teng Qine4db7682018-04-24 16:24:20 -0700309 line = [k.name.decode()]
310 # if we failed to get the stack is, such as due to no space (-ENOMEM) or
311 # hash collision (-EEXIST), we still print a placeholder for consistency
312 if not args.kernel_stacks_only:
313 if stack_id_err(k.user_stack_id):
314 line.append("[Missed User Stack]")
315 else:
316 line.extend([b.sym(addr, k.pid) for addr in reversed(user_stack)])
317 if not args.user_stacks_only:
318 line.extend(["-"] if (need_delimiter and k.kernel_stack_id >= 0 and k.user_stack_id >= 0) else [])
319 if stack_id_err(k.kernel_stack_id):
320 line.append("[Missed Kernel Stack]")
321 else:
322 line.extend([b.ksym(addr) for addr in reversed(kernel_stack)])
Brendan Greggf4bf2752016-07-21 18:13:24 -0700323 print("%s %d" % (";".join(line), v.value))
324 else:
Teng Qine4db7682018-04-24 16:24:20 -0700325 # print default multi-line stack output
326 if not args.user_stacks_only:
327 if stack_id_err(k.kernel_stack_id):
328 print(" [Missed Kernel Stack]")
329 else:
330 for addr in kernel_stack:
331 print(" %s" % aksym(addr))
332 if not args.kernel_stacks_only:
333 if need_delimiter and k.user_stack_id >= 0 and k.kernel_stack_id >= 0:
334 print(" --")
335 if stack_id_err(k.user_stack_id):
336 print(" [Missed User Stack]")
337 else:
338 for addr in user_stack:
339 print(" %s" % b.sym(addr, k.pid))
Rafael F78948e42017-03-26 14:54:25 +0200340 print(" %-16s %s (%d)" % ("-", k.name.decode(), k.pid))
Brendan Greggf4bf2752016-07-21 18:13:24 -0700341 print(" %d\n" % v.value)
342
343# check missing
344if missing_stacks > 0:
345 enomem_str = "" if not has_enomem else \
346 " Consider increasing --stack-storage-size."
347 print("WARNING: %d stack traces could not be displayed.%s" %
348 (missing_stacks, enomem_str),
349 file=stderr)