Skip to content

Commit 55e6866

Browse files
committed
selftests/seccomp: Pin benchmark to single CPU
The seccomp benchmark test (for validating the benefit of bitmaps) can be sensitive to scheduling speed, so pin the process to a single CPU, which appears to significantly improve reliability, and loosen the "close enough" checking to allow up to 10% variance instead of 1%. Reported-by: kernel test robot <[email protected]> Closes: https://lore.kernel.org/oe-lkp/[email protected] Cc: Andy Lutomirski <[email protected]> Cc: Will Drewry <[email protected]> Reviewed-by: Mark Brown <[email protected]> Signed-off-by: Kees Cook <[email protected]>
1 parent 8e3c9f9 commit 55e6866

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

tools/testing/selftests/seccomp/seccomp_benchmark.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55
#define _GNU_SOURCE
66
#include <assert.h>
7+
#include <err.h>
78
#include <limits.h>
9+
#include <sched.h>
810
#include <stdbool.h>
911
#include <stddef.h>
1012
#include <stdio.h>
@@ -76,8 +78,12 @@ unsigned long long calibrate(void)
7678

7779
bool approx(int i_one, int i_two)
7880
{
79-
double one = i_one, one_bump = one * 0.01;
80-
double two = i_two, two_bump = two * 0.01;
81+
/*
82+
* This continues to be a noisy test. Instead of a 1% comparison
83+
* go with 10%.
84+
*/
85+
double one = i_one, one_bump = one * 0.1;
86+
double two = i_two, two_bump = two * 0.1;
8187

8288
one_bump = one + MAX(one_bump, 2.0);
8389
two_bump = two + MAX(two_bump, 2.0);
@@ -119,6 +125,32 @@ long compare(const char *name_one, const char *name_eval, const char *name_two,
119125
return good ? 0 : 1;
120126
}
121127

128+
/* Pin to a single CPU so the benchmark won't bounce around the system. */
129+
void affinity(void)
130+
{
131+
long cpu;
132+
ulong ncores = sysconf(_SC_NPROCESSORS_CONF);
133+
cpu_set_t *setp = CPU_ALLOC(ncores);
134+
ulong setsz = CPU_ALLOC_SIZE(ncores);
135+
136+
/*
137+
* Totally unscientific way to avoid CPUs that might be busier:
138+
* choose the highest CPU instead of the lowest.
139+
*/
140+
for (cpu = ncores - 1; cpu >= 0; cpu--) {
141+
CPU_ZERO_S(setsz, setp);
142+
CPU_SET_S(cpu, setsz, setp);
143+
if (sched_setaffinity(getpid(), setsz, setp) == -1)
144+
continue;
145+
printf("Pinned to CPU %lu of %lu\n", cpu + 1, ncores);
146+
goto out;
147+
}
148+
fprintf(stderr, "Could not set CPU affinity -- calibration may not work well");
149+
150+
out:
151+
CPU_FREE(setp);
152+
}
153+
122154
int main(int argc, char *argv[])
123155
{
124156
struct sock_filter bitmap_filter[] = {
@@ -153,6 +185,8 @@ int main(int argc, char *argv[])
153185
system("grep -H . /proc/sys/net/core/bpf_jit_enable");
154186
system("grep -H . /proc/sys/net/core/bpf_jit_harden");
155187

188+
affinity();
189+
156190
if (argc > 1)
157191
samples = strtoull(argv[1], NULL, 0);
158192
else

0 commit comments

Comments
 (0)