blob: e0dddd8761268487c8ce15f2c000a4cd8603e18a [file] [log] [blame]
Jens Axboea4f4fdd2007-02-14 01:16:39 +01001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * 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.
Jens Axboea4f4fdd2007-02-14 01:16:39 +01006 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
12#include <assert.h>
Jens Axboeb8846352007-03-14 14:29:16 +010013#include <asm/unistd.h>
Jens Axboea4f4fdd2007-02-14 01:16:39 +010014
15#include "../fio.h"
Jens Axboea4f4fdd2007-02-14 01:16:39 +010016
17#ifdef FIO_HAVE_SYSLET
18
Jens Axboe1760e672007-03-14 20:41:42 +010019#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
Jens Axboea4f4fdd2007-02-14 01:16:39 +010027struct syslet_data {
28 struct io_u **events;
29 unsigned int nr_events;
30
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010031 struct async_head_user ahu;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010032 struct syslet_uatom **ring;
Jens Axboe9ff9de62007-02-23 13:21:45 +010033
34 struct syslet_uatom *head, *tail;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010035};
36
Jens Axboe9ff9de62007-02-23 13:21:45 +010037static void fio_syslet_complete_atom(struct thread_data *td,
38 struct syslet_uatom *atom)
39{
40 struct syslet_data *sd = td->io_ops->data;
Jens Axboe5b38ee82007-02-26 10:44:22 +010041 struct syslet_uatom *last;
Jens Axboe9ff9de62007-02-23 13:21:45 +010042 struct io_u *io_u;
Jens Axboe9ff9de62007-02-23 13:21:45 +010043
44 /*
Jens Axboe5b38ee82007-02-26 10:44:22 +010045 * complete from the beginning of the sequence up to (and
46 * including) this atom
Jens Axboe9ff9de62007-02-23 13:21:45 +010047 */
Jens Axboe5b38ee82007-02-26 10:44:22 +010048 last = atom;
49 io_u = atom->private;
50 atom = io_u->req.head;
Jens Axboe9ff9de62007-02-23 13:21:45 +010051
52 /*
53 * now complete in right order
54 */
Jens Axboe5b38ee82007-02-26 10:44:22 +010055 do {
Jens Axboe9ff9de62007-02-23 13:21:45 +010056 long ret;
57
Jens Axboe9ff9de62007-02-23 13:21:45 +010058 io_u = atom->private;
59 ret = *atom->ret_ptr;
Jens Axboee2e67912007-03-12 09:43:05 +010060 if (ret >= 0)
Jens Axboe9ff9de62007-02-23 13:21:45 +010061 io_u->resid = io_u->xfer_buflen - ret;
62 else if (ret < 0)
63 io_u->error = ret;
64
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010065 assert(sd->nr_events < td->o.iodepth);
Jens Axboe9ff9de62007-02-23 13:21:45 +010066 sd->events[sd->nr_events++] = io_u;
Jens Axboe9ff9de62007-02-23 13:21:45 +010067
Jens Axboe5b38ee82007-02-26 10:44:22 +010068 if (atom == last)
69 break;
Jens Axboe9ff9de62007-02-23 13:21:45 +010070
Jens Axboe5b38ee82007-02-26 10:44:22 +010071 atom = atom->next;
72 } while (1);
73
74 assert(!last->next);
Jens Axboe9ff9de62007-02-23 13:21:45 +010075}
76
Jens Axboea4f4fdd2007-02-14 01:16:39 +010077/*
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;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010086
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010087 atom = sd->ring[sd->ahu.user_ring_idx];
Jens Axboea4f4fdd2007-02-14 01:16:39 +010088 if (!atom)
89 break;
90
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010091 sd->ring[sd->ahu.user_ring_idx] = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010092 if (++sd->ahu.user_ring_idx == td->o.iodepth)
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010093 sd->ahu.user_ring_idx = 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010094
Jens Axboe9ff9de62007-02-23 13:21:45 +010095 fio_syslet_complete_atom(td, atom);
Jens Axboea4f4fdd2007-02-14 01:16:39 +010096 } 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;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100104 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 */
Jens Axboe9ff9de62007-02-23 13:21:45 +0100118 ret = async_wait(1, sd->ahu.user_ring_idx, &sd->ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100119 if (ret < 0)
Jens Axboee49499f2007-02-22 11:08:52 +0100120 return -errno;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100121 } 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,
Jens Axboea2e1b082007-02-14 08:06:19 +0100136 void *arg1, void *arg2, void *arg3, void *ret_ptr,
137 unsigned long flags, void *priv)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100138{
139 atom->flags = flags;
140 atom->nr = nr;
141 atom->ret_ptr = ret_ptr;
Jens Axboea2e1b082007-02-14 08:06:19 +0100142 atom->next = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100143 atom->arg_ptr[0] = arg0;
144 atom->arg_ptr[1] = arg1;
145 atom->arg_ptr[2] = arg2;
Jens Axboea2e1b082007-02-14 08:06:19 +0100146 atom->arg_ptr[3] = arg3;
147 atom->arg_ptr[4] = atom->arg_ptr[5] = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100148 atom->private = priv;
149}
150
151/*
152 * Use seek atom for sync
153 */
154static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f)
155{
Jens Axboea2e1b082007-02-14 08:06:19 +0100156 init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL,
Jens Axboe7d44a742007-02-14 17:32:08 +0100157 &io_u->req.ret, 0, io_u);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100158}
159
160static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f)
161{
162 int nr;
163
164 /*
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100165 * prepare rw
166 */
167 if (io_u->ddir == DDIR_READ)
Jens Axboe1760e672007-03-14 20:41:42 +0100168 nr = __NR_fio_pread;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100169 else
Jens Axboe1760e672007-03-14 20:41:42 +0100170 nr = __NR_fio_pwrite;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100171
Jens Axboea2e1b082007-02-14 08:06:19 +0100172 init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf,
Jens Axboe7d44a742007-02-14 17:32:08 +0100173 &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret, 0, io_u);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100174}
175
176static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u)
177{
178 struct fio_file *f = io_u->file;
179
180 if (io_u->ddir == DDIR_SYNC)
181 fio_syslet_prep_sync(io_u, f);
182 else
183 fio_syslet_prep_rw(io_u, f);
184
185 return 0;
186}
187
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100188static void cachemiss_thread_start(void)
189{
190 while (1)
Jens Axboe7756b0d2007-02-26 10:22:31 +0100191 async_thread(NULL, NULL);
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100192}
193
194#define THREAD_STACK_SIZE (16384)
195
196static unsigned long thread_stack_alloc()
197{
Jens Axboe5b38ee82007-02-26 10:44:22 +0100198 return (unsigned long) malloc(THREAD_STACK_SIZE) + THREAD_STACK_SIZE;
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100199}
200
Jens Axboea0a930e2007-02-27 19:47:13 +0100201static void fio_syslet_queued(struct thread_data *td, struct syslet_data *sd)
202{
203 struct syslet_uatom *atom;
204 struct timeval now;
205
206 fio_gettime(&now, NULL);
207
208 atom = sd->head;
209 while (atom) {
210 struct io_u *io_u = atom->private;
211
212 memcpy(&io_u->issue_time, &now, sizeof(now));
213 io_u_queued(td, io_u);
214 atom = atom->next;
215 }
216}
217
Jens Axboe9ff9de62007-02-23 13:21:45 +0100218static int fio_syslet_commit(struct thread_data *td)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100219{
220 struct syslet_data *sd = td->io_ops->data;
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100221 struct syslet_uatom *done;
Jens Axboe9ff9de62007-02-23 13:21:45 +0100222
223 if (!sd->head)
224 return 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100225
Jens Axboe5b38ee82007-02-26 10:44:22 +0100226 assert(!sd->tail->next);
227
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100228 if (!sd->ahu.new_thread_stack)
229 sd->ahu.new_thread_stack = thread_stack_alloc();
230
Jens Axboea0a930e2007-02-27 19:47:13 +0100231 fio_syslet_queued(td, sd);
232
Jens Axboe7d44a742007-02-14 17:32:08 +0100233 /*
234 * On sync completion, the atom is returned. So on NULL return
235 * it's queued asynchronously.
236 */
Jens Axboe9ff9de62007-02-23 13:21:45 +0100237 done = async_exec(sd->head, &sd->ahu);
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100238
Jens Axboe76f58b92007-03-29 13:26:17 +0200239 if (done == (void *) -1) {
240 log_err("fio: syslets don't appear to work\n");
241 return -1;
242 }
243
Jens Axboe9ff9de62007-02-23 13:21:45 +0100244 sd->head = sd->tail = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100245
Jens Axboe9ff9de62007-02-23 13:21:45 +0100246 if (done)
247 fio_syslet_complete_atom(td, done);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100248
Jens Axboe9ff9de62007-02-23 13:21:45 +0100249 return 0;
250}
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100251
Jens Axboe9ff9de62007-02-23 13:21:45 +0100252static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
253{
254 struct syslet_data *sd = td->io_ops->data;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100255
Jens Axboe7101d9c2007-09-12 13:12:39 +0200256 fio_ro_check(td, io_u);
257
Jens Axboe9ff9de62007-02-23 13:21:45 +0100258 if (sd->tail) {
259 sd->tail->next = &io_u->req.atom;
260 sd->tail = &io_u->req.atom;
261 } else
262 sd->head = sd->tail = &io_u->req.atom;
263
Jens Axboe5b38ee82007-02-26 10:44:22 +0100264 io_u->req.head = sd->head;
Jens Axboe9ff9de62007-02-23 13:21:45 +0100265 return FIO_Q_QUEUED;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100266}
267
Jens Axboedb64e9b2007-02-14 02:10:59 +0100268static int async_head_init(struct syslet_data *sd, unsigned int depth)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100269{
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100270 unsigned long ring_size;
271
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100272 memset(&sd->ahu, 0, sizeof(struct async_head_user));
Jens Axboe2ca50be2007-02-14 08:31:15 +0100273
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100274 ring_size = sizeof(struct syslet_uatom *) * depth;
275 sd->ring = malloc(ring_size);
276 memset(sd->ring, 0, ring_size);
277
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100278 sd->ahu.user_ring_idx = 0;
279 sd->ahu.completion_ring = sd->ring;
280 sd->ahu.ring_size_bytes = ring_size;
281 sd->ahu.head_stack = thread_stack_alloc();
Jens Axboe5b38ee82007-02-26 10:44:22 +0100282 sd->ahu.head_eip = (unsigned long) cachemiss_thread_start;
283 sd->ahu.new_thread_eip = (unsigned long) cachemiss_thread_start;
Jens Axboedb64e9b2007-02-14 02:10:59 +0100284
285 return 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100286}
287
Jens Axboe2ca50be2007-02-14 08:31:15 +0100288static void async_head_exit(struct syslet_data *sd)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100289{
Jens Axboe7f059a72007-02-14 08:53:11 +0100290 free(sd->ring);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100291}
292
Jens Axboe76f58b92007-03-29 13:26:17 +0200293static int check_syslet_support(struct syslet_data *sd)
294{
295 struct syslet_uatom atom;
296 void *ret;
297
298 init_atom(&atom, __NR_getpid, NULL, NULL, NULL, NULL, NULL, 0, NULL);
299 ret = async_exec(sd->head, &sd->ahu);
300 if (ret == (void *) -1)
301 return 1;
302
303 return 0;
304}
305
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100306static void fio_syslet_cleanup(struct thread_data *td)
307{
308 struct syslet_data *sd = td->io_ops->data;
309
310 if (sd) {
Jens Axboe2ca50be2007-02-14 08:31:15 +0100311 async_head_exit(sd);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100312 free(sd->events);
313 free(sd);
314 td->io_ops->data = NULL;
315 }
316}
317
318static int fio_syslet_init(struct thread_data *td)
319{
320 struct syslet_data *sd;
321
322 sd = malloc(sizeof(*sd));
323 memset(sd, 0, sizeof(*sd));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100324 sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth);
325 memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth);
Jens Axboedb64e9b2007-02-14 02:10:59 +0100326
327 /*
328 * This will handily fail for kernels where syslet isn't available
329 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100330 if (async_head_init(sd, td->o.iodepth)) {
Jens Axboedb64e9b2007-02-14 02:10:59 +0100331 free(sd->events);
332 free(sd);
333 return 1;
334 }
335
Jens Axboe76f58b92007-03-29 13:26:17 +0200336 if (check_syslet_support(sd)) {
337 log_err("fio: syslets do not appear to work\n");
338 free(sd->events);
339 free(sd);
340 return 1;
341 }
342
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100343 td->io_ops->data = sd;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100344 return 0;
345}
346
347static struct ioengine_ops ioengine = {
348 .name = "syslet-rw",
349 .version = FIO_IOOPS_VERSION,
350 .init = fio_syslet_init,
351 .prep = fio_syslet_prep,
352 .queue = fio_syslet_queue,
Jens Axboe9ff9de62007-02-23 13:21:45 +0100353 .commit = fio_syslet_commit,
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100354 .getevents = fio_syslet_getevents,
355 .event = fio_syslet_event,
356 .cleanup = fio_syslet_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100357 .open_file = generic_open_file,
358 .close_file = generic_close_file,
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100359};
360
361#else /* FIO_HAVE_SYSLET */
362
363/*
364 * When we have a proper configure system in place, we simply wont build
365 * and install this io engine. For now install a crippled version that
366 * just complains and fails to load.
367 */
368static int fio_syslet_init(struct thread_data fio_unused *td)
369{
370 fprintf(stderr, "fio: syslet not available\n");
371 return 1;
372}
373
374static struct ioengine_ops ioengine = {
375 .name = "syslet-rw",
376 .version = FIO_IOOPS_VERSION,
377 .init = fio_syslet_init,
378};
379
380#endif /* FIO_HAVE_SYSLET */
381
382static void fio_init fio_syslet_register(void)
383{
384 register_ioengine(&ioengine);
385}
386
387static void fio_exit fio_syslet_unregister(void)
388{
389 unregister_ioengine(&ioengine);
390}