Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <chen.dylane@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250925175030.1615837-2-chen.dylane@linux.dev

authored by

Tao Chen and committed by
Andrii Nakryiko
363b17e2 17f0d1f6

+18 -40
+17 -39
tools/testing/selftests/bpf/prog_tests/stacktrace_map.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 #include <test_progs.h> 3 + #include "stacktrace_map.skel.h" 3 4 4 5 void test_stacktrace_map(void) 5 6 { 7 + struct stacktrace_map *skel; 6 8 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; 10 10 __u32 key, val, duration = 0; 11 - struct bpf_program *prog; 12 - struct bpf_object *obj; 13 - struct bpf_link *link; 14 11 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")) 17 14 return; 18 15 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; 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); 22 20 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; 43 - 21 + err = stacktrace_map__attach(skel); 22 + if (!ASSERT_OK(err, "skel_attach")) 23 + goto out; 44 24 /* give some time for bpf program run */ 45 25 sleep(1); 46 26 ··· 35 55 err = compare_map_keys(stackid_hmap_fd, stackmap_fd); 36 56 if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap", 37 57 "err %d errno %d\n", err, errno)) 38 - goto disable_pmu; 58 + goto out; 39 59 40 60 err = compare_map_keys(stackmap_fd, stackid_hmap_fd); 41 61 if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap", 42 62 "err %d errno %d\n", err, errno)) 43 - goto disable_pmu; 63 + goto out; 44 64 45 65 stack_trace_len = PERF_MAX_STACK_DEPTH * sizeof(__u64); 46 66 err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len); 47 67 if (CHECK(err, "compare_stack_ips stackmap vs. stack_amap", 48 68 "err %d errno %d\n", err, errno)) 49 - goto disable_pmu; 69 + goto out; 50 70 51 - disable_pmu: 52 - bpf_link__destroy(link); 53 - close_prog: 54 - bpf_object__close(obj); 71 + out: 72 + stacktrace_map__destroy(skel); 55 73 }
+1 -1
tools/testing/selftests/bpf/prog_tests/stacktrace_map_raw_tp.c
··· 5 5 { 6 6 const char *prog_name = "oncpu"; 7 7 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"; 9 9 __u32 key, val, duration = 0; 10 10 int err, prog_fd; 11 11 struct bpf_program *prog;
tools/testing/selftests/bpf/progs/test_stacktrace_map.c tools/testing/selftests/bpf/progs/stacktrace_map.c