[PATCH] engines/Makefile should use -g as well
[fio.git] / engines / fio-engine-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>
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)
49{
50 return fsync(td->fd);
51}
52
53static int fio_posixaio_cancel(struct thread_data *td, struct io_u *io_u)
54{
55 int r = aio_cancel(td->fd, &io_u->aiocb);
56
57 if (r == 1 || r == AIO_CANCELED)
58 return 0;
59
60 return 1;
61}
62
63static int fio_posixaio_prep(struct thread_data *td, struct io_u *io_u)
64{
65 struct aiocb *aiocb = &io_u->aiocb;
66
67 aiocb->aio_fildes = td->fd;
68 aiocb->aio_buf = io_u->buf;
69 aiocb->aio_nbytes = io_u->buflen;
70 aiocb->aio_offset = io_u->offset;
71
72 io_u->seen = 0;
73 return 0;
74}
75
76static int fio_posixaio_getevents(struct thread_data *td, int min, int max,
77 struct timespec *t)
78{
79 struct posixaio_data *pd = td->io_ops->data;
80 struct list_head *entry;
81 struct timespec start;
82 int r, have_timeout = 0;
83
84 if (t && !fill_timespec(&start))
85 have_timeout = 1;
86
87 r = 0;
88restart:
89 list_for_each(entry, &td->io_u_busylist) {
90 struct io_u *io_u = list_entry(entry, struct io_u, list);
91 int err;
92
93 if (io_u->seen)
94 continue;
95
96 err = aio_error(&io_u->aiocb);
97 switch (err) {
98 default:
99 io_u->error = err;
100 case ECANCELED:
101 case 0:
102 pd->aio_events[r++] = io_u;
103 io_u->seen = 1;
104 break;
105 case EINPROGRESS:
106 break;
107 }
108
109 if (r >= max)
110 break;
111 }
112
113 if (r >= min)
114 return r;
115
116 if (have_timeout) {
117 unsigned long long usec;
118
119 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
120 if (ts_utime_since_now(&start) > usec)
121 return r;
122 }
123
124 /*
125 * hrmpf, we need to wait for more. we should use aio_suspend, for
126 * now just sleep a little and recheck status of busy-and-not-seen
127 */
128 usleep(1000);
129 goto restart;
130}
131
132static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
133{
134 struct posixaio_data *pd = td->io_ops->data;
135
136 return pd->aio_events[event];
137}
138
139static int fio_posixaio_queue(struct thread_data fio_unused *td,
140 struct io_u *io_u)
141{
142 struct aiocb *aiocb = &io_u->aiocb;
143 int ret;
144
145 if (io_u->ddir == DDIR_READ)
146 ret = aio_read(aiocb);
147 else
148 ret = aio_write(aiocb);
149
150 if (ret)
151 io_u->error = errno;
152
153 return io_u->error;
154}
155
156static void fio_posixaio_cleanup(struct thread_data *td)
157{
158 struct posixaio_data *pd = td->io_ops->data;
159
160 if (pd) {
161 free(pd->aio_events);
162 free(pd);
163 td->io_ops->data = NULL;
164 }
165}
166
167static int fio_posixaio_init(struct thread_data *td)
168{
169 struct posixaio_data *pd = malloc(sizeof(*pd));
170
171 pd->aio_events = malloc(td->iodepth * sizeof(struct io_u *));
172
173 td->io_ops->data = pd;
174 return 0;
175}
176
177struct ioengine_ops ioengine = {
178 .name = "posixaio",
179 .version = FIO_IOOPS_VERSION,
180 .init = fio_posixaio_init,
181 .prep = fio_posixaio_prep,
182 .queue = fio_posixaio_queue,
183 .cancel = fio_posixaio_cancel,
184 .getevents = fio_posixaio_getevents,
185 .event = fio_posixaio_event,
186 .cleanup = fio_posixaio_cleanup,
187 .sync = fio_posixaio_sync,
188};