tools/testing/selftests/bpf/progs/local_storage_bench.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/local_storage_bench.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/local_storage_bench.c
Extension
.c
Size
2324 bytes
Lines
105
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

struct loop_ctx {
	struct task_struct *task;
	long loop_hits;
	long loop_important_hits;
};

static int do_lookup(unsigned int elem, struct loop_ctx *lctx)
{
	void *map, *inner_map;
	int idx = 0;

	if (use_hashmap)
		map = &array_of_hash_maps;
	else
		map = &array_of_local_storage_maps;

	inner_map = bpf_map_lookup_elem(map, &elem);
	if (!inner_map)
		return -1;

	if (use_hashmap) {
		idx = bpf_get_prandom_u32() % hashmap_num_keys;
		bpf_map_lookup_elem(inner_map, &idx);
	} else {
		bpf_task_storage_get(inner_map, lctx->task, &idx,
				     BPF_LOCAL_STORAGE_GET_F_CREATE);
	}

	lctx->loop_hits++;
	if (!elem)
		lctx->loop_important_hits++;
	return 0;
}

static long loop(u32 index, void *ctx)
{
	struct loop_ctx *lctx = (struct loop_ctx *)ctx;
	unsigned int map_idx = index % num_maps;

	do_lookup(map_idx, lctx);
	if (interleave && map_idx % 3 == 0)
		do_lookup(0, lctx);
	return 0;
}

SEC("fentry/" SYS_PREFIX "sys_getpgid")
int get_local(void *ctx)
{
	struct loop_ctx lctx;

	lctx.task = bpf_get_current_task_btf();
	lctx.loop_hits = 0;
	lctx.loop_important_hits = 0;
	bpf_loop(10000, &loop, &lctx, 0);
	__sync_add_and_fetch(&hits, lctx.loop_hits);
	__sync_add_and_fetch(&important_hits, lctx.loop_important_hits);
	return 0;
}

char _license[] SEC("license") = "GPL";

Annotation

Implementation Notes