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") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 41 | args = parser.parse_args() |
| 42 | countdown = int(args.count) |
| 43 | if args.nanoseconds: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 44 | factor = 1 |
| 45 | label = "nsecs" |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 46 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 47 | factor = 1000 |
| 48 | label = "usecs" |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 49 | debug = 0 |
| 50 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 51 | # define BPF program |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 52 | bpf_text = """ |
| 53 | #include <uapi/linux/ptrace.h> |
| 54 | |
| 55 | typedef struct irq_key { |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 56 | u32 vec; |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 57 | u64 slot; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 58 | } irq_key_t; |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 59 | |
| 60 | typedef struct account_val { |
| 61 | u64 ts; |
| 62 | u32 vec; |
| 63 | } account_val_t; |
| 64 | |
| 65 | BPF_HASH(start, u32, account_val_t); |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 66 | BPF_HASH(iptr, u32); |
| 67 | BPF_HISTOGRAM(dist, irq_key_t); |
| 68 | |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 69 | TRACEPOINT_PROBE(irq, softirq_entry) |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 70 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 71 | u32 pid = bpf_get_current_pid_tgid(); |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 72 | account_val_t val = {}; |
| 73 | val.ts = bpf_ktime_get_ns(); |
| 74 | val.vec = args->vec; |
| 75 | start.update(&pid, &val); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 76 | return 0; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 79 | TRACEPOINT_PROBE(irq, softirq_exit) |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 80 | { |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 81 | u64 delta; |
| 82 | u32 vec; |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 83 | u32 pid = bpf_get_current_pid_tgid(); |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 84 | account_val_t *valp; |
| 85 | irq_key_t key = {0}; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 86 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 87 | // fetch timestamp and calculate delta |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 88 | valp = start.lookup(&pid); |
| 89 | if (valp == 0) { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 90 | return 0; // missed start |
| 91 | } |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 92 | delta = bpf_ktime_get_ns() - valp->ts; |
| 93 | vec = valp->vec; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 94 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 95 | // store as sum or histogram |
| 96 | STORE |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 97 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 98 | start.delete(&pid); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 99 | return 0; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 100 | } |
| 101 | """ |
| 102 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 103 | # code substitutions |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 104 | if args.dist: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 105 | bpf_text = bpf_text.replace('STORE', |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 106 | 'key.vec = vec; key.slot = bpf_log2l(delta); ' + |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 107 | 'dist.increment(key);') |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 108 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 109 | bpf_text = bpf_text.replace('STORE', |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 110 | 'key.vec = valp->vec; ' + |
| 111 | 'u64 zero = 0, *vp = dist.lookup_or_init(&key, &zero); ' + |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 112 | '(*vp) += delta;') |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 113 | if debug: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 114 | print(bpf_text) |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 115 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 116 | # load BPF program |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 117 | b = BPF(text=bpf_text) |
| 118 | |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 119 | def vec_to_name(vec): |
| 120 | # copied from softirq_to_name() in kernel/softirq.c |
| 121 | # may need updates if new softirq handlers are added |
| 122 | return ["hi", "timer", "net_tx", "net_rx", "block", "irq_poll", |
| 123 | "tasklet", "sched", "hrtimer", "rcu"][vec] |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 124 | |
| 125 | print("Tracing soft irq event time... Hit Ctrl-C to end.") |
| 126 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 127 | # output |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 128 | exiting = 0 if args.interval else 1 |
| 129 | dist = b.get_table("dist") |
| 130 | while (1): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 131 | try: |
| 132 | sleep(int(args.interval)) |
| 133 | except KeyboardInterrupt: |
| 134 | exiting = 1 |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 135 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 136 | print() |
| 137 | if args.timestamp: |
| 138 | print("%-8s\n" % strftime("%H:%M:%S"), end="") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 139 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 140 | if args.dist: |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 141 | dist.print_log2_hist(label, "softirq", section_print_fn=vec_to_name) |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 142 | else: |
Sasha Goldshtein | 69ceaca | 2017-04-08 12:35:09 +0300 | [diff] [blame] | 143 | print("%-16s %11s" % ("SOFTIRQ", "TOTAL_" + label)) |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 144 | 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] | 145 | print("%-16s %11d" % (vec_to_name(k.vec), v.value / factor)) |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 146 | dist.clear() |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 147 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 148 | countdown -= 1 |
| 149 | if exiting or countdown == 0: |
| 150 | exit() |