[PATCH] syslet engine: use pread instead lseek+read
[fio.git] / engines / syslet-rw.c
... / ...
CommitLineData
1/*
2 * read/write() engine that uses syslet to be async
3 *
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
10
11#include "../fio.h"
12#include "../os.h"
13
14#ifdef FIO_HAVE_SYSLET
15
16struct syslet_data {
17 struct io_u **events;
18 unsigned int nr_events;
19
20 struct syslet_uatom **ring;
21 unsigned int ring_index;
22};
23
24/*
25 * Inspect the ring to see if we have completed events
26 */
27static void fio_syslet_complete(struct thread_data *td)
28{
29 struct syslet_data *sd = td->io_ops->data;
30
31 do {
32 struct syslet_uatom *atom;
33 struct io_u *io_u;
34 long ret;
35
36 atom = sd->ring[sd->ring_index];
37 if (!atom)
38 break;
39
40 sd->ring[sd->ring_index] = NULL;
41 if (++sd->ring_index == td->iodepth)
42 sd->ring_index = 0;
43
44 io_u = atom->private;
45 ret = *atom->ret_ptr;
46 if (ret > 0)
47 io_u->resid = io_u->xfer_buflen - ret;
48 else if (ret < 0)
49 io_u->error = ret;
50
51 sd->events[sd->nr_events++] = io_u;
52 } while (1);
53}
54
55static int fio_syslet_getevents(struct thread_data *td, int min,
56 int fio_unused max,
57 struct timespec fio_unused *t)
58{
59 struct syslet_data *sd = td->io_ops->data;
60 int get_events;
61 long ret;
62
63 do {
64 fio_syslet_complete(td);
65
66 /*
67 * do we have enough immediate completions?
68 */
69 if (sd->nr_events >= (unsigned int) min)
70 break;
71
72 /*
73 * OK, we need to wait for some events...
74 */
75 get_events = min - sd->nr_events;
76 ret = async_wait(get_events);
77 if (ret < 0)
78 return errno;
79 } while (1);
80
81 ret = sd->nr_events;
82 sd->nr_events = 0;
83 return ret;
84}
85
86static struct io_u *fio_syslet_event(struct thread_data *td, int event)
87{
88 struct syslet_data *sd = td->io_ops->data;
89
90 return sd->events[event];
91}
92
93static void init_atom(struct syslet_uatom *atom, int nr, void *arg0,
94 void *arg1, void *arg2, void *arg3, void *ret_ptr,
95 unsigned long flags, void *priv)
96{
97 atom->flags = flags;
98 atom->nr = nr;
99 atom->ret_ptr = ret_ptr;
100 atom->next = NULL;
101 atom->arg_ptr[0] = arg0;
102 atom->arg_ptr[1] = arg1;
103 atom->arg_ptr[2] = arg2;
104 atom->arg_ptr[3] = arg3;
105 atom->arg_ptr[4] = atom->arg_ptr[5] = NULL;
106 atom->private = priv;
107}
108
109/*
110 * Use seek atom for sync
111 */
112static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f)
113{
114 init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL,
115 &io_u->req.ret, SYSLET_STOP_ON_NEGATIVE, io_u);
116}
117
118static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f)
119{
120 int nr;
121
122 /*
123 * prepare rw
124 */
125 if (io_u->ddir == DDIR_READ)
126 nr = __NR_pread64;
127 else
128 nr = __NR_pwrite64;
129
130 init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf,
131 &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret,
132 SYSLET_STOP_ON_NEGATIVE, io_u);
133}
134
135static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u)
136{
137 struct fio_file *f = io_u->file;
138
139 if (io_u->ddir == DDIR_SYNC)
140 fio_syslet_prep_sync(io_u, f);
141 else
142 fio_syslet_prep_rw(io_u, f);
143
144 return 0;
145}
146
147static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
148{
149 struct syslet_data *sd = td->io_ops->data;
150 struct syslet_uatom *done;
151 long ret;
152
153 done = async_exec(&io_u->req.atom);
154 if (!done)
155 return 0;
156
157 /*
158 * completed sync
159 */
160 ret = io_u->req.ret;
161 if (ret != (long) io_u->xfer_buflen) {
162 if (ret > 0) {
163 io_u->resid = io_u->xfer_buflen - ret;
164 io_u->error = 0;
165 return ret;
166 } else
167 io_u->error = errno;
168 }
169
170 if (!io_u->error)
171 sd->events[sd->nr_events++] = io_u;
172 else
173 td_verror(td, io_u->error);
174
175 return io_u->error;
176}
177
178static int async_head_init(struct syslet_data *sd, unsigned int depth)
179{
180 struct async_head_user ahu;
181 unsigned long ring_size;
182
183 ring_size = sizeof(struct syslet_uatom *) * depth;
184 sd->ring = malloc(ring_size);
185 memset(sd->ring, 0, ring_size);
186
187 memset(&ahu, 0, sizeof(ahu));
188 ahu.completion_ring = sd->ring;
189 ahu.ring_size_bytes = ring_size;
190 ahu.max_nr_threads = -1;
191
192 if (async_register(&ahu, sizeof(ahu)) < 0) {
193 perror("async_register");
194 fprintf(stderr, "fio: syslet likely not supported\n");
195 free(sd->ring);
196 return 1;
197 }
198
199 return 0;
200}
201
202static void async_head_exit(struct syslet_data *sd, unsigned int depth)
203{
204 struct async_head_user ahu;
205
206 memset(&ahu, 0, sizeof(ahu));
207 ahu.completion_ring = sd->ring;
208 ahu.ring_size_bytes = sizeof(struct syslet_uatom *) * depth;
209
210 if (async_unregister(&ahu, sizeof(ahu)) < 0)
211 perror("async_register");
212}
213
214static void fio_syslet_cleanup(struct thread_data *td)
215{
216 struct syslet_data *sd = td->io_ops->data;
217
218 if (sd) {
219 async_head_exit(sd, td->iodepth);
220 free(sd->events);
221 free(sd);
222 td->io_ops->data = NULL;
223 }
224}
225
226static int fio_syslet_init(struct thread_data *td)
227{
228 struct syslet_data *sd;
229
230
231 sd = malloc(sizeof(*sd));
232 memset(sd, 0, sizeof(*sd));
233 sd->events = malloc(sizeof(struct io_u *) * td->iodepth);
234 memset(sd->events, 0, sizeof(struct io_u *) * td->iodepth);
235
236 /*
237 * This will handily fail for kernels where syslet isn't available
238 */
239 if (async_head_init(sd, td->iodepth)) {
240 free(sd->events);
241 free(sd);
242 return 1;
243 }
244
245 td->io_ops->data = sd;
246 return 0;
247}
248
249static struct ioengine_ops ioengine = {
250 .name = "syslet-rw",
251 .version = FIO_IOOPS_VERSION,
252 .init = fio_syslet_init,
253 .prep = fio_syslet_prep,
254 .queue = fio_syslet_queue,
255 .getevents = fio_syslet_getevents,
256 .event = fio_syslet_event,
257 .cleanup = fio_syslet_cleanup,
258};
259
260#else /* FIO_HAVE_SYSLET */
261
262/*
263 * When we have a proper configure system in place, we simply wont build
264 * and install this io engine. For now install a crippled version that
265 * just complains and fails to load.
266 */
267static int fio_syslet_init(struct thread_data fio_unused *td)
268{
269 fprintf(stderr, "fio: syslet not available\n");
270 return 1;
271}
272
273static struct ioengine_ops ioengine = {
274 .name = "syslet-rw",
275 .version = FIO_IOOPS_VERSION,
276 .init = fio_syslet_init,
277};
278
279#endif /* FIO_HAVE_SYSLET */
280
281static void fio_init fio_syslet_register(void)
282{
283 register_ioengine(&ioengine);
284}
285
286static void fio_exit fio_syslet_unregister(void)
287{
288 unregister_ioengine(&ioengine);
289}