xref: /freebsd/contrib/capsicum-test/overhead.cc (revision d6eb98610fa65663bf0df4574b7cb2c5c4ffda71)
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <time.h>
5 #include <unistd.h>
6 
7 #include "capsicum.h"
8 #include "syscalls.h"
9 #include "capsicum-test.h"
10 
11 #ifdef HAVE_SYSCALL
12 double RepeatSyscall(int count, int nr, long arg1, long arg2, long arg3) {
13   const clock_t t0 = clock(); // or gettimeofday or whatever
14   for (int ii = 0; ii < count; ii++) {
15     syscall(nr, arg1, arg2, arg3);
16   }
17   const clock_t t1 = clock();
18   return (t1 - t0) / (double)CLOCKS_PER_SEC;
19 }
20 
21 typedef int (*EntryFn)(void);
22 
23 double CompareSyscall(EntryFn entry_fn, int count, int nr,
24                       long arg1, long arg2, long arg3) {
25   double bare = RepeatSyscall(count, nr, arg1, arg2, arg3);
26   EXPECT_OK(entry_fn());
27   double capmode = RepeatSyscall(count, nr, arg1, arg2, arg3);
28   if (verbose) fprintf(stderr, "%d iterations bare=%fs capmode=%fs ratio=%.2f%%\n",
29                        count, bare, capmode, 100.0*capmode/bare);
30   if (bare==0.0) {
31     if (capmode==0.0) return 1.0;
32     return 999.0;
33   }
34   return capmode/bare;
35 }
36 
37 FORK_TEST(Overhead, GetTid) {
38   EXPECT_GT(10, CompareSyscall(&cap_enter, 10000, __NR_gettid, 0, 0, 0));
39 }
40 FORK_TEST(Overhead, Seek) {
41   int fd = open("/etc/passwd", O_RDONLY);
42   EXPECT_GT(50, CompareSyscall(&cap_enter, 10000, __NR_lseek, fd, 0, SEEK_SET));
43   close(fd);
44 }
45 #endif
46