tools/testing/selftests/bpf/test_lru_map.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/test_lru_map.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/test_lru_map.c- Extension
.c- Size
- 23725 bytes
- Lines
- 888
- 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.hsched.hstdlib.htime.hsys/wait.hbpf/bpf.hbpf/libbpf.hbpf_util.h../../../include/linux/filter.h
Detected Declarations
function create_mapfunction bpf_map_lookup_elem_with_ref_bitfunction map_subsetfunction map_equalfunction sched_next_onlinefunction __tgt_sizefunction __map_sizefunction test_lru_sanity0function test_lru_sanity1function test_lru_sanity2function test_lru_sanity3function test_lru_sanity4function do_test_lru_sanity5function test_lru_sanity5function test_lru_sanity6function test_lru_sanity7function test_lru_sanity8function main
Annotated Snippet
if (ret) {
printf("key:%llu not found from map. %s(%d)\n",
next_key, strerror(errno), errno);
return 0;
}
if (value0[0] != value1[0]) {
printf("key:%llu value0:%llu != value1:%llu\n",
next_key, value0[0], value1[0]);
return 0;
}
}
return 1;
}
static int map_equal(int lru_map, int expected)
{
return map_subset(lru_map, expected) && map_subset(expected, lru_map);
}
static int sched_next_online(int pid, int *next_to_try)
{
cpu_set_t cpuset;
int next = *next_to_try;
int ret = -1;
while (next < nr_cpus) {
CPU_ZERO(&cpuset);
CPU_SET(next, &cpuset);
next++;
if (!sched_setaffinity(pid, sizeof(cpuset), &cpuset)) {
ret = 0;
break;
}
}
*next_to_try = next;
return ret;
}
/* Derive target_free from map_size, same as bpf_common_lru_populate */
static unsigned int __tgt_size(unsigned int map_size)
{
return (map_size / nr_cpus) / 2;
}
/* Inverse of how bpf_common_lru_populate derives target_free from map_size. */
static unsigned int __map_size(unsigned int tgt_free)
{
return tgt_free * nr_cpus * 2;
}
/* Size of the LRU map is 2
* Add key=1 (+1 key)
* Add key=2 (+1 key)
* Lookup Key=1
* Add Key=3
* => Key=2 will be removed by LRU
* Iterate map. Only found key=1 and key=3
*/
static void test_lru_sanity0(int map_type, int map_flags)
{
unsigned long long key, value[nr_cpus];
int lru_map_fd, expected_map_fd;
int next_cpu = 0;
printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
map_flags);
assert(sched_next_online(0, &next_cpu) != -1);
if (map_flags & BPF_F_NO_COMMON_LRU)
lru_map_fd = create_map(map_type, map_flags, 2 * nr_cpus);
else
lru_map_fd = create_map(map_type, map_flags, 2);
assert(lru_map_fd != -1);
expected_map_fd = create_map(BPF_MAP_TYPE_HASH, 0, 2);
assert(expected_map_fd != -1);
value[0] = 1234;
/* insert key=1 element */
key = 1;
assert(!bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST));
/* BPF_NOEXIST means: add new element if it doesn't exist */
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST) == -EEXIST);
Annotation
- Immediate include surface: `stdio.h`, `unistd.h`, `errno.h`, `string.h`, `assert.h`, `sched.h`, `stdlib.h`, `time.h`.
- Detected declarations: `function create_map`, `function bpf_map_lookup_elem_with_ref_bit`, `function map_subset`, `function map_equal`, `function sched_next_online`, `function __tgt_size`, `function __map_size`, `function test_lru_sanity0`, `function test_lru_sanity1`, `function test_lru_sanity2`.
- 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.