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