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