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