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