blob: d98b93871a7f7bf5bff5b72e123bc7bdc7cfe790 [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#
xingfeng2510c5baa7b2022-03-07 17:35:49 +08007# USAGE: softirqs [-h] [-T] [-N] [-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.
Brendan Gregg860b6492015-10-20 15:52:23 -070015
16from __future__ import print_function
17from bcc import BPF
18from time import sleep, strftime
19import argparse
20
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030021# arguments
Brendan Gregg860b6492015-10-20 15:52:23 -070022examples = """examples:
23 ./softirqs # sum soft irq event time
24 ./softirqs -d # show soft irq event time as histograms
25 ./softirqs 1 10 # print 1 second summaries, 10 times
26 ./softirqs -NT 1 # 1s summaries, nanoseconds, and timestamps
xingfeng2510c5baa7b2022-03-07 17:35:49 +080027 ./softirqs -c 1 # sum soft irq event time on CPU 1 only
Brendan Gregg860b6492015-10-20 15:52:23 -070028"""
29parser = argparse.ArgumentParser(
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030030 description="Summarize soft irq event time as histograms.",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080031 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 Gregg860b6492015-10-20 15:52:23 -070037parser.add_argument("-d", "--dist", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080038 help="show distributions as histograms")
xingfeng2510c5baa7b2022-03-07 17:35:49 +080039parser.add_argument("-c", "--cpu", type=int,
40 help="trace this CPU only")
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 Gregg860b6492015-10-20 15:52:23 -070043parser.add_argument("count", 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()
48countdown = int(args.count)
49if args.nanoseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080050 factor = 1
51 label = "nsecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070052else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080053 factor = 1000
54 label = "usecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070055debug = 0
56
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030057# define BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -070058bpf_text = """
59#include <uapi/linux/ptrace.h>
60
edwardwu9465f8c2020-03-09 14:09:52 +080061typedef struct entry_key {
62 u32 pid;
63 u32 cpu;
64} entry_key_t;
65
Brendan Gregg860b6492015-10-20 15:52:23 -070066typedef struct irq_key {
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030067 u32 vec;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080068 u64 slot;
Brendan Gregg860b6492015-10-20 15:52:23 -070069} irq_key_t;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030070
71typedef struct account_val {
72 u64 ts;
73 u32 vec;
74} account_val_t;
75
edwardwu9465f8c2020-03-09 14:09:52 +080076BPF_HASH(start, entry_key_t, account_val_t);
Brendan Gregg860b6492015-10-20 15:52:23 -070077BPF_HISTOGRAM(dist, irq_key_t);
78
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030079TRACEPOINT_PROBE(irq, softirq_entry)
Brendan Gregg860b6492015-10-20 15:52:23 -070080{
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030081 account_val_t val = {};
edwardwu9465f8c2020-03-09 14:09:52 +080082 entry_key_t key = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +080083 u32 cpu = bpf_get_smp_processor_id();
84
85 FILTER_CPU
edwardwu9465f8c2020-03-09 14:09:52 +080086
87 key.pid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +080088 key.cpu = cpu;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030089 val.ts = bpf_ktime_get_ns();
90 val.vec = args->vec;
edwardwu9465f8c2020-03-09 14:09:52 +080091
92 start.update(&key, &val);
93
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080094 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -070095}
96
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030097TRACEPOINT_PROBE(irq, softirq_exit)
Brendan Gregg860b6492015-10-20 15:52:23 -070098{
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030099 u64 delta;
100 u32 vec;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300101 account_val_t *valp;
102 irq_key_t key = {0};
edwardwu9465f8c2020-03-09 14:09:52 +0800103 entry_key_t entry_key = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800104 u32 cpu = bpf_get_smp_processor_id();
105
106 FILTER_CPU
edwardwu9465f8c2020-03-09 14:09:52 +0800107
108 entry_key.pid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800109 entry_key.cpu = cpu;
Brendan Gregg860b6492015-10-20 15:52:23 -0700110
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800111 // fetch timestamp and calculate delta
edwardwu9465f8c2020-03-09 14:09:52 +0800112 valp = start.lookup(&entry_key);
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300113 if (valp == 0) {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800114 return 0; // missed start
115 }
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300116 delta = bpf_ktime_get_ns() - valp->ts;
117 vec = valp->vec;
Brendan Gregg860b6492015-10-20 15:52:23 -0700118
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800119 // store as sum or histogram
120 STORE
Brendan Gregg860b6492015-10-20 15:52:23 -0700121
edwardwu9465f8c2020-03-09 14:09:52 +0800122 start.delete(&entry_key);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800123 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700124}
125"""
126
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300127# code substitutions
Brendan Gregg860b6492015-10-20 15:52:23 -0700128if args.dist:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800129 bpf_text = bpf_text.replace('STORE',
Kirill Smelkovf2d125e2017-09-25 11:23:03 +0300130 'key.vec = vec; key.slot = bpf_log2l(delta / %d); ' % factor +
zcy80242fb2021-07-02 00:12:32 +0800131 'dist.atomic_increment(key);')
Brendan Gregg860b6492015-10-20 15:52:23 -0700132else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800133 bpf_text = bpf_text.replace('STORE',
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300134 'key.vec = valp->vec; ' +
zcy80242fb2021-07-02 00:12:32 +0800135 'dist.atomic_increment(key, delta);')
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800136if args.cpu is not None:
137 bpf_text = bpf_text.replace('FILTER_CPU',
138 'if (cpu != %d) { return 0; }' % int(args.cpu))
139else:
140 bpf_text = bpf_text.replace('FILTER_CPU', '')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100141if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800142 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100143 if args.ebpf:
144 exit()
Brendan Gregg860b6492015-10-20 15:52:23 -0700145
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300146# load BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -0700147b = BPF(text=bpf_text)
148
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300149def vec_to_name(vec):
150 # copied from softirq_to_name() in kernel/softirq.c
151 # may need updates if new softirq handlers are added
152 return ["hi", "timer", "net_tx", "net_rx", "block", "irq_poll",
153 "tasklet", "sched", "hrtimer", "rcu"][vec]
Brendan Gregg860b6492015-10-20 15:52:23 -0700154
155print("Tracing soft irq event time... Hit Ctrl-C to end.")
156
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300157# output
Brendan Gregg860b6492015-10-20 15:52:23 -0700158exiting = 0 if args.interval else 1
159dist = b.get_table("dist")
160while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800161 try:
162 sleep(int(args.interval))
163 except KeyboardInterrupt:
164 exiting = 1
Brendan Gregg860b6492015-10-20 15:52:23 -0700165
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800166 print()
167 if args.timestamp:
168 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg860b6492015-10-20 15:52:23 -0700169
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800170 if args.dist:
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300171 dist.print_log2_hist(label, "softirq", section_print_fn=vec_to_name)
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800172 else:
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300173 print("%-16s %11s" % ("SOFTIRQ", "TOTAL_" + label))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800174 for k, v in sorted(dist.items(), key=lambda dist: dist[1].value):
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300175 print("%-16s %11d" % (vec_to_name(k.vec), v.value / factor))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800176 dist.clear()
Brendan Gregg860b6492015-10-20 15:52:23 -0700177
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800178 countdown -= 1
179 if exiting or countdown == 0:
180 exit()