Skip to content

Commit 2df9c00

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 7ed89d9 commit 2df9c00

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33

44
void test_stacktrace_map(void)
55
{
6-
int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
6+
int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd, stack_key_map_fd;
77
const char *prog_name = "oncpu";
88
int err, prog_fd, stack_trace_len;
99
const char *file = "./test_stacktrace_map.bpf.o";
1010
__u32 key, val, duration = 0;
1111
struct bpf_program *prog;
1212
struct bpf_object *obj;
1313
struct bpf_link *link;
14+
__u32 stack_id;
15+
char val_buf[PERF_MAX_STACK_DEPTH * sizeof(struct bpf_stack_build_id)];
1416

1517
err = bpf_prog_test_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
1618
if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno))
@@ -41,6 +43,10 @@ void test_stacktrace_map(void)
4143
if (CHECK_FAIL(stack_amap_fd < 0))
4244
goto disable_pmu;
4345

46+
stack_key_map_fd = bpf_find_map(__func__, obj, "stack_key_map");
47+
if (CHECK_FAIL(stack_key_map_fd < 0))
48+
goto disable_pmu;
49+
4450
/* give some time for bpf program run */
4551
sleep(1);
4652

@@ -68,6 +74,19 @@ void test_stacktrace_map(void)
6874
"err %d errno %d\n", err, errno))
6975
goto disable_pmu;
7076

77+
err = bpf_map_lookup_elem(stack_key_map_fd, &key, &stack_id);
78+
if (CHECK(err, "stack_key_map lookup", "err %d errno %d\n", err, errno))
79+
goto disable_pmu;
80+
81+
err = bpf_map_lookup_and_delete_elem(stackmap_fd, &stack_id, &val_buf);
82+
if (CHECK(err, "stackmap lookup and delete",
83+
"err %d errno %d\n", err, errno))
84+
goto disable_pmu;
85+
86+
err = bpf_map_lookup_elem(stackmap_fd, &stack_id, &val_buf);
87+
CHECK((!err || errno != ENOENT), "stackmap lookup deleted stack_id",
88+
"err %d errno %d\n", err, errno);
89+
7190
disable_pmu:
7291
bpf_link__destroy(link);
7392
close_prog:

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ struct {
3838
__type(value, stack_trace_t);
3939
} stack_amap SEC(".maps");
4040

41+
struct {
42+
__uint(type, BPF_MAP_TYPE_ARRAY);
43+
__uint(max_entries, 1);
44+
__type(key, __u32);
45+
__type(value, __u32);
46+
} stack_key_map SEC(".maps");
47+
4148
/* taken from /sys/kernel/tracing/events/sched/sched_switch/format */
4249
struct sched_switch_args {
4350
unsigned long long pad;
@@ -64,6 +71,7 @@ int oncpu(struct sched_switch_args *ctx)
6471
/* The size of stackmap and stackid_hmap should be the same */
6572
key = bpf_get_stackid(ctx, &stackmap, 0);
6673
if ((int)key >= 0) {
74+
bpf_map_update_elem(&stack_key_map, &val, &key, 0);
6775
bpf_map_update_elem(&stackid_hmap, &key, &val, 0);
6876
stack_p = bpf_map_lookup_elem(&stack_amap, &key);
6977
if (stack_p)

0 commit comments

Comments
 (0)