1 // SPDX-License-Identifier: GPL-2.0
5 #include <sys/socket.h>
6 #include <test_progs.h>
7 #include "bpf/libbpf_internal.h"
8 #include "test_perf_branches.skel.h"
10 static void check_good_sample(struct test_perf_branches *skel)
12 int written_global = skel->bss->written_global_out;
13 int required_size = skel->bss->required_size_out;
14 int written_stack = skel->bss->written_stack_out;
15 int pbe_size = sizeof(struct perf_branch_entry);
18 if (CHECK(!skel->bss->valid, "output not valid",
19 "no valid sample from prog"))
23 * It's hard to validate the contents of the branch entries b/c it
24 * would require some kind of disassembler and also encoding the
25 * valid jump instructions for supported architectures. So just check
26 * the easy stuff for now.
28 CHECK(required_size <= 0, "read_branches_size", "err %d\n", required_size);
29 CHECK(written_stack < 0, "read_branches_stack", "err %d\n", written_stack);
30 CHECK(written_stack % pbe_size != 0, "read_branches_stack",
31 "stack bytes written=%d not multiple of struct size=%d\n",
32 written_stack, pbe_size);
33 CHECK(written_global < 0, "read_branches_global", "err %d\n", written_global);
34 CHECK(written_global % pbe_size != 0, "read_branches_global",
35 "global bytes written=%d not multiple of struct size=%d\n",
36 written_global, pbe_size);
37 CHECK(written_global < written_stack, "read_branches_size",
38 "written_global=%d < written_stack=%d\n", written_global, written_stack);
41 static void check_bad_sample(struct test_perf_branches *skel)
43 int written_global = skel->bss->written_global_out;
44 int required_size = skel->bss->required_size_out;
45 int written_stack = skel->bss->written_stack_out;
48 if (CHECK(!skel->bss->valid, "output not valid",
49 "no valid sample from prog"))
52 CHECK((required_size != -EINVAL && required_size != -ENOENT),
53 "read_branches_size", "err %d\n", required_size);
54 CHECK((written_stack != -EINVAL && written_stack != -ENOENT),
55 "read_branches_stack", "written %d\n", written_stack);
56 CHECK((written_global != -EINVAL && written_global != -ENOENT),
57 "read_branches_global", "written %d\n", written_global);
60 static void test_perf_branches_common(int perf_fd,
61 void (*cb)(struct test_perf_branches *))
63 struct test_perf_branches *skel;
64 int err, i, duration = 0;
65 bool detached = false;
66 struct bpf_link *link;
70 skel = test_perf_branches__open_and_load();
71 if (CHECK(!skel, "test_perf_branches_load",
72 "perf_branches skeleton failed\n"))
75 /* attach perf_event */
76 link = bpf_program__attach_perf_event(skel->progs.perf_branches, perf_fd);
77 if (!ASSERT_OK_PTR(link, "attach_perf_event"))
78 goto out_destroy_skel;
80 /* generate some branches on cpu 0 */
83 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set);
84 if (CHECK(err, "set_affinity", "cpu #0, err %d\n", err))
86 /* spin the loop for a while (random high number) */
87 for (i = 0; i < 1000000; ++i)
90 test_perf_branches__detach(skel);
95 bpf_link__destroy(link);
98 test_perf_branches__detach(skel);
99 test_perf_branches__destroy(skel);
102 static void test_perf_branches_hw(void)
104 struct perf_event_attr attr = {0};
108 /* create perf event */
109 attr.size = sizeof(attr);
110 attr.type = PERF_TYPE_HARDWARE;
111 attr.config = PERF_COUNT_HW_CPU_CYCLES;
113 attr.sample_freq = 1000;
114 attr.sample_type = PERF_SAMPLE_BRANCH_STACK;
115 attr.branch_sample_type = PERF_SAMPLE_BRANCH_USER | PERF_SAMPLE_BRANCH_ANY;
116 pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
119 * Some setups don't support branch records (virtual machines, !x86),
120 * so skip test in this case.
123 if (errno == ENOENT || errno == EOPNOTSUPP) {
124 printf("%s:SKIP:no PERF_SAMPLE_BRANCH_STACK\n",
129 if (CHECK(pfd < 0, "perf_event_open", "err %d errno %d\n",
134 test_perf_branches_common(pfd, check_good_sample);
140 * Tests negative case -- run bpf_read_branch_records() on improperly configured
143 static void test_perf_branches_no_hw(void)
145 struct perf_event_attr attr = {0};
149 /* create perf event */
150 attr.size = sizeof(attr);
151 attr.type = PERF_TYPE_SOFTWARE;
152 attr.config = PERF_COUNT_SW_CPU_CLOCK;
154 attr.sample_freq = 1000;
155 pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
156 if (CHECK(pfd < 0, "perf_event_open", "err %d\n", pfd))
159 test_perf_branches_common(pfd, check_bad_sample);
164 void test_perf_branches(void)
166 if (test__start_subtest("perf_branches_hw"))
167 test_perf_branches_hw();
168 if (test__start_subtest("perf_branches_no_hw"))
169 test_perf_branches_no_hw();