Skip to content

Commit 7ecb167

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 bcc822f commit 7ecb167

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"))
@@ -60,6 +61,14 @@ void test_stacktrace_map(void)
6061
"err %d errno %d\n", err, errno))
6162
goto out;
6263

64+
stack_id = skel->bss->stack_id;
65+
err = bpf_map_lookup_and_delete_elem(stackmap_fd, &stack_id, stack);
66+
if (!ASSERT_OK(err, "lookup and delete target stack_id"))
67+
goto out;
68+
69+
err = bpf_map_lookup_elem(stackmap_fd, &stack_id, stack);
70+
if (!ASSERT_EQ(err, -ENOENT, "lookup deleted stack_id"))
71+
goto out;
6372
out:
6473
stacktrace_map__destroy(skel);
6574
}

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)