Makefile: libdir is not used anymore
[fio.git] / engines / posixaio.c
CommitLineData
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>
5f350952
JA
10
11#include "../fio.h"
12#include "../os.h"
2866c82d 13
34cfcdaf
JA
14#ifdef FIO_HAVE_POSIXAIO
15
2866c82d
JA
16struct posixaio_data {
17 struct io_u **aio_events;
18};
19
20static 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
31static 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
7a16dd02
JA
51static int fio_posixaio_cancel(struct thread_data fio_unused *td,
52 struct io_u *io_u)
2866c82d 53{
53cdc686
JA
54 struct fio_file *f = io_u->file;
55 int r = aio_cancel(f->fd, &io_u->aiocb);
2866c82d
JA
56
57 if (r == 1 || r == AIO_CANCELED)
58 return 0;
59
60 return 1;
61}
62
7a16dd02
JA
63static int fio_posixaio_prep(struct thread_data fio_unused *td,
64 struct io_u *io_u)
2866c82d
JA
65{
66 struct aiocb *aiocb = &io_u->aiocb;
53cdc686 67 struct fio_file *f = io_u->file;
2866c82d 68
53cdc686 69 aiocb->aio_fildes = f->fd;
cec6b55d
JA
70 aiocb->aio_buf = io_u->xfer_buf;
71 aiocb->aio_nbytes = io_u->xfer_buflen;
2866c82d
JA
72 aiocb->aio_offset = io_u->offset;
73
74 io_u->seen = 0;
75 return 0;
76}
77
78static 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;
90restart:
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 case 0:
104 pd->aio_events[r++] = io_u;
105 io_u->seen = 1;
106 break;
107 case EINPROGRESS:
108 break;
109 }
110
111 if (r >= max)
112 break;
113 }
114
115 if (r >= min)
116 return r;
117
118 if (have_timeout) {
119 unsigned long long usec;
120
121 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
122 if (ts_utime_since_now(&start) > usec)
123 return r;
124 }
125
126 /*
127 * hrmpf, we need to wait for more. we should use aio_suspend, for
128 * now just sleep a little and recheck status of busy-and-not-seen
129 */
130 usleep(1000);
131 goto restart;
132}
133
134static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
135{
136 struct posixaio_data *pd = td->io_ops->data;
137
138 return pd->aio_events[event];
139}
140
141static int fio_posixaio_queue(struct thread_data fio_unused *td,
142 struct io_u *io_u)
143{
144 struct aiocb *aiocb = &io_u->aiocb;
145 int ret;
146
147 if (io_u->ddir == DDIR_READ)
148 ret = aio_read(aiocb);
87dc1ab1 149 else if (io_u->ddir == DDIR_WRITE)
2866c82d 150 ret = aio_write(aiocb);
87dc1ab1
JA
151 else
152 ret = aio_fsync(O_SYNC, aiocb);
2866c82d 153
95bcd815 154 if (ret) {
2866c82d 155 io_u->error = errno;
95bcd815 156 td_verror(td, io_u->error);
36167d82 157 return FIO_Q_COMPLETED;
95bcd815 158 }
36167d82
JA
159
160 return FIO_Q_QUEUED;
2866c82d
JA
161}
162
163static void fio_posixaio_cleanup(struct thread_data *td)
164{
165 struct posixaio_data *pd = td->io_ops->data;
166
167 if (pd) {
168 free(pd->aio_events);
169 free(pd);
170 td->io_ops->data = NULL;
171 }
172}
173
174static int fio_posixaio_init(struct thread_data *td)
175{
176 struct posixaio_data *pd = malloc(sizeof(*pd));
177
cb781c75 178 memset(pd, 0, sizeof(*pd));
2866c82d 179 pd->aio_events = malloc(td->iodepth * sizeof(struct io_u *));
cb781c75 180 memset(pd->aio_events, 0, td->iodepth * sizeof(struct io_u *));
2866c82d
JA
181
182 td->io_ops->data = pd;
183 return 0;
184}
185
5f350952 186static struct ioengine_ops ioengine = {
2866c82d
JA
187 .name = "posixaio",
188 .version = FIO_IOOPS_VERSION,
189 .init = fio_posixaio_init,
190 .prep = fio_posixaio_prep,
191 .queue = fio_posixaio_queue,
192 .cancel = fio_posixaio_cancel,
193 .getevents = fio_posixaio_getevents,
194 .event = fio_posixaio_event,
195 .cleanup = fio_posixaio_cleanup,
2866c82d 196};
34cfcdaf
JA
197
198#else /* FIO_HAVE_POSIXAIO */
199
200/*
201 * When we have a proper configure system in place, we simply wont build
202 * and install this io engine. For now install a crippled version that
203 * just complains and fails to load.
204 */
205static int fio_posixaio_init(struct thread_data fio_unused *td)
206{
207 fprintf(stderr, "fio: posixaio not available\n");
208 return 1;
209}
210
5f350952 211static struct ioengine_ops ioengine = {
34cfcdaf
JA
212 .name = "posixaio",
213 .version = FIO_IOOPS_VERSION,
214 .init = fio_posixaio_init,
215};
216
217#endif
5f350952
JA
218
219static void fio_init fio_posixaio_register(void)
220{
221 register_ioengine(&ioengine);
222}
223
224static void fio_exit fio_posixaio_unregister(void)
225{
226 unregister_ioengine(&ioengine);
227}