1*f371f2dcSAnton Protopopov // SPDX-License-Identifier: GPL-2.0
2*f371f2dcSAnton Protopopov /* Copyright (c) 2023 Isovalent */
3*f371f2dcSAnton Protopopov 
4*f371f2dcSAnton Protopopov #include "vmlinux.h"
5*f371f2dcSAnton Protopopov 
6*f371f2dcSAnton Protopopov #include <bpf/bpf_helpers.h>
7*f371f2dcSAnton Protopopov #include "bpf_misc.h"
8*f371f2dcSAnton Protopopov 
9*f371f2dcSAnton Protopopov char _license[] SEC("license") = "GPL";
10*f371f2dcSAnton Protopopov 
11*f371f2dcSAnton Protopopov struct {
12*f371f2dcSAnton Protopopov 	__uint(type, BPF_MAP_TYPE_HASH);
13*f371f2dcSAnton Protopopov } hash_map_bench SEC(".maps");
14*f371f2dcSAnton Protopopov 
15*f371f2dcSAnton Protopopov /* The number of slots to store times */
16*f371f2dcSAnton Protopopov #define NR_SLOTS 32
17*f371f2dcSAnton Protopopov #define NR_CPUS 256
18*f371f2dcSAnton Protopopov #define CPU_MASK (NR_CPUS-1)
19*f371f2dcSAnton Protopopov 
20*f371f2dcSAnton Protopopov /* Configured by userspace */
21*f371f2dcSAnton Protopopov u64 nr_entries;
22*f371f2dcSAnton Protopopov u64 nr_loops;
23*f371f2dcSAnton Protopopov u32 __attribute__((__aligned__(8))) key[NR_CPUS];
24*f371f2dcSAnton Protopopov 
25*f371f2dcSAnton Protopopov /* Filled by us */
26*f371f2dcSAnton Protopopov u64 __attribute__((__aligned__(256))) percpu_times_index[NR_CPUS];
27*f371f2dcSAnton Protopopov u64 __attribute__((__aligned__(256))) percpu_times[NR_CPUS][NR_SLOTS];
28*f371f2dcSAnton Protopopov 
patch_key(u32 i)29*f371f2dcSAnton Protopopov static inline void patch_key(u32 i)
30*f371f2dcSAnton Protopopov {
31*f371f2dcSAnton Protopopov #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
32*f371f2dcSAnton Protopopov 	key[0] = i + 1;
33*f371f2dcSAnton Protopopov #else
34*f371f2dcSAnton Protopopov 	key[0] = __builtin_bswap32(i + 1);
35*f371f2dcSAnton Protopopov #endif
36*f371f2dcSAnton Protopopov 	/* the rest of key is random and is configured by userspace */
37*f371f2dcSAnton Protopopov }
38*f371f2dcSAnton Protopopov 
lookup_callback(__u32 index,u32 * unused)39*f371f2dcSAnton Protopopov static int lookup_callback(__u32 index, u32 *unused)
40*f371f2dcSAnton Protopopov {
41*f371f2dcSAnton Protopopov 	patch_key(index);
42*f371f2dcSAnton Protopopov 	return bpf_map_lookup_elem(&hash_map_bench, key) ? 0 : 1;
43*f371f2dcSAnton Protopopov }
44*f371f2dcSAnton Protopopov 
loop_lookup_callback(__u32 index,u32 * unused)45*f371f2dcSAnton Protopopov static int loop_lookup_callback(__u32 index, u32 *unused)
46*f371f2dcSAnton Protopopov {
47*f371f2dcSAnton Protopopov 	return bpf_loop(nr_entries, lookup_callback, NULL, 0) ? 0 : 1;
48*f371f2dcSAnton Protopopov }
49*f371f2dcSAnton Protopopov 
50*f371f2dcSAnton Protopopov SEC("fentry/" SYS_PREFIX "sys_getpgid")
benchmark(void * ctx)51*f371f2dcSAnton Protopopov int benchmark(void *ctx)
52*f371f2dcSAnton Protopopov {
53*f371f2dcSAnton Protopopov 	u32 cpu = bpf_get_smp_processor_id();
54*f371f2dcSAnton Protopopov 	u32 times_index;
55*f371f2dcSAnton Protopopov 	u64 start_time;
56*f371f2dcSAnton Protopopov 
57*f371f2dcSAnton Protopopov 	times_index = percpu_times_index[cpu & CPU_MASK] % NR_SLOTS;
58*f371f2dcSAnton Protopopov 	start_time = bpf_ktime_get_ns();
59*f371f2dcSAnton Protopopov 	bpf_loop(nr_loops, loop_lookup_callback, NULL, 0);
60*f371f2dcSAnton Protopopov 	percpu_times[cpu & CPU_MASK][times_index] = bpf_ktime_get_ns() - start_time;
61*f371f2dcSAnton Protopopov 	percpu_times_index[cpu & CPU_MASK] += 1;
62*f371f2dcSAnton Protopopov 	return 0;
63*f371f2dcSAnton Protopopov }
64