Skip to content

Commit

Permalink
tools/profile: add multi process/thread support, like perf convention
Browse files Browse the repository at this point in the history
  • Loading branch information
yezhengmao1 authored and yonghong-song committed Jul 29, 2022
1 parent cb23ebb commit 4a8b593
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
4 changes: 2 additions & 2 deletions man/man8/profile.8
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ for an older version that may work on Linux 4.6 - 4.8.
Print usage message.
.TP
\-p PID
Trace this process ID only (filtered in-kernel).
Trace process with one or more comma separated PIDs only (filtered in-kernel).
.TP
\-L TID
Trace this thread ID only (filtered in-kernel).
Trace thread with one or more comma separated TIDs only (filtered in-kernel).
.TP
\-F frequency
Frequency to sample stacks.
Expand Down
21 changes: 14 additions & 7 deletions tools/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def positive_int(val):
raise argparse.ArgumentTypeError("must be positive")
return ival

def positive_int_list(val):
vlist = val.split(",")
if len(vlist) <= 0:
raise argparse.ArgumentTypeError("must be an integer list")

return [positive_int(v) for v in vlist]

def positive_nonzero_int(val):
ival = positive_int(val)
if ival == 0:
Expand Down Expand Up @@ -81,10 +88,10 @@ def stack_id_err(stack_id):
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=examples)
thread_group = parser.add_mutually_exclusive_group()
thread_group.add_argument("-p", "--pid", type=positive_int,
help="profile process with this PID only")
thread_group.add_argument("-L", "--tid", type=positive_int,
help="profile thread with this TID only")
thread_group.add_argument("-p", "--pid", type=positive_int_list,
help="profile process with one or more comma separated PIDs only")
thread_group.add_argument("-L", "--tid", type=positive_int_list,
help="profile thread with one or more comma separated TIDs only")
# TODO: add options for user/kernel threads only
stack_group = parser.add_mutually_exclusive_group()
stack_group.add_argument("-U", "--user-stacks-only", action="store_true",
Expand Down Expand Up @@ -122,7 +129,6 @@ def stack_id_err(stack_id):

# option logic
args = parser.parse_args()
pid = int(args.pid) if args.pid is not None else -1
duration = int(args.duration)
debug = 0
need_delimiter = args.delimited and not (args.kernel_stacks_only or
Expand Down Expand Up @@ -214,12 +220,13 @@ def stack_id_err(stack_id):

# set process/thread filter
thread_context = ""
thread_filter = ""
if args.pid is not None:
thread_context = "PID %s" % args.pid
thread_filter = 'tgid == %s' % args.pid
thread_filter = " || ".join("tgid == " + str(pid) for pid in args.pid)
elif args.tid is not None:
thread_context = "TID %s" % args.tid
thread_filter = 'pid == %s' % args.tid
thread_filter = " || ".join("pid == " + str(tid) for tid in args.tid)
else:
thread_context = "all threads"
thread_filter = '1'
Expand Down
29 changes: 29 additions & 0 deletions tools/profile_example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,35 @@ but we're lacking the symbol translation. This is a common for all profilers
on Linux, and is usually fixable. See the DEBUGGING section of the profile(8)
man page.

You can also profile different process:
# ./profile -p 2040,1316151
Sampling at 49 Hertz of PID [2040, 1316151] by user + kernel stack... Hit Ctrl-C to end.
^C
PyEval_RestoreThread
[unknown]
[unknown]
- python3 (1316151)
1
[...]
rcu_all_qs
rcu_all_qs
dput
step_into
handle_dots.part.0
walk_component
link_path_walk.part.0
path_openat
do_filp_open
do_sys_openat2
do_sys_open
__x64_sys_openat
do_syscall_64
entry_SYSCALL_64_after_hwframe
__libc_open64
[unknown]
- python3 (2040)
1


Lets add delimiters between the user and kernel stacks, using -d:

Expand Down

0 comments on commit 4a8b593

Please sign in to comment.