Skip to content

Commit 81a0c8b

Browse files
committed
selftests/seccomp: Improve calibration loop
The seccomp benchmark calibration loop did not need to take so long. Instead, use a simple 1 second timeout and multiply up to target. It does not need to be accurate. Signed-off-by: Kees Cook <[email protected]>
1 parent bc32c9c commit 81a0c8b

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

tools/testing/selftests/seccomp/seccomp_benchmark.c

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
unsigned long long timing(clockid_t clk_id, unsigned long long samples)
2020
{
21-
pid_t pid, ret;
22-
unsigned long long i;
2321
struct timespec start, finish;
22+
unsigned long long i;
23+
pid_t pid, ret;
2424

2525
pid = getpid();
2626
assert(clock_gettime(clk_id, &start) == 0);
@@ -31,30 +31,43 @@ unsigned long long timing(clockid_t clk_id, unsigned long long samples)
3131
assert(clock_gettime(clk_id, &finish) == 0);
3232

3333
i = finish.tv_sec - start.tv_sec;
34-
i *= 1000000000;
34+
i *= 1000000000ULL;
3535
i += finish.tv_nsec - start.tv_nsec;
3636

37-
printf("%lu.%09lu - %lu.%09lu = %llu\n",
37+
printf("%lu.%09lu - %lu.%09lu = %llu (%.1fs)\n",
3838
finish.tv_sec, finish.tv_nsec,
3939
start.tv_sec, start.tv_nsec,
40-
i);
40+
i, (double)i / 1000000000.0);
4141

4242
return i;
4343
}
4444

4545
unsigned long long calibrate(void)
4646
{
47-
unsigned long long i;
48-
49-
printf("Calibrating reasonable sample size...\n");
47+
struct timespec start, finish;
48+
unsigned long long i, samples, step = 9973;
49+
pid_t pid, ret;
50+
int seconds = 15;
5051

51-
for (i = 5; ; i++) {
52-
unsigned long long samples = 1 << i;
52+
printf("Calibrating sample size for %d seconds worth of syscalls ...\n", seconds);
5353

54-
/* Find something that takes more than 5 seconds to run. */
55-
if (timing(CLOCK_REALTIME, samples) / 1000000000ULL > 5)
56-
return samples;
57-
}
54+
samples = 0;
55+
pid = getpid();
56+
assert(clock_gettime(CLOCK_MONOTONIC, &start) == 0);
57+
do {
58+
for (i = 0; i < step; i++) {
59+
ret = syscall(__NR_getpid);
60+
assert(pid == ret);
61+
}
62+
assert(clock_gettime(CLOCK_MONOTONIC, &finish) == 0);
63+
64+
samples += step;
65+
i = finish.tv_sec - start.tv_sec;
66+
i *= 1000000000ULL;
67+
i += finish.tv_nsec - start.tv_nsec;
68+
} while (i < 1000000000ULL);
69+
70+
return samples * seconds;
5871
}
5972

6073
int main(int argc, char *argv[])
@@ -70,15 +83,16 @@ int main(int argc, char *argv[])
7083
unsigned long long samples;
7184
unsigned long long native, filter1, filter2;
7285

86+
printf("Current BPF sysctl settings:\n");
87+
system("sysctl net.core.bpf_jit_enable");
88+
system("sysctl net.core.bpf_jit_harden");
89+
7390
if (argc > 1)
7491
samples = strtoull(argv[1], NULL, 0);
7592
else
7693
samples = calibrate();
7794

78-
printf("Current BPF sysctl settings:\n");
79-
system("sysctl net.core.bpf_jit_enable");
80-
system("sysctl net.core.bpf_jit_harden");
81-
printf("Benchmarking %llu samples...\n", samples);
95+
printf("Benchmarking %llu syscalls...\n", samples);
8296

8397
/* Native call */
8498
native = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;

0 commit comments

Comments
 (0)