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