Skip to content

Commit dbd9100

Browse files
namhyungNobody
authored and
Nobody
committed
bpf/selftests: Test skipping stacktrace
Add a test case for stacktrace with skip > 0 using a small sized buffer. It didn't support skipping entries greater than or equal to the size of buffer and filled the skipped part with 0. Signed-off-by: Namhyung Kim <[email protected]>
1 parent e9e2435 commit dbd9100

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
#include "stacktrace_map_skip.skel.h"
4+
5+
#define TEST_STACK_DEPTH 2
6+
7+
void test_stacktrace_map_skip(void)
8+
{
9+
struct stacktrace_map_skip *skel;
10+
int stackid_hmap_fd, stackmap_fd, stack_amap_fd;
11+
int err, stack_trace_len;
12+
13+
skel = stacktrace_map_skip__open_and_load();
14+
if (ASSERT_OK_PTR(skel, "skel_open_and_load"))
15+
return;
16+
17+
/* find map fds */
18+
stackid_hmap_fd = bpf_map__fd(skel->maps.stackid_hmap);
19+
if (ASSERT_GE(stackid_hmap_fd, 0, "stackid_hmap fd"))
20+
goto out;
21+
22+
stackmap_fd = bpf_map__fd(skel->maps.stackmap);
23+
if (ASSERT_GE(stackmap_fd, 0, "stackmap fd"))
24+
goto out;
25+
26+
stack_amap_fd = bpf_map__fd(skel->maps.stack_amap);
27+
if (ASSERT_GE(stack_amap_fd, 0, "stack_amap fd"))
28+
goto out;
29+
30+
err = stacktrace_map_skip__attach(skel);
31+
if (ASSERT_OK(err, "skel_attach"))
32+
goto out;
33+
34+
/* give some time for bpf program run */
35+
sleep(1);
36+
37+
/* disable stack trace collection */
38+
skel->bss->control = 1;
39+
40+
/* for every element in stackid_hmap, we can find a corresponding one
41+
* in stackmap, and vise versa.
42+
*/
43+
err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
44+
if (ASSERT_OK(err, "compare_map_keys stackid_hmap vs. stackmap"))
45+
goto out;
46+
47+
err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
48+
if (ASSERT_OK(err, "compare_map_keys stackmap vs. stackid_hmap"))
49+
goto out;
50+
51+
stack_trace_len = TEST_STACK_DEPTH * sizeof(__u64);
52+
err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len);
53+
if (ASSERT_OK(err, "compare_stack_ips stackmap vs. stack_amap"))
54+
goto out;
55+
56+
if (ASSERT_EQ(skel->bss->failed, 0, "skip_failed"))
57+
goto out;
58+
59+
out:
60+
stacktrace_map_skip__destroy(skel);
61+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <vmlinux.h>
3+
#include <bpf/bpf_helpers.h>
4+
5+
#define TEST_STACK_DEPTH 2
6+
#define TEST_MAX_ENTRIES 16384
7+
8+
typedef __u64 stack_trace_t[TEST_STACK_DEPTH];
9+
10+
struct {
11+
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
12+
__uint(max_entries, TEST_MAX_ENTRIES);
13+
__type(key, __u32);
14+
__type(value, stack_trace_t);
15+
} stackmap SEC(".maps");
16+
17+
struct {
18+
__uint(type, BPF_MAP_TYPE_HASH);
19+
__uint(max_entries, TEST_MAX_ENTRIES);
20+
__type(key, __u32);
21+
__type(value, __u32);
22+
} stackid_hmap SEC(".maps");
23+
24+
struct {
25+
__uint(type, BPF_MAP_TYPE_ARRAY);
26+
__uint(max_entries, TEST_MAX_ENTRIES);
27+
__type(key, __u32);
28+
__type(value, stack_trace_t);
29+
} stack_amap SEC(".maps");
30+
31+
int control = 0;
32+
int failed = 0;
33+
34+
SEC("tracepoint/sched/sched_switch")
35+
int oncpu(struct trace_event_raw_sched_switch *ctx)
36+
{
37+
__u32 max_len = TEST_STACK_DEPTH * sizeof(__u64);
38+
__u32 key = 0, val = 0;
39+
__u64 *stack_p;
40+
41+
if (control)
42+
return 0;
43+
44+
/* it should allow skipping whole buffer size entries */
45+
key = bpf_get_stackid(ctx, &stackmap, TEST_STACK_DEPTH);
46+
if ((int)key >= 0) {
47+
/* The size of stackmap and stack_amap should be the same */
48+
bpf_map_update_elem(&stackid_hmap, &key, &val, 0);
49+
stack_p = bpf_map_lookup_elem(&stack_amap, &key);
50+
if (stack_p) {
51+
bpf_get_stack(ctx, stack_p, max_len, TEST_STACK_DEPTH);
52+
/* it wrongly skipped all the entries and filled zero */
53+
if (stack_p[0] == 0)
54+
failed = 1;
55+
}
56+
} else {
57+
/* old kernel doesn't support skipping that many entries */
58+
failed = 2;
59+
}
60+
61+
return 0;
62+
}
63+
64+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)