blob: 3d4f8d2735b29a959a782d67ee9aab8d68fe6cbe [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_HASH(iptr, u32);
78BPF_HISTOGRAM(dist, irq_key_t);
79
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030080TRACEPOINT_PROBE(irq, softirq_entry)
Brendan Gregg860b6492015-10-20 15:52:23 -070081{
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030082 account_val_t val = {};
edwardwu9465f8c2020-03-09 14:09:52 +080083 entry_key_t key = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +080084 u32 cpu = bpf_get_smp_processor_id();
85
86 FILTER_CPU
edwardwu9465f8c2020-03-09 14:09:52 +080087
88 key.pid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +080089 key.cpu = cpu;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030090 val.ts = bpf_ktime_get_ns();
91 val.vec = args->vec;
edwardwu9465f8c2020-03-09 14:09:52 +080092
93 start.update(&key, &val);
94
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080095 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -070096}
97
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030098TRACEPOINT_PROBE(irq, softirq_exit)
Brendan Gregg860b6492015-10-20 15:52:23 -070099{
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300100 u64 delta;
101 u32 vec;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300102 account_val_t *valp;
103 irq_key_t key = {0};
edwardwu9465f8c2020-03-09 14:09:52 +0800104 entry_key_t entry_key = {};
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800105 u32 cpu = bpf_get_smp_processor_id();
106
107 FILTER_CPU
edwardwu9465f8c2020-03-09 14:09:52 +0800108
109 entry_key.pid = bpf_get_current_pid_tgid();
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800110 entry_key.cpu = cpu;
Brendan Gregg860b6492015-10-20 15:52:23 -0700111
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800112 // fetch timestamp and calculate delta
edwardwu9465f8c2020-03-09 14:09:52 +0800113 valp = start.lookup(&entry_key);
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300114 if (valp == 0) {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800115 return 0; // missed start
116 }
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300117 delta = bpf_ktime_get_ns() - valp->ts;
118 vec = valp->vec;
Brendan Gregg860b6492015-10-20 15:52:23 -0700119
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800120 // store as sum or histogram
121 STORE
Brendan Gregg860b6492015-10-20 15:52:23 -0700122
edwardwu9465f8c2020-03-09 14:09:52 +0800123 start.delete(&entry_key);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800124 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700125}
126"""
127
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300128# code substitutions
Brendan Gregg860b6492015-10-20 15:52:23 -0700129if args.dist:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800130 bpf_text = bpf_text.replace('STORE',
Kirill Smelkovf2d125e2017-09-25 11:23:03 +0300131 'key.vec = vec; key.slot = bpf_log2l(delta / %d); ' % factor +
zcy80242fb2021-07-02 00:12:32 +0800132 'dist.atomic_increment(key);')
Brendan Gregg860b6492015-10-20 15:52:23 -0700133else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800134 bpf_text = bpf_text.replace('STORE',
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300135 'key.vec = valp->vec; ' +
zcy80242fb2021-07-02 00:12:32 +0800136 'dist.atomic_increment(key, delta);')
xingfeng2510c5baa7b2022-03-07 17:35:49 +0800137if args.cpu is not None:
138 bpf_text = bpf_text.replace('FILTER_CPU',
139 'if (cpu != %d) { return 0; }' % int(args.cpu))
140else:
141 bpf_text = bpf_text.replace('FILTER_CPU', '')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100142if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800143 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100144 if args.ebpf:
145 exit()
Brendan Gregg860b6492015-10-20 15:52:23 -0700146
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300147# load BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -0700148b = BPF(text=bpf_text)
149
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300150def vec_to_name(vec):
151 # copied from softirq_to_name() in kernel/softirq.c
152 # may need updates if new softirq handlers are added
153 return ["hi", "timer", "net_tx", "net_rx", "block", "irq_poll",
154 "tasklet", "sched", "hrtimer", "rcu"][vec]
Brendan Gregg860b6492015-10-20 15:52:23 -0700155
156print("Tracing soft irq event time... Hit Ctrl-C to end.")
157
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300158# output
Brendan Gregg860b6492015-10-20 15:52:23 -0700159exiting = 0 if args.interval else 1
160dist = b.get_table("dist")
161while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800162 try:
163 sleep(int(args.interval))
164 except KeyboardInterrupt:
165 exiting = 1
Brendan Gregg860b6492015-10-20 15:52:23 -0700166
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800167 print()
168 if args.timestamp:
169 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg860b6492015-10-20 15:52:23 -0700170
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800171 if args.dist:
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300172 dist.print_log2_hist(label, "softirq", section_print_fn=vec_to_name)
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800173 else:
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300174 print("%-16s %11s" % ("SOFTIRQ", "TOTAL_" + label))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800175 for k, v in sorted(dist.items(), key=lambda dist: dist[1].value):
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300176 print("%-16s %11d" % (vec_to_name(k.vec), v.value / factor))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800177 dist.clear()
Brendan Gregg860b6492015-10-20 15:52:23 -0700178
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800179 countdown -= 1
180 if exiting or countdown == 0:
181 exit()