Skip to content

Commit 586b3dd

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 #397 stacktrace_map:OK ... Signed-off-by: Tao Chen <chen.dylane@linux.dev>
1 parent 5151ca7 commit 586b3dd

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

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

Lines changed: 17 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 stackmap_key;
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,15 @@ 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, &stackmap_key);
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, &stackmap_key, &val_buf);
83+
if (CHECK(err, "stackmap lookup and detele",
84+
"err %d errno %d\n", err, errno))
85+
goto disable_pmu;
86+
7187
disable_pmu:
7288
bpf_link__destroy(link);
7389
close_prog:

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

Lines changed: 11 additions & 1 deletion
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;
@@ -54,7 +61,7 @@ SEC("tracepoint/sched/sched_switch")
5461
int oncpu(struct sched_switch_args *ctx)
5562
{
5663
__u32 max_len = PERF_MAX_STACK_DEPTH * sizeof(__u64);
57-
__u32 key = 0, val = 0, *value_p;
64+
__u32 key = 0, val = 0, *value_p, stackmap_key = 0;
5865
void *stack_p;
5966

6067
value_p = bpf_map_lookup_elem(&control_map, &key);
@@ -64,6 +71,9 @@ 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+
val = key;
75+
bpf_map_update_elem(&stack_key_map, &stackmap_key, &val, 0);
76+
val = 0;
6777
bpf_map_update_elem(&stackid_hmap, &key, &val, 0);
6878
stack_p = bpf_map_lookup_elem(&stack_amap, &key);
6979
if (stack_p)

0 commit comments

Comments
 (0)