blob: ba0dac363c83ee1e362fe4569591a17d8862b405 [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#
7# USAGE: softirqs [-h] [-T] [-N] [-d] [interval] [count]
8#
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.
Brendan Gregg860b6492015-10-20 15:52:23 -070014
15from __future__ import print_function
16from bcc import BPF
17from time import sleep, strftime
18import argparse
19
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030020# arguments
Brendan Gregg860b6492015-10-20 15:52:23 -070021examples = """examples:
22 ./softirqs # sum soft irq event time
23 ./softirqs -d # show soft irq event time as histograms
24 ./softirqs 1 10 # print 1 second summaries, 10 times
25 ./softirqs -NT 1 # 1s summaries, nanoseconds, and timestamps
26"""
27parser = argparse.ArgumentParser(
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030028 description="Summarize soft irq event time as histograms.",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080029 formatter_class=argparse.RawDescriptionHelpFormatter,
30 epilog=examples)
Brendan Gregg860b6492015-10-20 15:52:23 -070031parser.add_argument("-T", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080032 help="include timestamp on output")
Brendan Gregg860b6492015-10-20 15:52:23 -070033parser.add_argument("-N", "--nanoseconds", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080034 help="output in nanoseconds")
Brendan Gregg860b6492015-10-20 15:52:23 -070035parser.add_argument("-d", "--dist", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080036 help="show distributions as histograms")
Brendan Gregg860b6492015-10-20 15:52:23 -070037parser.add_argument("interval", nargs="?", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080038 help="output interval, in seconds")
Brendan Gregg860b6492015-10-20 15:52:23 -070039parser.add_argument("count", nargs="?", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080040 help="number of outputs")
Nathan Scottcf0792f2018-02-02 16:56:50 +110041parser.add_argument("--ebpf", action="store_true",
42 help=argparse.SUPPRESS)
Brendan Gregg860b6492015-10-20 15:52:23 -070043args = parser.parse_args()
44countdown = int(args.count)
45if args.nanoseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080046 factor = 1
47 label = "nsecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070048else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080049 factor = 1000
50 label = "usecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070051debug = 0
52
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030053# define BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -070054bpf_text = """
55#include <uapi/linux/ptrace.h>
56
edwardwu9465f8c2020-03-09 14:09:52 +080057typedef struct entry_key {
58 u32 pid;
59 u32 cpu;
60} entry_key_t;
61
Brendan Gregg860b6492015-10-20 15:52:23 -070062typedef struct irq_key {
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030063 u32 vec;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080064 u64 slot;
Brendan Gregg860b6492015-10-20 15:52:23 -070065} irq_key_t;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030066
67typedef struct account_val {
68 u64 ts;
69 u32 vec;
70} account_val_t;
71
edwardwu9465f8c2020-03-09 14:09:52 +080072BPF_HASH(start, entry_key_t, account_val_t);
Brendan Gregg860b6492015-10-20 15:52:23 -070073BPF_HASH(iptr, u32);
74BPF_HISTOGRAM(dist, irq_key_t);
75
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030076TRACEPOINT_PROBE(irq, softirq_entry)
Brendan Gregg860b6492015-10-20 15:52:23 -070077{
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030078 account_val_t val = {};
edwardwu9465f8c2020-03-09 14:09:52 +080079 entry_key_t key = {};
80
81 key.pid = bpf_get_current_pid_tgid();
82 key.cpu = bpf_get_smp_processor_id();
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030083 val.ts = bpf_ktime_get_ns();
84 val.vec = args->vec;
edwardwu9465f8c2020-03-09 14:09:52 +080085
86 start.update(&key, &val);
87
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080088 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -070089}
90
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030091TRACEPOINT_PROBE(irq, softirq_exit)
Brendan Gregg860b6492015-10-20 15:52:23 -070092{
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030093 u64 delta;
94 u32 vec;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030095 account_val_t *valp;
96 irq_key_t key = {0};
edwardwu9465f8c2020-03-09 14:09:52 +080097 entry_key_t entry_key = {};
98
99 entry_key.pid = bpf_get_current_pid_tgid();
100 entry_key.cpu = bpf_get_smp_processor_id();
Brendan Gregg860b6492015-10-20 15:52:23 -0700101
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800102 // fetch timestamp and calculate delta
edwardwu9465f8c2020-03-09 14:09:52 +0800103 valp = start.lookup(&entry_key);
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300104 if (valp == 0) {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800105 return 0; // missed start
106 }
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300107 delta = bpf_ktime_get_ns() - valp->ts;
108 vec = valp->vec;
Brendan Gregg860b6492015-10-20 15:52:23 -0700109
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800110 // store as sum or histogram
111 STORE
Brendan Gregg860b6492015-10-20 15:52:23 -0700112
edwardwu9465f8c2020-03-09 14:09:52 +0800113 start.delete(&entry_key);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800114 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700115}
116"""
117
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300118# code substitutions
Brendan Gregg860b6492015-10-20 15:52:23 -0700119if args.dist:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800120 bpf_text = bpf_text.replace('STORE',
Kirill Smelkovf2d125e2017-09-25 11:23:03 +0300121 'key.vec = vec; key.slot = bpf_log2l(delta / %d); ' % factor +
zcy80242fb2021-07-02 00:12:32 +0800122 'dist.atomic_increment(key);')
Brendan Gregg860b6492015-10-20 15:52:23 -0700123else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800124 bpf_text = bpf_text.replace('STORE',
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300125 'key.vec = valp->vec; ' +
zcy80242fb2021-07-02 00:12:32 +0800126 'dist.atomic_increment(key, delta);')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100127if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800128 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100129 if args.ebpf:
130 exit()
Brendan Gregg860b6492015-10-20 15:52:23 -0700131
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300132# load BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -0700133b = BPF(text=bpf_text)
134
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300135def vec_to_name(vec):
136 # copied from softirq_to_name() in kernel/softirq.c
137 # may need updates if new softirq handlers are added
138 return ["hi", "timer", "net_tx", "net_rx", "block", "irq_poll",
139 "tasklet", "sched", "hrtimer", "rcu"][vec]
Brendan Gregg860b6492015-10-20 15:52:23 -0700140
141print("Tracing soft irq event time... Hit Ctrl-C to end.")
142
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300143# output
Brendan Gregg860b6492015-10-20 15:52:23 -0700144exiting = 0 if args.interval else 1
145dist = b.get_table("dist")
146while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800147 try:
148 sleep(int(args.interval))
149 except KeyboardInterrupt:
150 exiting = 1
Brendan Gregg860b6492015-10-20 15:52:23 -0700151
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800152 print()
153 if args.timestamp:
154 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg860b6492015-10-20 15:52:23 -0700155
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800156 if args.dist:
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300157 dist.print_log2_hist(label, "softirq", section_print_fn=vec_to_name)
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800158 else:
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300159 print("%-16s %11s" % ("SOFTIRQ", "TOTAL_" + label))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800160 for k, v in sorted(dist.items(), key=lambda dist: dist[1].value):
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300161 print("%-16s %11d" % (vec_to_name(k.vec), v.value / factor))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800162 dist.clear()
Brendan Gregg860b6492015-10-20 15:52:23 -0700163
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800164 countdown -= 1
165 if exiting or countdown == 0:
166 exit()