blob: 21862772a43384a2a98765faa676e3e46f92d1ad [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
xingfeng25107b2c1142022-03-24 10:51:26 +080020import sys
Brendan Gregg860b6492015-10-20 15:52:23 -070021
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030022# arguments
Brendan Gregg860b6492015-10-20 15:52:23 -070023examples = """examples:
24 ./softirqs # sum soft irq event time
25 ./softirqs -d # show soft irq event time as histograms
26 ./softirqs 1 10 # print 1 second summaries, 10 times
27 ./softirqs -NT 1 # 1s summaries, nanoseconds, and timestamps
xingfeng2510c5baa7b2022-03-07 17:35:49 +080028 ./softirqs -c 1 # sum soft irq event time on CPU 1 only
Brendan Gregg860b6492015-10-20 15:52:23 -070029"""
30parser = argparse.ArgumentParser(
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030031 description="Summarize soft irq event time as histograms.",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080032 formatter_class=argparse.RawDescriptionHelpFormatter,
33 epilog=examples)
Brendan Gregg860b6492015-10-20 15:52:23 -070034parser.add_argument("-T", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080035 help="include timestamp on output")
Brendan Gregg860b6492015-10-20 15:52:23 -070036parser.add_argument("-N", "--nanoseconds", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080037 help="output in nanoseconds")
Brendan Gregg860b6492015-10-20 15:52:23 -070038parser.add_argument("-d", "--dist", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080039 help="show distributions as histograms")
xingfeng2510c5baa7b2022-03-07 17:35:49 +080040parser.add_argument("-c", "--cpu", type=int,
41 help="trace this CPU only")
Brendan Gregg860b6492015-10-20 15:52:23 -070042parser.add_argument("interval", nargs="?", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080043 help="output interval, in seconds")
Brendan Gregg860b6492015-10-20 15:52:23 -070044parser.add_argument("count", nargs="?", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080045 help="number of outputs")
Nathan Scottcf0792f2018-02-02 16:56:50 +110046parser.add_argument("--ebpf", action="store_true",
47 help=argparse.SUPPRESS)
Brendan Gregg860b6492015-10-20 15:52:23 -070048args = parser.parse_args()
49countdown = int(args.count)
50if args.nanoseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080051 factor = 1
52 label = "nsecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070053else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080054 factor = 1000
55 label = "usecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070056debug = 0
57
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030058# define BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -070059bpf_text = """
60#include <uapi/linux/ptrace.h>
61
edwardwu9465f8c2020-03-09 14:09:52 +080062typedef struct entry_key {
63 u32 pid;
64 u32 cpu;
65} entry_key_t;
66
Brendan Gregg860b6492015-10-20 15:52:23 -070067typedef struct irq_key {
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030068 u32 vec;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080069 u64 slot;
Brendan Gregg860b6492015-10-20 15:52:23 -070070} irq_key_t;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030071
72typedef struct account_val {
73 u64 ts;
74 u32 vec;
75} account_val_t;
76
edwardwu9465f8c2020-03-09 14:09:52 +080077BPF_HASH(start, entry_key_t, account_val_t);
Brendan Gregg860b6492015-10-20 15:52:23 -070078BPF_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
xingfeng25107b2c1142022-03-24 10:51:26 +0800179 sys.stdout.flush()
180
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800181 countdown -= 1
182 if exiting or countdown == 0:
183 exit()