blob: 71597315dda693d6b8c36826986f4b5b4fc2c05e [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# hardirqs Summarize hard IRQ (interrupt) event time.
5# For Linux, uses BCC, eBPF.
Brendan Gregg860b6492015-10-20 15:52:23 -07006#
7# USAGE: hardirqs [-h] [-T] [-Q] [-m] [-D] [interval] [count]
8#
9# Thanks Amer Ather for help understanding irq behavior.
10#
11# Copyright (c) 2015 Brendan Gregg.
12# Licensed under the Apache License, Version 2.0 (the "License")
13#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080014# 19-Oct-2015 Brendan Gregg Created this.
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 ./hardirqs # sum hard irq event time
24 ./hardirqs -d # show hard irq event time as histograms
25 ./hardirqs 1 10 # print 1 second summaries, 10 times
26 ./hardirqs -NT 1 # 1s summaries, nanoseconds, and timestamps
27"""
28parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080029 description="Summarize hard irq event time as histograms",
30 formatter_class=argparse.RawDescriptionHelpFormatter,
31 epilog=examples)
Brendan Gregg860b6492015-10-20 15:52:23 -070032parser.add_argument("-T", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080033 help="include timestamp on output")
Brendan Gregg860b6492015-10-20 15:52:23 -070034parser.add_argument("-N", "--nanoseconds", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080035 help="output in nanoseconds")
Brendan Gregg860b6492015-10-20 15:52:23 -070036parser.add_argument("-d", "--dist", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080037 help="show distributions as histograms")
Brendan Gregg860b6492015-10-20 15:52:23 -070038parser.add_argument("interval", nargs="?", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080039 help="output interval, in seconds")
Brendan Gregg860b6492015-10-20 15:52:23 -070040parser.add_argument("count", nargs="?", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080041 help="number of outputs")
Brendan Gregg860b6492015-10-20 15:52:23 -070042args = parser.parse_args()
43countdown = int(args.count)
44if args.nanoseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080045 factor = 1
46 label = "nsecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070047else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080048 factor = 1000
49 label = "usecs"
Brendan Gregg860b6492015-10-20 15:52:23 -070050debug = 0
51
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030052# define BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -070053bpf_text = """
54#include <uapi/linux/ptrace.h>
55#include <linux/irq.h>
56#include <linux/irqdesc.h>
57#include <linux/interrupt.h>
58
59typedef struct irq_key {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080060 char name[32];
61 u64 slot;
Brendan Gregg860b6492015-10-20 15:52:23 -070062} irq_key_t;
63BPF_HASH(start, u32);
64BPF_HASH(irqdesc, u32, struct irq_desc *);
65BPF_HISTOGRAM(dist, irq_key_t);
66
67// time IRQ
68int trace_start(struct pt_regs *ctx, struct irq_desc *desc)
69{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080070 u32 pid = bpf_get_current_pid_tgid();
71 u64 ts = bpf_ktime_get_ns();
72 start.update(&pid, &ts);
73 irqdesc.update(&pid, &desc);
74 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -070075}
76
77int trace_completion(struct pt_regs *ctx)
78{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080079 u64 *tsp, delta;
80 struct irq_desc **descp;
81 u32 pid = bpf_get_current_pid_tgid();
Brendan Gregg860b6492015-10-20 15:52:23 -070082
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080083 // fetch timestamp and calculate delta
84 tsp = start.lookup(&pid);
85 descp = irqdesc.lookup(&pid);
86 if (tsp == 0 || descp == 0) {
87 return 0; // missed start
88 }
89 // Note: descp is a value from map, so '&' can be done without
90 // probe_read, but the next level irqaction * needs a probe read.
91 // Do these steps first after reading the map, otherwise some of these
92 // pointers may get pushed onto the stack and verifier will fail.
93 struct irqaction *action = 0;
94 bpf_probe_read(&action, sizeof(action), &(*descp)->action);
95 const char **namep = &action->name;
96 char *name = 0;
97 bpf_probe_read(&name, sizeof(name), namep);
98 delta = bpf_ktime_get_ns() - *tsp;
Brendan Gregg860b6492015-10-20 15:52:23 -070099
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800100 // store as sum or histogram
101 STORE
Brendan Gregg860b6492015-10-20 15:52:23 -0700102
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800103 start.delete(&pid);
104 irqdesc.delete(&pid);
105 return 0;
Brendan Gregg860b6492015-10-20 15:52:23 -0700106}
107"""
108
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300109# code substitutions
Brendan Gregg860b6492015-10-20 15:52:23 -0700110if args.dist:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800111 bpf_text = bpf_text.replace('STORE',
112 'irq_key_t key = {.slot = bpf_log2l(delta)};' +
113 'bpf_probe_read(&key.name, sizeof(key.name), name);' +
114 'dist.increment(key);')
Brendan Gregg860b6492015-10-20 15:52:23 -0700115else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800116 bpf_text = bpf_text.replace('STORE',
117 'irq_key_t key = {.slot = 0 /* ignore */};' +
118 'bpf_probe_read(&key.name, sizeof(key.name), name);' +
119 'u64 zero = 0, *vp = dist.lookup_or_init(&key, &zero);' +
120 '(*vp) += delta;')
Brendan Gregg860b6492015-10-20 15:52:23 -0700121if debug:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800122 print(bpf_text)
Brendan Gregg860b6492015-10-20 15:52:23 -0700123
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300124# load BPF program
Brendan Gregg860b6492015-10-20 15:52:23 -0700125b = BPF(text=bpf_text)
126
127# these should really use irq:irq_handler_entry/exit tracepoints:
128b.attach_kprobe(event="handle_irq_event_percpu", fn_name="trace_start")
129b.attach_kretprobe(event="handle_irq_event_percpu", fn_name="trace_completion")
130
131print("Tracing hard irq event time... Hit Ctrl-C to end.")
132
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300133# output
Brendan Gregg860b6492015-10-20 15:52:23 -0700134exiting = 0 if args.interval else 1
135dist = b.get_table("dist")
136while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800137 try:
138 sleep(int(args.interval))
139 except KeyboardInterrupt:
140 exiting = 1
Brendan Gregg860b6492015-10-20 15:52:23 -0700141
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800142 print()
143 if args.timestamp:
144 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg860b6492015-10-20 15:52:23 -0700145
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800146 if args.dist:
147 dist.print_log2_hist(label, "hardirq")
148 else:
149 print("%-26s %11s" % ("HARDIRQ", "TOTAL_" + label))
150 for k, v in sorted(dist.items(), key=lambda dist: dist[1].value):
Rafael F78948e42017-03-26 14:54:25 +0200151 print("%-26s %11d" % (k.name.decode(), v.value / factor))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800152 dist.clear()
Brendan Gregg860b6492015-10-20 15:52:23 -0700153
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800154 countdown -= 1
155 if exiting or countdown == 0:
156 exit()