blob: 0eeddddc086ee8e40e0479d04a2551d8e729871a [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#
Brendan Greggc32b8452017-11-26 23:38:32 -08007# USAGE: hardirqs [-h] [-T] [-N] [-C] [-d] [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.
Brendan Gregg860b6492015-10-20 15:52:23 -070016
17from __future__ import print_function
18from bcc import BPF
19from time import sleep, strftime
20import argparse
21
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030022# arguments
Brendan Gregg860b6492015-10-20 15:52:23 -070023examples = """examples:
24 ./hardirqs # sum hard irq event time
25 ./hardirqs -d # show hard irq event time as histograms
26 ./hardirqs 1 10 # print 1 second summaries, 10 times
27 ./hardirqs -NT 1 # 1s summaries, nanoseconds, and timestamps
28"""
29parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080030 description="Summarize hard irq event time as histograms",
31 formatter_class=argparse.RawDescriptionHelpFormatter,
32 epilog=examples)
Brendan Gregg860b6492015-10-20 15:52:23 -070033parser.add_argument("-T", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080034 help="include timestamp on output")
Brendan Gregg860b6492015-10-20 15:52:23 -070035parser.add_argument("-N", "--nanoseconds", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080036 help="output in nanoseconds")
Brendan Greggc32b8452017-11-26 23:38:32 -080037parser.add_argument("-C", "--count", action="store_true",
38 help="show event counts instead of timing")
Brendan Gregg860b6492015-10-20 15:52:23 -070039parser.add_argument("-d", "--dist", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080040 help="show distributions as histograms")
Brendan Gregg860b6492015-10-20 15:52:23 -070041parser.add_argument("interval", nargs="?", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080042 help="output interval, in seconds")
Brendan Greggc32b8452017-11-26 23:38:32 -080043parser.add_argument("outputs", nargs="?", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080044 help="number of outputs")
Nathan Scottcf0792f2018-02-02 16:56:50 +110045parser.add_argument("--ebpf", action="store_true",
46 help=argparse.SUPPRESS)
Brendan Gregg860b6492015-10-20 15:52:23 -070047args = parser.parse_args()
Brendan Greggc32b8452017-11-26 23:38:32 -080048countdown = int(args.outputs)
49if args.count and (args.dist or args.nanoseconds):
50 print("The --count option can't be used with time-based options")
51 exit()
52if args.count:
53 factor = 1
54 label = "count"
55elif args.nanoseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080056 factor = 1
57 label = "nsecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070058else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080059 factor = 1000
60 label = "usecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070061debug = 0
62
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030063# define BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -070064bpf_text = """
65#include <uapi/linux/ptrace.h>
66#include <linux/irq.h>
67#include <linux/irqdesc.h>
68#include <linux/interrupt.h>
69
Ism Hong99bfe8a2021-12-08 10:17:20 +080070// Add cpu_id as part of key for irq entry event to handle the case which irq
71// is triggered while idle thread(swapper/x, tid=0) for each cpu core.
72// Please see more detail at pull request #2804, #3733.
73typedef struct entry_key {
74 u32 tid;
75 u32 cpu_id;
76} entry_key_t;
77
Brendan Gregg860b6492015-10-20 15:52:23 -070078typedef struct irq_key {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080079 char name[32];
80 u64 slot;
Brendan Gregg860b6492015-10-20 15:52:23 -070081} irq_key_t;
Hengqi Chene6aa65e2021-05-22 16:07:36 +080082
83typedef struct irq_name {
84 char name[32];
85} irq_name_t;
86
Ism Hong99bfe8a2021-12-08 10:17:20 +080087BPF_HASH(start, entry_key_t);
88BPF_HASH(irqnames, entry_key_t, irq_name_t);
Brendan Gregg860b6492015-10-20 15:52:23 -070089BPF_HISTOGRAM(dist, irq_key_t);
Hengqi Chene6aa65e2021-05-22 16:07:36 +080090"""
Brendan Gregg860b6492015-10-20 15:52:23 -070091
Hengqi Chene6aa65e2021-05-22 16:07:36 +080092bpf_text_count = """
93TRACEPOINT_PROBE(irq, irq_handler_entry)
Brendan Greggc32b8452017-11-26 23:38:32 -080094{
Ism Hong99bfe8a2021-12-08 10:17:20 +080095 struct entry_key key = {};
Ism Hong814c2642021-12-06 11:28:34 +080096 irq_name_t name = {};
97
Ism Hong99bfe8a2021-12-08 10:17:20 +080098 key.tid = bpf_get_current_pid_tgid();
99 key.cpu_id = bpf_get_smp_processor_id();
100
Ism Hong814c2642021-12-06 11:28:34 +0800101 TP_DATA_LOC_READ_STR(&name.name, name, sizeof(name));
Ism Hong99bfe8a2021-12-08 10:17:20 +0800102 irqnames.update(&key, &name);
Ism Hong814c2642021-12-06 11:28:34 +0800103 return 0;
104}
105
106TRACEPOINT_PROBE(irq, irq_handler_exit)
107{
Ism Hong99bfe8a2021-12-08 10:17:20 +0800108 struct entry_key key = {};
109
110 key.tid = bpf_get_current_pid_tgid();
111 key.cpu_id = bpf_get_smp_processor_id();
Ism Hong814c2642021-12-06 11:28:34 +0800112
113 // check ret value of irq handler is not IRQ_NONE to make sure
114 // the current event belong to this irq handler
115 if (args->ret != IRQ_NONE) {
116 irq_name_t *namep;
117
Ism Hong99bfe8a2021-12-08 10:17:20 +0800118 namep = irqnames.lookup(&key);
Ism Hong814c2642021-12-06 11:28:34 +0800119 if (namep == 0) {
120 return 0; // missed irq name
121 }
122 char *name = (char *)namep->name;
123 irq_key_t key = {.slot = 0 /* ignore */};
124
125 bpf_probe_read_kernel(&key.name, sizeof(key.name), name);
126 dist.atomic_increment(key);
127 }
128
Ism Hong99bfe8a2021-12-08 10:17:20 +0800129 irqnames.delete(&key);
Brendan Greggc32b8452017-11-26 23:38:32 -0800130 return 0;
131}
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800132"""
Brendan Greggc32b8452017-11-26 23:38:32 -0800133
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800134bpf_text_time = """
135TRACEPOINT_PROBE(irq, irq_handler_entry)
Brendan Gregg860b6492015-10-20 15:52:23 -0700136{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800137 u64 ts = bpf_ktime_get_ns();
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800138 irq_name_t name = {};
Ism Hong99bfe8a2021-12-08 10:17:20 +0800139 struct entry_key key = {};
140
141 key.tid = bpf_get_current_pid_tgid();
142 key.cpu_id = bpf_get_smp_processor_id();
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800143
Hengqi Chen2cffe362021-11-22 21:54:51 +0800144 TP_DATA_LOC_READ_STR(&name.name, name, sizeof(name));
Ism Hong99bfe8a2021-12-08 10:17:20 +0800145 irqnames.update(&key, &name);
146 start.update(&key, &ts);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800147 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700148}
149
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800150TRACEPOINT_PROBE(irq, irq_handler_exit)
Brendan Gregg860b6492015-10-20 15:52:23 -0700151{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800152 u64 *tsp, delta;
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800153 irq_name_t *namep;
Ism Hong99bfe8a2021-12-08 10:17:20 +0800154 struct entry_key key = {};
155
156 key.tid = bpf_get_current_pid_tgid();
157 key.cpu_id = bpf_get_smp_processor_id();
Brendan Gregg860b6492015-10-20 15:52:23 -0700158
Ism Hong814c2642021-12-06 11:28:34 +0800159 // check ret value of irq handler is not IRQ_NONE to make sure
160 // the current event belong to this irq handler
161 if (args->ret != IRQ_NONE) {
162 // fetch timestamp and calculate delta
Ism Hong99bfe8a2021-12-08 10:17:20 +0800163 tsp = start.lookup(&key);
164 namep = irqnames.lookup(&key);
Ism Hong814c2642021-12-06 11:28:34 +0800165 if (tsp == 0 || namep == 0) {
166 return 0; // missed start
167 }
168
169 char *name = (char *)namep->name;
170 delta = bpf_ktime_get_ns() - *tsp;
171
172 // store as sum or histogram
173 STORE
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800174 }
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800175
Ism Hong99bfe8a2021-12-08 10:17:20 +0800176 start.delete(&key);
177 irqnames.delete(&key);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800178 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700179}
180"""
181
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800182if args.count:
183 bpf_text += bpf_text_count
184else:
185 bpf_text += bpf_text_time
186
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300187# code substitutions
Brendan Gregg860b6492015-10-20 15:52:23 -0700188if args.dist:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800189 bpf_text = bpf_text.replace('STORE',
Kirill Smelkovf2d125e2017-09-25 11:23:03 +0300190 'irq_key_t key = {.slot = bpf_log2l(delta / %d)};' % factor +
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500191 'bpf_probe_read_kernel(&key.name, sizeof(key.name), name);' +
zcy80242fb2021-07-02 00:12:32 +0800192 'dist.atomic_increment(key);')
Brendan Gregg860b6492015-10-20 15:52:23 -0700193else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800194 bpf_text = bpf_text.replace('STORE',
195 'irq_key_t key = {.slot = 0 /* ignore */};' +
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500196 'bpf_probe_read_kernel(&key.name, sizeof(key.name), name);' +
zcy80242fb2021-07-02 00:12:32 +0800197 'dist.atomic_increment(key, delta);')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100198if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800199 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100200 if args.ebpf:
201 exit()
Brendan Gregg860b6492015-10-20 15:52:23 -0700202
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300203# load BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -0700204b = BPF(text=bpf_text)
205
Brendan Greggc32b8452017-11-26 23:38:32 -0800206if args.count:
Brendan Greggc32b8452017-11-26 23:38:32 -0800207 print("Tracing hard irq events... Hit Ctrl-C to end.")
208else:
Brendan Greggc32b8452017-11-26 23:38:32 -0800209 print("Tracing hard irq event time... Hit Ctrl-C to end.")
Brendan Gregg860b6492015-10-20 15:52:23 -0700210
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300211# output
Brendan Gregg860b6492015-10-20 15:52:23 -0700212exiting = 0 if args.interval else 1
213dist = b.get_table("dist")
214while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800215 try:
216 sleep(int(args.interval))
217 except KeyboardInterrupt:
218 exiting = 1
Brendan Gregg860b6492015-10-20 15:52:23 -0700219
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800220 print()
221 if args.timestamp:
222 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg860b6492015-10-20 15:52:23 -0700223
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800224 if args.dist:
225 dist.print_log2_hist(label, "hardirq")
226 else:
227 print("%-26s %11s" % ("HARDIRQ", "TOTAL_" + label))
228 for k, v in sorted(dist.items(), key=lambda dist: dist[1].value):
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200229 print("%-26s %11d" % (k.name.decode('utf-8', 'replace'), v.value / factor))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800230 dist.clear()
Brendan Gregg860b6492015-10-20 15:52:23 -0700231
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800232 countdown -= 1
233 if exiting or countdown == 0:
234 exit()