~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/seccomp/seccomp_benchmark.c

Version: ~ [ linux-6.1-rc5 ] ~ [ linux-6.0.8 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.78 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.154 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.224 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.265 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.299 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.333 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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>
 16 
 17 #define ARRAY_SIZE(a)    (sizeof(a) / sizeof(a[0]))
 18 
 19 unsigned long long timing(clockid_t clk_id, unsigned long long samples)
 20 {
 21         pid_t pid, ret;
 22         unsigned long long i;
 23         struct timespec start, finish;
 24 
 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);
 30         }
 31         assert(clock_gettime(clk_id, &finish) == 0);
 32 
 33         i = finish.tv_sec - start.tv_sec;
 34         i *= 1000000000;
 35         i += finish.tv_nsec - start.tv_nsec;
 36 
 37         printf("%lu.%09lu - %lu.%09lu = %llu\n",
 38                 finish.tv_sec, finish.tv_nsec,
 39                 start.tv_sec, start.tv_nsec,
 40                 i);
 41 
 42         return i;
 43 }
 44 
 45 unsigned long long calibrate(void)
 46 {
 47         unsigned long long i;
 48 
 49         printf("Calibrating reasonable sample size...\n");
 50 
 51         for (i = 5; ; i++) {
 52                 unsigned long long samples = 1 << i;
 53 
 54                 /* Find something that takes more than 5 seconds to run. */
 55                 if (timing(CLOCK_REALTIME, samples) / 1000000000ULL > 5)
 56                         return samples;
 57         }
 58 }
 59 
 60 int main(int argc, char *argv[])
 61 {
 62         struct sock_filter filter[] = {
 63                 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
 64         };
 65         struct sock_fprog prog = {
 66                 .len = (unsigned short)ARRAY_SIZE(filter),
 67                 .filter = filter,
 68         };
 69         long ret;
 70         unsigned long long samples;
 71         unsigned long long native, filtered;
 72 
 73         if (argc > 1)
 74                 samples = strtoull(argv[1], NULL, 0);
 75         else
 76                 samples = calibrate();
 77 
 78         printf("Benchmarking %llu samples...\n", samples);
 79 
 80         native = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
 81         printf("getpid native: %llu ns\n", native);
 82 
 83         ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
 84         assert(ret == 0);
 85 
 86         ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
 87         assert(ret == 0);
 88 
 89         filtered = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
 90         printf("getpid RET_ALLOW: %llu ns\n", filtered);
 91 
 92         printf("Estimated seccomp overhead per syscall: %llu ns\n",
 93                 filtered - native);
 94 
 95         if (filtered == native)
 96                 printf("Trying running again with more samples.\n");
 97 
 98         return 0;
 99 }
100 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp