Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 3 | # offcputime Summarize off-CPU time by stack trace |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 4 | # For Linux, uses BCC, eBPF. |
| 5 | # |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 6 | # USAGE: offcputime [-h] [-p PID | -u | -k] [-U | -K] [-f] [duration] |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 7 | # |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 8 | # Copyright 2016 Netflix, Inc. |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 10 | # |
| 11 | # 13-Jan-2016 Brendan Gregg Created this. |
| 12 | |
| 13 | from __future__ import print_function |
| 14 | from bcc import BPF |
Andrew Birchall | ee7e5b4 | 2016-05-03 16:54:00 -0700 | [diff] [blame] | 15 | from sys import stderr |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 16 | from time import sleep, strftime |
| 17 | import argparse |
Teng Qin | 0b11d22 | 2016-07-18 13:21:10 -0700 | [diff] [blame] | 18 | import errno |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 19 | import signal |
| 20 | |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 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 |
| 37 | |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 38 | # arguments |
| 39 | examples = """examples: |
| 40 | ./offcputime # trace off-CPU stack time until Ctrl-C |
| 41 | ./offcputime 5 # trace for 5 seconds only |
| 42 | ./offcputime -f 5 # 5 seconds, and output in folded format |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 43 | ./offcputime -m 1000 # trace only events that last more than 1000 usec |
| 44 | ./offcputime -M 10000 # trace only events that last less than 10000 usec |
Andrew Birchall | 582b5dd | 2016-05-04 16:03:34 -0700 | [diff] [blame] | 45 | ./offcputime -p 185 # only trace threads for PID 185 |
Mark Drayton | 66bf2e8 | 2016-07-31 22:47:07 +0100 | [diff] [blame] | 46 | ./offcputime -t 188 # only trace thread 188 |
Andrew Birchall | 582b5dd | 2016-05-04 16:03:34 -0700 | [diff] [blame] | 47 | ./offcputime -u # only trace user threads (no kernel) |
| 48 | ./offcputime -k # only trace kernel threads (no user) |
Andrew Birchall | 7f0a6f8 | 2016-05-24 01:44:41 -0700 | [diff] [blame] | 49 | ./offcputime -U # only show user space stacks (no kernel) |
| 50 | ./offcputime -K # only show kernel space stacks (no user) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 51 | """ |
| 52 | parser = argparse.ArgumentParser( |
Andrew Birchall | 7f0a6f8 | 2016-05-24 01:44:41 -0700 | [diff] [blame] | 53 | description="Summarize off-CPU time by stack trace", |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 54 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 55 | epilog=examples) |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 56 | thread_group = parser.add_mutually_exclusive_group() |
Mark Drayton | 66bf2e8 | 2016-07-31 22:47:07 +0100 | [diff] [blame] | 57 | # Note: this script provides --pid and --tid flags but their arguments are |
| 58 | # referred to internally using kernel nomenclature: TGID and PID. |
| 59 | thread_group.add_argument("-p", "--pid", metavar="PID", dest="tgid", |
| 60 | help="trace this PID only", type=positive_int) |
| 61 | thread_group.add_argument("-t", "--tid", metavar="TID", dest="pid", |
| 62 | help="trace this TID only", type=positive_int) |
Andrew Birchall | 582b5dd | 2016-05-04 16:03:34 -0700 | [diff] [blame] | 63 | thread_group.add_argument("-u", "--user-threads-only", action="store_true", |
| 64 | help="user threads only (no kernel threads)") |
Andrew Birchall | 7f0a6f8 | 2016-05-24 01:44:41 -0700 | [diff] [blame] | 65 | thread_group.add_argument("-k", "--kernel-threads-only", action="store_true", |
| 66 | help="kernel threads only (no user threads)") |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 67 | stack_group = parser.add_mutually_exclusive_group() |
| 68 | stack_group.add_argument("-U", "--user-stacks-only", action="store_true", |
Andrew Birchall | 7f0a6f8 | 2016-05-24 01:44:41 -0700 | [diff] [blame] | 69 | help="show stacks from user space only (no kernel space stacks)") |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 70 | stack_group.add_argument("-K", "--kernel-stacks-only", action="store_true", |
Andrew Birchall | 7f0a6f8 | 2016-05-24 01:44:41 -0700 | [diff] [blame] | 71 | help="show stacks from kernel space only (no user space stacks)") |
Evgeny Vereshchagin | 4509f09 | 2016-06-08 06:33:54 +1000 | [diff] [blame] | 72 | parser.add_argument("-d", "--delimited", action="store_true", |
| 73 | help="insert delimiter between kernel/user stacks") |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 74 | parser.add_argument("-f", "--folded", action="store_true", |
| 75 | help="output folded format") |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 76 | parser.add_argument("--stack-storage-size", default=1024, |
| 77 | type=positive_nonzero_int, |
Mark Drayton | 66bf2e8 | 2016-07-31 22:47:07 +0100 | [diff] [blame] | 78 | help="the number of unique stack traces that can be stored and " |
| 79 | "displayed (default 1024)") |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 80 | parser.add_argument("duration", nargs="?", default=99999999, |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 81 | type=positive_nonzero_int, |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 82 | help="duration of trace, in seconds") |
Glauber Costa | 5246458 | 2016-09-26 12:59:32 -0700 | [diff] [blame] | 83 | parser.add_argument("-m", "--min-block-time", default=1, |
| 84 | type=positive_nonzero_int, |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 85 | help="the amount of time in microseconds over which we " + |
| 86 | "store traces (default 1)") |
| 87 | parser.add_argument("-M", "--max-block-time", default=(1 << 64) - 1, |
Glauber Costa | 5246458 | 2016-09-26 12:59:32 -0700 | [diff] [blame] | 88 | type=positive_nonzero_int, |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 89 | help="the amount of time in microseconds under which we " + |
| 90 | "store traces (default U64_MAX)") |
Brendan Gregg | 4870819 | 2017-08-03 11:49:27 -0700 | [diff] [blame] | 91 | parser.add_argument("--state", type=positive_int, |
Brendan Gregg | 843340d | 2017-07-31 18:39:30 -0700 | [diff] [blame] | 92 | help="filter on this thread state bitmask (eg, 2 == TASK_UNINTERRUPTIBLE" + |
| 93 | ") see include/linux/sched.h") |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 94 | args = parser.parse_args() |
Mark Drayton | 66bf2e8 | 2016-07-31 22:47:07 +0100 | [diff] [blame] | 95 | if args.pid and args.tgid: |
| 96 | parser.error("specify only one of -p and -t") |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 97 | folded = args.folded |
| 98 | duration = int(args.duration) |
Brendan Gregg | 843340d | 2017-07-31 18:39:30 -0700 | [diff] [blame] | 99 | debug = 0 |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 100 | |
| 101 | # signal handler |
| 102 | def signal_ignore(signal, frame): |
| 103 | print() |
| 104 | |
Brendan Gregg | d364d04 | 2016-01-19 17:12:52 -0800 | [diff] [blame] | 105 | # define BPF program |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 106 | bpf_text = """ |
| 107 | #include <uapi/linux/ptrace.h> |
| 108 | #include <linux/sched.h> |
| 109 | |
Glauber Costa | 5246458 | 2016-09-26 12:59:32 -0700 | [diff] [blame] | 110 | #define MINBLOCK_US MINBLOCK_US_VALUEULL |
| 111 | #define MAXBLOCK_US MAXBLOCK_US_VALUEULL |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 112 | |
| 113 | struct key_t { |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 114 | u32 pid; |
Mark Drayton | 66bf2e8 | 2016-07-31 22:47:07 +0100 | [diff] [blame] | 115 | u32 tgid; |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 116 | int user_stack_id; |
| 117 | int kernel_stack_id; |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 118 | char name[TASK_COMM_LEN]; |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 119 | }; |
| 120 | BPF_HASH(counts, struct key_t); |
| 121 | BPF_HASH(start, u32); |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 122 | BPF_STACK_TRACE(stack_traces, STACK_STORAGE_SIZE) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 123 | |
Brendan Gregg | d364d04 | 2016-01-19 17:12:52 -0800 | [diff] [blame] | 124 | int oncpu(struct pt_regs *ctx, struct task_struct *prev) { |
Evgeny Vereshchagin | 9858ca5 | 2016-05-27 06:13:52 +0000 | [diff] [blame] | 125 | u32 pid = prev->pid; |
Mark Drayton | 66bf2e8 | 2016-07-31 22:47:07 +0100 | [diff] [blame] | 126 | u32 tgid = prev->tgid; |
Brendan Gregg | d364d04 | 2016-01-19 17:12:52 -0800 | [diff] [blame] | 127 | u64 ts, *tsp; |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 128 | |
Brendan Gregg | d364d04 | 2016-01-19 17:12:52 -0800 | [diff] [blame] | 129 | // record previous thread sleep time |
Brendan Gregg | 843340d | 2017-07-31 18:39:30 -0700 | [diff] [blame] | 130 | if ((THREAD_FILTER) && (STATE_FILTER)) { |
Brendan Gregg | d364d04 | 2016-01-19 17:12:52 -0800 | [diff] [blame] | 131 | ts = bpf_ktime_get_ns(); |
| 132 | start.update(&pid, &ts); |
| 133 | } |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 134 | |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 135 | // get the current thread's start time |
Brendan Gregg | d364d04 | 2016-01-19 17:12:52 -0800 | [diff] [blame] | 136 | pid = bpf_get_current_pid_tgid(); |
Mark Drayton | 66bf2e8 | 2016-07-31 22:47:07 +0100 | [diff] [blame] | 137 | tgid = bpf_get_current_pid_tgid() >> 32; |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 138 | tsp = start.lookup(&pid); |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 139 | if (tsp == 0) { |
Brendan Gregg | f747114 | 2016-01-19 14:40:41 -0800 | [diff] [blame] | 140 | return 0; // missed start or filtered |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // calculate current thread's delta time |
Brendan Gregg | f747114 | 2016-01-19 14:40:41 -0800 | [diff] [blame] | 144 | u64 delta = bpf_ktime_get_ns() - *tsp; |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 145 | start.delete(&pid); |
| 146 | delta = delta / 1000; |
Glauber Costa | 5246458 | 2016-09-26 12:59:32 -0700 | [diff] [blame] | 147 | if ((delta < MINBLOCK_US) || (delta > MAXBLOCK_US)) { |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 148 | return 0; |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 149 | } |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 150 | |
Brendan Gregg | f747114 | 2016-01-19 14:40:41 -0800 | [diff] [blame] | 151 | // create map key |
Vicent Marti | e82fb1b | 2016-03-25 17:21:44 +0100 | [diff] [blame] | 152 | u64 zero = 0, *val; |
Brendan Gregg | f747114 | 2016-01-19 14:40:41 -0800 | [diff] [blame] | 153 | struct key_t key = {}; |
Vicent Marti | e82fb1b | 2016-03-25 17:21:44 +0100 | [diff] [blame] | 154 | |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 155 | key.pid = pid; |
Mark Drayton | 66bf2e8 | 2016-07-31 22:47:07 +0100 | [diff] [blame] | 156 | key.tgid = tgid; |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 157 | key.user_stack_id = USER_STACK_GET; |
| 158 | key.kernel_stack_id = KERNEL_STACK_GET; |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 159 | bpf_get_current_comm(&key.name, sizeof(key.name)); |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 160 | |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 161 | val = counts.lookup_or_init(&key, &zero); |
| 162 | (*val) += delta; |
| 163 | return 0; |
| 164 | } |
| 165 | """ |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 166 | |
| 167 | # set thread filter |
Andrew Birchall | 582b5dd | 2016-05-04 16:03:34 -0700 | [diff] [blame] | 168 | thread_context = "" |
Mark Drayton | 66bf2e8 | 2016-07-31 22:47:07 +0100 | [diff] [blame] | 169 | if args.tgid is not None: |
| 170 | thread_context = "PID %d" % args.tgid |
| 171 | thread_filter = 'tgid == %d' % args.tgid |
| 172 | elif args.pid is not None: |
| 173 | thread_context = "TID %d" % args.pid |
| 174 | thread_filter = 'pid == %d' % args.pid |
Andrew Birchall | 582b5dd | 2016-05-04 16:03:34 -0700 | [diff] [blame] | 175 | elif args.user_threads_only: |
| 176 | thread_context = "user threads" |
| 177 | thread_filter = '!(prev->flags & PF_KTHREAD)' |
| 178 | elif args.kernel_threads_only: |
| 179 | thread_context = "kernel threads" |
| 180 | thread_filter = 'prev->flags & PF_KTHREAD' |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 181 | else: |
Andrew Birchall | 582b5dd | 2016-05-04 16:03:34 -0700 | [diff] [blame] | 182 | thread_context = "all threads" |
| 183 | thread_filter = '1' |
Brendan Gregg | 843340d | 2017-07-31 18:39:30 -0700 | [diff] [blame] | 184 | if args.state == 0: |
| 185 | state_filter = 'prev->state == 0' |
Brendan Gregg | 4870819 | 2017-08-03 11:49:27 -0700 | [diff] [blame] | 186 | elif args.state: |
Brendan Gregg | 843340d | 2017-07-31 18:39:30 -0700 | [diff] [blame] | 187 | # these states are sometimes bitmask checked |
| 188 | state_filter = 'prev->state & %d' % args.state |
| 189 | else: |
| 190 | state_filter = '1' |
Andrew Birchall | 582b5dd | 2016-05-04 16:03:34 -0700 | [diff] [blame] | 191 | bpf_text = bpf_text.replace('THREAD_FILTER', thread_filter) |
Brendan Gregg | 843340d | 2017-07-31 18:39:30 -0700 | [diff] [blame] | 192 | bpf_text = bpf_text.replace('STATE_FILTER', state_filter) |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 193 | |
| 194 | # set stack storage size |
| 195 | bpf_text = bpf_text.replace('STACK_STORAGE_SIZE', str(args.stack_storage_size)) |
Glauber Costa | 5246458 | 2016-09-26 12:59:32 -0700 | [diff] [blame] | 196 | bpf_text = bpf_text.replace('MINBLOCK_US_VALUE', str(args.min_block_time)) |
| 197 | bpf_text = bpf_text.replace('MAXBLOCK_US_VALUE', str(args.max_block_time)) |
Brendan Gregg | d364d04 | 2016-01-19 17:12:52 -0800 | [diff] [blame] | 198 | |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 199 | # handle stack args |
| 200 | kernel_stack_get = "stack_traces.get_stackid(ctx, BPF_F_REUSE_STACKID)" |
| 201 | user_stack_get = \ |
| 202 | "stack_traces.get_stackid(ctx, BPF_F_REUSE_STACKID | BPF_F_USER_STACK)" |
| 203 | stack_context = "" |
| 204 | if args.user_stacks_only: |
| 205 | stack_context = "user" |
| 206 | kernel_stack_get = "-1" |
| 207 | elif args.kernel_stacks_only: |
| 208 | stack_context = "kernel" |
| 209 | user_stack_get = "-1" |
| 210 | else: |
| 211 | stack_context = "user + kernel" |
| 212 | bpf_text = bpf_text.replace('USER_STACK_GET', user_stack_get) |
| 213 | bpf_text = bpf_text.replace('KERNEL_STACK_GET', kernel_stack_get) |
| 214 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 215 | need_delimiter = args.delimited and not (args.kernel_stacks_only or |
| 216 | args.user_stacks_only) |
Evgeny Vereshchagin | 4509f09 | 2016-06-08 06:33:54 +1000 | [diff] [blame] | 217 | |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 218 | # check for an edge case; the code below will handle this case correctly |
| 219 | # but ultimately nothing will be displayed |
| 220 | if args.kernel_threads_only and args.user_stacks_only: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 221 | print("ERROR: Displaying user stacks for kernel threads " + |
| 222 | "doesn't make sense.", file=stderr) |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 223 | exit(1) |
| 224 | |
Brendan Gregg | 843340d | 2017-07-31 18:39:30 -0700 | [diff] [blame] | 225 | if (debug): |
| 226 | print(bpf_text) |
| 227 | |
Brendan Gregg | d364d04 | 2016-01-19 17:12:52 -0800 | [diff] [blame] | 228 | # initialize BPF |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 229 | b = BPF(text=bpf_text) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 230 | b.attach_kprobe(event="finish_task_switch", fn_name="oncpu") |
| 231 | matched = b.num_open_kprobes() |
| 232 | if matched == 0: |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 233 | print("error: 0 functions traced. Exiting.", file=stderr) |
| 234 | exit(1) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 235 | |
| 236 | # header |
| 237 | if not folded: |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 238 | print("Tracing off-CPU time (us) of %s by %s stack" % |
| 239 | (thread_context, stack_context), end="") |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 240 | if duration < 99999999: |
| 241 | print(" for %d secs." % duration) |
| 242 | else: |
| 243 | print("... Hit Ctrl-C to end.") |
| 244 | |
Andrew Birchall | ee7e5b4 | 2016-05-03 16:54:00 -0700 | [diff] [blame] | 245 | try: |
| 246 | sleep(duration) |
| 247 | except KeyboardInterrupt: |
| 248 | # as cleanup can take many seconds, trap Ctrl-C: |
| 249 | signal.signal(signal.SIGINT, signal_ignore) |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 250 | |
Andrew Birchall | ee7e5b4 | 2016-05-03 16:54:00 -0700 | [diff] [blame] | 251 | if not folded: |
| 252 | print() |
Brendan Gregg | 38cef48 | 2016-01-15 17:26:30 -0800 | [diff] [blame] | 253 | |
Andrew Birchall | ee7e5b4 | 2016-05-03 16:54:00 -0700 | [diff] [blame] | 254 | missing_stacks = 0 |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 255 | has_enomem = False |
Andrew Birchall | ee7e5b4 | 2016-05-03 16:54:00 -0700 | [diff] [blame] | 256 | counts = b.get_table("counts") |
| 257 | stack_traces = b.get_table("stack_traces") |
| 258 | for k, v in sorted(counts.items(), key=lambda counts: counts[1].value): |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 259 | # handle get_stackid erorrs |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 260 | if (not args.user_stacks_only and k.kernel_stack_id < 0) or \ |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 261 | (not args.kernel_stacks_only and k.user_stack_id < 0 and |
| 262 | k.user_stack_id != -errno.EFAULT): |
Andrew Birchall | ee7e5b4 | 2016-05-03 16:54:00 -0700 | [diff] [blame] | 263 | missing_stacks += 1 |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 264 | # check for an ENOMEM error |
Teng Qin | 0b11d22 | 2016-07-18 13:21:10 -0700 | [diff] [blame] | 265 | if k.kernel_stack_id == -errno.ENOMEM or \ |
| 266 | k.user_stack_id == -errno.ENOMEM: |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 267 | has_enomem = True |
Andrew Birchall | ee7e5b4 | 2016-05-03 16:54:00 -0700 | [diff] [blame] | 268 | continue |
| 269 | |
Mark Drayton | 66bf2e8 | 2016-07-31 22:47:07 +0100 | [diff] [blame] | 270 | # user stacks will be symbolized by tgid, not pid, to avoid the overhead |
| 271 | # of one symbol resolver per thread |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 272 | user_stack = [] if k.user_stack_id < 0 else \ |
| 273 | stack_traces.walk(k.user_stack_id) |
| 274 | kernel_stack = [] if k.kernel_stack_id < 0 else \ |
| 275 | stack_traces.walk(k.kernel_stack_id) |
Andrew Birchall | ee7e5b4 | 2016-05-03 16:54:00 -0700 | [diff] [blame] | 276 | |
| 277 | if folded: |
| 278 | # print folded stack output |
Evgeny Vereshchagin | ff39d0c | 2016-06-07 18:00:01 +1000 | [diff] [blame] | 279 | user_stack = list(user_stack) |
| 280 | kernel_stack = list(kernel_stack) |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 281 | line = [k.name.decode()] + \ |
Mark Drayton | 66bf2e8 | 2016-07-31 22:47:07 +0100 | [diff] [blame] | 282 | [b.sym(addr, k.tgid) for addr in reversed(user_stack)] + \ |
Evgeny Vereshchagin | 4509f09 | 2016-06-08 06:33:54 +1000 | [diff] [blame] | 283 | (need_delimiter and ["-"] or []) + \ |
Evgeny Vereshchagin | f988644 | 2016-06-08 06:06:33 +1000 | [diff] [blame] | 284 | [b.ksym(addr) for addr in reversed(kernel_stack)] |
Andrew Birchall | ee7e5b4 | 2016-05-03 16:54:00 -0700 | [diff] [blame] | 285 | print("%s %d" % (";".join(line), v.value)) |
| 286 | else: |
| 287 | # print default multi-line stack output |
Andrew Birchall | 1f202e7 | 2016-05-05 10:56:40 -0700 | [diff] [blame] | 288 | for addr in kernel_stack: |
Sasha Goldshtein | 2f78068 | 2017-02-09 00:20:56 -0500 | [diff] [blame] | 289 | print(" %s" % b.ksym(addr)) |
Evgeny Vereshchagin | 4509f09 | 2016-06-08 06:33:54 +1000 | [diff] [blame] | 290 | if need_delimiter: |
| 291 | print(" --") |
Evgeny Vereshchagin | f988644 | 2016-06-08 06:06:33 +1000 | [diff] [blame] | 292 | for addr in user_stack: |
Sasha Goldshtein | 2f78068 | 2017-02-09 00:20:56 -0500 | [diff] [blame] | 293 | print(" %s" % b.sym(addr, k.tgid)) |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 294 | print(" %-16s %s (%d)" % ("-", k.name.decode(), k.pid)) |
Andrew Birchall | ee7e5b4 | 2016-05-03 16:54:00 -0700 | [diff] [blame] | 295 | print(" %d\n" % v.value) |
| 296 | |
| 297 | if missing_stacks > 0: |
Andrew Birchall | 47d871f | 2016-05-11 18:31:49 -0700 | [diff] [blame] | 298 | enomem_str = "" if not has_enomem else \ |
| 299 | " Consider increasing --stack-storage-size." |
| 300 | print("WARNING: %d stack traces could not be displayed.%s" % |
| 301 | (missing_stacks, enomem_str), |
Andrew Birchall | ee7e5b4 | 2016-05-03 16:54:00 -0700 | [diff] [blame] | 302 | file=stderr) |