blob: 72545941c42058fcbed622f51253df4b48a339d0 [file] [log] [blame]
Brendan Gregg7bf0e492016-01-27 23:17:40 -08001#!/usr/bin/python
2#
3# wakeuptime Summarize sleep to wakeup time by waker kernel stack
4# For Linux, uses BCC, eBPF.
5#
Brendan Gregg62f4c282016-01-30 11:05:40 -08006# USAGE: wakeuptime [-h] [-u] [-p PID] [-v] [-f] [duration]
Brendan Gregg7bf0e492016-01-27 23:17:40 -08007#
Brendan Gregg7bf0e492016-01-27 23:17:40 -08008# Copyright 2016 Netflix, Inc.
9# Licensed under the Apache License, Version 2.0 (the "License")
10#
11# 14-Jan-2016 Brendan Gregg Created this.
12
13from __future__ import print_function
14from bcc import BPF
Brenden Blancoe8001c32018-07-23 08:15:56 -070015from bcc.utils import printb
Brendan Gregg7bf0e492016-01-27 23:17:40 -080016from time import sleep, strftime
17import argparse
18import signal
Sandipan Das2c2d46f2017-11-16 17:14:38 +053019import errno
20from sys import stderr
21
22# arg validation
23def positive_int(val):
24 try:
25 ival = int(val)
26 except ValueError:
27 raise argparse.ArgumentTypeError("must be an integer")
28
29 if ival < 0:
30 raise argparse.ArgumentTypeError("must be positive")
31 return ival
32
33def positive_nonzero_int(val):
34 ival = positive_int(val)
35 if ival == 0:
36 raise argparse.ArgumentTypeError("must be nonzero")
37 return ival
Brendan Gregg7bf0e492016-01-27 23:17:40 -080038
39# arguments
40examples = """examples:
41 ./wakeuptime # trace blocked time with waker stacks
42 ./wakeuptime 5 # trace for 5 seconds only
43 ./wakeuptime -f 5 # 5 seconds, and output in folded format
44 ./wakeuptime -u # don't include kernel threads (user only)
45 ./wakeuptime -p 185 # trace fo PID 185 only
46"""
47parser = argparse.ArgumentParser(
48 description="Summarize sleep to wakeup time by waker kernel stack",
49 formatter_class=argparse.RawDescriptionHelpFormatter,
50 epilog=examples)
51parser.add_argument("-u", "--useronly", action="store_true",
52 help="user threads only (no kernel threads)")
53parser.add_argument("-p", "--pid",
Sandipan Das2c2d46f2017-11-16 17:14:38 +053054 type=positive_int,
Brendan Gregg7bf0e492016-01-27 23:17:40 -080055 help="trace this PID only")
56parser.add_argument("-v", "--verbose", action="store_true",
57 help="show raw addresses")
58parser.add_argument("-f", "--folded", action="store_true",
59 help="output folded format")
Sandipan Das2c2d46f2017-11-16 17:14:38 +053060parser.add_argument("--stack-storage-size", default=1024,
61 type=positive_nonzero_int,
62 help="the number of unique stack traces that can be stored and "
63 "displayed (default 1024)")
Brendan Gregg7bf0e492016-01-27 23:17:40 -080064parser.add_argument("duration", nargs="?", default=99999999,
Sandipan Das2c2d46f2017-11-16 17:14:38 +053065 type=positive_nonzero_int,
Brendan Gregg7bf0e492016-01-27 23:17:40 -080066 help="duration of trace, in seconds")
Sandipan Das2c2d46f2017-11-16 17:14:38 +053067parser.add_argument("-m", "--min-block-time", default=1,
68 type=positive_nonzero_int,
69 help="the amount of time in microseconds over which we " +
70 "store traces (default 1)")
71parser.add_argument("-M", "--max-block-time", default=(1 << 64) - 1,
72 type=positive_nonzero_int,
73 help="the amount of time in microseconds under which we " +
74 "store traces (default U64_MAX)")
Nathan Scottcf0792f2018-02-02 16:56:50 +110075parser.add_argument("--ebpf", action="store_true",
76 help=argparse.SUPPRESS)
Brendan Gregg7bf0e492016-01-27 23:17:40 -080077args = parser.parse_args()
78folded = args.folded
79duration = int(args.duration)
80debug = 0
Brendan Gregg7bf0e492016-01-27 23:17:40 -080081if args.pid and args.useronly:
Sandipan Das2c2d46f2017-11-16 17:14:38 +053082 parser.error("use either -p or -u.")
Brendan Gregg7bf0e492016-01-27 23:17:40 -080083
84# signal handler
85def signal_ignore(signal, frame):
86 print()
87
88# define BPF program
89bpf_text = """
90#include <uapi/linux/ptrace.h>
91#include <linux/sched.h>
92
Sandipan Das2c2d46f2017-11-16 17:14:38 +053093#define MINBLOCK_US MINBLOCK_US_VALUEULL
94#define MAXBLOCK_US MAXBLOCK_US_VALUEULL
Brendan Gregg7bf0e492016-01-27 23:17:40 -080095
96struct key_t {
Sandipan Das2c2d46f2017-11-16 17:14:38 +053097 int w_k_stack_id;
Brendan Gregg7bf0e492016-01-27 23:17:40 -080098 char waker[TASK_COMM_LEN];
99 char target[TASK_COMM_LEN];
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800100};
101BPF_HASH(counts, struct key_t);
102BPF_HASH(start, u32);
Song Liu67ae6052018-02-01 14:59:24 -0800103BPF_STACK_TRACE(stack_traces, STACK_STORAGE_SIZE);
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800104
105int offcpu(struct pt_regs *ctx) {
106 u32 pid = bpf_get_current_pid_tgid();
Sandipan Das3edb4532017-11-17 12:41:06 +0530107 struct task_struct *p = (struct task_struct *) bpf_get_current_task();
108 u64 ts;
109
110 if (FILTER)
111 return 0;
112
113 ts = bpf_ktime_get_ns();
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800114 start.update(&pid, &ts);
115 return 0;
116}
117
118int waker(struct pt_regs *ctx, struct task_struct *p) {
119 u32 pid = p->pid;
120 u64 delta, *tsp, ts;
121
122 tsp = start.lookup(&pid);
123 if (tsp == 0)
124 return 0; // missed start
125 start.delete(&pid);
126
127 if (FILTER)
128 return 0;
129
130 // calculate delta time
131 delta = bpf_ktime_get_ns() - *tsp;
132 delta = delta / 1000;
Sandipan Das2c2d46f2017-11-16 17:14:38 +0530133 if ((delta < MINBLOCK_US) || (delta > MAXBLOCK_US))
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800134 return 0;
135
136 struct key_t key = {};
Sandipan Das2c2d46f2017-11-16 17:14:38 +0530137 u64 zero = 0, *val;
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800138
Sandipan Das2c2d46f2017-11-16 17:14:38 +0530139 key.w_k_stack_id = stack_traces.get_stackid(ctx, BPF_F_REUSE_STACKID);
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800140 bpf_probe_read(&key.target, sizeof(key.target), p->comm);
141 bpf_get_current_comm(&key.waker, sizeof(key.waker));
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800142
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800143 val = counts.lookup_or_init(&key, &zero);
144 (*val) += delta;
145 return 0;
146}
147"""
148if args.pid:
149 filter = 'pid != %s' % args.pid
150elif args.useronly:
151 filter = 'p->flags & PF_KTHREAD'
152else:
153 filter = '0'
154bpf_text = bpf_text.replace('FILTER', filter)
Sandipan Das2c2d46f2017-11-16 17:14:38 +0530155
156# set stack storage size
157bpf_text = bpf_text.replace('STACK_STORAGE_SIZE', str(args.stack_storage_size))
158bpf_text = bpf_text.replace('MINBLOCK_US_VALUE', str(args.min_block_time))
159bpf_text = bpf_text.replace('MAXBLOCK_US_VALUE', str(args.max_block_time))
160
Nathan Scottcf0792f2018-02-02 16:56:50 +1100161if debug or args.ebpf:
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800162 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100163 if args.ebpf:
164 exit()
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800165
166# initialize BPF
167b = BPF(text=bpf_text)
168b.attach_kprobe(event="schedule", fn_name="offcpu")
169b.attach_kprobe(event="try_to_wake_up", fn_name="waker")
170matched = b.num_open_kprobes()
171if matched == 0:
172 print("0 functions traced. Exiting.")
173 exit()
174
175# header
176if not folded:
177 print("Tracing blocked time (us) by kernel stack", end="")
178 if duration < 99999999:
179 print(" for %d secs." % duration)
180 else:
181 print("... Hit Ctrl-C to end.")
182
183# output
184while (1):
185 try:
186 sleep(duration)
187 except KeyboardInterrupt:
188 # as cleanup can take many seconds, trap Ctrl-C:
189 signal.signal(signal.SIGINT, signal_ignore)
190
191 if not folded:
192 print()
Sandipan Das2c2d46f2017-11-16 17:14:38 +0530193 missing_stacks = 0
194 has_enomem = False
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800195 counts = b.get_table("counts")
Sandipan Das2c2d46f2017-11-16 17:14:38 +0530196 stack_traces = b.get_table("stack_traces")
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800197 for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
Sandipan Das2c2d46f2017-11-16 17:14:38 +0530198 # handle get_stackid errors
199 # check for an ENOMEM error
200 if k.w_k_stack_id == -errno.ENOMEM:
201 missing_stacks += 1
202 continue
203
204 waker_kernel_stack = [] if k.w_k_stack_id < 1 else \
205 reversed(list(stack_traces.walk(k.w_k_stack_id))[1:])
206
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800207 if folded:
208 # print folded stack output
Sandipan Das2c2d46f2017-11-16 17:14:38 +0530209 line = \
Brenden Blancoe8001c32018-07-23 08:15:56 -0700210 [k.waker] + \
Sandipan Das2c2d46f2017-11-16 17:14:38 +0530211 [b.ksym(addr)
212 for addr in reversed(list(waker_kernel_stack))] + \
Brenden Blancoe8001c32018-07-23 08:15:56 -0700213 [k.target]
214 printb(b"%s %d" % (b";".join(line), v.value))
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800215 else:
216 # print default multi-line stack output
Brenden Blancoe8001c32018-07-23 08:15:56 -0700217 printb(b" %-16s %s" % (b"target:", k.target))
Sandipan Das2c2d46f2017-11-16 17:14:38 +0530218 for addr in waker_kernel_stack:
Brenden Blancoe8001c32018-07-23 08:15:56 -0700219 printb(b" %-16x %s" % (addr, b.ksym(addr)))
220 printb(b" %-16s %s" % (b"waker:", k.waker))
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800221 print(" %d\n" % v.value)
222 counts.clear()
223
Sandipan Das2c2d46f2017-11-16 17:14:38 +0530224 if missing_stacks > 0:
225 enomem_str = " Consider increasing --stack-storage-size."
226 print("WARNING: %d stack traces could not be displayed.%s" %
227 (missing_stacks, enomem_str),
228 file=stderr)
229
Brendan Gregg7bf0e492016-01-27 23:17:40 -0800230 if not folded:
231 print("Detaching...")
232 exit()