Skip to content

Commit b95870d

Browse files
Tao ChenKernel Patches Daemon
authored andcommitted
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 <[email protected]>
1 parent e4ac1af commit b95870d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

tools/testing/selftests/bpf/prog_tests/stacktrace_map.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ void test_stacktrace_map(void)
77
struct stacktrace_map *skel;
88
int stackid_hmap_fd, stackmap_fd, stack_amap_fd;
99
int err, stack_trace_len;
10-
__u32 duration = 0;
10+
__u32 stack_id, duration = 0;
11+
__u64 val[PERF_MAX_STACK_DEPTH];
1112

1213
skel = stacktrace_map__open_and_load();
1314
if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
@@ -54,6 +55,14 @@ void test_stacktrace_map(void)
5455
"err %d errno %d\n", err, errno))
5556
goto out;
5657

58+
stack_id = skel->bss->stack_id;
59+
err = bpf_map_lookup_and_delete_elem(stackmap_fd, &stack_id, val);
60+
if (!ASSERT_OK(err, "lookup and delete target stack_id"))
61+
goto out;
62+
63+
err = bpf_map_lookup_elem(stackmap_fd, &stack_id, val);
64+
if (!ASSERT_EQ(err, -ENOENT, "lookup deleted stack_id"))
65+
goto out;
5766
out:
5867
stacktrace_map__destroy(skel);
5968
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct sched_switch_args {
4444
};
4545

4646
int control = 0;
47+
__u32 stack_id;
4748
SEC("tracepoint/sched/sched_switch")
4849
int oncpu(struct sched_switch_args *ctx)
4950
{
@@ -57,6 +58,7 @@ int oncpu(struct sched_switch_args *ctx)
5758
/* The size of stackmap and stackid_hmap should be the same */
5859
key = bpf_get_stackid(ctx, &stackmap, 0);
5960
if ((int)key >= 0) {
61+
stack_id = key;
6062
bpf_map_update_elem(&stackid_hmap, &key, &val, 0);
6163
stack_p = bpf_map_lookup_elem(&stack_amap, &key);
6264
if (stack_p)

0 commit comments

Comments
 (0)