selftests/bpf: Add stacktrace map lookup_and_delete_elem test case

Add tests for stacktrace map lookup and delete:
1. use bpf_map_lookup_and_delete_elem to lookup and delete the target
   stack_id,
2. lookup the deleted stack_id again to double check.

Signed-off-by: Tao Chen <chen.dylane@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250925175030.1615837-3-chen.dylane@linux.dev
This commit is contained in:
Tao Chen
2025-09-25 16:17:30 -07:00
committed by Andrii Nakryiko
parent 363b17e273
commit d43029ff7d
2 changed files with 12 additions and 1 deletions
@@ -7,7 +7,8 @@ void test_stacktrace_map(void)
struct stacktrace_map *skel;
int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
int err, stack_trace_len;
__u32 key, val, duration = 0;
__u32 key, val, stack_id, duration = 0;
__u64 stack[PERF_MAX_STACK_DEPTH];
skel = stacktrace_map__open_and_load();
if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
@@ -48,6 +49,14 @@ void test_stacktrace_map(void)
"err %d errno %d\n", err, errno))
goto out;
stack_id = skel->bss->stack_id;
err = bpf_map_lookup_and_delete_elem(stackmap_fd, &stack_id, stack);
if (!ASSERT_OK(err, "lookup and delete target stack_id"))
goto out;
err = bpf_map_lookup_elem(stackmap_fd, &stack_id, stack);
if (!ASSERT_EQ(err, -ENOENT, "lookup deleted stack_id"))
goto out;
out:
stacktrace_map__destroy(skel);
}
@@ -50,6 +50,7 @@ struct sched_switch_args {
int next_prio;
};
__u32 stack_id;
SEC("tracepoint/sched/sched_switch")
int oncpu(struct sched_switch_args *ctx)
{
@@ -64,6 +65,7 @@ int oncpu(struct sched_switch_args *ctx)
/* The size of stackmap and stackid_hmap should be the same */
key = bpf_get_stackid(ctx, &stackmap, 0);
if ((int)key >= 0) {
stack_id = key;
bpf_map_update_elem(&stackid_hmap, &key, &val, 0);
stack_p = bpf_map_lookup_elem(&stack_amap, &key);
if (stack_p)