blob: 3bcf649278f68a4e00591774be3e8c5a3ccedebe [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
xingfeng25107b2c1142022-03-24 10:51:26 +080022import sys
Brendan Gregg860b6492015-10-20 15:52:23 -070023
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030024# arguments
Brendan Gregg860b6492015-10-20 15:52:23 -070025examples = """examples:
26 ./hardirqs # sum hard irq event time
27 ./hardirqs -d # show hard irq event time as histograms
28 ./hardirqs 1 10 # print 1 second summaries, 10 times
29 ./hardirqs -NT 1 # 1s summaries, nanoseconds, and timestamps
xingfeng2510c5baa7b2022-03-07 17:35:49 +080030 ./hardirqs -c 1 # sum hard irq event time on CPU 1 only
Brendan Gregg860b6492015-10-20 15:52:23 -070031"""
32parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080033 description="Summarize hard irq event time as histograms",
34 formatter_class=argparse.RawDescriptionHelpFormatter,
35 epilog=examples)
Brendan Gregg860b6492015-10-20 15:52:23 -070036parser.add_argument("-T", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080037 help="include timestamp on output")
Brendan Gregg860b6492015-10-20 15:52:23 -070038parser.add_argument("-N", "--nanoseconds", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080039 help="output in nanoseconds")
Brendan Greggc32b8452017-11-26 23:38:32 -080040parser.add_argument("-C", "--count", action="store_true",
41 help="show event counts instead of timing")
Brendan Gregg860b6492015-10-20 15:52:23 -070042parser.add_argument("-d", "--dist", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080043 help="show distributions as histograms")
xingfeng2510c5baa7b2022-03-07 17:35:49 +080044parser.add_argument("-c", "--cpu", type=int,
45 help="trace this CPU only")
Brendan Gregg860b6492015-10-20 15:52:23 -070046parser.add_argument("interval", nargs="?", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080047 help="output interval, in seconds")
Brendan Greggc32b8452017-11-26 23:38:32 -080048parser.add_argument("outputs", nargs="?", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080049 help="number of outputs")
Nathan Scottcf0792f2018-02-02 16:56:50 +110050parser.add_argument("--ebpf", action="store_true",
51 help=argparse.SUPPRESS)
Brendan Gregg860b6492015-10-20 15:52:23 -070052args = parser.parse_args()
Brendan Greggc32b8452017-11-26 23:38:32 -080053countdown = int(args.outputs)
54if args.count and (args.dist or args.nanoseconds):
55 print("The --count option can't be used with time-based options")
56 exit()
57if args.count:
58 factor = 1
59 label = "count"
60elif args.nanoseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080061 factor = 1
62 label = "nsecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070063else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080064 factor = 1000
65 label = "usecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070066debug = 0
67
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030068# define BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -070069bpf_text = """
70#include <uapi/linux/ptrace.h>
71#include <linux/irq.h>
72#include <linux/irqdesc.h>
73#include <linux/interrupt.h>
74
Ism Hong99bfe8a2021-12-08 10:17:20 +080075// Add cpu_id as part of key for irq entry event to handle the case which irq
76// is triggered while idle thread(swapper/x, tid=0) for each cpu core.
77// Please see more detail at pull request #2804, #3733.
78typedef struct entry_key {
79 u32 tid;
80 u32 cpu_id;
81} entry_key_t;
82
Brendan Gregg860b6492015-10-20 15:52:23 -070083typedef struct irq_key {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080084 char name[32];
85 u64 slot;
Brendan Gregg860b6492015-10-20 15:52:23 -070086} irq_key_t;
Hengqi Chene6aa65e2021-05-22 16:07:36 +080087
88typedef struct irq_name {
89 char name[32];
90} irq_name_t;
91
Ism Hong99bfe8a2021-12-08 10:17:20 +080092BPF_HASH(start, entry_key_t);
93BPF_HASH(irqnames, entry_key_t, irq_name_t);
Brendan Gregg860b6492015-10-20 15:52:23 -070094BPF_HISTOGRAM(dist, irq_key_t);
Hengqi Chene6aa65e2021-05-22 16:07:36 +080095"""
Brendan Gregg860b6492015-10-20 15:52:23 -070096
Hengqi Chene6aa65e2021-05-22 16:07:36 +080097bpf_text_count = """
98TRACEPOINT_PROBE(irq, irq_handler_entry)
Brendan Greggc32b8452017-11-26 23:38:32 -080099{
Ism Hong99bfe8a2021-12-08 10:17:20 +0800100 struct entry_key key = {};
Ism Hong814c2642021-12-06 11:28:34 +0800101 irq_name_t name = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800102 u32 cpu = bpf_get_smp_processor_id();
103
104 FILTER_CPU
Ism Hong814c2642021-12-06 11:28:34 +0800105
Ism Hong99bfe8a2021-12-08 10:17:20 +0800106 key.tid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800107 key.cpu_id = cpu;
Ism Hong99bfe8a2021-12-08 10:17:20 +0800108
Ism Hong814c2642021-12-06 11:28:34 +0800109 TP_DATA_LOC_READ_STR(&name.name, name, sizeof(name));
Ism Hong99bfe8a2021-12-08 10:17:20 +0800110 irqnames.update(&key, &name);
Ism Hong814c2642021-12-06 11:28:34 +0800111 return 0;
112}
113
114TRACEPOINT_PROBE(irq, irq_handler_exit)
115{
Ism Hong99bfe8a2021-12-08 10:17:20 +0800116 struct entry_key key = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800117 u32 cpu = bpf_get_smp_processor_id();
118
119 FILTER_CPU
Ism Hong99bfe8a2021-12-08 10:17:20 +0800120
121 key.tid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800122 key.cpu_id = cpu;
Ism Hong814c2642021-12-06 11:28:34 +0800123
124 // check ret value of irq handler is not IRQ_NONE to make sure
125 // the current event belong to this irq handler
126 if (args->ret != IRQ_NONE) {
127 irq_name_t *namep;
128
Ism Hong99bfe8a2021-12-08 10:17:20 +0800129 namep = irqnames.lookup(&key);
Ism Hong814c2642021-12-06 11:28:34 +0800130 if (namep == 0) {
131 return 0; // missed irq name
132 }
133 char *name = (char *)namep->name;
134 irq_key_t key = {.slot = 0 /* ignore */};
135
136 bpf_probe_read_kernel(&key.name, sizeof(key.name), name);
137 dist.atomic_increment(key);
138 }
139
Ism Hong99bfe8a2021-12-08 10:17:20 +0800140 irqnames.delete(&key);
Brendan Greggc32b8452017-11-26 23:38:32 -0800141 return 0;
142}
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800143"""
Brendan Greggc32b8452017-11-26 23:38:32 -0800144
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800145bpf_text_time = """
146TRACEPOINT_PROBE(irq, irq_handler_entry)
Brendan Gregg860b6492015-10-20 15:52:23 -0700147{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800148 u64 ts = bpf_ktime_get_ns();
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800149 irq_name_t name = {};
Ism Hong99bfe8a2021-12-08 10:17:20 +0800150 struct entry_key key = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800151 u32 cpu = bpf_get_smp_processor_id();
152
153 FILTER_CPU
Ism Hong99bfe8a2021-12-08 10:17:20 +0800154
155 key.tid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800156 key.cpu_id = cpu;
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800157
Hengqi Chen2cffe362021-11-22 21:54:51 +0800158 TP_DATA_LOC_READ_STR(&name.name, name, sizeof(name));
Ism Hong99bfe8a2021-12-08 10:17:20 +0800159 irqnames.update(&key, &name);
160 start.update(&key, &ts);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800161 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700162}
163
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800164TRACEPOINT_PROBE(irq, irq_handler_exit)
Brendan Gregg860b6492015-10-20 15:52:23 -0700165{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800166 u64 *tsp, delta;
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800167 irq_name_t *namep;
Ism Hong99bfe8a2021-12-08 10:17:20 +0800168 struct entry_key key = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800169 u32 cpu = bpf_get_smp_processor_id();
Ism Hong99bfe8a2021-12-08 10:17:20 +0800170
171 key.tid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800172 key.cpu_id = cpu;
Brendan Gregg860b6492015-10-20 15:52:23 -0700173
Ism Hong814c2642021-12-06 11:28:34 +0800174 // check ret value of irq handler is not IRQ_NONE to make sure
175 // the current event belong to this irq handler
176 if (args->ret != IRQ_NONE) {
177 // fetch timestamp and calculate delta
Ism Hong99bfe8a2021-12-08 10:17:20 +0800178 tsp = start.lookup(&key);
179 namep = irqnames.lookup(&key);
Ism Hong814c2642021-12-06 11:28:34 +0800180 if (tsp == 0 || namep == 0) {
181 return 0; // missed start
182 }
183
184 char *name = (char *)namep->name;
185 delta = bpf_ktime_get_ns() - *tsp;
186
187 // store as sum or histogram
188 STORE
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800189 }
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800190
Ism Hong99bfe8a2021-12-08 10:17:20 +0800191 start.delete(&key);
192 irqnames.delete(&key);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800193 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700194}
195"""
196
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800197if args.count:
198 bpf_text += bpf_text_count
199else:
200 bpf_text += bpf_text_time
201
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300202# code substitutions
Brendan Gregg860b6492015-10-20 15:52:23 -0700203if args.dist:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800204 bpf_text = bpf_text.replace('STORE',
Kirill Smelkovf2d125e2017-09-25 11:23:03 +0300205 'irq_key_t key = {.slot = bpf_log2l(delta / %d)};' % factor +
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500206 'bpf_probe_read_kernel(&key.name, sizeof(key.name), name);' +
zcy80242fb2021-07-02 00:12:32 +0800207 'dist.atomic_increment(key);')
Brendan Gregg860b6492015-10-20 15:52:23 -0700208else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800209 bpf_text = bpf_text.replace('STORE',
210 'irq_key_t key = {.slot = 0 /* ignore */};' +
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500211 'bpf_probe_read_kernel(&key.name, sizeof(key.name), name);' +
zcy80242fb2021-07-02 00:12:32 +0800212 'dist.atomic_increment(key, delta);')
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800213if args.cpu is not None:
214 bpf_text = bpf_text.replace('FILTER_CPU',
215 'if (cpu != %d) { return 0; }' % int(args.cpu))
216else:
217 bpf_text = bpf_text.replace('FILTER_CPU', '')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100218if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800219 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100220 if args.ebpf:
221 exit()
Brendan Gregg860b6492015-10-20 15:52:23 -0700222
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300223# load BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -0700224b = BPF(text=bpf_text)
225
Brendan Greggc32b8452017-11-26 23:38:32 -0800226if args.count:
Brendan Greggc32b8452017-11-26 23:38:32 -0800227 print("Tracing hard irq events... Hit Ctrl-C to end.")
228else:
Brendan Greggc32b8452017-11-26 23:38:32 -0800229 print("Tracing hard irq event time... Hit Ctrl-C to end.")
Brendan Gregg860b6492015-10-20 15:52:23 -0700230
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300231# output
Brendan Gregg860b6492015-10-20 15:52:23 -0700232exiting = 0 if args.interval else 1
233dist = b.get_table("dist")
234while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800235 try:
236 sleep(int(args.interval))
237 except KeyboardInterrupt:
238 exiting = 1
Brendan Gregg860b6492015-10-20 15:52:23 -0700239
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800240 print()
241 if args.timestamp:
242 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg860b6492015-10-20 15:52:23 -0700243
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800244 if args.dist:
xingfeng251055dcd2e2022-03-13 18:13:57 +0800245 dist.print_log2_hist(label, "hardirq", section_print_fn=bytes.decode)
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800246 else:
247 print("%-26s %11s" % ("HARDIRQ", "TOTAL_" + label))
248 for k, v in sorted(dist.items(), key=lambda dist: dist[1].value):
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200249 print("%-26s %11d" % (k.name.decode('utf-8', 'replace'), v.value / factor))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800250 dist.clear()
Brendan Gregg860b6492015-10-20 15:52:23 -0700251
xingfeng25107b2c1142022-03-24 10:51:26 +0800252 sys.stdout.flush()
253
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800254 countdown -= 1
255 if exiting or countdown == 0:
256 exit()