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