blob: 4e373d9cde6cb34490663ab8a8e52053dfd3891a [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08002# @lint-avoid-python-3-compatibility-imports
Brendan Gregg860b6492015-10-20 15:52:23 -07003#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08004# hardirqs Summarize hard IRQ (interrupt) event time.
5# For Linux, uses BCC, eBPF.
Brendan Gregg860b6492015-10-20 15:52:23 -07006#
xingfeng2510c5baa7b2022-03-07 17:35:49 +08007# USAGE: hardirqs [-h] [-T] [-N] [-C] [-d] [-c CPU] [interval] [outputs]
Brendan Gregg860b6492015-10-20 15:52:23 -07008#
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 Starovoitovbdf07732016-01-14 10:09:20 -080014# 19-Oct-2015 Brendan Gregg Created this.
Hengqi Chene6aa65e2021-05-22 16:07:36 +080015# 22-May-2021 Hengqi Chen Migrated to kernel tracepoints.
xingfeng2510c5baa7b2022-03-07 17:35:49 +080016# 07-Mar-2022 Rocky Xing Added CPU filter support.
Brendan Gregg860b6492015-10-20 15:52:23 -070017
18from __future__ import print_function
19from bcc import BPF
20from time import sleep, strftime
21import argparse
22
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030023# arguments
Brendan Gregg860b6492015-10-20 15:52:23 -070024examples = """examples:
25 ./hardirqs # sum hard irq event time
26 ./hardirqs -d # show hard irq event time as histograms
27 ./hardirqs 1 10 # print 1 second summaries, 10 times
28 ./hardirqs -NT 1 # 1s summaries, nanoseconds, and timestamps
xingfeng2510c5baa7b2022-03-07 17:35:49 +080029 ./hardirqs -c 1 # sum hard irq event time on CPU 1 only
Brendan Gregg860b6492015-10-20 15:52:23 -070030"""
31parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080032 description="Summarize hard irq event time as histograms",
33 formatter_class=argparse.RawDescriptionHelpFormatter,
34 epilog=examples)
Brendan Gregg860b6492015-10-20 15:52:23 -070035parser.add_argument("-T", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080036 help="include timestamp on output")
Brendan Gregg860b6492015-10-20 15:52:23 -070037parser.add_argument("-N", "--nanoseconds", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080038 help="output in nanoseconds")
Brendan Greggc32b8452017-11-26 23:38:32 -080039parser.add_argument("-C", "--count", action="store_true",
40 help="show event counts instead of timing")
Brendan Gregg860b6492015-10-20 15:52:23 -070041parser.add_argument("-d", "--dist", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080042 help="show distributions as histograms")
xingfeng2510c5baa7b2022-03-07 17:35:49 +080043parser.add_argument("-c", "--cpu", type=int,
44 help="trace this CPU only")
Brendan Gregg860b6492015-10-20 15:52:23 -070045parser.add_argument("interval", nargs="?", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080046 help="output interval, in seconds")
Brendan Greggc32b8452017-11-26 23:38:32 -080047parser.add_argument("outputs", nargs="?", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080048 help="number of outputs")
Nathan Scottcf0792f2018-02-02 16:56:50 +110049parser.add_argument("--ebpf", action="store_true",
50 help=argparse.SUPPRESS)
Brendan Gregg860b6492015-10-20 15:52:23 -070051args = parser.parse_args()
Brendan Greggc32b8452017-11-26 23:38:32 -080052countdown = int(args.outputs)
53if args.count and (args.dist or args.nanoseconds):
54 print("The --count option can't be used with time-based options")
55 exit()
56if args.count:
57 factor = 1
58 label = "count"
59elif args.nanoseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080060 factor = 1
61 label = "nsecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070062else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080063 factor = 1000
64 label = "usecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070065debug = 0
66
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030067# define BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -070068bpf_text = """
69#include <uapi/linux/ptrace.h>
70#include <linux/irq.h>
71#include <linux/irqdesc.h>
72#include <linux/interrupt.h>
73
Ism Hong99bfe8a2021-12-08 10:17:20 +080074// Add cpu_id as part of key for irq entry event to handle the case which irq
75// is triggered while idle thread(swapper/x, tid=0) for each cpu core.
76// Please see more detail at pull request #2804, #3733.
77typedef struct entry_key {
78 u32 tid;
79 u32 cpu_id;
80} entry_key_t;
81
Brendan Gregg860b6492015-10-20 15:52:23 -070082typedef struct irq_key {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080083 char name[32];
84 u64 slot;
Brendan Gregg860b6492015-10-20 15:52:23 -070085} irq_key_t;
Hengqi Chene6aa65e2021-05-22 16:07:36 +080086
87typedef struct irq_name {
88 char name[32];
89} irq_name_t;
90
Ism Hong99bfe8a2021-12-08 10:17:20 +080091BPF_HASH(start, entry_key_t);
92BPF_HASH(irqnames, entry_key_t, irq_name_t);
Brendan Gregg860b6492015-10-20 15:52:23 -070093BPF_HISTOGRAM(dist, irq_key_t);
Hengqi Chene6aa65e2021-05-22 16:07:36 +080094"""
Brendan Gregg860b6492015-10-20 15:52:23 -070095
Hengqi Chene6aa65e2021-05-22 16:07:36 +080096bpf_text_count = """
97TRACEPOINT_PROBE(irq, irq_handler_entry)
Brendan Greggc32b8452017-11-26 23:38:32 -080098{
Ism Hong99bfe8a2021-12-08 10:17:20 +080099 struct entry_key key = {};
Ism Hong814c2642021-12-06 11:28:34 +0800100 irq_name_t name = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800101 u32 cpu = bpf_get_smp_processor_id();
102
103 FILTER_CPU
Ism Hong814c2642021-12-06 11:28:34 +0800104
Ism Hong99bfe8a2021-12-08 10:17:20 +0800105 key.tid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800106 key.cpu_id = cpu;
Ism Hong99bfe8a2021-12-08 10:17:20 +0800107
Ism Hong814c2642021-12-06 11:28:34 +0800108 TP_DATA_LOC_READ_STR(&name.name, name, sizeof(name));
Ism Hong99bfe8a2021-12-08 10:17:20 +0800109 irqnames.update(&key, &name);
Ism Hong814c2642021-12-06 11:28:34 +0800110 return 0;
111}
112
113TRACEPOINT_PROBE(irq, irq_handler_exit)
114{
Ism Hong99bfe8a2021-12-08 10:17:20 +0800115 struct entry_key key = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800116 u32 cpu = bpf_get_smp_processor_id();
117
118 FILTER_CPU
Ism Hong99bfe8a2021-12-08 10:17:20 +0800119
120 key.tid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800121 key.cpu_id = cpu;
Ism Hong814c2642021-12-06 11:28:34 +0800122
123 // check ret value of irq handler is not IRQ_NONE to make sure
124 // the current event belong to this irq handler
125 if (args->ret != IRQ_NONE) {
126 irq_name_t *namep;
127
Ism Hong99bfe8a2021-12-08 10:17:20 +0800128 namep = irqnames.lookup(&key);
Ism Hong814c2642021-12-06 11:28:34 +0800129 if (namep == 0) {
130 return 0; // missed irq name
131 }
132 char *name = (char *)namep->name;
133 irq_key_t key = {.slot = 0 /* ignore */};
134
135 bpf_probe_read_kernel(&key.name, sizeof(key.name), name);
136 dist.atomic_increment(key);
137 }
138
Ism Hong99bfe8a2021-12-08 10:17:20 +0800139 irqnames.delete(&key);
Brendan Greggc32b8452017-11-26 23:38:32 -0800140 return 0;
141}
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800142"""
Brendan Greggc32b8452017-11-26 23:38:32 -0800143
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800144bpf_text_time = """
145TRACEPOINT_PROBE(irq, irq_handler_entry)
Brendan Gregg860b6492015-10-20 15:52:23 -0700146{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800147 u64 ts = bpf_ktime_get_ns();
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800148 irq_name_t name = {};
Ism Hong99bfe8a2021-12-08 10:17:20 +0800149 struct entry_key key = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800150 u32 cpu = bpf_get_smp_processor_id();
151
152 FILTER_CPU
Ism Hong99bfe8a2021-12-08 10:17:20 +0800153
154 key.tid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800155 key.cpu_id = cpu;
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800156
Hengqi Chen2cffe362021-11-22 21:54:51 +0800157 TP_DATA_LOC_READ_STR(&name.name, name, sizeof(name));
Ism Hong99bfe8a2021-12-08 10:17:20 +0800158 irqnames.update(&key, &name);
159 start.update(&key, &ts);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800160 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700161}
162
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800163TRACEPOINT_PROBE(irq, irq_handler_exit)
Brendan Gregg860b6492015-10-20 15:52:23 -0700164{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800165 u64 *tsp, delta;
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800166 irq_name_t *namep;
Ism Hong99bfe8a2021-12-08 10:17:20 +0800167 struct entry_key key = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800168 u32 cpu = bpf_get_smp_processor_id();
Ism Hong99bfe8a2021-12-08 10:17:20 +0800169
170 key.tid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800171 key.cpu_id = cpu;
Brendan Gregg860b6492015-10-20 15:52:23 -0700172
Ism Hong814c2642021-12-06 11:28:34 +0800173 // check ret value of irq handler is not IRQ_NONE to make sure
174 // the current event belong to this irq handler
175 if (args->ret != IRQ_NONE) {
176 // fetch timestamp and calculate delta
Ism Hong99bfe8a2021-12-08 10:17:20 +0800177 tsp = start.lookup(&key);
178 namep = irqnames.lookup(&key);
Ism Hong814c2642021-12-06 11:28:34 +0800179 if (tsp == 0 || namep == 0) {
180 return 0; // missed start
181 }
182
183 char *name = (char *)namep->name;
184 delta = bpf_ktime_get_ns() - *tsp;
185
186 // store as sum or histogram
187 STORE
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800188 }
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800189
Ism Hong99bfe8a2021-12-08 10:17:20 +0800190 start.delete(&key);
191 irqnames.delete(&key);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800192 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700193}
194"""
195
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800196if args.count:
197 bpf_text += bpf_text_count
198else:
199 bpf_text += bpf_text_time
200
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300201# code substitutions
Brendan Gregg860b6492015-10-20 15:52:23 -0700202if args.dist:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800203 bpf_text = bpf_text.replace('STORE',
Kirill Smelkovf2d125e2017-09-25 11:23:03 +0300204 'irq_key_t key = {.slot = bpf_log2l(delta / %d)};' % factor +
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500205 'bpf_probe_read_kernel(&key.name, sizeof(key.name), name);' +
zcy80242fb2021-07-02 00:12:32 +0800206 'dist.atomic_increment(key);')
Brendan Gregg860b6492015-10-20 15:52:23 -0700207else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800208 bpf_text = bpf_text.replace('STORE',
209 'irq_key_t key = {.slot = 0 /* ignore */};' +
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500210 'bpf_probe_read_kernel(&key.name, sizeof(key.name), name);' +
zcy80242fb2021-07-02 00:12:32 +0800211 'dist.atomic_increment(key, delta);')
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800212if args.cpu is not None:
213 bpf_text = bpf_text.replace('FILTER_CPU',
214 'if (cpu != %d) { return 0; }' % int(args.cpu))
215else:
216 bpf_text = bpf_text.replace('FILTER_CPU', '')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100217if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800218 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100219 if args.ebpf:
220 exit()
Brendan Gregg860b6492015-10-20 15:52:23 -0700221
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300222# load BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -0700223b = BPF(text=bpf_text)
224
Brendan Greggc32b8452017-11-26 23:38:32 -0800225if args.count:
Brendan Greggc32b8452017-11-26 23:38:32 -0800226 print("Tracing hard irq events... Hit Ctrl-C to end.")
227else:
Brendan Greggc32b8452017-11-26 23:38:32 -0800228 print("Tracing hard irq event time... Hit Ctrl-C to end.")
Brendan Gregg860b6492015-10-20 15:52:23 -0700229
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300230# output
Brendan Gregg860b6492015-10-20 15:52:23 -0700231exiting = 0 if args.interval else 1
232dist = b.get_table("dist")
233while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800234 try:
235 sleep(int(args.interval))
236 except KeyboardInterrupt:
237 exiting = 1
Brendan Gregg860b6492015-10-20 15:52:23 -0700238
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800239 print()
240 if args.timestamp:
241 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg860b6492015-10-20 15:52:23 -0700242
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800243 if args.dist:
244 dist.print_log2_hist(label, "hardirq")
245 else:
246 print("%-26s %11s" % ("HARDIRQ", "TOTAL_" + label))
247 for k, v in sorted(dist.items(), key=lambda dist: dist[1].value):
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200248 print("%-26s %11d" % (k.name.decode('utf-8', 'replace'), v.value / factor))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800249 dist.clear()
Brendan Gregg860b6492015-10-20 15:52:23 -0700250
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800251 countdown -= 1
252 if exiting or countdown == 0:
253 exit()