Skip to content

Commit 363b17e

Browse files
Tao Chenanakryiko
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 variable feature in the next patch. Signed-off-by: Tao Chen <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 17f0d1f commit 363b17e

File tree

3 files changed

+18
-40
lines changed

3 files changed

+18
-40
lines changed
Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,26 @@
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-
27-
/* find map fds */
28-
control_map_fd = bpf_find_map(__func__, obj, "control_map");
29-
if (CHECK_FAIL(control_map_fd < 0))
30-
goto disable_pmu;
31-
32-
stackid_hmap_fd = bpf_find_map(__func__, obj, "stackid_hmap");
33-
if (CHECK_FAIL(stackid_hmap_fd < 0))
34-
goto disable_pmu;
35-
36-
stackmap_fd = bpf_find_map(__func__, obj, "stackmap");
37-
if (CHECK_FAIL(stackmap_fd < 0))
38-
goto disable_pmu;
39-
40-
stack_amap_fd = bpf_find_map(__func__, obj, "stack_amap");
41-
if (CHECK_FAIL(stack_amap_fd < 0))
42-
goto disable_pmu;
16+
control_map_fd = bpf_map__fd(skel->maps.control_map);
17+
stackid_hmap_fd = bpf_map__fd(skel->maps.stackid_hmap);
18+
stackmap_fd = bpf_map__fd(skel->maps.stackmap);
19+
stack_amap_fd = bpf_map__fd(skel->maps.stack_amap);
4320

21+
err = stacktrace_map__attach(skel);
22+
if (!ASSERT_OK(err, "skel_attach"))
23+
goto out;
4424
/* give some time for bpf program run */
4525
sleep(1);
4626

@@ -55,21 +35,19 @@ void test_stacktrace_map(void)
5535
err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
5636
if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
5737
"err %d errno %d\n", err, errno))
58-
goto disable_pmu;
38+
goto out;
5939

6040
err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
6141
if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
6242
"err %d errno %d\n", err, errno))
63-
goto disable_pmu;
43+
goto out;
6444

6545
stack_trace_len = PERF_MAX_STACK_DEPTH * sizeof(__u64);
6646
err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len);
6747
if (CHECK(err, "compare_stack_ips stackmap vs. stack_amap",
6848
"err %d errno %d\n", err, errno))
69-
goto disable_pmu;
49+
goto out;
7050

71-
disable_pmu:
72-
bpf_link__destroy(link);
73-
close_prog:
74-
bpf_object__close(obj);
51+
out:
52+
stacktrace_map__destroy(skel);
7553
}

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;
File renamed without changes.

0 commit comments

Comments
 (0)