Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # offwaketime Summarize blocked time by kernel off-CPU stack + waker stack |
| 4 | # For Linux, uses BCC, eBPF. |
| 5 | # |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 6 | # USAGE: offwaketime [-h] [-p PID | -u | -k] [-U | -K] [-f] [duration] |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 7 | # |
| 8 | # Copyright 2016 Netflix, Inc. |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 10 | # |
Alexei Starovoitov | 7583a4e | 2016-02-03 21:25:43 -0800 | [diff] [blame] | 11 | # 20-Jan-2016 Brendan Gregg Created this. |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 12 | |
| 13 | from __future__ import print_function |
| 14 | from bcc import BPF |
Alexei Starovoitov | 7583a4e | 2016-02-03 21:25:43 -0800 | [diff] [blame] | 15 | from time import sleep |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 16 | import argparse |
| 17 | import signal |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 18 | import errno |
| 19 | from sys import stderr |
| 20 | |
| 21 | # arg validation |
| 22 | def positive_int(val): |
| 23 | try: |
| 24 | ival = int(val) |
| 25 | except ValueError: |
| 26 | raise argparse.ArgumentTypeError("must be an integer") |
| 27 | |
| 28 | if ival < 0: |
| 29 | raise argparse.ArgumentTypeError("must be positive") |
| 30 | return ival |
| 31 | |
| 32 | def positive_nonzero_int(val): |
| 33 | ival = positive_int(val) |
| 34 | if ival == 0: |
| 35 | raise argparse.ArgumentTypeError("must be nonzero") |
| 36 | return ival |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 37 | |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 38 | def stack_id_err(stack_id): |
| 39 | # -EFAULT in get_stackid normally means the stack-trace is not availible, |
| 40 | # Such as getting kernel stack trace in userspace code |
| 41 | return (stack_id < 0) and (stack_id != -errno.EFAULT) |
| 42 | |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 43 | # arguments |
| 44 | examples = """examples: |
| 45 | ./offwaketime # trace off-CPU + waker stack time until Ctrl-C |
| 46 | ./offwaketime 5 # trace for 5 seconds only |
| 47 | ./offwaketime -f 5 # 5 seconds, and output in folded format |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 48 | ./offwaketime -m 1000 # trace only events that last more than 1000 usec |
| 49 | ./offwaketime -M 9000 # trace only events that last less than 9000 usec |
| 50 | ./offwaketime -p 185 # only trace threads for PID 185 |
| 51 | ./offwaketime -t 188 # only trace thread 188 |
| 52 | ./offwaketime -u # only trace user threads (no kernel) |
| 53 | ./offwaketime -k # only trace kernel threads (no user) |
| 54 | ./offwaketime -U # only show user space stacks (no kernel) |
| 55 | ./offwaketime -K # only show kernel space stacks (no user) |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 56 | """ |
| 57 | parser = argparse.ArgumentParser( |
| 58 | description="Summarize blocked time by kernel stack trace + waker stack", |
| 59 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 60 | epilog=examples) |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 61 | thread_group = parser.add_mutually_exclusive_group() |
| 62 | # Note: this script provides --pid and --tid flags but their arguments are |
| 63 | # referred to internally using kernel nomenclature: TGID and PID. |
| 64 | thread_group.add_argument("-p", "--pid", metavar="PID", dest="tgid", |
| 65 | help="trace this PID only", type=positive_int) |
| 66 | thread_group.add_argument("-t", "--tid", metavar="TID", dest="pid", |
| 67 | help="trace this TID only", type=positive_int) |
| 68 | thread_group.add_argument("-u", "--user-threads-only", action="store_true", |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 69 | help="user threads only (no kernel threads)") |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 70 | thread_group.add_argument("-k", "--kernel-threads-only", action="store_true", |
| 71 | help="kernel threads only (no user threads)") |
| 72 | stack_group = parser.add_mutually_exclusive_group() |
| 73 | stack_group.add_argument("-U", "--user-stacks-only", action="store_true", |
| 74 | help="show stacks from user space only (no kernel space stacks)") |
| 75 | stack_group.add_argument("-K", "--kernel-stacks-only", action="store_true", |
| 76 | help="show stacks from kernel space only (no user space stacks)") |
| 77 | parser.add_argument("-d", "--delimited", action="store_true", |
| 78 | help="insert delimiter between kernel/user stacks") |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 79 | parser.add_argument("-f", "--folded", action="store_true", |
| 80 | help="output folded format") |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 81 | parser.add_argument("--stack-storage-size", default=1024, |
| 82 | type=positive_nonzero_int, |
| 83 | help="the number of unique stack traces that can be stored and " |
| 84 | "displayed (default 1024)") |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 85 | parser.add_argument("duration", nargs="?", default=99999999, |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 86 | type=positive_nonzero_int, |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 87 | help="duration of trace, in seconds") |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 88 | parser.add_argument("-m", "--min-block-time", default=1, |
| 89 | type=positive_nonzero_int, |
| 90 | help="the amount of time in microseconds over which we " + |
| 91 | "store traces (default 1)") |
| 92 | parser.add_argument("-M", "--max-block-time", default=(1 << 64) - 1, |
| 93 | type=positive_nonzero_int, |
| 94 | help="the amount of time in microseconds under which we " + |
| 95 | "store traces (default U64_MAX)") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 96 | parser.add_argument("--ebpf", action="store_true", |
| 97 | help=argparse.SUPPRESS) |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 98 | args = parser.parse_args() |
| 99 | folded = args.folded |
| 100 | duration = int(args.duration) |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 101 | |
| 102 | # signal handler |
| 103 | def signal_ignore(signal, frame): |
| 104 | print() |
| 105 | |
| 106 | # define BPF program |
| 107 | bpf_text = """ |
| 108 | #include <uapi/linux/ptrace.h> |
| 109 | #include <linux/sched.h> |
| 110 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 111 | #define MINBLOCK_US MINBLOCK_US_VALUEULL |
| 112 | #define MAXBLOCK_US MAXBLOCK_US_VALUEULL |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 113 | |
| 114 | struct key_t { |
| 115 | char waker[TASK_COMM_LEN]; |
| 116 | char target[TASK_COMM_LEN]; |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 117 | int w_k_stack_id; |
| 118 | int w_u_stack_id; |
| 119 | int t_k_stack_id; |
| 120 | int t_u_stack_id; |
| 121 | u32 t_pid; |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 122 | u32 t_tgid; |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 123 | u32 w_pid; |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 124 | u32 w_tgid; |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 125 | }; |
| 126 | BPF_HASH(counts, struct key_t); |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 127 | |
| 128 | // Key of this hash is PID of waiting Process, |
| 129 | // value is timestamp when it went into waiting |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 130 | BPF_HASH(start, u32); |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 131 | |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 132 | struct wokeby_t { |
| 133 | char name[TASK_COMM_LEN]; |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 134 | int k_stack_id; |
| 135 | int u_stack_id; |
| 136 | int w_pid; |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 137 | int w_tgid; |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 138 | }; |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 139 | // Key of the hash is PID of the Process to be waken, value is information |
| 140 | // of the Process who wakes it |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 141 | BPF_HASH(wokeby, u32, struct wokeby_t); |
| 142 | |
Song Liu | 67ae605 | 2018-02-01 14:59:24 -0800 | [diff] [blame] | 143 | BPF_STACK_TRACE(stack_traces, STACK_STORAGE_SIZE); |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 144 | |
| 145 | int waker(struct pt_regs *ctx, struct task_struct *p) { |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 146 | // PID and TGID of the target Process to be waken |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 147 | u32 pid = p->pid; |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 148 | u32 tgid = p->tgid; |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 149 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 150 | if (!(THREAD_FILTER)) { |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 151 | return 0; |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 152 | } |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 153 | |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 154 | // Construct information about current (the waker) Process |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 155 | struct wokeby_t woke = {}; |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 156 | bpf_get_current_comm(&woke.name, sizeof(woke.name)); |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 157 | woke.k_stack_id = KERNEL_STACK_GET; |
| 158 | woke.u_stack_id = USER_STACK_GET; |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 159 | woke.w_pid = bpf_get_current_pid_tgid(); |
| 160 | woke.w_tgid = bpf_get_current_pid_tgid() >> 32; |
| 161 | |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 162 | wokeby.update(&pid, &woke); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | int oncpu(struct pt_regs *ctx, struct task_struct *p) { |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 167 | // PID and TGID of the previous Process (Process going into waiting) |
| 168 | u32 pid = p->pid; |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 169 | u32 tgid = p->tgid; |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 170 | u64 *tsp; |
| 171 | u64 ts = bpf_ktime_get_ns(); |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 172 | |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 173 | // Record timestamp for the previous Process (Process going into waiting) |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 174 | if (THREAD_FILTER) { |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 175 | start.update(&pid, &ts); |
| 176 | } |
| 177 | |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 178 | // Calculate current Process's wait time by finding the timestamp of when |
| 179 | // it went into waiting. |
| 180 | // pid and tgid are now the PID and TGID of the current (waking) Process. |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 181 | pid = bpf_get_current_pid_tgid(); |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 182 | tgid = bpf_get_current_pid_tgid() >> 32; |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 183 | tsp = start.lookup(&pid); |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 184 | if (tsp == 0) { |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 185 | // Missed or filtered when the Process went into waiting |
| 186 | return 0; |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 187 | } |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 188 | u64 delta = ts - *tsp; |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 189 | start.delete(&pid); |
| 190 | delta = delta / 1000; |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 191 | if ((delta < MINBLOCK_US) || (delta > MAXBLOCK_US)) { |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 192 | return 0; |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 193 | } |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 194 | |
| 195 | // create map key |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 196 | struct key_t key = {}; |
| 197 | struct wokeby_t *woke; |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 198 | |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 199 | bpf_get_current_comm(&key.target, sizeof(key.target)); |
| 200 | key.t_pid = pid; |
| 201 | key.t_tgid = tgid; |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 202 | key.t_k_stack_id = KERNEL_STACK_GET; |
| 203 | key.t_u_stack_id = USER_STACK_GET; |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 204 | |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 205 | woke = wokeby.lookup(&pid); |
| 206 | if (woke) { |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 207 | key.w_k_stack_id = woke->k_stack_id; |
| 208 | key.w_u_stack_id = woke->u_stack_id; |
| 209 | key.w_pid = woke->w_pid; |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 210 | key.w_tgid = woke->w_tgid; |
Alexei Starovoitov | 7583a4e | 2016-02-03 21:25:43 -0800 | [diff] [blame] | 211 | __builtin_memcpy(&key.waker, woke->name, TASK_COMM_LEN); |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 212 | wokeby.delete(&pid); |
| 213 | } |
| 214 | |
Javier Honduvilla Coto | 64bf965 | 2018-08-01 06:50:19 +0200 | [diff] [blame] | 215 | counts.increment(key, delta); |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | """ |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 219 | |
| 220 | # set thread filter |
| 221 | thread_context = "" |
| 222 | if args.tgid is not None: |
| 223 | thread_context = "PID %d" % args.tgid |
| 224 | thread_filter = 'tgid == %d' % args.tgid |
| 225 | elif args.pid is not None: |
| 226 | thread_context = "TID %d" % args.pid |
| 227 | thread_filter = 'pid == %d' % args.pid |
| 228 | elif args.user_threads_only: |
| 229 | thread_context = "user threads" |
| 230 | thread_filter = '!(p->flags & PF_KTHREAD)' |
| 231 | elif args.kernel_threads_only: |
| 232 | thread_context = "kernel threads" |
| 233 | thread_filter = 'p->flags & PF_KTHREAD' |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 234 | else: |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 235 | thread_context = "all threads" |
| 236 | thread_filter = '1' |
| 237 | bpf_text = bpf_text.replace('THREAD_FILTER', thread_filter) |
| 238 | |
| 239 | # set stack storage size |
| 240 | bpf_text = bpf_text.replace('STACK_STORAGE_SIZE', str(args.stack_storage_size)) |
| 241 | bpf_text = bpf_text.replace('MINBLOCK_US_VALUE', str(args.min_block_time)) |
| 242 | bpf_text = bpf_text.replace('MAXBLOCK_US_VALUE', str(args.max_block_time)) |
| 243 | |
| 244 | # handle stack args |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 245 | kernel_stack_get = "stack_traces.get_stackid(ctx, 0)" |
| 246 | user_stack_get = "stack_traces.get_stackid(ctx, BPF_F_USER_STACK)" |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 247 | stack_context = "" |
| 248 | if args.user_stacks_only: |
| 249 | stack_context = "user" |
| 250 | kernel_stack_get = "-1" |
| 251 | elif args.kernel_stacks_only: |
| 252 | stack_context = "kernel" |
| 253 | user_stack_get = "-1" |
| 254 | else: |
| 255 | stack_context = "user + kernel" |
| 256 | bpf_text = bpf_text.replace('USER_STACK_GET', user_stack_get) |
| 257 | bpf_text = bpf_text.replace('KERNEL_STACK_GET', kernel_stack_get) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 258 | if args.ebpf: |
| 259 | print(bpf_text) |
| 260 | exit() |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 261 | |
| 262 | # initialize BPF |
| 263 | b = BPF(text=bpf_text) |
| 264 | b.attach_kprobe(event="finish_task_switch", fn_name="oncpu") |
| 265 | b.attach_kprobe(event="try_to_wake_up", fn_name="waker") |
| 266 | matched = b.num_open_kprobes() |
| 267 | if matched == 0: |
| 268 | print("0 functions traced. Exiting.") |
| 269 | exit() |
| 270 | |
| 271 | # header |
| 272 | if not folded: |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 273 | print("Tracing blocked time (us) by %s off-CPU and waker stack" % |
| 274 | stack_context, end="") |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 275 | if duration < 99999999: |
| 276 | print(" for %d secs." % duration) |
| 277 | else: |
| 278 | print("... Hit Ctrl-C to end.") |
| 279 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 280 | # as cleanup can take many seconds, trap Ctrl-C: |
| 281 | # print a newline for folded output on Ctrl-C |
| 282 | signal.signal(signal.SIGINT, signal_ignore) |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 283 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 284 | sleep(duration) |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 285 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 286 | if not folded: |
| 287 | print() |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 288 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 289 | missing_stacks = 0 |
| 290 | has_enomem = False |
| 291 | counts = b.get_table("counts") |
| 292 | stack_traces = b.get_table("stack_traces") |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 293 | need_delimiter = args.delimited and not (args.kernel_stacks_only or |
| 294 | args.user_stacks_only) |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 295 | for k, v in sorted(counts.items(), key=lambda counts: counts[1].value): |
| 296 | # handle get_stackid errors |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 297 | if not args.user_stacks_only: |
| 298 | missing_stacks += int(stack_id_err(k.w_k_stack_id)) |
| 299 | missing_stacks += int(stack_id_err(k.t_k_stack_id)) |
| 300 | has_enomem = has_enomem or (k.w_k_stack_id == -errno.ENOMEM) or \ |
| 301 | (k.t_k_stack_id == -errno.ENOMEM) |
| 302 | if not args.kernel_stacks_only: |
| 303 | missing_stacks += int(stack_id_err(k.w_u_stack_id)) |
| 304 | missing_stacks += int(stack_id_err(k.t_u_stack_id)) |
| 305 | has_enomem = has_enomem or (k.w_u_stack_id == -errno.ENOMEM) or \ |
| 306 | (k.t_u_stack_id == -errno.ENOMEM) |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 307 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 308 | waker_user_stack = [] if k.w_u_stack_id < 1 else \ |
| 309 | reversed(list(stack_traces.walk(k.w_u_stack_id))[1:]) |
| 310 | waker_kernel_stack = [] if k.w_k_stack_id < 1 else \ |
| 311 | reversed(list(stack_traces.walk(k.w_k_stack_id))[1:]) |
| 312 | target_user_stack = [] if k.t_u_stack_id < 1 else \ |
| 313 | stack_traces.walk(k.t_u_stack_id) |
| 314 | target_kernel_stack = [] if k.t_k_stack_id < 1 else \ |
| 315 | stack_traces.walk(k.t_k_stack_id) |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 316 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 317 | if folded: |
| 318 | # print folded stack output |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 319 | line = [k.target.decode()] |
| 320 | if not args.kernel_stacks_only: |
| 321 | if stack_id_err(k.t_u_stack_id): |
| 322 | line.append("[Missed User Stack]") |
| 323 | else: |
| 324 | line.extend([b.sym(addr, k.t_tgid) |
| 325 | for addr in reversed(list(target_user_stack)[1:])]) |
| 326 | if not args.user_stacks_only: |
| 327 | line.extend(["-"] if (need_delimiter and k.t_k_stack_id > 0 and k.t_u_stack_id > 0) else []) |
| 328 | if stack_id_err(k.t_k_stack_id): |
| 329 | line.append("[Missed Kernel Stack]") |
| 330 | else: |
| 331 | line.extend([b.ksym(addr) |
| 332 | for addr in reversed(list(target_kernel_stack)[1:])]) |
| 333 | line.append("--") |
| 334 | if not args.user_stacks_only: |
| 335 | if stack_id_err(k.w_k_stack_id): |
| 336 | line.append("[Missed Kernel Stack]") |
| 337 | else: |
| 338 | line.extend([b.ksym(addr) |
| 339 | for addr in reversed(list(waker_kernel_stack))]) |
| 340 | if not args.kernel_stacks_only: |
| 341 | line.extend(["-"] if (need_delimiter and k.w_u_stack_id > 0 and k.w_k_stack_id > 0) else []) |
| 342 | if stack_id_err(k.w_u_stack_id): |
| 343 | line.extend("[Missed User Stack]") |
| 344 | else: |
| 345 | line.extend([b.sym(addr, k.w_tgid) |
| 346 | for addr in reversed(list(waker_user_stack))]) |
| 347 | line.append(k.waker.decode()) |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 348 | print("%s %d" % (";".join(line), v.value)) |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 349 | else: |
| 350 | # print wakeup name then stack in reverse order |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 351 | print(" %-16s %s %s" % ("waker:", k.waker.decode(), k.t_pid)) |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 352 | if not args.kernel_stacks_only: |
| 353 | if stack_id_err(k.w_u_stack_id): |
| 354 | print(" [Missed User Stack]") |
| 355 | else: |
| 356 | for addr in waker_user_stack: |
| 357 | print(" %s" % b.sym(addr, k.w_tgid)) |
| 358 | if not args.user_stacks_only: |
| 359 | if need_delimiter and k.w_u_stack_id > 0 and k.w_k_stack_id > 0: |
| 360 | print(" -") |
| 361 | if stack_id_err(k.w_k_stack_id): |
| 362 | print(" [Missed Kernel Stack]") |
| 363 | else: |
| 364 | for addr in waker_kernel_stack: |
| 365 | print(" %s" % b.ksym(addr)) |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 366 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 367 | # print waker/wakee delimiter |
| 368 | print(" %-16s %s" % ("--", "--")) |
Javier Honduvilla Coto | 64bf965 | 2018-08-01 06:50:19 +0200 | [diff] [blame] | 369 | |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 370 | if not args.user_stacks_only: |
| 371 | if stack_id_err(k.t_k_stack_id): |
| 372 | print(" [Missed Kernel Stack]") |
| 373 | else: |
| 374 | for addr in target_kernel_stack: |
| 375 | print(" %s" % b.ksym(addr)) |
| 376 | if not args.kernel_stacks_only: |
| 377 | if need_delimiter and k.t_u_stack_id > 0 and k.t_k_stack_id > 0: |
| 378 | print(" -") |
| 379 | if stack_id_err(k.t_u_stack_id): |
| 380 | print(" [Missed User Stack]") |
| 381 | else: |
| 382 | for addr in target_user_stack: |
| 383 | print(" %s" % b.sym(addr, k.t_tgid)) |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 384 | print(" %-16s %s %s" % ("target:", k.target.decode(), k.w_pid)) |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 385 | print(" %d\n" % v.value) |
| 386 | |
| 387 | if missing_stacks > 0: |
| 388 | enomem_str = " Consider increasing --stack-storage-size." |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 389 | print("WARNING: %d stack traces lost and could not be displayed.%s" % |
| 390 | (missing_stacks, (enomem_str if has_enomem else "")), |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 391 | file=stderr) |