Skip to content

Commit b20f5e7

Browse files
authored
offwaketime: Add support for --state (#2940)
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 <[email protected]>
1 parent 5558e36 commit b20f5e7

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

man/man8/offwaketime.8

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.SH NAME
33
offwaketime \- Summarize blocked time by off-CPU stack + waker stack. Uses Linux eBPF/bcc.
44
.SH SYNOPSIS
5-
.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]
5+
.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]
66
.SH DESCRIPTION
77
This program shows kernel stack traces and task names that were blocked and
88
"off-CPU", along with the stack traces and task names for the threads that woke
@@ -64,6 +64,10 @@ The amount of time in microseconds over which we store traces (default 1)
6464
.TP
6565
\-M MAX_BLOCK_TIME
6666
The amount of time in microseconds under which we store traces (default U64_MAX)
67+
.TP
68+
\-\-state
69+
Filter on this thread state bitmask (eg, 2 == TASK_UNINTERRUPTIBLE).
70+
See include/linux/sched.h for states.
6771
.SH EXAMPLES
6872
.TP
6973
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:

tools/offwaketime.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ def stack_id_err(stack_id):
9393
type=positive_nonzero_int,
9494
help="the amount of time in microseconds under which we " +
9595
"store traces (default U64_MAX)")
96+
parser.add_argument("--state", type=positive_int,
97+
help="filter on this thread state bitmask (eg, 2 == TASK_UNINTERRUPTIBLE" +
98+
") see include/linux/sched.h")
9699
parser.add_argument("--ebpf", action="store_true",
97100
help=argparse.SUPPRESS)
98101
args = parser.parse_args()
@@ -147,7 +150,7 @@ def signal_ignore(signal, frame):
147150
u32 pid = p->pid;
148151
u32 tgid = p->tgid;
149152
150-
if (!(THREAD_FILTER)) {
153+
if (!((THREAD_FILTER) && (STATE_FILTER))) {
151154
return 0;
152155
}
153156
@@ -171,7 +174,7 @@ def signal_ignore(signal, frame):
171174
u64 ts = bpf_ktime_get_ns();
172175
173176
// Record timestamp for the previous Process (Process going into waiting)
174-
if (THREAD_FILTER) {
177+
if ((THREAD_FILTER) && (STATE_FILTER)) {
175178
start.update(&pid, &ts);
176179
}
177180
@@ -234,7 +237,15 @@ def signal_ignore(signal, frame):
234237
else:
235238
thread_context = "all threads"
236239
thread_filter = '1'
240+
if args.state == 0:
241+
state_filter = 'p->state == 0'
242+
elif args.state:
243+
# these states are sometimes bitmask checked
244+
state_filter = 'p->state & %d' % args.state
245+
else:
246+
state_filter = '1'
237247
bpf_text = bpf_text.replace('THREAD_FILTER', thread_filter)
248+
bpf_text = bpf_text.replace('STATE_FILTER', state_filter)
238249

239250
# set stack storage size
240251
bpf_text = bpf_text.replace('STACK_STORAGE_SIZE', str(args.stack_storage_size))

tools/offwaketime_example.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ USAGE message:
307307
# ./offwaketime -h
308308
usage: offwaketime [-h] [-p PID | -t TID | -u | -k] [-U | -K] [-d] [-f]
309309
[--stack-storage-size STACK_STORAGE_SIZE]
310-
[-m MIN_BLOCK_TIME] [-M MAX_BLOCK_TIME]
310+
[-m MIN_BLOCK_TIME] [-M MAX_BLOCK_TIME] [--state STATE]
311311
[duration]
312312

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

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

0 commit comments

Comments
 (0)