Update syslet-rw to fixed size ABI structures
[fio.git] / engines / syslet-rw.c
CommitLineData
a4f4fdd7 1/*
da751ca9
JA
2 * syslet engine
3 *
4 * IO engine that does regular pread(2)/pwrite(2) to transfer data, but
5 * with syslets to make the execution async.
a4f4fdd7
JA
6 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
12#include <assert.h>
b8846354 13#include <asm/unistd.h>
a4f4fdd7
JA
14
15#include "../fio.h"
a4f4fdd7
JA
16
17#ifdef FIO_HAVE_SYSLET
18
1760e679
JA
19#ifdef __NR_pread64
20#define __NR_fio_pread __NR_pread64
21#define __NR_fio_pwrite __NR_pwrite64
22#else
23#define __NR_fio_pread __NR_pread
24#define __NR_fio_pwrite __NR_pwrite
25#endif
26
a4f4fdd7
JA
27struct syslet_data {
28 struct io_u **events;
29 unsigned int nr_events;
30
bf0dc8fa 31 struct async_head_user ahu;
a4f4fdd7 32 struct syslet_uatom **ring;
9ff9de69
JA
33
34 struct syslet_uatom *head, *tail;
a4f4fdd7
JA
35};
36
9ff9de69
JA
37static void fio_syslet_complete_atom(struct thread_data *td,
38 struct syslet_uatom *atom)
39{
40 struct syslet_data *sd = td->io_ops->data;
5b38ee84 41 struct syslet_uatom *last;
9ff9de69 42 struct io_u *io_u;
9ff9de69
JA
43
44 /*
5b38ee84
JA
45 * complete from the beginning of the sequence up to (and
46 * including) this atom
9ff9de69 47 */
5b38ee84 48 last = atom;
1d79a6d1 49 io_u = (struct io_u *)atom->private;
5b38ee84 50 atom = io_u->req.head;
9ff9de69
JA
51
52 /*
53 * now complete in right order
54 */
5b38ee84 55 do {
9ff9de69
JA
56 long ret;
57
1d79a6d1
ZB
58 io_u = (struct io_u *)atom->private;
59 ret = *(long *)atom->ret_ptr;
e2e67912 60 if (ret >= 0)
9ff9de69
JA
61 io_u->resid = io_u->xfer_buflen - ret;
62 else if (ret < 0)
63 io_u->error = ret;
64
2dc1bbeb 65 assert(sd->nr_events < td->o.iodepth);
9ff9de69 66 sd->events[sd->nr_events++] = io_u;
9ff9de69 67
5b38ee84
JA
68 if (atom == last)
69 break;
9ff9de69 70
1d79a6d1 71 atom = (struct syslet_uatom *)atom->next;
5b38ee84
JA
72 } while (1);
73
74 assert(!last->next);
9ff9de69
JA
75}
76
a4f4fdd7
JA
77/*
78 * Inspect the ring to see if we have completed events
79 */
80static void fio_syslet_complete(struct thread_data *td)
81{
82 struct syslet_data *sd = td->io_ops->data;
83
84 do {
85 struct syslet_uatom *atom;
a4f4fdd7 86
bf0dc8fa 87 atom = sd->ring[sd->ahu.user_ring_idx];
a4f4fdd7
JA
88 if (!atom)
89 break;
90
bf0dc8fa 91 sd->ring[sd->ahu.user_ring_idx] = NULL;
2dc1bbeb 92 if (++sd->ahu.user_ring_idx == td->o.iodepth)
bf0dc8fa 93 sd->ahu.user_ring_idx = 0;
a4f4fdd7 94
9ff9de69 95 fio_syslet_complete_atom(td, atom);
a4f4fdd7
JA
96 } while (1);
97}
98
99static int fio_syslet_getevents(struct thread_data *td, int min,
100 int fio_unused max,
101 struct timespec fio_unused *t)
102{
103 struct syslet_data *sd = td->io_ops->data;
a4f4fdd7
JA
104 long ret;
105
106 do {
107 fio_syslet_complete(td);
108
109 /*
110 * do we have enough immediate completions?
111 */
112 if (sd->nr_events >= (unsigned int) min)
113 break;
114
115 /*
116 * OK, we need to wait for some events...
117 */
9ff9de69 118 ret = async_wait(1, sd->ahu.user_ring_idx, &sd->ahu);
a4f4fdd7 119 if (ret < 0)
e49499f8 120 return -errno;
a4f4fdd7
JA
121 } while (1);
122
123 ret = sd->nr_events;
124 sd->nr_events = 0;
125 return ret;
126}
127
128static struct io_u *fio_syslet_event(struct thread_data *td, int event)
129{
130 struct syslet_data *sd = td->io_ops->data;
131
132 return sd->events[event];
133}
134
135static void init_atom(struct syslet_uatom *atom, int nr, void *arg0,
a2e1b08a
JA
136 void *arg1, void *arg2, void *arg3, void *ret_ptr,
137 unsigned long flags, void *priv)
a4f4fdd7
JA
138{
139 atom->flags = flags;
140 atom->nr = nr;
1d79a6d1
ZB
141 atom->ret_ptr = (uint64_t)ret_ptr;
142 atom->next = 0;
143 atom->arg_ptr[0] = (uint64_t)arg0;
144 atom->arg_ptr[1] = (uint64_t)arg1;
145 atom->arg_ptr[2] = (uint64_t)arg2;
146 atom->arg_ptr[3] = (uint64_t)arg3;
147 atom->arg_ptr[4] = 0;
148 atom->arg_ptr[5] = 0;
149 atom->private = (uint64_t)priv;
a4f4fdd7
JA
150}
151
152/*
153 * Use seek atom for sync
154 */
155static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f)
156{
a2e1b08a 157 init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL,
7d44a745 158 &io_u->req.ret, 0, io_u);
a4f4fdd7
JA
159}
160
161static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f)
162{
163 int nr;
164
a4f4fdd7
JA
165 /*
166 * prepare rw
167 */
168 if (io_u->ddir == DDIR_READ)
1760e679 169 nr = __NR_fio_pread;
a4f4fdd7 170 else
1760e679 171 nr = __NR_fio_pwrite;
a4f4fdd7 172
a2e1b08a 173 init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf,
7d44a745 174 &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret, 0, io_u);
a4f4fdd7
JA
175}
176
177static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u)
178{
179 struct fio_file *f = io_u->file;
180
181 if (io_u->ddir == DDIR_SYNC)
182 fio_syslet_prep_sync(io_u, f);
183 else
184 fio_syslet_prep_rw(io_u, f);
185
186 return 0;
187}
188
bf0dc8fa
IM
189static void cachemiss_thread_start(void)
190{
191 while (1)
7756b0d0 192 async_thread(NULL, NULL);
bf0dc8fa
IM
193}
194
195#define THREAD_STACK_SIZE (16384)
196
197static unsigned long thread_stack_alloc()
198{
5b38ee84 199 return (unsigned long) malloc(THREAD_STACK_SIZE) + THREAD_STACK_SIZE;
bf0dc8fa
IM
200}
201
a0a930ef
JA
202static void fio_syslet_queued(struct thread_data *td, struct syslet_data *sd)
203{
204 struct syslet_uatom *atom;
205 struct timeval now;
206
207 fio_gettime(&now, NULL);
208
209 atom = sd->head;
210 while (atom) {
1d79a6d1 211 struct io_u *io_u = (struct io_u *)atom->private;
a0a930ef
JA
212
213 memcpy(&io_u->issue_time, &now, sizeof(now));
214 io_u_queued(td, io_u);
1d79a6d1 215 atom = (struct syslet_uatom *)atom->next;
a0a930ef
JA
216 }
217}
218
9ff9de69 219static int fio_syslet_commit(struct thread_data *td)
a4f4fdd7
JA
220{
221 struct syslet_data *sd = td->io_ops->data;
bf0dc8fa 222 struct syslet_uatom *done;
9ff9de69
JA
223
224 if (!sd->head)
225 return 0;
a4f4fdd7 226
5b38ee84
JA
227 assert(!sd->tail->next);
228
bf0dc8fa
IM
229 if (!sd->ahu.new_thread_stack)
230 sd->ahu.new_thread_stack = thread_stack_alloc();
231
a0a930ef
JA
232 fio_syslet_queued(td, sd);
233
7d44a745
JA
234 /*
235 * On sync completion, the atom is returned. So on NULL return
236 * it's queued asynchronously.
237 */
9ff9de69 238 done = async_exec(sd->head, &sd->ahu);
bf0dc8fa 239
76f58b92
JA
240 if (done == (void *) -1) {
241 log_err("fio: syslets don't appear to work\n");
242 return -1;
243 }
244
9ff9de69 245 sd->head = sd->tail = NULL;
a4f4fdd7 246
9ff9de69
JA
247 if (done)
248 fio_syslet_complete_atom(td, done);
a4f4fdd7 249
9ff9de69
JA
250 return 0;
251}
252
253static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
254{
255 struct syslet_data *sd = td->io_ops->data;
bf0dc8fa 256
7101d9c2
JA
257 fio_ro_check(td, io_u);
258
9ff9de69 259 if (sd->tail) {
1d79a6d1 260 sd->tail->next = (uint64_t)&io_u->req.atom;
9ff9de69
JA
261 sd->tail = &io_u->req.atom;
262 } else
1d79a6d1 263 sd->head = sd->tail = (struct syslet_uatom *)&io_u->req.atom;
a4f4fdd7 264
5b38ee84 265 io_u->req.head = sd->head;
9ff9de69 266 return FIO_Q_QUEUED;
a4f4fdd7
JA
267}
268
db64e9bc 269static int async_head_init(struct syslet_data *sd, unsigned int depth)
a4f4fdd7 270{
a4f4fdd7
JA
271 unsigned long ring_size;
272
bf0dc8fa 273 memset(&sd->ahu, 0, sizeof(struct async_head_user));
2ca50be4 274
a4f4fdd7
JA
275 ring_size = sizeof(struct syslet_uatom *) * depth;
276 sd->ring = malloc(ring_size);
277 memset(sd->ring, 0, ring_size);
278
bf0dc8fa 279 sd->ahu.user_ring_idx = 0;
1d79a6d1 280 sd->ahu.completion_ring_ptr = (uint64_t)sd->ring;
bf0dc8fa
IM
281 sd->ahu.ring_size_bytes = ring_size;
282 sd->ahu.head_stack = thread_stack_alloc();
1d79a6d1
ZB
283 sd->ahu.head_ip = (uint64_t)cachemiss_thread_start;
284 sd->ahu.new_thread_ip = (uint64_t)cachemiss_thread_start;
6a117e0c 285 sd->ahu.new_thread_stack = thread_stack_alloc();
db64e9bc
JA
286
287 return 0;
a4f4fdd7
JA
288}
289
2ca50be4 290static void async_head_exit(struct syslet_data *sd)
a4f4fdd7 291{
7f059a76 292 free(sd->ring);
a4f4fdd7
JA
293}
294
76f58b92
JA
295static int check_syslet_support(struct syslet_data *sd)
296{
297 struct syslet_uatom atom;
298 void *ret;
299
300 init_atom(&atom, __NR_getpid, NULL, NULL, NULL, NULL, NULL, 0, NULL);
b4d8ddac 301 ret = async_exec(&atom, &sd->ahu);
76f58b92
JA
302 if (ret == (void *) -1)
303 return 1;
304
305 return 0;
306}
307
a4f4fdd7
JA
308static void fio_syslet_cleanup(struct thread_data *td)
309{
310 struct syslet_data *sd = td->io_ops->data;
311
312 if (sd) {
2ca50be4 313 async_head_exit(sd);
a4f4fdd7
JA
314 free(sd->events);
315 free(sd);
316 td->io_ops->data = NULL;
317 }
318}
319
320static int fio_syslet_init(struct thread_data *td)
321{
322 struct syslet_data *sd;
323
324 sd = malloc(sizeof(*sd));
325 memset(sd, 0, sizeof(*sd));
2dc1bbeb
JA
326 sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth);
327 memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth);
db64e9bc
JA
328
329 /*
330 * This will handily fail for kernels where syslet isn't available
331 */
2dc1bbeb 332 if (async_head_init(sd, td->o.iodepth)) {
db64e9bc
JA
333 free(sd->events);
334 free(sd);
335 return 1;
336 }
337
76f58b92
JA
338 if (check_syslet_support(sd)) {
339 log_err("fio: syslets do not appear to work\n");
340 free(sd->events);
341 free(sd);
342 return 1;
343 }
344
a4f4fdd7 345 td->io_ops->data = sd;
a4f4fdd7
JA
346 return 0;
347}
348
349static struct ioengine_ops ioengine = {
350 .name = "syslet-rw",
351 .version = FIO_IOOPS_VERSION,
352 .init = fio_syslet_init,
353 .prep = fio_syslet_prep,
354 .queue = fio_syslet_queue,
9ff9de69 355 .commit = fio_syslet_commit,
a4f4fdd7
JA
356 .getevents = fio_syslet_getevents,
357 .event = fio_syslet_event,
358 .cleanup = fio_syslet_cleanup,
b5af8293
JA
359 .open_file = generic_open_file,
360 .close_file = generic_close_file,
a4f4fdd7
JA
361};
362
363#else /* FIO_HAVE_SYSLET */
364
365/*
366 * When we have a proper configure system in place, we simply wont build
367 * and install this io engine. For now install a crippled version that
368 * just complains and fails to load.
369 */
370static int fio_syslet_init(struct thread_data fio_unused *td)
371{
372 fprintf(stderr, "fio: syslet not available\n");
373 return 1;
374}
375
376static struct ioengine_ops ioengine = {
377 .name = "syslet-rw",
378 .version = FIO_IOOPS_VERSION,
379 .init = fio_syslet_init,
380};
381
382#endif /* FIO_HAVE_SYSLET */
383
384static void fio_init fio_syslet_register(void)
385{
386 register_ioengine(&ioengine);
387}
388
389static void fio_exit fio_syslet_unregister(void)
390{
391 unregister_ioengine(&ioengine);
392}