Skip to content

Commit 02cbb8e

Browse files
Tao ChenKernel Patches Daemon
authored andcommitted
selftests/bpf: Add stacktrace map lookup_and_delete_elem test case
... test_stacktrace_map:PASS:compare_stack_ips stackmap vs. stack_amap 0 nsec test_stacktrace_map:PASS:stack_key_map lookup 0 nsec test_stacktrace_map:PASS:stackmap lookup and detele 0 nsec test_stacktrace_map:PASS:stackmap lookup deleted stack_id 0 nsec #397 stacktrace_map:OK ... Signed-off-by: Tao Chen <[email protected]>
1 parent 1bea4ff commit 02cbb8e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
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 *
16+
sizeof(struct bpf_stack_build_id)];
1417

1518
err = bpf_prog_test_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
1619
if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno))
@@ -41,6 +44,10 @@ void test_stacktrace_map(void)
4144
if (CHECK_FAIL(stack_amap_fd < 0))
4245
goto disable_pmu;
4346

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

@@ -68,6 +75,19 @@ void test_stacktrace_map(void)
6875
"err %d errno %d\n", err, errno))
6976
goto disable_pmu;
7077

78+
err = bpf_map_lookup_elem(stack_key_map_fd, &key, &stack_id);
79+
if (CHECK(err, "stack_key_map lookup", "err %d errno %d\n", err, errno))
80+
goto disable_pmu;
81+
82+
err = bpf_map_lookup_and_delete_elem(stackmap_fd, &stack_id, &val_buf);
83+
if (CHECK(err, "stackmap lookup and delete",
84+
"err %d errno %d\n", err, errno))
85+
goto disable_pmu;
86+
87+
err = bpf_map_lookup_elem(stackmap_fd, &stack_id, &val_buf);
88+
CHECK((!err || errno != ENOENT), "stackmap lookup deleted stack_id",
89+
"err %d errno %d\n", err, errno);
90+
7191
disable_pmu:
7292
bpf_link__destroy(link);
7393
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)