[PATCH] Split out the memory handling from fio.c
[fio.git] / engines / fio-engine-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 #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
48 static int fio_posixaio_sync(struct thread_data fio_unused *td,
49                              struct fio_file *f)
50 {
51         return fsync(f->fd);
52 }
53
54 static int fio_posixaio_cancel(struct thread_data fio_unused *td,
55                                struct io_u *io_u)
56 {
57         struct fio_file *f = io_u->file;
58         int r = aio_cancel(f->fd, &io_u->aiocb);
59
60         if (r == 1 || r == AIO_CANCELED)
61                 return 0;
62
63         return 1;
64 }
65
66 static int fio_posixaio_prep(struct thread_data fio_unused *td,
67                              struct io_u *io_u)
68 {
69         struct aiocb *aiocb = &io_u->aiocb;
70         struct fio_file *f = io_u->file;
71
72         aiocb->aio_fildes = f->fd;
73         aiocb->aio_buf = io_u->buf;
74         aiocb->aio_nbytes = io_u->buflen;
75         aiocb->aio_offset = io_u->offset;
76
77         io_u->seen = 0;
78         return 0;
79 }
80
81 static int fio_posixaio_getevents(struct thread_data *td, int min, int max,
82                                   struct timespec *t)
83 {
84         struct posixaio_data *pd = td->io_ops->data;
85         struct list_head *entry;
86         struct timespec start;
87         int r, have_timeout = 0;
88
89         if (t && !fill_timespec(&start))
90                 have_timeout = 1;
91
92         r = 0;
93 restart:
94         list_for_each(entry, &td->io_u_busylist) {
95                 struct io_u *io_u = list_entry(entry, struct io_u, list);
96                 int err;
97
98                 if (io_u->seen)
99                         continue;
100
101                 err = aio_error(&io_u->aiocb);
102                 switch (err) {
103                         default:
104                                 io_u->error = err;
105                         case ECANCELED:
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
153                 ret = aio_write(aiocb);
154
155         if (ret)
156                 io_u->error = errno;
157                 
158         return io_u->error;
159 }
160
161 static void fio_posixaio_cleanup(struct thread_data *td)
162 {
163         struct posixaio_data *pd = td->io_ops->data;
164
165         if (pd) {
166                 free(pd->aio_events);
167                 free(pd);
168                 td->io_ops->data = NULL;
169         }
170 }
171
172 static int fio_posixaio_init(struct thread_data *td)
173 {
174         struct posixaio_data *pd = malloc(sizeof(*pd));
175
176         pd->aio_events = malloc(td->iodepth * sizeof(struct io_u *));
177
178         td->io_ops->data = pd;
179         return 0;
180 }
181
182 struct ioengine_ops ioengine = {
183         .name           = "posixaio",
184         .version        = FIO_IOOPS_VERSION,
185         .init           = fio_posixaio_init,
186         .prep           = fio_posixaio_prep,
187         .queue          = fio_posixaio_queue,
188         .cancel         = fio_posixaio_cancel,
189         .getevents      = fio_posixaio_getevents,
190         .event          = fio_posixaio_event,
191         .cleanup        = fio_posixaio_cleanup,
192         .sync           = fio_posixaio_sync,
193 };