don't set page size by default
[tridge/junkcode.git] / aio_open2.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sched.h>
4 #include <unistd.h>
5 #include <sys/time.h>
6 #include <time.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <sys/wait.h>
12
13 static struct timeval tp1,tp2;
14
15 static void start_timer()
16 {
17         gettimeofday(&tp1,NULL);
18 }
19
20 static double end_timer()
21 {
22         gettimeofday(&tp2,NULL);
23         return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
24                 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
25 }
26
27 struct aio_open_args {
28         const char *fname;
29         int flags;
30         mode_t mode;
31         int error;
32         int ret;
33 };
34
35 static int aio_pipe[2];
36
37 static int aio_open_child(void *ptr)
38 {
39         struct aio_open_args *args = ptr;
40         args->ret = open(args->fname, args->flags, args->mode);
41         args->error = errno;
42         write(aio_pipe[1], &ptr, sizeof(ptr));
43         return 0;
44 }
45
46 static int aio_open(const char *fname, int flags, mode_t mode)
47 {
48         static char stack[8192];
49         struct aio_open_args *args, *a2;
50         pid_t pid;
51         int ret, status;
52
53         args = malloc(sizeof(*args));
54         args->fname = fname;
55         args->flags = flags;
56         args->mode  = mode;
57
58         pid = clone(aio_open_child, stack+sizeof(stack)-32, 
59                     CLONE_FS|CLONE_FILES|CLONE_VM, 
60                     args);
61         
62         if (read(aio_pipe[0], &a2, sizeof(a2)) != sizeof(a2)) {
63                 errno = EIO;
64                 return -1;
65         }
66
67         waitpid(pid, &status, __WCLONE);
68
69 //      printf("args=%p a2=%p\n", args, a2);
70 //      fflush(stdout);
71
72         ret = args->ret;
73         if (ret == -1) {
74                 errno = args->error;
75         }
76         free(args);
77
78         return ret;
79 }
80
81
82 static void run_test(const char *name, int (*fn)(const char *, int, mode_t))
83 {
84         const char *fname = "test.dat";
85         const int timeout=3;
86         int count=0;
87
88         pipe(aio_pipe);
89
90         start_timer();
91         while (end_timer() < timeout) {
92                 int fd = fn(fname, O_CREAT|O_TRUNC|O_RDWR, 0600);
93                 if (fd == -1) {
94                         perror(fname);
95                         exit(1);
96                 }
97                 close(fd);
98 //              unlink(fname);
99                 count++;
100         }
101         printf("%.1f ops/sec (%s)\n", count/end_timer(), name);
102
103         close(aio_pipe[0]);
104         close(aio_pipe[1]);
105 }
106
107 int main(void)
108 {
109         run_test("aio_open", aio_open);
110         run_test("open", open);
111         run_test("aio_open", aio_open);
112         run_test("open", open);
113
114         return 0;
115 }