Skip to content

Commit

Permalink
tools/offcputime: Add option to show symbol offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
xingfeng2510 authored and yonghong-song committed Mar 31, 2023
1 parent ce78071 commit 48ce356
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 18 additions & 3 deletions tools/offcputime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
# offcputime Summarize off-CPU time by stack trace
# For Linux, uses BCC, eBPF.
#
# USAGE: offcputime [-h] [-p PID | -u | -k] [-U | -K] [-f] [duration]
# USAGE: offcputime [-h] [-p PID | -t TID | -u | -k] [-U | -K] [-d] [-f] [-s]
# [--stack-storage-size STACK_STORAGE_SIZE]
# [-m MIN_BLOCK_TIME] [-M MAX_BLOCK_TIME] [--state STATE]
# [duration]
#
# Copyright 2016 Netflix, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 13-Jan-2016 Brendan Gregg Created this.
# 27-Mar-2023 Rocky Xing Added option to show symbol offsets.

from __future__ import print_function
from bcc import BPF
Expand Down Expand Up @@ -45,6 +49,7 @@ def stack_id_err(stack_id):
./offcputime # trace off-CPU stack time until Ctrl-C
./offcputime 5 # trace for 5 seconds only
./offcputime -f 5 # 5 seconds, and output in folded format
./offcputime -s 5 # 5 seconds, and show symbol offsets
./offcputime -m 1000 # trace only events that last more than 1000 usec
./offcputime -M 10000 # trace only events that last less than 10000 usec
./offcputime -p 185 # only trace threads for PID 185
Expand Down Expand Up @@ -78,6 +83,8 @@ def stack_id_err(stack_id):
help="insert delimiter between kernel/user stacks")
parser.add_argument("-f", "--folded", action="store_true",
help="output folded format")
parser.add_argument("-s", "--offset", action="store_true",
help="show address offsets")
parser.add_argument("--stack-storage-size", default=1024,
type=positive_nonzero_int,
help="the number of unique stack traces that can be stored and "
Expand All @@ -103,6 +110,10 @@ def stack_id_err(stack_id):
duration = int(args.duration)
debug = 0

if args.folded and args.offset:
print("ERROR: can only use -f or -s. Exiting.")
exit()

# signal handler
def signal_ignore(signal, frame):
print()
Expand Down Expand Up @@ -296,6 +307,10 @@ def print_warn_event(cpu, data, size):
if not folded:
print()

show_offset = False
if args.offset:
show_offset = True

missing_stacks = 0
has_enomem = False
counts = b.get_table("counts")
Expand Down Expand Up @@ -344,15 +359,15 @@ def print_warn_event(cpu, data, size):
print(" [Missed Kernel Stack]")
else:
for addr in kernel_stack:
print(" %s" % b.ksym(addr).decode('utf-8', 'replace'))
print(" %s" % b.ksym(addr, show_offset=show_offset).decode('utf-8', 'replace'))
if not args.kernel_stacks_only:
if need_delimiter and k.user_stack_id >= 0 and k.kernel_stack_id >= 0:
print(" --")
if stack_id_err(k.user_stack_id):
print(" [Missed User Stack]")
else:
for addr in user_stack:
print(" %s" % b.sym(addr, k.tgid).decode('utf-8', 'replace'))
print(" %s" % b.sym(addr, k.tgid, show_offset=show_offset).decode('utf-8', 'replace'))
print(" %-16s %s (%d)" % ("-", k.name.decode('utf-8', 'replace'), k.pid))
print(" %d\n" % v.value)

Expand Down
4 changes: 3 additions & 1 deletion tools/offcputime_example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ creating your "off-CPU time flame graphs".
USAGE message:

# ./offcputime.py -h
usage: offcputime.py [-h] [-p PID | -t TID | -u | -k] [-U | -K] [-d] [-f]
usage: offcputime.py [-h] [-p PID | -t TID | -u | -k] [-U | -K] [-d] [-f] [-s]
[--stack-storage-size STACK_STORAGE_SIZE]
[-m MIN_BLOCK_TIME] [-M MAX_BLOCK_TIME] [--state STATE]
[duration]
Expand All @@ -745,6 +745,7 @@ optional arguments:
stacks)
-d, --delimited insert delimiter between kernel/user stacks
-f, --folded output folded format
-s, --offset show address offsets
--stack-storage-size STACK_STORAGE_SIZE
the number of unique stack traces that can be stored
and displayed (default 1024)
Expand All @@ -761,6 +762,7 @@ examples:
./offcputime # trace off-CPU stack time until Ctrl-C
./offcputime 5 # trace for 5 seconds only
./offcputime -f 5 # 5 seconds, and output in folded format
./offcputime -s 5 # 5 seconds, and show symbol offsets
./offcputime -m 1000 # trace only events that last more than 1000 usec
./offcputime -M 10000 # trace only events that last less than 10000 usec
./offcputime -p 185 # only trace threads for PID 185
Expand Down

0 comments on commit 48ce356

Please sign in to comment.