Re: [PATCH bpf] selftests/bpf: Run test_perf_branches more than 1 time
From: Alexei Starovoitov
Date: Sun Apr 12 2026 - 18:39:47 EST
On Wed, Apr 8, 2026 at 2:57 AM Maya Matuszczyk
<mayamatuszczyk@xxxxxxxxxx> wrote:
>
> test_perf_branches bpf program was only being ran once, and
> perf_branches test would fail if the only sample that was passed to
> test_perf_branches didn't have branch data.
> Fix those failures by running it a few more times and waiting until a valid
> sample shows up, and leave the perf event only enabled for the duration of
> test spins.
>
> Signed-off-by: Maya Matuszczyk <mayamatuszczyk@xxxxxxxxxx>
> ---
> .../selftests/bpf/prog_tests/perf_branches.c | 57 +++++++++++++------
> .../selftests/bpf/progs/test_perf_branches.c | 11 ++--
> 2 files changed, 43 insertions(+), 25 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/perf_branches.c b/tools/testing/selftests/bpf/prog_tests/perf_branches.c
> index 0a7ef770c487..1dcb7c70f545 100644
> --- a/tools/testing/selftests/bpf/prog_tests/perf_branches.c
> +++ b/tools/testing/selftests/bpf/prog_tests/perf_branches.c
> @@ -15,10 +15,6 @@ static void check_good_sample(struct test_perf_branches *skel)
> int pbe_size = sizeof(struct perf_branch_entry);
> int duration = 0;
>
> - if (CHECK(!skel->bss->run_cnt, "invalid run_cnt",
> - "checked sample validity before prog run"))
> - return;
> -
> if (CHECK(!skel->bss->valid, "output not valid",
> "no valid sample from prog"))
> return;
> @@ -49,10 +45,6 @@ static void check_bad_sample(struct test_perf_branches *skel)
> int written_stack = skel->bss->written_stack_out;
> int duration = 0;
>
> - if (CHECK(!skel->bss->run_cnt, "invalid run_cnt",
> - "checked sample validity before prog run"))
> - return;
> -
> if (CHECK(!skel->bss->valid, "output not valid",
> "no valid sample from prog"))
> return;
> @@ -73,7 +65,6 @@ static void test_perf_branches_common(int perf_fd,
> bool detached = false;
> struct bpf_link *link;
> volatile int j = 0;
> - cpu_set_t cpu_set;
>
> skel = test_perf_branches__open_and_load();
> if (CHECK(!skel, "test_perf_branches_load",
> @@ -85,19 +76,35 @@ static void test_perf_branches_common(int perf_fd,
> if (!ASSERT_OK_PTR(link, "attach_perf_event"))
> goto out_destroy_skel;
>
> - /* generate some branches on cpu 0 */
> - CPU_ZERO(&cpu_set);
> - CPU_SET(0, &cpu_set);
> - err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set);
> - if (CHECK(err, "set_affinity", "cpu #0, err %d\n", err))
> + err = ioctl(perf_fd, PERF_EVENT_IOC_RESET, 0);
> + if (CHECK(err == -1, "perf event reset", "ioctl err %d", errno))
> goto out_destroy;
Since you're touching this part, please use ASSERT*() macros,
since CHECK() is deprecated.
sashiko thinks the same.