blob: b60b63ada71498ddb8244d558d27d7cd7b240ac1 [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
70typedef struct irq_key {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080071 char name[32];
72 u64 slot;
Brendan Gregg860b6492015-10-20 15:52:23 -070073} irq_key_t;
Hengqi Chene6aa65e2021-05-22 16:07:36 +080074
75typedef struct irq_name {
76 char name[32];
77} irq_name_t;
78
Brendan Gregg860b6492015-10-20 15:52:23 -070079BPF_HASH(start, u32);
Hengqi Chene6aa65e2021-05-22 16:07:36 +080080BPF_HASH(irqnames, u32, irq_name_t);
Brendan Gregg860b6492015-10-20 15:52:23 -070081BPF_HISTOGRAM(dist, irq_key_t);
Hengqi Chene6aa65e2021-05-22 16:07:36 +080082"""
Brendan Gregg860b6492015-10-20 15:52:23 -070083
Hengqi Chene6aa65e2021-05-22 16:07:36 +080084bpf_text_count = """
85TRACEPOINT_PROBE(irq, irq_handler_entry)
Brendan Greggc32b8452017-11-26 23:38:32 -080086{
Brendan Greggc32b8452017-11-26 23:38:32 -080087 irq_key_t key = {.slot = 0 /* ignore */};
Hengqi Chene6aa65e2021-05-22 16:07:36 +080088 TP_DATA_LOC_READ_CONST(&key.name, name, sizeof(key.name));
Javier Honduvilla Coto64bf9652018-08-01 06:50:19 +020089 dist.increment(key);
Brendan Greggc32b8452017-11-26 23:38:32 -080090 return 0;
91}
Hengqi Chene6aa65e2021-05-22 16:07:36 +080092"""
Brendan Greggc32b8452017-11-26 23:38:32 -080093
Hengqi Chene6aa65e2021-05-22 16:07:36 +080094bpf_text_time = """
95TRACEPOINT_PROBE(irq, irq_handler_entry)
Brendan Gregg860b6492015-10-20 15:52:23 -070096{
Hengqi Chene6aa65e2021-05-22 16:07:36 +080097 u32 tid = bpf_get_current_pid_tgid();
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080098 u64 ts = bpf_ktime_get_ns();
Hengqi Chene6aa65e2021-05-22 16:07:36 +080099 irq_name_t name = {};
100
101 TP_DATA_LOC_READ_CONST(&name.name, name, sizeof(name));
102 irqnames.update(&tid, &name);
103 start.update(&tid, &ts);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800104 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700105}
106
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800107TRACEPOINT_PROBE(irq, irq_handler_exit)
Brendan Gregg860b6492015-10-20 15:52:23 -0700108{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800109 u64 *tsp, delta;
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800110 irq_name_t *namep;
111 u32 tid = bpf_get_current_pid_tgid();
Brendan Gregg860b6492015-10-20 15:52:23 -0700112
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800113 // fetch timestamp and calculate delta
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800114 tsp = start.lookup(&tid);
115 namep = irqnames.lookup(&tid);
116 if (tsp == 0 || namep == 0) {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800117 return 0; // missed start
118 }
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800119
120 char *name = (char *)namep->name;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800121 delta = bpf_ktime_get_ns() - *tsp;
Brendan Gregg860b6492015-10-20 15:52:23 -0700122
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800123 // store as sum or histogram
124 STORE
Brendan Gregg860b6492015-10-20 15:52:23 -0700125
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800126 start.delete(&tid);
127 irqnames.delete(&tid);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800128 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700129}
130"""
131
Hengqi Chene6aa65e2021-05-22 16:07:36 +0800132if args.count:
133 bpf_text += bpf_text_count
134else:
135 bpf_text += bpf_text_time
136
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300137# code substitutions
Brendan Gregg860b6492015-10-20 15:52:23 -0700138if args.dist:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800139 bpf_text = bpf_text.replace('STORE',
Kirill Smelkovf2d125e2017-09-25 11:23:03 +0300140 'irq_key_t key = {.slot = bpf_log2l(delta / %d)};' % factor +
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500141 'bpf_probe_read_kernel(&key.name, sizeof(key.name), name);' +
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800142 'dist.increment(key);')
Brendan Gregg860b6492015-10-20 15:52:23 -0700143else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800144 bpf_text = bpf_text.replace('STORE',
145 'irq_key_t key = {.slot = 0 /* ignore */};' +
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500146 'bpf_probe_read_kernel(&key.name, sizeof(key.name), name);' +
Javier Honduvilla Coto64bf9652018-08-01 06:50:19 +0200147 'dist.increment(key, delta);')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100148if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800149 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100150 if args.ebpf:
151 exit()
Brendan Gregg860b6492015-10-20 15:52:23 -0700152
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300153# load BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -0700154b = BPF(text=bpf_text)
155
Brendan Greggc32b8452017-11-26 23:38:32 -0800156if args.count:
Brendan Greggc32b8452017-11-26 23:38:32 -0800157 print("Tracing hard irq events... Hit Ctrl-C to end.")
158else:
Brendan Greggc32b8452017-11-26 23:38:32 -0800159 print("Tracing hard irq event time... Hit Ctrl-C to end.")
Brendan Gregg860b6492015-10-20 15:52:23 -0700160
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300161# output
Brendan Gregg860b6492015-10-20 15:52:23 -0700162exiting = 0 if args.interval else 1
163dist = b.get_table("dist")
164while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800165 try:
166 sleep(int(args.interval))
167 except KeyboardInterrupt:
168 exiting = 1
Brendan Gregg860b6492015-10-20 15:52:23 -0700169
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800170 print()
171 if args.timestamp:
172 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg860b6492015-10-20 15:52:23 -0700173
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800174 if args.dist:
175 dist.print_log2_hist(label, "hardirq")
176 else:
177 print("%-26s %11s" % ("HARDIRQ", "TOTAL_" + label))
178 for k, v in sorted(dist.items(), key=lambda dist: dist[1].value):
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200179 print("%-26s %11d" % (k.name.decode('utf-8', 'replace'), v.value / factor))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800180 dist.clear()
Brendan Gregg860b6492015-10-20 15:52:23 -0700181
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800182 countdown -= 1
183 if exiting or countdown == 0:
184 exit()