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