blob: 0ed18c40dd1d52e511e82f1c4c58256423cd9949 [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# softirqs Summarize soft IRQ (interrupt) event time.
5# For Linux, uses BCC, eBPF.
Brendan Gregg860b6492015-10-20 15:52:23 -07006#
xingfeng251015ed9972022-03-24 21:10:28 +08007# USAGE: softirqs [-h] [-T] [-N] [-C] [-d] [-c CPU] [interval] [count]
Brendan Gregg860b6492015-10-20 15:52:23 -07008#
9# Copyright (c) 2015 Brendan Gregg.
10# Licensed under the Apache License, Version 2.0 (the "License")
11#
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030012# 20-Oct-2015 Brendan Gregg Created this.
13# 03-Apr-2017 Sasha Goldshtein Migrated to kernel tracepoints.
xingfeng2510c5baa7b2022-03-07 17:35:49 +080014# 07-Mar-2022 Rocky Xing Added CPU filter support.
xingfeng251015ed9972022-03-24 21:10:28 +080015# 24-Mar-2022 Rocky Xing Added event counting support.
Brendan Gregg860b6492015-10-20 15:52:23 -070016
17from __future__ import print_function
18from bcc import BPF
19from time import sleep, strftime
20import argparse
xingfeng25107b2c1142022-03-24 10:51:26 +080021import sys
Brendan Gregg860b6492015-10-20 15:52:23 -070022
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030023# arguments
Brendan Gregg860b6492015-10-20 15:52:23 -070024examples = """examples:
25 ./softirqs # sum soft irq event time
xingfeng251015ed9972022-03-24 21:10:28 +080026 ./softirqs -C # show the number of soft irq events
Brendan Gregg860b6492015-10-20 15:52:23 -070027 ./softirqs -d # show soft irq event time as histograms
28 ./softirqs 1 10 # print 1 second summaries, 10 times
29 ./softirqs -NT 1 # 1s summaries, nanoseconds, and timestamps
xingfeng2510c5baa7b2022-03-07 17:35:49 +080030 ./softirqs -c 1 # sum soft irq event time on CPU 1 only
Brendan Gregg860b6492015-10-20 15:52:23 -070031"""
32parser = argparse.ArgumentParser(
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030033 description="Summarize soft irq event time as histograms.",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080034 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")
xingfeng251015ed9972022-03-24 21:10:28 +080040parser.add_argument("-C", "--events", action="store_true",
41 help="show the number of soft irq events")
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 Gregg860b6492015-10-20 15:52:23 -070048parser.add_argument("count", 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()
53countdown = int(args.count)
xingfeng251015ed9972022-03-24 21:10:28 +080054if args.events and (args.dist or args.nanoseconds):
55 print("The --events option can't be used with time-based options")
56 exit()
57if args.events:
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
edwardwu9465f8c2020-03-09 14:09:52 +080072typedef struct entry_key {
73 u32 pid;
74 u32 cpu;
75} entry_key_t;
76
Brendan Gregg860b6492015-10-20 15:52:23 -070077typedef struct irq_key {
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030078 u32 vec;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080079 u64 slot;
Brendan Gregg860b6492015-10-20 15:52:23 -070080} irq_key_t;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030081
82typedef struct account_val {
83 u64 ts;
84 u32 vec;
85} account_val_t;
86
edwardwu9465f8c2020-03-09 14:09:52 +080087BPF_HASH(start, entry_key_t, account_val_t);
Brendan Gregg860b6492015-10-20 15:52:23 -070088BPF_HISTOGRAM(dist, irq_key_t);
xingfeng251015ed9972022-03-24 21:10:28 +080089"""
Brendan Gregg860b6492015-10-20 15:52:23 -070090
xingfeng251015ed9972022-03-24 21:10:28 +080091bpf_text_count = """
92TRACEPOINT_PROBE(irq, softirq_entry)
93{
94 u32 cpu = bpf_get_smp_processor_id();
95
96 FILTER_CPU
97
98 irq_key_t key = { .slot = 0 /* ignore */ };
99 key.vec = args->vec;
100
101 dist.atomic_increment(key);
102
103 return 0;
104}
105"""
106
107bpf_text_time = """
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300108TRACEPOINT_PROBE(irq, softirq_entry)
Brendan Gregg860b6492015-10-20 15:52:23 -0700109{
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300110 account_val_t val = {};
edwardwu9465f8c2020-03-09 14:09:52 +0800111 entry_key_t key = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800112 u32 cpu = bpf_get_smp_processor_id();
113
114 FILTER_CPU
edwardwu9465f8c2020-03-09 14:09:52 +0800115
116 key.pid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800117 key.cpu = cpu;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300118 val.ts = bpf_ktime_get_ns();
119 val.vec = args->vec;
edwardwu9465f8c2020-03-09 14:09:52 +0800120
121 start.update(&key, &val);
122
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800123 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700124}
125
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300126TRACEPOINT_PROBE(irq, softirq_exit)
Brendan Gregg860b6492015-10-20 15:52:23 -0700127{
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300128 u64 delta;
129 u32 vec;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300130 account_val_t *valp;
131 irq_key_t key = {0};
edwardwu9465f8c2020-03-09 14:09:52 +0800132 entry_key_t entry_key = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800133 u32 cpu = bpf_get_smp_processor_id();
134
135 FILTER_CPU
edwardwu9465f8c2020-03-09 14:09:52 +0800136
137 entry_key.pid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800138 entry_key.cpu = cpu;
Brendan Gregg860b6492015-10-20 15:52:23 -0700139
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800140 // fetch timestamp and calculate delta
edwardwu9465f8c2020-03-09 14:09:52 +0800141 valp = start.lookup(&entry_key);
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300142 if (valp == 0) {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800143 return 0; // missed start
144 }
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300145 delta = bpf_ktime_get_ns() - valp->ts;
146 vec = valp->vec;
Brendan Gregg860b6492015-10-20 15:52:23 -0700147
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800148 // store as sum or histogram
149 STORE
Brendan Gregg860b6492015-10-20 15:52:23 -0700150
edwardwu9465f8c2020-03-09 14:09:52 +0800151 start.delete(&entry_key);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800152 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700153}
154"""
155
xingfeng251015ed9972022-03-24 21:10:28 +0800156if args.events:
157 bpf_text += bpf_text_count
158else:
159 bpf_text += bpf_text_time
160
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300161# code substitutions
Brendan Gregg860b6492015-10-20 15:52:23 -0700162if args.dist:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800163 bpf_text = bpf_text.replace('STORE',
Kirill Smelkovf2d125e2017-09-25 11:23:03 +0300164 'key.vec = vec; key.slot = bpf_log2l(delta / %d); ' % factor +
zcy80242fb2021-07-02 00:12:32 +0800165 'dist.atomic_increment(key);')
Brendan Gregg860b6492015-10-20 15:52:23 -0700166else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800167 bpf_text = bpf_text.replace('STORE',
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300168 'key.vec = valp->vec; ' +
zcy80242fb2021-07-02 00:12:32 +0800169 'dist.atomic_increment(key, delta);')
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800170if args.cpu is not None:
171 bpf_text = bpf_text.replace('FILTER_CPU',
172 'if (cpu != %d) { return 0; }' % int(args.cpu))
173else:
174 bpf_text = bpf_text.replace('FILTER_CPU', '')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100175if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800176 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100177 if args.ebpf:
178 exit()
Brendan Gregg860b6492015-10-20 15:52:23 -0700179
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300180# load BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -0700181b = BPF(text=bpf_text)
182
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300183def vec_to_name(vec):
184 # copied from softirq_to_name() in kernel/softirq.c
185 # may need updates if new softirq handlers are added
186 return ["hi", "timer", "net_tx", "net_rx", "block", "irq_poll",
187 "tasklet", "sched", "hrtimer", "rcu"][vec]
Brendan Gregg860b6492015-10-20 15:52:23 -0700188
xingfeng251015ed9972022-03-24 21:10:28 +0800189if args.events:
190 print("Tracing soft irq events... Hit Ctrl-C to end.")
191else:
192 print("Tracing soft irq event time... Hit Ctrl-C to end.")
Brendan Gregg860b6492015-10-20 15:52:23 -0700193
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300194# output
Brendan Gregg860b6492015-10-20 15:52:23 -0700195exiting = 0 if args.interval else 1
196dist = b.get_table("dist")
197while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800198 try:
199 sleep(int(args.interval))
200 except KeyboardInterrupt:
201 exiting = 1
Brendan Gregg860b6492015-10-20 15:52:23 -0700202
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800203 print()
204 if args.timestamp:
205 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg860b6492015-10-20 15:52:23 -0700206
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800207 if args.dist:
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300208 dist.print_log2_hist(label, "softirq", section_print_fn=vec_to_name)
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800209 else:
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300210 print("%-16s %11s" % ("SOFTIRQ", "TOTAL_" + label))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800211 for k, v in sorted(dist.items(), key=lambda dist: dist[1].value):
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300212 print("%-16s %11d" % (vec_to_name(k.vec), v.value / factor))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800213 dist.clear()
Brendan Gregg860b6492015-10-20 15:52:23 -0700214
xingfeng25107b2c1142022-03-24 10:51:26 +0800215 sys.stdout.flush()
216
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800217 countdown -= 1
218 if exiting or countdown == 0:
219 exit()