[PATCH] Make fio -W clean again
[fio.git] / engines / fio-engine-posixaio.c
... / ...
CommitLineData
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
13struct posixaio_data {
14 struct io_u **aio_events;
15};
16
17static 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
28static 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
48static int fio_posixaio_sync(struct thread_data *td, struct fio_file *f)
49{
50 return fsync(f->fd);
51}
52
53static int fio_posixaio_cancel(struct thread_data *td, struct io_u *io_u)
54{
55 struct fio_file *f = io_u->file;
56 int r = aio_cancel(f->fd, &io_u->aiocb);
57
58 if (r == 1 || r == AIO_CANCELED)
59 return 0;
60
61 return 1;
62}
63
64static int fio_posixaio_prep(struct thread_data *td, 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->buf;
71 aiocb->aio_nbytes = io_u->buflen;
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);
149 else
150 ret = aio_write(aiocb);
151
152 if (ret)
153 io_u->error = errno;
154
155 return io_u->error;
156}
157
158static void fio_posixaio_cleanup(struct thread_data *td)
159{
160 struct posixaio_data *pd = td->io_ops->data;
161
162 if (pd) {
163 free(pd->aio_events);
164 free(pd);
165 td->io_ops->data = NULL;
166 }
167}
168
169static int fio_posixaio_init(struct thread_data *td)
170{
171 struct posixaio_data *pd = malloc(sizeof(*pd));
172
173 pd->aio_events = malloc(td->iodepth * sizeof(struct io_u *));
174
175 td->io_ops->data = pd;
176 return 0;
177}
178
179struct ioengine_ops ioengine = {
180 .name = "posixaio",
181 .version = FIO_IOOPS_VERSION,
182 .init = fio_posixaio_init,
183 .prep = fio_posixaio_prep,
184 .queue = fio_posixaio_queue,
185 .cancel = fio_posixaio_cancel,
186 .getevents = fio_posixaio_getevents,
187 .event = fio_posixaio_event,
188 .cleanup = fio_posixaio_cleanup,
189 .sync = fio_posixaio_sync,
190};