Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / seccomp / seccomp_benchmark.c
blob5838c8697ec3256e434e07fefd9d1eb6d420d252
1 /*
2 * Strictly speaking, this is not a test. But it can report during test
3 * runs so relative performace can be measured.
4 */
5 #define _GNU_SOURCE
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <time.h>
10 #include <unistd.h>
11 #include <linux/filter.h>
12 #include <linux/seccomp.h>
13 #include <sys/prctl.h>
14 #include <sys/syscall.h>
15 #include <sys/types.h>
17 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
19 unsigned long long timing(clockid_t clk_id, unsigned long long samples)
21 pid_t pid, ret;
22 unsigned long long i;
23 struct timespec start, finish;
25 pid = getpid();
26 assert(clock_gettime(clk_id, &start) == 0);
27 for (i = 0; i < samples; i++) {
28 ret = syscall(__NR_getpid);
29 assert(pid == ret);
31 assert(clock_gettime(clk_id, &finish) == 0);
33 i = finish.tv_sec - start.tv_sec;
34 i *= 1000000000;
35 i += finish.tv_nsec - start.tv_nsec;
37 printf("%lu.%09lu - %lu.%09lu = %llu\n",
38 finish.tv_sec, finish.tv_nsec,
39 start.tv_sec, start.tv_nsec,
40 i);
42 return i;
45 unsigned long long calibrate(void)
47 unsigned long long i;
49 printf("Calibrating reasonable sample size...\n");
51 for (i = 5; ; i++) {
52 unsigned long long samples = 1 << i;
54 /* Find something that takes more than 5 seconds to run. */
55 if (timing(CLOCK_REALTIME, samples) / 1000000000ULL > 5)
56 return samples;
60 int main(int argc, char *argv[])
62 struct sock_filter filter[] = {
63 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
65 struct sock_fprog prog = {
66 .len = (unsigned short)ARRAY_SIZE(filter),
67 .filter = filter,
69 long ret;
70 unsigned long long samples;
71 unsigned long long native, filtered;
73 if (argc > 1)
74 samples = strtoull(argv[1], NULL, 0);
75 else
76 samples = calibrate();
78 printf("Benchmarking %llu samples...\n", samples);
80 native = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
81 printf("getpid native: %llu ns\n", native);
83 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
84 assert(ret == 0);
86 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
87 assert(ret == 0);
89 filtered = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
90 printf("getpid RET_ALLOW: %llu ns\n", filtered);
92 printf("Estimated seccomp overhead per syscall: %llu ns\n",
93 filtered - native);
95 if (filtered == native)
96 printf("Trying running again with more samples.\n");
98 return 0;