Skip to content

Commit d43029f

Browse files
Tao Chenanakryiko
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]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 363b17e commit d43029f

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 control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
99
int err, stack_trace_len;
10-
__u32 key, val, duration = 0;
10+
__u32 key, val, stack_id, duration = 0;
11+
__u64 stack[PERF_MAX_STACK_DEPTH];
1112

1213
skel = stacktrace_map__open_and_load();
1314
if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
@@ -48,6 +49,14 @@ void test_stacktrace_map(void)
4849
"err %d errno %d\n", err, errno))
4950
goto out;
5051

52+
stack_id = skel->bss->stack_id;
53+
err = bpf_map_lookup_and_delete_elem(stackmap_fd, &stack_id, stack);
54+
if (!ASSERT_OK(err, "lookup and delete target stack_id"))
55+
goto out;
56+
57+
err = bpf_map_lookup_elem(stackmap_fd, &stack_id, stack);
58+
if (!ASSERT_EQ(err, -ENOENT, "lookup deleted stack_id"))
59+
goto out;
5160
out:
5261
stacktrace_map__destroy(skel);
5362
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct sched_switch_args {
5050
int next_prio;
5151
};
5252

53+
__u32 stack_id;
5354
SEC("tracepoint/sched/sched_switch")
5455
int oncpu(struct sched_switch_args *ctx)
5556
{
@@ -64,6 +65,7 @@ int oncpu(struct sched_switch_args *ctx)
6465
/* The size of stackmap and stackid_hmap should be the same */
6566
key = bpf_get_stackid(ctx, &stackmap, 0);
6667
if ((int)key >= 0) {
68+
stack_id = key;
6769
bpf_map_update_elem(&stackid_hmap, &key, &val, 0);
6870
stack_p = bpf_map_lookup_elem(&stack_amap, &key);
6971
if (stack_p)

0 commit comments

Comments
 (0)