tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_update.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_update.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_update.c
Extension
.c
Size
2612 bytes
Lines
120
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2022 Bytedance */

#include "bench.h"
#include "bpf_hashmap_full_update_bench.skel.h"
#include "bpf_util.h"

/* BPF triggering benchmarks */
static struct ctx {
	struct bpf_hashmap_full_update_bench *skel;
} ctx;

#define MAX_LOOP_NUM 10000

static void validate(void)
{
	if (env.consumer_cnt != 0) {
		fprintf(stderr, "benchmark doesn't support consumer!\n");
		exit(1);
	}
}

static void *producer(void *input)
{
	while (true) {
		/* trigger the bpf program */
		syscall(__NR_getpgid);
	}

	return NULL;
}

static void measure(struct bench_res *res)
{
}

static void hashmap_full_update_setup(enum bpf_map_type map_type)
{
	struct bpf_link *link;
	int map_fd, i, max_entries;

	setup_libbpf();

	ctx.skel = bpf_hashmap_full_update_bench__open();
	if (!ctx.skel) {
		fprintf(stderr, "failed to open skeleton\n");
		exit(1);
	}

	bpf_map__set_type(ctx.skel->maps.hash_map_bench, map_type);
	if (map_type == BPF_MAP_TYPE_RHASH)
		bpf_map__set_map_flags(ctx.skel->maps.hash_map_bench,
				       BPF_F_NO_PREALLOC);

	if (bpf_hashmap_full_update_bench__load(ctx.skel)) {
		fprintf(stderr, "failed to load skeleton\n");
		exit(1);
	}

	ctx.skel->bss->nr_loops = MAX_LOOP_NUM;

	link = bpf_program__attach(ctx.skel->progs.benchmark);
	if (!link) {
		fprintf(stderr, "failed to attach program!\n");
		exit(1);
	}

	/* fill hash_map */
	map_fd = bpf_map__fd(ctx.skel->maps.hash_map_bench);
	max_entries = bpf_map__max_entries(ctx.skel->maps.hash_map_bench);
	for (i = 0; i < max_entries; i++)
		bpf_map_update_elem(map_fd, &i, &i, BPF_ANY);
}

static void setup(void)
{
	hashmap_full_update_setup(BPF_MAP_TYPE_HASH);
}

static void rhash_setup(void)
{
	hashmap_full_update_setup(BPF_MAP_TYPE_RHASH);
}

static void hashmap_report_final(struct bench_res res[], int res_cnt)
{
	unsigned int nr_cpus = bpf_num_possible_cpus();
	int i;

	for (i = 0; i < nr_cpus; i++) {

Annotation

Implementation Notes