tools/testing/selftests/bpf/map_tests/map_percpu_stats.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/map_tests/map_percpu_stats.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/map_tests/map_percpu_stats.c- Extension
.c- Size
- 12750 bytes
- Lines
- 489
- 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
errno.hunistd.hpthread.hbpf/bpf.hbpf/libbpf.hbpf_util.htest_maps.hmap_percpu_stats.skel.h
Detected Declarations
struct upsert_optsfunction map_infofunction map_count_elementsfunction delete_and_lookup_batchfunction delete_all_elementsfunction is_lrufunction is_percpufunction create_small_hashfunction retry_for_nomem_fnfunction upsert_elementsfunction read_cur_elementsfunction get_cur_elementsfunction check_expected_number_elementsfunction __testfunction map_create_optsfunction map_createfunction create_hashfunction create_percpu_hashfunction create_hash_preallocfunction create_percpu_hash_preallocfunction create_lru_hashfunction create_hash_of_mapsfunction map_percpu_stats_hashfunction map_percpu_stats_percpu_hashfunction map_percpu_stats_hash_preallocfunction map_percpu_stats_percpu_hash_preallocfunction map_percpu_stats_lru_hashfunction map_percpu_stats_lru_hash_no_commonfunction map_percpu_stats_percpu_lru_hashfunction map_percpu_stats_percpu_lru_hash_no_commonfunction map_percpu_stats_hash_of_mapsfunction map_percpu_stats_map_value_sizefunction test_map_percpu_stats
Annotated Snippet
struct upsert_opts {
__u32 map_type;
int map_fd;
__u32 n;
bool retry_for_nomem;
};
static int create_small_hash(void)
{
int map_fd;
map_fd = bpf_map_create(BPF_MAP_TYPE_HASH, "small", 4, 4, 4, NULL);
CHECK(map_fd < 0, "bpf_map_create()", "error:%s (name=%s)\n",
strerror(errno), "small");
return map_fd;
}
static bool retry_for_nomem_fn(int err)
{
return err == ENOMEM;
}
static void *patch_map_thread(void *arg)
{
/* 8KB is enough for 1024 CPUs. And it is shared between N_THREADS. */
static __u8 blob[8 << 10];
struct upsert_opts *opts = arg;
void *val_ptr;
int val;
int ret;
int i;
for (i = 0; i < opts->n; i++) {
if (opts->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
val = create_small_hash();
val_ptr = &val;
} else if (is_percpu(opts->map_type)) {
val_ptr = blob;
} else {
val = rand();
val_ptr = &val;
}
/* 2 seconds may be enough ? */
if (opts->retry_for_nomem)
ret = map_update_retriable(opts->map_fd, &i, val_ptr, 0,
40, retry_for_nomem_fn);
else
ret = bpf_map_update_elem(opts->map_fd, &i, val_ptr, 0);
CHECK(ret < 0, "bpf_map_update_elem", "key=%d error: %s\n", i, strerror(errno));
if (opts->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
close(val);
}
return NULL;
}
static void upsert_elements(struct upsert_opts *opts)
{
pthread_t threads[N_THREADS];
int ret;
int i;
for (i = 0; i < ARRAY_SIZE(threads); i++) {
ret = pthread_create(&i[threads], NULL, patch_map_thread, opts);
CHECK(ret != 0, "pthread_create", "error: %s\n", strerror(ret));
}
for (i = 0; i < ARRAY_SIZE(threads); i++) {
ret = pthread_join(i[threads], NULL);
CHECK(ret != 0, "pthread_join", "error: %s\n", strerror(ret));
}
}
static __u32 read_cur_elements(int iter_fd)
{
char buf[64];
ssize_t n;
__u32 ret;
n = read(iter_fd, buf, sizeof(buf)-1);
CHECK(n <= 0, "read", "error: %s\n", strerror(errno));
buf[n] = '\0';
errno = 0;
ret = (__u32)strtol(buf, NULL, 10);
CHECK(errno != 0, "strtol", "error: %s\n", strerror(errno));
return ret;
Annotation
- Immediate include surface: `errno.h`, `unistd.h`, `pthread.h`, `bpf/bpf.h`, `bpf/libbpf.h`, `bpf_util.h`, `test_maps.h`, `map_percpu_stats.skel.h`.
- Detected declarations: `struct upsert_opts`, `function map_info`, `function map_count_elements`, `function delete_and_lookup_batch`, `function delete_all_elements`, `function is_lru`, `function is_percpu`, `function create_small_hash`, `function retry_for_nomem_fn`, `function upsert_elements`.
- 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.