Extend --readonly
[fio.git] / engines / posixaio.c
1 /*
2  * posixaio engine
3  *
4  * IO engine that uses the posix defined aio interface.
5  *
6  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <assert.h>
12
13 #include "../fio.h"
14
15 #ifdef FIO_HAVE_POSIXAIO
16
17 struct posixaio_data {
18         struct io_u **aio_events;
19 };
20
21 static int fill_timespec(struct timespec *ts)
22 {
23 #ifdef _POSIX_TIMERS
24         if (!clock_gettime(CLOCK_MONOTONIC, ts))
25                 return 0;
26
27         perror("clock_gettime");
28 #endif
29         return 1;
30 }
31
32 static unsigned long long ts_utime_since_now(struct timespec *t)
33 {
34         long long sec, nsec;
35         struct timespec now;
36
37         if (fill_timespec(&now))
38                 return 0;
39         
40         sec = now.tv_sec - t->tv_sec;
41         nsec = now.tv_nsec - t->tv_nsec;
42         if (sec > 0 && nsec < 0) {
43                 sec--;
44                 nsec += 1000000000;
45         }
46
47         sec *= 1000000;
48         nsec /= 1000;
49         return sec + nsec;
50 }
51
52 static int fio_posixaio_cancel(struct thread_data fio_unused *td,
53                                struct io_u *io_u)
54 {
55         struct fio_file *f = io_u->file;
56         int r = aio_cancel(f->fd, &io_u->aiocb);
57
58         if (r == 1 || r == AIO_CANCELED)
59                 return 0;
60
61         return 1;
62 }
63
64 static int fio_posixaio_prep(struct thread_data fio_unused *td,
65                              struct io_u *io_u)
66 {
67         struct aiocb *aiocb = &io_u->aiocb;
68         struct fio_file *f = io_u->file;
69
70         aiocb->aio_fildes = f->fd;
71         aiocb->aio_buf = io_u->xfer_buf;
72         aiocb->aio_nbytes = io_u->xfer_buflen;
73         aiocb->aio_offset = io_u->offset;
74
75         io_u->seen = 0;
76         return 0;
77 }
78
79 static int fio_posixaio_getevents(struct thread_data *td, int min, int max,
80                                   struct timespec *t)
81 {
82         struct posixaio_data *pd = td->io_ops->data;
83         struct list_head *entry;
84         struct timespec start;
85         int r, have_timeout = 0;
86
87         if (t && !fill_timespec(&start))
88                 have_timeout = 1;
89
90         r = 0;
91 restart:
92         list_for_each(entry, &td->io_u_busylist) {
93                 struct io_u *io_u = list_entry(entry, struct io_u, list);
94                 int err;
95
96                 if (io_u->seen)
97                         continue;
98
99                 err = aio_error(&io_u->aiocb);
100                 if (err == EINPROGRESS)
101                         continue;
102
103                 io_u->seen = 1;
104                 pd->aio_events[r++] = io_u;
105
106                 if (err == ECANCELED)
107                         io_u->resid = io_u->xfer_buflen;
108                 else if (!err) {
109                         ssize_t retval = aio_return(&io_u->aiocb);
110
111                         io_u->resid = io_u->xfer_buflen - retval;
112                 } else
113                         io_u->error = err;
114
115                 if (r >= max)
116                         break;
117         }
118
119         if (r >= min)
120                 return r;
121
122         if (have_timeout) {
123                 unsigned long long usec;
124
125                 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
126                 if (ts_utime_since_now(&start) > usec)
127                         return r;
128         }
129
130         /*
131          * hrmpf, we need to wait for more. we should use aio_suspend, for
132          * now just sleep a little and recheck status of busy-and-not-seen
133          */
134         usleep(1000);
135         goto restart;
136 }
137
138 static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
139 {
140         struct posixaio_data *pd = td->io_ops->data;
141
142         return pd->aio_events[event];
143 }
144
145 static int fio_posixaio_queue(struct thread_data fio_unused *td,
146                               struct io_u *io_u)
147 {
148         struct aiocb *aiocb = &io_u->aiocb;
149         int ret;
150
151         if (io_u->ddir == DDIR_READ)
152                 ret = aio_read(aiocb);
153         else if (io_u->ddir == DDIR_WRITE)
154                 ret = aio_write(aiocb);
155         else
156                 ret = aio_fsync(O_SYNC, aiocb);
157
158         if (ret) {
159                 io_u->error = errno;
160                 td_verror(td, io_u->error, "xfer");
161                 return FIO_Q_COMPLETED;
162         }
163
164         return FIO_Q_QUEUED;
165 }
166
167 static void fio_posixaio_cleanup(struct thread_data *td)
168 {
169         struct posixaio_data *pd = td->io_ops->data;
170
171         if (pd) {
172                 free(pd->aio_events);
173                 free(pd);
174                 td->io_ops->data = NULL;
175         }
176 }
177
178 static int fio_posixaio_init(struct thread_data *td)
179 {
180         struct posixaio_data *pd = malloc(sizeof(*pd));
181
182         memset(pd, 0, sizeof(*pd));
183         pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
184         memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
185
186         td->io_ops->data = pd;
187         return 0;
188 }
189
190 static struct ioengine_ops ioengine = {
191         .name           = "posixaio",
192         .version        = FIO_IOOPS_VERSION,
193         .init           = fio_posixaio_init,
194         .prep           = fio_posixaio_prep,
195         .queue          = fio_posixaio_queue,
196         .cancel         = fio_posixaio_cancel,
197         .getevents      = fio_posixaio_getevents,
198         .event          = fio_posixaio_event,
199         .cleanup        = fio_posixaio_cleanup,
200         .open_file      = generic_open_file,
201         .close_file     = generic_close_file,
202 };
203
204 #else /* FIO_HAVE_POSIXAIO */
205
206 /*
207  * When we have a proper configure system in place, we simply wont build
208  * and install this io engine. For now install a crippled version that
209  * just complains and fails to load.
210  */
211 static int fio_posixaio_init(struct thread_data fio_unused *td)
212 {
213         fprintf(stderr, "fio: posixaio not available\n");
214         return 1;
215 }
216
217 static struct ioengine_ops ioengine = {
218         .name           = "posixaio",
219         .version        = FIO_IOOPS_VERSION,
220         .init           = fio_posixaio_init,
221 };
222
223 #endif
224
225 static void fio_init fio_posixaio_register(void)
226 {
227         register_ioengine(&ioengine);
228 }
229
230 static void fio_exit fio_posixaio_unregister(void)
231 {
232         unregister_ioengine(&ioengine);
233 }