Commit 19b61ebf authored by yonghong-song's avatar yonghong-song Committed by GitHub

Merge pull request #1705 from palmtenor/profilestack

Refactor profile.py stack id error handling
parents 84664dc3 e4db7686
...@@ -59,6 +59,11 @@ def positive_nonzero_int(val): ...@@ -59,6 +59,11 @@ def positive_nonzero_int(val):
raise argparse.ArgumentTypeError("must be nonzero") raise argparse.ArgumentTypeError("must be nonzero")
return ival return ival
def stack_id_err(stack_id):
# -EFAULT in get_stackid normally means the stack-trace is not availible,
# Such as getting kernel stack trace in userspace code
return (stack_id < 0) and (stack_id != -errno.EFAULT)
# arguments # arguments
examples = """examples: examples = """examples:
./profile # profile stack traces at 49 Hertz until Ctrl-C ./profile # profile stack traces at 49 Hertz until Ctrl-C
...@@ -201,11 +206,8 @@ bpf_text = bpf_text.replace('THREAD_FILTER', thread_filter) ...@@ -201,11 +206,8 @@ bpf_text = bpf_text.replace('THREAD_FILTER', thread_filter)
bpf_text = bpf_text.replace('STACK_STORAGE_SIZE', str(args.stack_storage_size)) bpf_text = bpf_text.replace('STACK_STORAGE_SIZE', str(args.stack_storage_size))
# handle stack args # handle stack args
kernel_stack_get = \ kernel_stack_get = "stack_traces.get_stackid(&ctx->regs, 0)"
"stack_traces.get_stackid(&ctx->regs, 0 | BPF_F_REUSE_STACKID)" user_stack_get = "stack_traces.get_stackid(&ctx->regs, BPF_F_USER_STACK)"
user_stack_get = \
"stack_traces.get_stackid(&ctx->regs, 0 | BPF_F_REUSE_STACKID | " \
"BPF_F_USER_STACK)"
stack_context = "" stack_context = ""
if args.user_stacks_only: if args.user_stacks_only:
stack_context = "user" stack_context = "user"
...@@ -279,17 +281,16 @@ missing_stacks = 0 ...@@ -279,17 +281,16 @@ missing_stacks = 0
has_enomem = False has_enomem = False
counts = b.get_table("counts") counts = b.get_table("counts")
stack_traces = b.get_table("stack_traces") stack_traces = b.get_table("stack_traces")
need_delimiter = args.delimited and not (args.kernel_stacks_only or
args.user_stacks_only)
for k, v in sorted(counts.items(), key=lambda counts: counts[1].value): for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
# handle get_stackid erorrs # handle get_stackid errors
if (not args.user_stacks_only and k.kernel_stack_id < 0 and if not args.user_stacks_only and stack_id_err(k.kernel_stack_id):
k.kernel_stack_id != -errno.EFAULT) or \
(not args.kernel_stacks_only and k.user_stack_id < 0 and
k.user_stack_id != -errno.EFAULT):
missing_stacks += 1 missing_stacks += 1
# check for an ENOMEM error has_enomem = has_enomem or k.kernel_stack_id == -errno.ENOMEM
if k.kernel_stack_id == -errno.ENOMEM or \ if not args.kernel_stacks_only and stack_id_err(k.user_stack_id):
k.user_stack_id == -errno.ENOMEM: missing_stacks += 1
has_enomem = True has_enomem = has_enomem or k.user_stack_id == -errno.ENOMEM
user_stack = [] if k.user_stack_id < 0 else \ user_stack = [] if k.user_stack_id < 0 else \
stack_traces.walk(k.user_stack_id) stack_traces.walk(k.user_stack_id)
...@@ -305,23 +306,39 @@ for k, v in sorted(counts.items(), key=lambda counts: counts[1].value): ...@@ -305,23 +306,39 @@ for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
if k.kernel_ip: if k.kernel_ip:
kernel_stack.insert(0, k.kernel_ip) kernel_stack.insert(0, k.kernel_ip)
do_delimiter = need_delimiter and kernel_stack
if args.folded: if args.folded:
# print folded stack output # print folded stack output
user_stack = list(user_stack) user_stack = list(user_stack)
kernel_stack = list(kernel_stack) kernel_stack = list(kernel_stack)
line = [k.name.decode()] + \ line = [k.name.decode()]
[b.sym(addr, k.pid) for addr in reversed(user_stack)] + \ # if we failed to get the stack is, such as due to no space (-ENOMEM) or
(do_delimiter and ["-"] or []) + \ # hash collision (-EEXIST), we still print a placeholder for consistency
[aksym(addr) for addr in reversed(kernel_stack)] if not args.kernel_stacks_only:
if stack_id_err(k.user_stack_id):
line.append("[Missed User Stack]")
else:
line.extend([b.sym(addr, k.pid) for addr in reversed(user_stack)])
if not args.user_stacks_only:
line.extend(["-"] if (need_delimiter and k.kernel_stack_id >= 0 and k.user_stack_id >= 0) else [])
if stack_id_err(k.kernel_stack_id):
line.append("[Missed Kernel Stack]")
else:
line.extend([b.ksym(addr) for addr in reversed(kernel_stack)])
print("%s %d" % (";".join(line), v.value)) print("%s %d" % (";".join(line), v.value))
else: else:
# print default multi-line stack output. # print default multi-line stack output
if not args.user_stacks_only:
if stack_id_err(k.kernel_stack_id):
print(" [Missed Kernel Stack]")
else:
for addr in kernel_stack: for addr in kernel_stack:
print(" %s" % aksym(addr)) print(" %s" % aksym(addr))
if do_delimiter: if not args.kernel_stacks_only:
if need_delimiter and k.user_stack_id >= 0 and k.kernel_stack_id >= 0:
print(" --") print(" --")
if stack_id_err(k.user_stack_id):
print(" [Missed User Stack]")
else:
for addr in user_stack: for addr in user_stack:
print(" %s" % b.sym(addr, k.pid)) print(" %s" % b.sym(addr, k.pid))
print(" %-16s %s (%d)" % ("-", k.name.decode(), k.pid)) print(" %-16s %s (%d)" % ("-", k.name.decode(), k.pid))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment