xref: /linux/tools/testing/selftests/bpf/progs/trigger_bench.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Facebook
3 #include <linux/bpf.h>
4 #include <asm/unistd.h>
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 #include "bpf_misc.h"
8 
9 char _license[] SEC("license") = "GPL";
10 
11 #define CPU_MASK 255
12 #define MAX_CPUS (CPU_MASK + 1) /* should match MAX_BUCKETS in benchs/bench_trigger.c */
13 
14 /* matches struct counter in bench.h */
15 struct counter {
16 	long value;
17 } __attribute__((aligned(128)));
18 
19 struct counter hits[MAX_CPUS];
20 
inc_counter(void)21 static __always_inline void inc_counter(void)
22 {
23 	int cpu = bpf_get_smp_processor_id();
24 
25 	__sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1);
26 }
27 
28 SEC("?uprobe")
bench_trigger_uprobe(void * ctx)29 int bench_trigger_uprobe(void *ctx)
30 {
31 	inc_counter();
32 	return 0;
33 }
34 
35 SEC("?uprobe.multi")
bench_trigger_uprobe_multi(void * ctx)36 int bench_trigger_uprobe_multi(void *ctx)
37 {
38 	inc_counter();
39 	return 0;
40 }
41 
42 const volatile int batch_iters = 0;
43 
44 SEC("?raw_tp")
trigger_count(void * ctx)45 int trigger_count(void *ctx)
46 {
47 	int i;
48 
49 	for (i = 0; i < batch_iters; i++)
50 		inc_counter();
51 
52 	return 0;
53 }
54 
55 SEC("?raw_tp")
trigger_driver(void * ctx)56 int trigger_driver(void *ctx)
57 {
58 	int i;
59 
60 	for (i = 0; i < batch_iters; i++)
61 		(void)bpf_get_numa_node_id(); /* attach point for benchmarking */
62 
63 	return 0;
64 }
65 
66 extern int bpf_modify_return_test_tp(int nonce) __ksym __weak;
67 
68 SEC("?raw_tp")
trigger_driver_kfunc(void * ctx)69 int trigger_driver_kfunc(void *ctx)
70 {
71 	int i;
72 
73 	for (i = 0; i < batch_iters; i++)
74 		(void)bpf_modify_return_test_tp(0); /* attach point for benchmarking */
75 
76 	return 0;
77 }
78 
79 SEC("?kprobe/bpf_get_numa_node_id")
bench_trigger_kprobe(void * ctx)80 int bench_trigger_kprobe(void *ctx)
81 {
82 	inc_counter();
83 	return 0;
84 }
85 
86 SEC("?kretprobe/bpf_get_numa_node_id")
bench_trigger_kretprobe(void * ctx)87 int bench_trigger_kretprobe(void *ctx)
88 {
89 	inc_counter();
90 	return 0;
91 }
92 
93 SEC("?kprobe.multi/bpf_get_numa_node_id")
bench_trigger_kprobe_multi(void * ctx)94 int bench_trigger_kprobe_multi(void *ctx)
95 {
96 	inc_counter();
97 	return 0;
98 }
99 
100 SEC("?kretprobe.multi/bpf_get_numa_node_id")
bench_trigger_kretprobe_multi(void * ctx)101 int bench_trigger_kretprobe_multi(void *ctx)
102 {
103 	inc_counter();
104 	return 0;
105 }
106 
107 SEC("?fentry/bpf_get_numa_node_id")
bench_trigger_fentry(void * ctx)108 int bench_trigger_fentry(void *ctx)
109 {
110 	inc_counter();
111 	return 0;
112 }
113 
114 SEC("?fexit/bpf_get_numa_node_id")
bench_trigger_fexit(void * ctx)115 int bench_trigger_fexit(void *ctx)
116 {
117 	inc_counter();
118 	return 0;
119 }
120 
121 SEC("?fmod_ret/bpf_modify_return_test_tp")
bench_trigger_fmodret(void * ctx)122 int bench_trigger_fmodret(void *ctx)
123 {
124 	inc_counter();
125 	return -22;
126 }
127 
128 SEC("?tp/bpf_test_run/bpf_trigger_tp")
bench_trigger_tp(void * ctx)129 int bench_trigger_tp(void *ctx)
130 {
131 	inc_counter();
132 	return 0;
133 }
134 
135 SEC("?raw_tp/bpf_trigger_tp")
bench_trigger_rawtp(void * ctx)136 int bench_trigger_rawtp(void *ctx)
137 {
138 	inc_counter();
139 	return 0;
140 }
141