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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/bloom_filter_bench.c
Extension
.c
Size
3077 bytes
Lines
155
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 callback_ctx {
	struct bpf_map *map;
	bool update;
};

/* Tracks the number of hits, drops, and false hits */
struct {
	__u32 stats[3];
} __attribute__((__aligned__(256))) percpu_stats[256];

const __u32 hit_key  = 0;
const __u32 drop_key  = 1;
const __u32 false_hit_key = 2;

__u8 value_size;

const volatile bool hashmap_use_bloom;
const volatile bool count_false_hits;

int error = 0;

static __always_inline void log_result(__u32 key)
{
	__u32 cpu = bpf_get_smp_processor_id();

	percpu_stats[cpu & 255].stats[key]++;
}

static __u64
bloom_callback(struct bpf_map *map, __u32 *key, void *val,
	       struct callback_ctx *data)
{
	int err;

	if (data->update)
		err = bpf_map_push_elem(data->map, val, 0);
	else
		err = bpf_map_peek_elem(data->map, val);

	if (err) {
		error |= 1;
		return 1; /* stop the iteration */
	}

	log_result(hit_key);

	return 0;
}

SEC("fentry/" SYS_PREFIX "sys_getpgid")
int bloom_lookup(void *ctx)
{
	struct callback_ctx data;

	data.map = (struct bpf_map *)&bloom_map;
	data.update = false;

	bpf_for_each_map_elem(&array_map, bloom_callback, &data, 0);

	return 0;
}

SEC("fentry/" SYS_PREFIX "sys_getpgid")
int bloom_update(void *ctx)
{
	struct callback_ctx data;

	data.map = (struct bpf_map *)&bloom_map;
	data.update = true;

	bpf_for_each_map_elem(&array_map, bloom_callback, &data, 0);

	return 0;
}

SEC("fentry/" SYS_PREFIX "sys_getpgid")
int bloom_hashmap_lookup(void *ctx)
{
	__u64 *result;
	int i, err;

	__u32 index = bpf_get_prandom_u32();
	__u32 bitmask = (1ULL << 21) - 1;

	for (i = 0; i < 1024; i++, index += value_size) {
		index = index & bitmask;

		if (hashmap_use_bloom) {
			err = bpf_map_peek_elem(&bloom_map,
						rand_vals + index);

Annotation

Implementation Notes