Commit | Line | Data |
---|---|---|
2866c82d JA |
1 | /* |
2 | * posix aio io engine | |
3 | * | |
4 | */ | |
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | #include <unistd.h> | |
8 | #include <errno.h> | |
9 | #include <assert.h> | |
10 | #include "fio.h" | |
11 | #include "os.h" | |
12 | ||
13 | struct posixaio_data { | |
14 | struct io_u **aio_events; | |
15 | }; | |
16 | ||
17 | static int fill_timespec(struct timespec *ts) | |
18 | { | |
19 | #ifdef _POSIX_TIMERS | |
20 | if (!clock_gettime(CLOCK_MONOTONIC, ts)) | |
21 | return 0; | |
22 | ||
23 | perror("clock_gettime"); | |
24 | #endif | |
25 | return 1; | |
26 | } | |
27 | ||
28 | static unsigned long long ts_utime_since_now(struct timespec *t) | |
29 | { | |
30 | long long sec, nsec; | |
31 | struct timespec now; | |
32 | ||
33 | if (fill_timespec(&now)) | |
34 | return 0; | |
35 | ||
36 | sec = now.tv_sec - t->tv_sec; | |
37 | nsec = now.tv_nsec - t->tv_nsec; | |
38 | if (sec > 0 && nsec < 0) { | |
39 | sec--; | |
40 | nsec += 1000000000; | |
41 | } | |
42 | ||
43 | sec *= 1000000; | |
44 | nsec /= 1000; | |
45 | return sec + nsec; | |
46 | } | |
47 | ||
7a16dd02 JA |
48 | static int fio_posixaio_cancel(struct thread_data fio_unused *td, |
49 | struct io_u *io_u) | |
2866c82d | 50 | { |
53cdc686 JA |
51 | struct fio_file *f = io_u->file; |
52 | int r = aio_cancel(f->fd, &io_u->aiocb); | |
2866c82d JA |
53 | |
54 | if (r == 1 || r == AIO_CANCELED) | |
55 | return 0; | |
56 | ||
57 | return 1; | |
58 | } | |
59 | ||
7a16dd02 JA |
60 | static int fio_posixaio_prep(struct thread_data fio_unused *td, |
61 | struct io_u *io_u) | |
2866c82d JA |
62 | { |
63 | struct aiocb *aiocb = &io_u->aiocb; | |
53cdc686 | 64 | struct fio_file *f = io_u->file; |
2866c82d | 65 | |
53cdc686 | 66 | aiocb->aio_fildes = f->fd; |
2866c82d JA |
67 | aiocb->aio_buf = io_u->buf; |
68 | aiocb->aio_nbytes = io_u->buflen; | |
69 | aiocb->aio_offset = io_u->offset; | |
70 | ||
71 | io_u->seen = 0; | |
72 | return 0; | |
73 | } | |
74 | ||
75 | static int fio_posixaio_getevents(struct thread_data *td, int min, int max, | |
76 | struct timespec *t) | |
77 | { | |
78 | struct posixaio_data *pd = td->io_ops->data; | |
79 | struct list_head *entry; | |
80 | struct timespec start; | |
81 | int r, have_timeout = 0; | |
82 | ||
83 | if (t && !fill_timespec(&start)) | |
84 | have_timeout = 1; | |
85 | ||
86 | r = 0; | |
87 | restart: | |
88 | list_for_each(entry, &td->io_u_busylist) { | |
89 | struct io_u *io_u = list_entry(entry, struct io_u, list); | |
90 | int err; | |
91 | ||
92 | if (io_u->seen) | |
93 | continue; | |
94 | ||
95 | err = aio_error(&io_u->aiocb); | |
96 | switch (err) { | |
97 | default: | |
98 | io_u->error = err; | |
99 | case ECANCELED: | |
100 | case 0: | |
101 | pd->aio_events[r++] = io_u; | |
102 | io_u->seen = 1; | |
103 | break; | |
104 | case EINPROGRESS: | |
105 | break; | |
106 | } | |
107 | ||
108 | if (r >= max) | |
109 | break; | |
110 | } | |
111 | ||
112 | if (r >= min) | |
113 | return r; | |
114 | ||
115 | if (have_timeout) { | |
116 | unsigned long long usec; | |
117 | ||
118 | usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000); | |
119 | if (ts_utime_since_now(&start) > usec) | |
120 | return r; | |
121 | } | |
122 | ||
123 | /* | |
124 | * hrmpf, we need to wait for more. we should use aio_suspend, for | |
125 | * now just sleep a little and recheck status of busy-and-not-seen | |
126 | */ | |
127 | usleep(1000); | |
128 | goto restart; | |
129 | } | |
130 | ||
131 | static struct io_u *fio_posixaio_event(struct thread_data *td, int event) | |
132 | { | |
133 | struct posixaio_data *pd = td->io_ops->data; | |
134 | ||
135 | return pd->aio_events[event]; | |
136 | } | |
137 | ||
138 | static int fio_posixaio_queue(struct thread_data fio_unused *td, | |
139 | struct io_u *io_u) | |
140 | { | |
141 | struct aiocb *aiocb = &io_u->aiocb; | |
142 | int ret; | |
143 | ||
144 | if (io_u->ddir == DDIR_READ) | |
145 | ret = aio_read(aiocb); | |
87dc1ab1 | 146 | else if (io_u->ddir == DDIR_WRITE) |
2866c82d | 147 | ret = aio_write(aiocb); |
87dc1ab1 JA |
148 | else |
149 | ret = aio_fsync(O_SYNC, aiocb); | |
2866c82d JA |
150 | |
151 | if (ret) | |
152 | io_u->error = errno; | |
153 | ||
154 | return io_u->error; | |
155 | } | |
156 | ||
157 | static void fio_posixaio_cleanup(struct thread_data *td) | |
158 | { | |
159 | struct posixaio_data *pd = td->io_ops->data; | |
160 | ||
161 | if (pd) { | |
162 | free(pd->aio_events); | |
163 | free(pd); | |
164 | td->io_ops->data = NULL; | |
165 | } | |
166 | } | |
167 | ||
168 | static int fio_posixaio_init(struct thread_data *td) | |
169 | { | |
170 | struct posixaio_data *pd = malloc(sizeof(*pd)); | |
171 | ||
172 | pd->aio_events = malloc(td->iodepth * sizeof(struct io_u *)); | |
173 | ||
174 | td->io_ops->data = pd; | |
175 | return 0; | |
176 | } | |
177 | ||
178 | struct ioengine_ops ioengine = { | |
179 | .name = "posixaio", | |
180 | .version = FIO_IOOPS_VERSION, | |
181 | .init = fio_posixaio_init, | |
182 | .prep = fio_posixaio_prep, | |
183 | .queue = fio_posixaio_queue, | |
184 | .cancel = fio_posixaio_cancel, | |
185 | .getevents = fio_posixaio_getevents, | |
186 | .event = fio_posixaio_event, | |
187 | .cleanup = fio_posixaio_cleanup, | |
2866c82d | 188 | }; |