xref: /linux/tools/testing/selftests/bpf/progs/bpf_hashmap_full_update_bench.c (revision 58f6259b7a08f8d47d4629609703d358b042f0fd)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Bytedance */
3 
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include "bpf_misc.h"
7 
8 char _license[] SEC("license") = "GPL";
9 
10 #define MAX_ENTRIES 1000
11 
12 struct {
13 	__uint(type, BPF_MAP_TYPE_HASH);
14 	__type(key, u32);
15 	__type(value, u64);
16 	__uint(max_entries, MAX_ENTRIES);
17 } hash_map_bench SEC(".maps");
18 
19 u64 __attribute__((__aligned__(256))) percpu_time[256];
20 u64 nr_loops;
21 
22 static int loop_update_callback(__u32 index, u32 *key)
23 {
24 	u64 init_val = 1;
25 
26 	bpf_map_update_elem(&hash_map_bench, key, &init_val, BPF_ANY);
27 	return 0;
28 }
29 
30 SEC("fentry/" SYS_PREFIX "sys_getpgid")
31 int benchmark(void *ctx)
32 {
33 	u32 cpu = bpf_get_smp_processor_id();
34 	u32 key = cpu + MAX_ENTRIES;
35 	u64 start_time = bpf_ktime_get_ns();
36 
37 	bpf_loop(nr_loops, loop_update_callback, &key, 0);
38 	percpu_time[cpu & 255] = bpf_ktime_get_ns() - start_time;
39 	return 0;
40 }
41