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 | # hardirqs Summarize hard 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: hardirqs [-h] [-T] [-Q] [-m] [-D] [interval] [count] |
| 8 | # |
| 9 | # Thanks Amer Ather for help understanding irq behavior. |
| 10 | # |
| 11 | # Copyright (c) 2015 Brendan Gregg. |
| 12 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 13 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 14 | # 19-Oct-2015 Brendan Gregg Created this. |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 15 | |
| 16 | from __future__ import print_function |
| 17 | from bcc import BPF |
| 18 | from time import sleep, strftime |
| 19 | import argparse |
| 20 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 21 | # arguments |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 22 | examples = """examples: |
| 23 | ./hardirqs # sum hard irq event time |
| 24 | ./hardirqs -d # show hard irq event time as histograms |
| 25 | ./hardirqs 1 10 # print 1 second summaries, 10 times |
| 26 | ./hardirqs -NT 1 # 1s summaries, nanoseconds, and timestamps |
| 27 | """ |
| 28 | parser = argparse.ArgumentParser( |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 29 | description="Summarize hard irq event time as histograms", |
| 30 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 31 | epilog=examples) |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 32 | parser.add_argument("-T", "--timestamp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 33 | help="include timestamp on output") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 34 | parser.add_argument("-N", "--nanoseconds", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 35 | help="output in nanoseconds") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 36 | parser.add_argument("-d", "--dist", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 37 | help="show distributions as histograms") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 38 | parser.add_argument("interval", nargs="?", default=99999999, |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 39 | help="output interval, in seconds") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 40 | parser.add_argument("count", nargs="?", default=99999999, |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 41 | help="number of outputs") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 42 | args = parser.parse_args() |
| 43 | countdown = int(args.count) |
| 44 | if args.nanoseconds: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 45 | factor = 1 |
| 46 | label = "nsecs" |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 47 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 48 | factor = 1000 |
| 49 | label = "usecs" |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 50 | debug = 0 |
| 51 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 52 | # define BPF program |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 53 | bpf_text = """ |
| 54 | #include <uapi/linux/ptrace.h> |
| 55 | #include <linux/irq.h> |
| 56 | #include <linux/irqdesc.h> |
| 57 | #include <linux/interrupt.h> |
| 58 | |
| 59 | typedef struct irq_key { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 60 | char name[32]; |
| 61 | u64 slot; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 62 | } irq_key_t; |
| 63 | BPF_HASH(start, u32); |
| 64 | BPF_HASH(irqdesc, u32, struct irq_desc *); |
| 65 | BPF_HISTOGRAM(dist, irq_key_t); |
| 66 | |
| 67 | // time IRQ |
| 68 | int trace_start(struct pt_regs *ctx, struct irq_desc *desc) |
| 69 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 70 | u32 pid = bpf_get_current_pid_tgid(); |
| 71 | u64 ts = bpf_ktime_get_ns(); |
| 72 | start.update(&pid, &ts); |
| 73 | irqdesc.update(&pid, &desc); |
| 74 | return 0; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int trace_completion(struct pt_regs *ctx) |
| 78 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 79 | u64 *tsp, delta; |
| 80 | struct irq_desc **descp; |
| 81 | u32 pid = bpf_get_current_pid_tgid(); |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 82 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 83 | // fetch timestamp and calculate delta |
| 84 | tsp = start.lookup(&pid); |
| 85 | descp = irqdesc.lookup(&pid); |
| 86 | if (tsp == 0 || descp == 0) { |
| 87 | return 0; // missed start |
| 88 | } |
| 89 | // Note: descp is a value from map, so '&' can be done without |
| 90 | // probe_read, but the next level irqaction * needs a probe read. |
| 91 | // Do these steps first after reading the map, otherwise some of these |
| 92 | // pointers may get pushed onto the stack and verifier will fail. |
| 93 | struct irqaction *action = 0; |
| 94 | bpf_probe_read(&action, sizeof(action), &(*descp)->action); |
| 95 | const char **namep = &action->name; |
| 96 | char *name = 0; |
| 97 | bpf_probe_read(&name, sizeof(name), namep); |
| 98 | delta = bpf_ktime_get_ns() - *tsp; |
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 | // store as sum or histogram |
| 101 | STORE |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 102 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 103 | start.delete(&pid); |
| 104 | irqdesc.delete(&pid); |
| 105 | return 0; |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 106 | } |
| 107 | """ |
| 108 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 109 | # code substitutions |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 110 | if args.dist: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 111 | bpf_text = bpf_text.replace('STORE', |
| 112 | 'irq_key_t key = {.slot = bpf_log2l(delta)};' + |
| 113 | 'bpf_probe_read(&key.name, sizeof(key.name), name);' + |
| 114 | 'dist.increment(key);') |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 115 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 116 | bpf_text = bpf_text.replace('STORE', |
| 117 | 'irq_key_t key = {.slot = 0 /* ignore */};' + |
| 118 | 'bpf_probe_read(&key.name, sizeof(key.name), name);' + |
| 119 | 'u64 zero = 0, *vp = dist.lookup_or_init(&key, &zero);' + |
| 120 | '(*vp) += delta;') |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 121 | if debug: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 122 | print(bpf_text) |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 123 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 124 | # load BPF program |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 125 | b = BPF(text=bpf_text) |
| 126 | |
| 127 | # these should really use irq:irq_handler_entry/exit tracepoints: |
| 128 | b.attach_kprobe(event="handle_irq_event_percpu", fn_name="trace_start") |
| 129 | b.attach_kretprobe(event="handle_irq_event_percpu", fn_name="trace_completion") |
| 130 | |
| 131 | print("Tracing hard irq event time... Hit Ctrl-C to end.") |
| 132 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 133 | # output |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 134 | exiting = 0 if args.interval else 1 |
| 135 | dist = b.get_table("dist") |
| 136 | while (1): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 137 | try: |
| 138 | sleep(int(args.interval)) |
| 139 | except KeyboardInterrupt: |
| 140 | exiting = 1 |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 141 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 142 | print() |
| 143 | if args.timestamp: |
| 144 | print("%-8s\n" % strftime("%H:%M:%S"), end="") |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 145 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 146 | if args.dist: |
| 147 | dist.print_log2_hist(label, "hardirq") |
| 148 | else: |
| 149 | print("%-26s %11s" % ("HARDIRQ", "TOTAL_" + label)) |
| 150 | for k, v in sorted(dist.items(), key=lambda dist: dist[1].value): |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 151 | print("%-26s %11d" % (k.name.decode(), v.value / factor)) |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 152 | dist.clear() |
Brendan Gregg | 860b649 | 2015-10-20 15:52:23 -0700 | [diff] [blame] | 153 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 154 | countdown -= 1 |
| 155 | if exiting or countdown == 0: |
| 156 | exit() |