Skip to content

Commit 3fb1a73

Browse files
Tao ChenKernel Patches Daemon
authored andcommitted
selftests/bpf: Refactor stacktrace_map case with skeleton
The loading method of the stacktrace_map test case looks too outdated, refactor it with skeleton, and we can use global avariable feature in the next patch. Signed-off-by: Tao Chen <[email protected]>
1 parent 2481b04 commit 3fb1a73

File tree

3 files changed

+22
-32
lines changed

3 files changed

+22
-32
lines changed
Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,38 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <test_progs.h>
3+
#include "stacktrace_map.skel.h"
34

45
void test_stacktrace_map(void)
56
{
7+
struct stacktrace_map *skel;
68
int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
7-
const char *prog_name = "oncpu";
8-
int err, prog_fd, stack_trace_len;
9-
const char *file = "./test_stacktrace_map.bpf.o";
9+
int err, stack_trace_len;
1010
__u32 key, val, duration = 0;
11-
struct bpf_program *prog;
12-
struct bpf_object *obj;
13-
struct bpf_link *link;
1411

15-
err = bpf_prog_test_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
16-
if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno))
12+
skel = stacktrace_map__open_and_load();
13+
if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
1714
return;
1815

19-
prog = bpf_object__find_program_by_name(obj, prog_name);
20-
if (CHECK(!prog, "find_prog", "prog '%s' not found\n", prog_name))
21-
goto close_prog;
22-
23-
link = bpf_program__attach_tracepoint(prog, "sched", "sched_switch");
24-
if (!ASSERT_OK_PTR(link, "attach_tp"))
25-
goto close_prog;
26-
2716
/* find map fds */
28-
control_map_fd = bpf_find_map(__func__, obj, "control_map");
17+
control_map_fd = bpf_map__fd(skel->maps.control_map);
2918
if (CHECK_FAIL(control_map_fd < 0))
30-
goto disable_pmu;
19+
goto out;
3120

32-
stackid_hmap_fd = bpf_find_map(__func__, obj, "stackid_hmap");
21+
stackid_hmap_fd = bpf_map__fd(skel->maps.stackid_hmap);
3322
if (CHECK_FAIL(stackid_hmap_fd < 0))
34-
goto disable_pmu;
23+
goto out;
3524

36-
stackmap_fd = bpf_find_map(__func__, obj, "stackmap");
25+
stackmap_fd = bpf_map__fd(skel->maps.stackmap);
3726
if (CHECK_FAIL(stackmap_fd < 0))
38-
goto disable_pmu;
27+
goto out;
3928

40-
stack_amap_fd = bpf_find_map(__func__, obj, "stack_amap");
29+
stack_amap_fd = bpf_map__fd(skel->maps.stack_amap);
4130
if (CHECK_FAIL(stack_amap_fd < 0))
42-
goto disable_pmu;
31+
goto out;
4332

33+
err = stacktrace_map__attach(skel);
34+
if (!ASSERT_OK(err, "skel_attach"))
35+
goto out;
4436
/* give some time for bpf program run */
4537
sleep(1);
4638

@@ -55,21 +47,19 @@ void test_stacktrace_map(void)
5547
err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
5648
if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
5749
"err %d errno %d\n", err, errno))
58-
goto disable_pmu;
50+
goto out;
5951

6052
err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
6153
if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
6254
"err %d errno %d\n", err, errno))
63-
goto disable_pmu;
55+
goto out;
6456

6557
stack_trace_len = PERF_MAX_STACK_DEPTH * sizeof(__u64);
6658
err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len);
6759
if (CHECK(err, "compare_stack_ips stackmap vs. stack_amap",
6860
"err %d errno %d\n", err, errno))
69-
goto disable_pmu;
61+
goto out;
7062

71-
disable_pmu:
72-
bpf_link__destroy(link);
73-
close_prog:
74-
bpf_object__close(obj);
63+
out:
64+
stacktrace_map__destroy(skel);
7565
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ void test_stacktrace_map_raw_tp(void)
55
{
66
const char *prog_name = "oncpu";
77
int control_map_fd, stackid_hmap_fd, stackmap_fd;
8-
const char *file = "./test_stacktrace_map.bpf.o";
8+
const char *file = "./stacktrace_map.bpf.o";
99
__u32 key, val, duration = 0;
1010
int err, prog_fd;
1111
struct bpf_program *prog;

0 commit comments

Comments
 (0)