xref: /aosp_15_r20/external/bcc/libbpf-tools/softirqs.bpf.c (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
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 "softirqs.h"
7 #include "bits.bpf.h"
8 #include "maps.bpf.h"
9 
10 const volatile bool targ_dist = false;
11 const volatile bool targ_ns = false;
12 
13 struct {
14 	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
15 	__uint(max_entries, 1);
16 	__type(key, u32);
17 	__type(value, u64);
18 } start SEC(".maps");
19 
20 __u64 counts[NR_SOFTIRQS] = {};
21 __u64 time[NR_SOFTIRQS] = {};
22 struct hist hists[NR_SOFTIRQS] = {};
23 
handle_entry(unsigned int vec_nr)24 static int handle_entry(unsigned int vec_nr)
25 {
26 	u64 ts = bpf_ktime_get_ns();
27 	u32 key = 0;
28 
29 	bpf_map_update_elem(&start, &key, &ts, BPF_ANY);
30 	return 0;
31 }
32 
handle_exit(unsigned int vec_nr)33 static int handle_exit(unsigned int vec_nr)
34 {
35 	u64 delta, *tsp;
36 	u32 key = 0;
37 
38 	if (vec_nr >= NR_SOFTIRQS)
39 		return 0;
40 	tsp = bpf_map_lookup_elem(&start, &key);
41 	if (!tsp)
42 		return 0;
43 	delta = bpf_ktime_get_ns() - *tsp;
44 	if (!targ_ns)
45 		delta /= 1000U;
46 
47 	if (!targ_dist) {
48 		__sync_fetch_and_add(&counts[vec_nr], 1);
49 		__sync_fetch_and_add(&time[vec_nr], delta);
50 	} else {
51 		struct hist *hist;
52 		u64 slot;
53 
54 		hist = &hists[vec_nr];
55 		slot = log2(delta);
56 		if (slot >= MAX_SLOTS)
57 			slot = MAX_SLOTS - 1;
58 		__sync_fetch_and_add(&hist->slots[slot], 1);
59 	}
60 
61 	return 0;
62 }
63 
64 SEC("tp_btf/softirq_entry")
BPF_PROG(softirq_entry_btf,unsigned int vec_nr)65 int BPF_PROG(softirq_entry_btf, unsigned int vec_nr)
66 {
67 	return handle_entry(vec_nr);
68 }
69 
70 SEC("tp_btf/softirq_exit")
BPF_PROG(softirq_exit_btf,unsigned int vec_nr)71 int BPF_PROG(softirq_exit_btf, unsigned int vec_nr)
72 {
73 	return handle_exit(vec_nr);
74 }
75 
76 SEC("raw_tp/softirq_entry")
BPF_PROG(softirq_entry,unsigned int vec_nr)77 int BPF_PROG(softirq_entry, unsigned int vec_nr)
78 {
79 	return handle_entry(vec_nr);
80 }
81 
82 SEC("raw_tp/softirq_exit")
BPF_PROG(softirq_exit,unsigned int vec_nr)83 int BPF_PROG(softirq_exit, unsigned int vec_nr)
84 {
85 	return handle_exit(vec_nr);
86 }
87 
88 char LICENSE[] SEC("license") = "GPL";
89