41 lines · c
1// SPDX-License-Identifier: GPL-2.02/* Copyright (c) 2022 Bytedance */3 4#include "vmlinux.h"5#include <bpf/bpf_helpers.h>6#include "bpf_misc.h"7 8char _license[] SEC("license") = "GPL";9 10#define MAX_ENTRIES 100011 12struct {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 19u64 __attribute__((__aligned__(256))) percpu_time[256];20u64 nr_loops;21 22static 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 30SEC("fentry/" SYS_PREFIX "sys_getpgid")31int 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