blob: 2cd924810d00884bdbe51f317443a25f5ea6ceca [file] [log] [blame]
Brendan Gregg860b6492015-10-20 15:52:23 -07001#!/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")
Brendan Gregg860b6492015-10-20 15:52:23 -070041args = parser.parse_args()
42countdown = int(args.count)
43if args.nanoseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080044 factor = 1
45 label = "nsecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070046else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080047 factor = 1000
48 label = "usecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070049debug = 0
50
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030051# define BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -070052bpf_text = """
53#include <uapi/linux/ptrace.h>
54
55typedef struct irq_key {
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030056 u32 vec;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080057 u64 slot;
Brendan Gregg860b6492015-10-20 15:52:23 -070058} irq_key_t;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030059
60typedef struct account_val {
61 u64 ts;
62 u32 vec;
63} account_val_t;
64
65BPF_HASH(start, u32, account_val_t);
Brendan Gregg860b6492015-10-20 15:52:23 -070066BPF_HASH(iptr, u32);
67BPF_HISTOGRAM(dist, irq_key_t);
68
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030069TRACEPOINT_PROBE(irq, softirq_entry)
Brendan Gregg860b6492015-10-20 15:52:23 -070070{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080071 u32 pid = bpf_get_current_pid_tgid();
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030072 account_val_t val = {};
73 val.ts = bpf_ktime_get_ns();
74 val.vec = args->vec;
75 start.update(&pid, &val);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080076 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -070077}
78
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030079TRACEPOINT_PROBE(irq, softirq_exit)
Brendan Gregg860b6492015-10-20 15:52:23 -070080{
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030081 u64 delta;
82 u32 vec;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080083 u32 pid = bpf_get_current_pid_tgid();
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030084 account_val_t *valp;
85 irq_key_t key = {0};
Brendan Gregg860b6492015-10-20 15:52:23 -070086
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080087 // fetch timestamp and calculate delta
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030088 valp = start.lookup(&pid);
89 if (valp == 0) {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080090 return 0; // missed start
91 }
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030092 delta = bpf_ktime_get_ns() - valp->ts;
93 vec = valp->vec;
Brendan Gregg860b6492015-10-20 15:52:23 -070094
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080095 // store as sum or histogram
96 STORE
Brendan Gregg860b6492015-10-20 15:52:23 -070097
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080098 start.delete(&pid);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080099 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700100}
101"""
102
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300103# code substitutions
Brendan Gregg860b6492015-10-20 15:52:23 -0700104if args.dist:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800105 bpf_text = bpf_text.replace('STORE',
Kirill Smelkovf2d125e2017-09-25 11:23:03 +0300106 'key.vec = vec; key.slot = bpf_log2l(delta / %d); ' % factor +
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800107 'dist.increment(key);')
Brendan Gregg860b6492015-10-20 15:52:23 -0700108else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800109 bpf_text = bpf_text.replace('STORE',
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300110 'key.vec = valp->vec; ' +
111 'u64 zero = 0, *vp = dist.lookup_or_init(&key, &zero); ' +
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800112 '(*vp) += delta;')
Brendan Gregg860b6492015-10-20 15:52:23 -0700113if debug:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800114 print(bpf_text)
Brendan Gregg860b6492015-10-20 15:52:23 -0700115
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300116# load BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -0700117b = BPF(text=bpf_text)
118
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300119def vec_to_name(vec):
120 # copied from softirq_to_name() in kernel/softirq.c
121 # may need updates if new softirq handlers are added
122 return ["hi", "timer", "net_tx", "net_rx", "block", "irq_poll",
123 "tasklet", "sched", "hrtimer", "rcu"][vec]
Brendan Gregg860b6492015-10-20 15:52:23 -0700124
125print("Tracing soft irq event time... Hit Ctrl-C to end.")
126
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300127# output
Brendan Gregg860b6492015-10-20 15:52:23 -0700128exiting = 0 if args.interval else 1
129dist = b.get_table("dist")
130while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800131 try:
132 sleep(int(args.interval))
133 except KeyboardInterrupt:
134 exiting = 1
Brendan Gregg860b6492015-10-20 15:52:23 -0700135
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800136 print()
137 if args.timestamp:
138 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg860b6492015-10-20 15:52:23 -0700139
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800140 if args.dist:
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300141 dist.print_log2_hist(label, "softirq", section_print_fn=vec_to_name)
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800142 else:
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300143 print("%-16s %11s" % ("SOFTIRQ", "TOTAL_" + label))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800144 for k, v in sorted(dist.items(), key=lambda dist: dist[1].value):
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300145 print("%-16s %11d" % (vec_to_name(k.vec), v.value / factor))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800146 dist.clear()
Brendan Gregg860b6492015-10-20 15:52:23 -0700147
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800148 countdown -= 1
149 if exiting or countdown == 0:
150 exit()