Skip to content

Commit

Permalink
offwaketime: Add support for --state (#2940)
Browse files Browse the repository at this point in the history
Since offwaketime is really an amalgamation of offcputime and wakeuptime
there is no reason why it shouldn't support the --state argument of the
former.

Co-authored-by: Nikolay Borisov <nborisov@suse.com>
  • Loading branch information
lorddoskias and lorddoskias committed May 30, 2020
1 parent 5558e36 commit b20f5e7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion man/man8/offwaketime.8
Expand Up @@ -2,7 +2,7 @@
.SH NAME
offwaketime \- Summarize blocked time by off-CPU stack + waker stack. Uses Linux eBPF/bcc.
.SH SYNOPSIS
.B offwaketime [\-h] [\-p PID | \-t TID | \-u | \-k] [\-U | \-K] [\-f] [\-\-stack-storage-size STACK_STORAGE_SIZE] [\-m MIN_BLOCK_TIME] [\-M MAX_BLOCK_TIME] [duration]
.B offwaketime [\-h] [\-p PID | \-t TID | \-u | \-k] [\-U | \-K] [\-f] [\-\-stack-storage-size STACK_STORAGE_SIZE] [\-m MIN_BLOCK_TIME] [\-M MAX_BLOCK_TIME] [\-\-state STATE] [duration]
.SH DESCRIPTION
This program shows kernel stack traces and task names that were blocked and
"off-CPU", along with the stack traces and task names for the threads that woke
Expand Down Expand Up @@ -64,6 +64,10 @@ The amount of time in microseconds over which we store traces (default 1)
.TP
\-M MAX_BLOCK_TIME
The amount of time in microseconds under which we store traces (default U64_MAX)
.TP
\-\-state
Filter on this thread state bitmask (eg, 2 == TASK_UNINTERRUPTIBLE).
See include/linux/sched.h for states.
.SH EXAMPLES
.TP
Trace all thread blocking events, and summarize (in-kernel) by user and kernel off-CPU stack trace, waker stack traces, task names, and total blocked time:
Expand Down
15 changes: 13 additions & 2 deletions tools/offwaketime.py
Expand Up @@ -93,6 +93,9 @@ def stack_id_err(stack_id):
type=positive_nonzero_int,
help="the amount of time in microseconds under which we " +
"store traces (default U64_MAX)")
parser.add_argument("--state", type=positive_int,
help="filter on this thread state bitmask (eg, 2 == TASK_UNINTERRUPTIBLE" +
") see include/linux/sched.h")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
Expand Down Expand Up @@ -147,7 +150,7 @@ def signal_ignore(signal, frame):
u32 pid = p->pid;
u32 tgid = p->tgid;
if (!(THREAD_FILTER)) {
if (!((THREAD_FILTER) && (STATE_FILTER))) {
return 0;
}
Expand All @@ -171,7 +174,7 @@ def signal_ignore(signal, frame):
u64 ts = bpf_ktime_get_ns();
// Record timestamp for the previous Process (Process going into waiting)
if (THREAD_FILTER) {
if ((THREAD_FILTER) && (STATE_FILTER)) {
start.update(&pid, &ts);
}
Expand Down Expand Up @@ -234,7 +237,15 @@ def signal_ignore(signal, frame):
else:
thread_context = "all threads"
thread_filter = '1'
if args.state == 0:
state_filter = 'p->state == 0'
elif args.state:
# these states are sometimes bitmask checked
state_filter = 'p->state & %d' % args.state
else:
state_filter = '1'
bpf_text = bpf_text.replace('THREAD_FILTER', thread_filter)
bpf_text = bpf_text.replace('STATE_FILTER', state_filter)

# set stack storage size
bpf_text = bpf_text.replace('STACK_STORAGE_SIZE', str(args.stack_storage_size))
Expand Down
4 changes: 3 additions & 1 deletion tools/offwaketime_example.txt
Expand Up @@ -307,7 +307,7 @@ USAGE message:
# ./offwaketime -h
usage: offwaketime [-h] [-p PID | -t TID | -u | -k] [-U | -K] [-d] [-f]
[--stack-storage-size STACK_STORAGE_SIZE]
[-m MIN_BLOCK_TIME] [-M MAX_BLOCK_TIME]
[-m MIN_BLOCK_TIME] [-M MAX_BLOCK_TIME] [--state STATE]
[duration]

Summarize blocked time by kernel stack trace + waker stack
Expand Down Expand Up @@ -340,6 +340,8 @@ optional arguments:
-M MAX_BLOCK_TIME, --max-block-time MAX_BLOCK_TIME
the amount of time in microseconds under which we
store traces (default U64_MAX)
--state STATE filter on this thread state bitmask (eg, 2 ==
TASK_UNINTERRUPTIBLE) see include/linux/sched.h

examples:
./offwaketime # trace off-CPU + waker stack time until Ctrl-C
Expand Down

0 comments on commit b20f5e7

Please sign in to comment.