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 |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 196 | u64 zero = 0, *val; |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 197 | struct key_t key = {}; |
| 198 | struct wokeby_t *woke; |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 199 | |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 200 | bpf_get_current_comm(&key.target, sizeof(key.target)); |
| 201 | key.t_pid = pid; |
| 202 | key.t_tgid = tgid; |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 203 | key.t_k_stack_id = KERNEL_STACK_GET; |
| 204 | key.t_u_stack_id = USER_STACK_GET; |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 205 | |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 206 | woke = wokeby.lookup(&pid); |
| 207 | if (woke) { |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 208 | key.w_k_stack_id = woke->k_stack_id; |
| 209 | key.w_u_stack_id = woke->u_stack_id; |
| 210 | key.w_pid = woke->w_pid; |
Teng Qin | e7432d4 | 2018-04-19 14:45:18 -0700 | [diff] [blame] | 211 | key.w_tgid = woke->w_tgid; |
Alexei Starovoitov | 7583a4e | 2016-02-03 21:25:43 -0800 | [diff] [blame] | 212 | __builtin_memcpy(&key.waker, woke->name, TASK_COMM_LEN); |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 213 | wokeby.delete(&pid); |
| 214 | } |
| 215 | |
| 216 | val = counts.lookup_or_init(&key, &zero); |
| 217 | (*val) += delta; |
| 218 | return 0; |
| 219 | } |
| 220 | """ |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 221 | |
| 222 | # set thread filter |
| 223 | thread_context = "" |
| 224 | if args.tgid is not None: |
| 225 | thread_context = "PID %d" % args.tgid |
| 226 | thread_filter = 'tgid == %d' % args.tgid |
| 227 | elif args.pid is not None: |
| 228 | thread_context = "TID %d" % args.pid |
| 229 | thread_filter = 'pid == %d' % args.pid |
| 230 | elif args.user_threads_only: |
| 231 | thread_context = "user threads" |
| 232 | thread_filter = '!(p->flags & PF_KTHREAD)' |
| 233 | elif args.kernel_threads_only: |
| 234 | thread_context = "kernel threads" |
| 235 | thread_filter = 'p->flags & PF_KTHREAD' |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 236 | else: |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 237 | thread_context = "all threads" |
| 238 | thread_filter = '1' |
| 239 | bpf_text = bpf_text.replace('THREAD_FILTER', thread_filter) |
| 240 | |
| 241 | # set stack storage size |
| 242 | bpf_text = bpf_text.replace('STACK_STORAGE_SIZE', str(args.stack_storage_size)) |
| 243 | bpf_text = bpf_text.replace('MINBLOCK_US_VALUE', str(args.min_block_time)) |
| 244 | bpf_text = bpf_text.replace('MAXBLOCK_US_VALUE', str(args.max_block_time)) |
| 245 | |
| 246 | # handle stack args |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 247 | kernel_stack_get = "stack_traces.get_stackid(ctx, 0)" |
| 248 | user_stack_get = "stack_traces.get_stackid(ctx, BPF_F_USER_STACK)" |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 249 | stack_context = "" |
| 250 | if args.user_stacks_only: |
| 251 | stack_context = "user" |
| 252 | kernel_stack_get = "-1" |
| 253 | elif args.kernel_stacks_only: |
| 254 | stack_context = "kernel" |
| 255 | user_stack_get = "-1" |
| 256 | else: |
| 257 | stack_context = "user + kernel" |
| 258 | bpf_text = bpf_text.replace('USER_STACK_GET', user_stack_get) |
| 259 | bpf_text = bpf_text.replace('KERNEL_STACK_GET', kernel_stack_get) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 260 | if args.ebpf: |
| 261 | print(bpf_text) |
| 262 | exit() |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 263 | |
| 264 | # initialize BPF |
| 265 | b = BPF(text=bpf_text) |
| 266 | b.attach_kprobe(event="finish_task_switch", fn_name="oncpu") |
| 267 | b.attach_kprobe(event="try_to_wake_up", fn_name="waker") |
| 268 | matched = b.num_open_kprobes() |
| 269 | if matched == 0: |
| 270 | print("0 functions traced. Exiting.") |
| 271 | exit() |
| 272 | |
| 273 | # header |
| 274 | if not folded: |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 275 | print("Tracing blocked time (us) by %s off-CPU and waker stack" % |
| 276 | stack_context, end="") |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 277 | if duration < 99999999: |
| 278 | print(" for %d secs." % duration) |
| 279 | else: |
| 280 | print("... Hit Ctrl-C to end.") |
| 281 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 282 | # as cleanup can take many seconds, trap Ctrl-C: |
| 283 | # print a newline for folded output on Ctrl-C |
| 284 | signal.signal(signal.SIGINT, signal_ignore) |
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 | sleep(duration) |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 287 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 288 | if not folded: |
| 289 | print() |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 290 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 291 | missing_stacks = 0 |
| 292 | has_enomem = False |
| 293 | counts = b.get_table("counts") |
| 294 | stack_traces = b.get_table("stack_traces") |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 295 | need_delimiter = args.delimited and not (args.kernel_stacks_only or |
| 296 | args.user_stacks_only) |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 297 | for k, v in sorted(counts.items(), key=lambda counts: counts[1].value): |
| 298 | # handle get_stackid errors |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 299 | if not args.user_stacks_only: |
| 300 | missing_stacks += int(stack_id_err(k.w_k_stack_id)) |
| 301 | missing_stacks += int(stack_id_err(k.t_k_stack_id)) |
| 302 | has_enomem = has_enomem or (k.w_k_stack_id == -errno.ENOMEM) or \ |
| 303 | (k.t_k_stack_id == -errno.ENOMEM) |
| 304 | if not args.kernel_stacks_only: |
| 305 | missing_stacks += int(stack_id_err(k.w_u_stack_id)) |
| 306 | missing_stacks += int(stack_id_err(k.t_u_stack_id)) |
| 307 | has_enomem = has_enomem or (k.w_u_stack_id == -errno.ENOMEM) or \ |
| 308 | (k.t_u_stack_id == -errno.ENOMEM) |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 309 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 310 | waker_user_stack = [] if k.w_u_stack_id < 1 else \ |
| 311 | reversed(list(stack_traces.walk(k.w_u_stack_id))[1:]) |
| 312 | waker_kernel_stack = [] if k.w_k_stack_id < 1 else \ |
| 313 | reversed(list(stack_traces.walk(k.w_k_stack_id))[1:]) |
| 314 | target_user_stack = [] if k.t_u_stack_id < 1 else \ |
| 315 | stack_traces.walk(k.t_u_stack_id) |
| 316 | target_kernel_stack = [] if k.t_k_stack_id < 1 else \ |
| 317 | stack_traces.walk(k.t_k_stack_id) |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 318 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 319 | if folded: |
| 320 | # print folded stack output |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 321 | line = [k.target.decode()] |
| 322 | if not args.kernel_stacks_only: |
| 323 | if stack_id_err(k.t_u_stack_id): |
| 324 | line.append("[Missed User Stack]") |
| 325 | else: |
| 326 | line.extend([b.sym(addr, k.t_tgid) |
| 327 | for addr in reversed(list(target_user_stack)[1:])]) |
| 328 | if not args.user_stacks_only: |
| 329 | line.extend(["-"] if (need_delimiter and k.t_k_stack_id > 0 and k.t_u_stack_id > 0) else []) |
| 330 | if stack_id_err(k.t_k_stack_id): |
| 331 | line.append("[Missed Kernel Stack]") |
| 332 | else: |
| 333 | line.extend([b.ksym(addr) |
| 334 | for addr in reversed(list(target_kernel_stack)[1:])]) |
| 335 | line.append("--") |
| 336 | if not args.user_stacks_only: |
| 337 | if stack_id_err(k.w_k_stack_id): |
| 338 | line.append("[Missed Kernel Stack]") |
| 339 | else: |
| 340 | line.extend([b.ksym(addr) |
| 341 | for addr in reversed(list(waker_kernel_stack))]) |
| 342 | if not args.kernel_stacks_only: |
| 343 | line.extend(["-"] if (need_delimiter and k.w_u_stack_id > 0 and k.w_k_stack_id > 0) else []) |
| 344 | if stack_id_err(k.w_u_stack_id): |
| 345 | line.extend("[Missed User Stack]") |
| 346 | else: |
| 347 | line.extend([b.sym(addr, k.w_tgid) |
| 348 | for addr in reversed(list(waker_user_stack))]) |
| 349 | line.append(k.waker.decode()) |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 350 | print("%s %d" % (";".join(line), v.value)) |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 351 | else: |
| 352 | # print wakeup name then stack in reverse order |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 353 | print(" %-16s %s %s" % ("waker:", k.waker.decode(), k.t_pid)) |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 354 | if not args.kernel_stacks_only: |
| 355 | if stack_id_err(k.w_u_stack_id): |
| 356 | print(" [Missed User Stack]") |
| 357 | else: |
| 358 | for addr in waker_user_stack: |
| 359 | print(" %s" % b.sym(addr, k.w_tgid)) |
| 360 | if not args.user_stacks_only: |
| 361 | if need_delimiter and k.w_u_stack_id > 0 and k.w_k_stack_id > 0: |
| 362 | print(" -") |
| 363 | if stack_id_err(k.w_k_stack_id): |
| 364 | print(" [Missed Kernel Stack]") |
| 365 | else: |
| 366 | for addr in waker_kernel_stack: |
| 367 | print(" %s" % b.ksym(addr)) |
Brendan Gregg | af2b46a | 2016-01-30 11:02:29 -0800 | [diff] [blame] | 368 | |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 369 | # print waker/wakee delimiter |
| 370 | print(" %-16s %s" % ("--", "--")) |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 371 | |
| 372 | if not args.user_stacks_only: |
| 373 | if stack_id_err(k.t_k_stack_id): |
| 374 | print(" [Missed Kernel Stack]") |
| 375 | else: |
| 376 | for addr in target_kernel_stack: |
| 377 | print(" %s" % b.ksym(addr)) |
| 378 | if not args.kernel_stacks_only: |
| 379 | if need_delimiter and k.t_u_stack_id > 0 and k.t_k_stack_id > 0: |
| 380 | print(" -") |
| 381 | if stack_id_err(k.t_u_stack_id): |
| 382 | print(" [Missed User Stack]") |
| 383 | else: |
| 384 | for addr in target_user_stack: |
| 385 | print(" %s" % b.sym(addr, k.t_tgid)) |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 386 | print(" %-16s %s %s" % ("target:", k.target.decode(), k.w_pid)) |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 387 | print(" %d\n" % v.value) |
| 388 | |
| 389 | if missing_stacks > 0: |
| 390 | enomem_str = " Consider increasing --stack-storage-size." |
Teng Qin | e778db0 | 2018-04-24 16:11:49 -0700 | [diff] [blame] | 391 | print("WARNING: %d stack traces lost and could not be displayed.%s" % |
| 392 | (missing_stacks, (enomem_str if has_enomem else "")), |
ceeaspb | 47cecb6 | 2016-11-26 22:36:10 +0000 | [diff] [blame] | 393 | file=stderr) |