blob: 1e2daf5f97def7c552ac286a8340847b7a4a570f [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")
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
57typedef struct irq_key {
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030058 u32 vec;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080059 u64 slot;
Brendan Gregg860b6492015-10-20 15:52:23 -070060} irq_key_t;
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030061
62typedef struct account_val {
63 u64 ts;
64 u32 vec;
65} account_val_t;
66
67BPF_HASH(start, u32, account_val_t);
Brendan Gregg860b6492015-10-20 15:52:23 -070068BPF_HASH(iptr, u32);
69BPF_HISTOGRAM(dist, irq_key_t);
70
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030071TRACEPOINT_PROBE(irq, softirq_entry)
Brendan Gregg860b6492015-10-20 15:52:23 -070072{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080073 u32 pid = bpf_get_current_pid_tgid();
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030074 account_val_t val = {};
75 val.ts = bpf_ktime_get_ns();
76 val.vec = args->vec;
77 start.update(&pid, &val);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080078 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -070079}
80
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030081TRACEPOINT_PROBE(irq, softirq_exit)
Brendan Gregg860b6492015-10-20 15:52:23 -070082{
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030083 u64 delta;
84 u32 vec;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080085 u32 pid = bpf_get_current_pid_tgid();
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030086 account_val_t *valp;
87 irq_key_t key = {0};
Brendan Gregg860b6492015-10-20 15:52:23 -070088
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080089 // fetch timestamp and calculate delta
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030090 valp = start.lookup(&pid);
91 if (valp == 0) {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080092 return 0; // missed start
93 }
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +030094 delta = bpf_ktime_get_ns() - valp->ts;
95 vec = valp->vec;
Brendan Gregg860b6492015-10-20 15:52:23 -070096
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080097 // store as sum or histogram
98 STORE
Brendan Gregg860b6492015-10-20 15:52:23 -070099
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800100 start.delete(&pid);
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800101 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700102}
103"""
104
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300105# code substitutions
Brendan Gregg860b6492015-10-20 15:52:23 -0700106if args.dist:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800107 bpf_text = bpf_text.replace('STORE',
Kirill Smelkovf2d125e2017-09-25 11:23:03 +0300108 'key.vec = vec; key.slot = bpf_log2l(delta / %d); ' % factor +
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800109 'dist.increment(key);')
Brendan Gregg860b6492015-10-20 15:52:23 -0700110else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800111 bpf_text = bpf_text.replace('STORE',
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300112 'key.vec = valp->vec; ' +
Javier Honduvilla Coto64bf9652018-08-01 06:50:19 +0200113 'dist.increment(key, delta);')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100114if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800115 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100116 if args.ebpf:
117 exit()
Brendan Gregg860b6492015-10-20 15:52:23 -0700118
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300119# load BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -0700120b = BPF(text=bpf_text)
121
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300122def vec_to_name(vec):
123 # copied from softirq_to_name() in kernel/softirq.c
124 # may need updates if new softirq handlers are added
125 return ["hi", "timer", "net_tx", "net_rx", "block", "irq_poll",
126 "tasklet", "sched", "hrtimer", "rcu"][vec]
Brendan Gregg860b6492015-10-20 15:52:23 -0700127
128print("Tracing soft irq event time... Hit Ctrl-C to end.")
129
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300130# output
Brendan Gregg860b6492015-10-20 15:52:23 -0700131exiting = 0 if args.interval else 1
132dist = b.get_table("dist")
133while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800134 try:
135 sleep(int(args.interval))
136 except KeyboardInterrupt:
137 exiting = 1
Brendan Gregg860b6492015-10-20 15:52:23 -0700138
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800139 print()
140 if args.timestamp:
141 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg860b6492015-10-20 15:52:23 -0700142
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800143 if args.dist:
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300144 dist.print_log2_hist(label, "softirq", section_print_fn=vec_to_name)
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800145 else:
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300146 print("%-16s %11s" % ("SOFTIRQ", "TOTAL_" + label))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800147 for k, v in sorted(dist.items(), key=lambda dist: dist[1].value):
Sasha Goldshtein69ceaca2017-04-08 12:35:09 +0300148 print("%-16s %11d" % (vec_to_name(k.vec), v.value / factor))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800149 dist.clear()
Brendan Gregg860b6492015-10-20 15:52:23 -0700150
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800151 countdown -= 1
152 if exiting or countdown == 0:
153 exit()