Skip to content

Commit

Permalink
Merge pull request iovisor#1699 from palmtenor/profile-sample
Browse files Browse the repository at this point in the history
Add -c option for profile.py
  • Loading branch information
yonghong-song committed Apr 24, 2018
2 parents 512710f + 86df2b8 commit a4782bd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
11 changes: 9 additions & 2 deletions man/man8/profile.8
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.SH NAME
profile \- Profile CPU usage by sampling stack traces. Uses Linux eBPF/bcc.
.SH SYNOPSIS
.B profile [\-adfh] [\-p PID] [\-U | \-k] [\-F FREQUENCY]
.B profile [\-adfh] [\-p PID] [\-U | \-K] [\-F FREQUENCY | \-c COUNT]
.B [\-\-stack\-storage\-size COUNT] [duration]
.SH DESCRIPTION
This is a CPU profiler. It works by taking samples of stack traces at timed
Expand Down Expand Up @@ -32,7 +32,10 @@ Trace this process ID only (filtered in-kernel). Without this, all CPUs are
profiled.
.TP
\-F frequency
Frequency to sample stacks (default 49).
Frequency to sample stacks.
.TP
\-c count
Sample stacks every one in this many events.
.TP
\-f
Print output in folded stack format.
Expand Down Expand Up @@ -67,6 +70,10 @@ Profile at 99 Hertz for 5 seconds only:
#
.B profile -F 99 5
.TP
Profile 1 in a million events for 5 seconds only:
#
.B profile -c 1000000 5
.TP
Profile PID 181 only:
#
.B profile -p 181
Expand Down
26 changes: 21 additions & 5 deletions tools/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def positive_nonzero_int(val):
examples = """examples:
./profile # profile stack traces at 49 Hertz until Ctrl-C
./profile -F 99 # profile stack traces at 99 Hertz
./profile -c 1000000 # profile stack traces every 1 in a million events
./profile 5 # profile at 49 Hertz for 5 seconds only
./profile -f 5 # output in folded format for flame graphs
./profile -p 185 # only profile threads for PID 185
Expand All @@ -82,8 +83,11 @@ def positive_nonzero_int(val):
help="show stacks from user space only (no kernel space stacks)")
stack_group.add_argument("-K", "--kernel-stacks-only", action="store_true",
help="show stacks from kernel space only (no user space stacks)")
parser.add_argument("-F", "--frequency", type=positive_int, default=49,
help="sample frequency, Hertz (default 49)")
sample_group = parser.add_mutually_exclusive_group()
sample_group.add_argument("-F", "--frequency", type=positive_int,
help="sample frequency, Hertz")
sample_group.add_argument("-c", "--count", type=positive_int,
help="sample period, number of events")
parser.add_argument("-d", "--delimited", action="store_true",
help="insert delimiter between kernel/user stacks")
parser.add_argument("-a", "--annotations", action="store_true",
Expand Down Expand Up @@ -214,10 +218,22 @@ def positive_nonzero_int(val):
bpf_text = bpf_text.replace('USER_STACK_GET', user_stack_get)
bpf_text = bpf_text.replace('KERNEL_STACK_GET', kernel_stack_get)

sample_freq = 0
sample_period = 0
if args.frequency:
sample_freq = args.frequency
elif args.count:
sample_period = args.count
else:
# If user didn't specify anything, use default 49Hz sampling
sample_freq = 49
sample_context = "%s%d %s" % (("", sample_freq, "Hertz") if sample_freq
else ("every ", sample_period, "events"))

# header
if not args.folded:
print("Sampling at %d Hertz of %s by %s stack" %
(args.frequency, thread_context, stack_context), end="")
print("Sampling at %s of %s by %s stack" %
(sample_context, thread_context, stack_context), end="")
if duration < 99999999:
print(" for %d secs." % duration)
else:
Expand All @@ -232,7 +248,7 @@ def positive_nonzero_int(val):
b = BPF(text=bpf_text)
b.attach_perf_event(ev_type=PerfType.SOFTWARE,
ev_config=PerfSWConfig.CPU_CLOCK, fn_name="do_perf_event",
sample_period=0, sample_freq=args.frequency)
sample_period=sample_period, sample_freq=sample_freq)

# signal handler
def signal_ignore(signal, frame):
Expand Down
9 changes: 6 additions & 3 deletions tools/profile_example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,8 @@ Run ./profile -h to see the default.
USAGE message:

# ./profile -h
usage: profile [-h] [-p PID] [-U | -K] [-F FREQUENCY] [-d] [-a] [-f]
[--stack-storage-size STACK_STORAGE_SIZE]
usage: profile [-h] [-p PID] [-U | -K] [-F FREQUENCY | -c COUNT] [-d] [-a]
[-f] [--stack-storage-size STACK_STORAGE_SIZE]
[duration]

Profile CPU stack traces at a timed interval
Expand All @@ -723,7 +723,9 @@ optional arguments:
show stacks from kernel space only (no user space
stacks)
-F FREQUENCY, --frequency FREQUENCY
sample frequency, Hertz (default 49)
sample frequency, Hertz
-c COUNT, --count COUNT
sample period, number of events
-d, --delimited insert delimiter between kernel/user stacks
-a, --annotations add _[k] annotations to kernel frames
-f, --folded output folded format, one line per stack (for flame
Expand All @@ -735,6 +737,7 @@ optional arguments:
examples:
./profile # profile stack traces at 49 Hertz until Ctrl-C
./profile -F 99 # profile stack traces at 99 Hertz
./profile -c 1000000 # profile stack traces every 1 in a million events
./profile 5 # profile at 49 Hertz for 5 seconds only
./profile -f 5 # output in folded format for flame graphs
./profile -p 185 # only profile threads for PID 185
Expand Down

0 comments on commit a4782bd

Please sign in to comment.