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