tools/testing/selftests/bpf/test_maps.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/test_maps.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/test_maps.c- Extension
.c- Size
- 51527 bytes
- Lines
- 1941
- 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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdio.hunistd.herrno.hstring.hassert.hstdlib.htime.hsys/wait.hsys/socket.hnetinet/in.hlinux/bpf.hbpf/bpf.hbpf/libbpf.hbpf_util.htest_maps.htesting_helpers.hsys/ioctl.harpa/inet.hsys/select.hlinux/err.hmap_tests/tests.h
Detected Declarations
struct bigkeyfunction test_hashmapfunction test_hashmap_sizesfunction test_hashmap_percpufunction can_retryfunction helper_fill_hashmapfunction test_hashmap_walkfunction test_hashmap_zero_seedfunction test_arraymapfunction test_arraymap_percpufunction test_arraymap_percpu_many_keysfunction test_devmapfunction test_devmap_hashfunction test_queuemapfunction test_stackmapfunction test_sockmapfunction test_map_in_mapfunction test_map_largefunction __run_parallelfunction test_map_stressfunction map_update_retriablefunction map_delete_retriablefunction test_update_deletefunction test_map_parallelfunction test_map_rdonlyfunction test_map_wronly_hashfunction test_map_wronly_stack_or_queuefunction test_map_wronlyfunction prepare_reuseport_grpfunction test_reuseport_arrayfunction run_all_testsfunction main
Annotated Snippet
struct bigkey {
int a;
char b[4096];
long long c;
} key;
int fd, i, value;
fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
MAP_SIZE, &map_opts);
if (fd < 0) {
printf("Failed to create large map '%s'!\n", strerror(errno));
exit(1);
}
for (i = 0; i < MAP_SIZE; i++) {
key = (struct bigkey) { .c = i };
value = i;
assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
}
key.c = -1;
assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
errno == E2BIG);
/* Iterate through all elements. */
assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
key.c = -1;
for (i = 0; i < MAP_SIZE; i++)
assert(bpf_map_get_next_key(fd, &key, &key) == 0);
assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
key.c = 0;
assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
key.a = 1;
assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
close(fd);
}
#define run_parallel(N, FN, DATA) \
printf("Fork %u tasks to '" #FN "'\n", N); \
__run_parallel(N, FN, DATA)
static void __run_parallel(unsigned int tasks,
void (*fn)(unsigned int task, void *data),
void *data)
{
pid_t pid[tasks];
int i;
fflush(stdout);
for (i = 0; i < tasks; i++) {
pid[i] = fork();
if (pid[i] == 0) {
fn(i, data);
exit(0);
} else if (pid[i] == -1) {
printf("Couldn't spawn #%d process!\n", i);
exit(1);
}
}
for (i = 0; i < tasks; i++) {
int status;
assert(waitpid(pid[i], &status, 0) == pid[i]);
assert(status == 0);
}
}
static void test_map_stress(void)
{
run_parallel(100, test_hashmap_walk, NULL);
run_parallel(100, test_hashmap, NULL);
run_parallel(100, test_hashmap_percpu, NULL);
run_parallel(100, test_hashmap_sizes, NULL);
run_parallel(100, test_arraymap, NULL);
run_parallel(100, test_arraymap_percpu, NULL);
}
#define TASKS 100
#define DO_UPDATE 1
#define DO_DELETE 0
#define MAX_DELAY_US 50000
#define MIN_DELAY_RANGE_US 5000
Annotation
- Immediate include surface: `stdio.h`, `unistd.h`, `errno.h`, `string.h`, `assert.h`, `stdlib.h`, `time.h`, `sys/wait.h`.
- Detected declarations: `struct bigkey`, `function test_hashmap`, `function test_hashmap_sizes`, `function test_hashmap_percpu`, `function can_retry`, `function helper_fill_hashmap`, `function test_hashmap_walk`, `function test_hashmap_zero_seed`, `function test_arraymap`, `function test_arraymap_percpu`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.