Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 4 | # softirqs Summarize soft IRQ (interrupt) event time. |
| 5 | # For Linux, uses BCC, eBPF. |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 6 | # |
| 7 | # USAGE: softirqs [-h] [-T] [-N] [-d] [interval] [count] |
| 8 | # |
| 9 | # Copyright (c) 2015 Brendan Gregg. |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 11 | # |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 12 | # 20-Oct-2015 Brendan Gregg Created this. |
| 13 | # 03-Apr-2017 Sasha Goldshtein Migrated to kernel tracepoints. |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 14 | |
| 15 | from __future__ import print_function |
| 16 | from bcc import BPF |
| 17 | from time import sleep, strftime |
| 18 | import argparse |
| 19 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 20 | # arguments |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 21 | examples = """examples: |
| 22 | ./softirqs # sum soft irq event time |
| 23 | ./softirqs -d # show soft irq event time as histograms |
| 24 | ./softirqs 1 10 # print 1 second summaries, 10 times |
| 25 | ./softirqs -NT 1 # 1s summaries, nanoseconds, and timestamps |
| 26 | """ |
| 27 | parser = argparse.ArgumentParser( |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 28 | description="Summarize soft irq event time as histograms.", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 29 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 30 | epilog=examples) |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 31 | parser.add_argument("-T", "--timestamp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 32 | help="include timestamp on output") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 33 | parser.add_argument("-N", "--nanoseconds", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 34 | help="output in nanoseconds") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 35 | parser.add_argument("-d", "--dist", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 36 | help="show distributions as histograms") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 37 | parser.add_argument("interval", nargs="?", default=99999999, |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 38 | help="output interval, in seconds") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 39 | parser.add_argument("count", nargs="?", default=99999999, |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 40 | help="number of outputs") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 41 | parser.add_argument("--ebpf", action="store_true", |
| 42 | help=argparse.SUPPRESS) |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 43 | args = parser.parse_args() |
| 44 | countdown = int(args.count) |
| 45 | if args.nanoseconds: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 46 | factor = 1 |
| 47 | label = "nsecs" |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 48 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 49 | factor = 1000 |
| 50 | label = "usecs" |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 51 | debug = 0 |
| 52 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 53 | # define BPF program |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 54 | bpf_text = """ |
| 55 | #include <uapi/linux/ptrace.h> |
| 56 | |
| 57 | typedef struct irq_key { |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 58 | u32 vec; |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 59 | u64 slot; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 60 | } irq_key_t; |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 61 | |
| 62 | typedef struct account_val { |
| 63 | u64 ts; |
| 64 | u32 vec; |
| 65 | } account_val_t; |
| 66 | |
| 67 | BPF_HASH(start, u32, account_val_t); |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 68 | BPF_HASH(iptr, u32); |
| 69 | BPF_HISTOGRAM(dist, irq_key_t); |
| 70 | |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 71 | TRACEPOINT_PROBE(irq, softirq_entry) |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 72 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 73 | u32 pid = bpf_get_current_pid_tgid(); |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 74 | account_val_t val = {}; |
| 75 | val.ts = bpf_ktime_get_ns(); |
| 76 | val.vec = args->vec; |
| 77 | start.update(&pid, &val); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 78 | return 0; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 81 | TRACEPOINT_PROBE(irq, softirq_exit) |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 82 | { |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 83 | u64 delta; |
| 84 | u32 vec; |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 85 | u32 pid = bpf_get_current_pid_tgid(); |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 86 | account_val_t *valp; |
| 87 | irq_key_t key = {0}; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 88 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 89 | // fetch timestamp and calculate delta |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 90 | valp = start.lookup(&pid); |
| 91 | if (valp == 0) { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 92 | return 0; // missed start |
| 93 | } |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 94 | delta = bpf_ktime_get_ns() - valp->ts; |
| 95 | vec = valp->vec; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 96 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 97 | // store as sum or histogram |
| 98 | STORE |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 99 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 100 | start.delete(&pid); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 101 | return 0; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 102 | } |
| 103 | """ |
| 104 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 105 | # code substitutions |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 106 | if args.dist: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 107 | bpf_text = bpf_text.replace('STORE', |
Kirill Smelkov | f2d125e | 2017-09-25 11:23:03 +0300 | [diff] [blame] | 108 | 'key.vec = vec; key.slot = bpf_log2l(delta / %d); ' % factor + |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 109 | 'dist.increment(key);') |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 110 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 111 | bpf_text = bpf_text.replace('STORE', |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 112 | 'key.vec = valp->vec; ' + |
Javier Honduvilla Coto | 64bf965 | 2018-08-01 06:50:19 +0200 | [diff] [blame] | 113 | 'dist.increment(key, delta);') |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 114 | if debug or args.ebpf: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 115 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 116 | if args.ebpf: |
| 117 | exit() |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 118 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 119 | # load BPF program |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 120 | b = BPF(text=bpf_text) |
| 121 | |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 122 | def vec_to_name(vec): |
| 123 | # copied from softirq_to_name() in kernel/softirq.c |
| 124 | # may need updates if new softirq handlers are added |
| 125 | return ["hi", "timer", "net_tx", "net_rx", "block", "irq_poll", |
| 126 | "tasklet", "sched", "hrtimer", "rcu"][vec] |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 127 | |
| 128 | print("Tracing soft irq event time... Hit Ctrl-C to end.") |
| 129 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 130 | # output |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 131 | exiting = 0 if args.interval else 1 |
| 132 | dist = b.get_table("dist") |
| 133 | while (1): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 134 | try: |
| 135 | sleep(int(args.interval)) |
| 136 | except KeyboardInterrupt: |
| 137 | exiting = 1 |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 138 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 139 | print() |
| 140 | if args.timestamp: |
| 141 | print("%-8s\n" % strftime("%H:%M:%S"), end="") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 142 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 143 | if args.dist: |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 144 | dist.print_log2_hist(label, "softirq", section_print_fn=vec_to_name) |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 145 | else: |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 146 | print("%-16s %11s" % ("SOFTIRQ", "TOTAL_" + label)) |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 147 | for k, v in sorted(dist.items(), key=lambda dist: dist[1].value): |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 148 | print("%-16s %11d" % (vec_to_name(k.vec), v.value / factor)) |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 149 | dist.clear() |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 150 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 151 | countdown -= 1 |
| 152 | if exiting or countdown == 0: |
| 153 | exit() |