1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Wenbo Zhang
3 #include <vmlinux.h>
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6 #include "hardirqs.h"
7 #include "bits.bpf.h"
8 #include "maps.bpf.h"
9
10 #define MAX_ENTRIES 256
11
12 const volatile bool targ_dist = false;
13 const volatile bool targ_ns = false;
14
15 struct {
16 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
17 __uint(max_entries, 1);
18 __type(key, u32);
19 __type(value, u64);
20 } start SEC(".maps");
21
22 struct {
23 __uint(type, BPF_MAP_TYPE_HASH);
24 __uint(max_entries, MAX_ENTRIES);
25 __type(key, struct irq_key);
26 __type(value, struct info);
27 } infos SEC(".maps");
28
29 static struct info zero;
30
31 SEC("tracepoint/irq/irq_handler_entry")
handle__irq_handler(struct trace_event_raw_irq_handler_entry * ctx)32 int handle__irq_handler(struct trace_event_raw_irq_handler_entry *ctx)
33 {
34 struct irq_key key = {};
35 struct info *info;
36
37 bpf_probe_read_kernel_str(&key.name, sizeof(key.name), ctx->__data);
38 info = bpf_map_lookup_or_try_init(&infos, &key, &zero);
39 if (!info)
40 return 0;
41 info->count += 1;
42 return 0;
43 }
44
45 SEC("tp_btf/irq_handler_entry")
BPF_PROG(irq_handler_entry)46 int BPF_PROG(irq_handler_entry)
47 {
48 u64 ts = bpf_ktime_get_ns();
49 u32 key = 0;
50
51 bpf_map_update_elem(&start, &key, &ts, 0);
52 return 0;
53 }
54
55 SEC("tp_btf/irq_handler_exit")
BPF_PROG(irq_handler_exit_exit,int irq,struct irqaction * action)56 int BPF_PROG(irq_handler_exit_exit, int irq, struct irqaction *action)
57 {
58 struct irq_key ikey = {};
59 struct info *info;
60 u32 key = 0;
61 s64 delta;
62 u64 *tsp;
63
64 tsp = bpf_map_lookup_elem(&start, &key);
65 if (!tsp || !*tsp)
66 return 0;
67
68 delta = bpf_ktime_get_ns() - *tsp;
69 if (delta < 0)
70 return 0;
71 if (!targ_ns)
72 delta /= 1000U;
73
74 bpf_probe_read_kernel_str(&ikey.name, sizeof(ikey.name), action->name);
75 info = bpf_map_lookup_or_try_init(&infos, &ikey, &zero);
76 if (!info)
77 return 0;
78
79 if (!targ_dist) {
80 info->count += delta;
81 } else {
82 u64 slot;
83
84 slot = log2(delta);
85 if (slot >= MAX_SLOTS)
86 slot = MAX_SLOTS - 1;
87 info->slots[slot]++;
88 }
89
90 return 0;
91 }
92
93 char LICENSE[] SEC("license") = "GPL";
94