Skip to content

Commit b8d4f03

Browse files
ahjf-07Kernel Patches Daemon
authored andcommitted
selftests/bpf: perf_link: avoid failures in concurrent mode
perf_link creates a system-wide perf event pinned to CPU 0 (pid=-1, cpu=0) and also pins the test thread to CPU 0. Under concurrent selftests this can lead to cross-test interference and CPU 0 contention, making the test flaky. Create a per-task perf event instead (pid=0, cpu=-1) and drop CPU pinning from burn_cpu(). Use barrier() to prevent the burn loop from being optimized away. Drop the serial_ prefix so the test can run in parallel. Also remove the stale TODO comment. Tested: ./test_progs -t perf_link -vv ./test_progs -j$(nproc) -t perf_link -vv for i in $(seq 1 50); do ./test_progs -j$(nproc) -t perf_link; done Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>
1 parent 59120bd commit b8d4f03

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Copyright (c) 2021 Facebook */
33
#define _GNU_SOURCE
4-
#include <pthread.h>
5-
#include <sched.h>
4+
#include <linux/compiler.h>
65
#include <test_progs.h>
76
#include "testing_helpers.h"
87
#include "test_perf_link.skel.h"
@@ -12,23 +11,14 @@
1211

1312
static void burn_cpu(void)
1413
{
15-
volatile int j = 0;
16-
cpu_set_t cpu_set;
17-
int i, err;
18-
19-
/* generate some branches on cpu 0 */
20-
CPU_ZERO(&cpu_set);
21-
CPU_SET(0, &cpu_set);
22-
err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set);
23-
ASSERT_OK(err, "set_thread_affinity");
14+
int i;
2415

2516
/* spin the loop for a while (random high number) */
2617
for (i = 0; i < 1000000; ++i)
27-
++j;
18+
barrier();
2819
}
2920

30-
/* TODO: often fails in concurrent mode */
31-
void serial_test_perf_link(void)
21+
void test_perf_link(void)
3222
{
3323
struct test_perf_link *skel = NULL;
3424
struct perf_event_attr attr;
@@ -45,7 +35,7 @@ void serial_test_perf_link(void)
4535
attr.config = PERF_COUNT_SW_CPU_CLOCK;
4636
attr.freq = 1;
4737
attr.sample_freq = 1000;
48-
pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
38+
pfd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, PERF_FLAG_FD_CLOEXEC);
4939
if (!ASSERT_GE(pfd, 0, "perf_fd"))
5040
goto cleanup;
5141

0 commit comments

Comments
 (0)