blob: 2a159d0893f0e40ddbbad0b2c259340487c7b7fc [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 Axboe67f5b272007-09-28 08:48:52 +020027#define ATOM_TO_IOU(p) ((struct io_u *) (unsigned long) (p))
28
Jens Axboea4f4fdd2007-02-14 01:16:39 +010029struct syslet_data {
30 struct io_u **events;
31 unsigned int nr_events;
32
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010033 struct async_head_user ahu;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010034 struct syslet_uatom **ring;
Jens Axboe9ff9de62007-02-23 13:21:45 +010035
36 struct syslet_uatom *head, *tail;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010037};
38
Jens Axboe9ff9de62007-02-23 13:21:45 +010039static void fio_syslet_complete_atom(struct thread_data *td,
40 struct syslet_uatom *atom)
41{
42 struct syslet_data *sd = td->io_ops->data;
Jens Axboe5b38ee82007-02-26 10:44:22 +010043 struct syslet_uatom *last;
Jens Axboe9ff9de62007-02-23 13:21:45 +010044 struct io_u *io_u;
Jens Axboe9ff9de62007-02-23 13:21:45 +010045
46 /*
Jens Axboe5b38ee82007-02-26 10:44:22 +010047 * complete from the beginning of the sequence up to (and
48 * including) this atom
Jens Axboe9ff9de62007-02-23 13:21:45 +010049 */
Jens Axboe5b38ee82007-02-26 10:44:22 +010050 last = atom;
Jens Axboe67f5b272007-09-28 08:48:52 +020051 io_u = ATOM_TO_IOU(atom);
Jens Axboe5b38ee82007-02-26 10:44:22 +010052 atom = io_u->req.head;
Jens Axboe9ff9de62007-02-23 13:21:45 +010053
54 /*
55 * now complete in right order
56 */
Jens Axboe5b38ee82007-02-26 10:44:22 +010057 do {
Jens Axboe9ff9de62007-02-23 13:21:45 +010058 long ret;
59
Jens Axboe67f5b272007-09-28 08:48:52 +020060 io_u = ATOM_TO_IOU(atom);
61 ret = *(long *) (unsigned long) atom->ret_ptr;
Jens Axboee2e67912007-03-12 09:43:05 +010062 if (ret >= 0)
Jens Axboe9ff9de62007-02-23 13:21:45 +010063 io_u->resid = io_u->xfer_buflen - ret;
64 else if (ret < 0)
65 io_u->error = ret;
66
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010067 assert(sd->nr_events < td->o.iodepth);
Jens Axboe9ff9de62007-02-23 13:21:45 +010068 sd->events[sd->nr_events++] = io_u;
Jens Axboe9ff9de62007-02-23 13:21:45 +010069
Jens Axboe5b38ee82007-02-26 10:44:22 +010070 if (atom == last)
71 break;
Jens Axboe9ff9de62007-02-23 13:21:45 +010072
Jens Axboe67f5b272007-09-28 08:48:52 +020073 atom = (struct syslet_uatom *) (unsigned long) atom->next;
Jens Axboe5b38ee82007-02-26 10:44:22 +010074 } while (1);
75
76 assert(!last->next);
Jens Axboe9ff9de62007-02-23 13:21:45 +010077}
78
Jens Axboea4f4fdd2007-02-14 01:16:39 +010079/*
80 * Inspect the ring to see if we have completed events
81 */
82static void fio_syslet_complete(struct thread_data *td)
83{
84 struct syslet_data *sd = td->io_ops->data;
85
86 do {
87 struct syslet_uatom *atom;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010088
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010089 atom = sd->ring[sd->ahu.user_ring_idx];
Jens Axboea4f4fdd2007-02-14 01:16:39 +010090 if (!atom)
91 break;
92
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010093 sd->ring[sd->ahu.user_ring_idx] = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010094 if (++sd->ahu.user_ring_idx == td->o.iodepth)
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010095 sd->ahu.user_ring_idx = 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010096
Jens Axboe9ff9de62007-02-23 13:21:45 +010097 fio_syslet_complete_atom(td, atom);
Jens Axboea4f4fdd2007-02-14 01:16:39 +010098 } while (1);
99}
100
101static int fio_syslet_getevents(struct thread_data *td, int min,
102 int fio_unused max,
103 struct timespec fio_unused *t)
104{
105 struct syslet_data *sd = td->io_ops->data;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100106 long ret;
107
108 do {
109 fio_syslet_complete(td);
110
111 /*
112 * do we have enough immediate completions?
113 */
114 if (sd->nr_events >= (unsigned int) min)
115 break;
116
117 /*
118 * OK, we need to wait for some events...
119 */
Jens Axboe9ff9de62007-02-23 13:21:45 +0100120 ret = async_wait(1, sd->ahu.user_ring_idx, &sd->ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100121 if (ret < 0)
Jens Axboee49499f2007-02-22 11:08:52 +0100122 return -errno;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100123 } while (1);
124
125 ret = sd->nr_events;
126 sd->nr_events = 0;
127 return ret;
128}
129
130static struct io_u *fio_syslet_event(struct thread_data *td, int event)
131{
132 struct syslet_data *sd = td->io_ops->data;
133
134 return sd->events[event];
135}
136
137static void init_atom(struct syslet_uatom *atom, int nr, void *arg0,
Jens Axboea2e1b082007-02-14 08:06:19 +0100138 void *arg1, void *arg2, void *arg3, void *ret_ptr,
139 unsigned long flags, void *priv)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100140{
141 atom->flags = flags;
142 atom->nr = nr;
Jens Axboe67f5b272007-09-28 08:48:52 +0200143 atom->ret_ptr = (uint64_t) (unsigned long) ret_ptr;
Zach Brown1d79a6d2007-09-18 16:28:48 -0700144 atom->next = 0;
Jens Axboe67f5b272007-09-28 08:48:52 +0200145 atom->arg_ptr[0] = (uint64_t) (unsigned long) arg0;
146 atom->arg_ptr[1] = (uint64_t) (unsigned long) arg1;
147 atom->arg_ptr[2] = (uint64_t) (unsigned long) arg2;
148 atom->arg_ptr[3] = (uint64_t) (unsigned long) arg3;
Zach Brown1d79a6d2007-09-18 16:28:48 -0700149 atom->arg_ptr[4] = 0;
150 atom->arg_ptr[5] = 0;
Jens Axboe67f5b272007-09-28 08:48:52 +0200151 atom->private = (uint64_t) (unsigned long) priv;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100152}
153
154/*
155 * Use seek atom for sync
156 */
157static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f)
158{
Jens Axboea2e1b082007-02-14 08:06:19 +0100159 init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL,
Jens Axboe7d44a742007-02-14 17:32:08 +0100160 &io_u->req.ret, 0, io_u);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100161}
162
163static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f)
164{
165 int nr;
166
167 /*
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100168 * prepare rw
169 */
170 if (io_u->ddir == DDIR_READ)
Jens Axboe1760e672007-03-14 20:41:42 +0100171 nr = __NR_fio_pread;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100172 else
Jens Axboe1760e672007-03-14 20:41:42 +0100173 nr = __NR_fio_pwrite;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100174
Jens Axboea2e1b082007-02-14 08:06:19 +0100175 init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf,
Jens Axboe7d44a742007-02-14 17:32:08 +0100176 &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret, 0, io_u);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100177}
178
179static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u)
180{
181 struct fio_file *f = io_u->file;
182
183 if (io_u->ddir == DDIR_SYNC)
184 fio_syslet_prep_sync(io_u, f);
185 else
186 fio_syslet_prep_rw(io_u, f);
187
188 return 0;
189}
190
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100191static void cachemiss_thread_start(void)
192{
193 while (1)
Jens Axboe7756b0d2007-02-26 10:22:31 +0100194 async_thread(NULL, NULL);
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100195}
196
197#define THREAD_STACK_SIZE (16384)
198
199static unsigned long thread_stack_alloc()
200{
Jens Axboe5b38ee82007-02-26 10:44:22 +0100201 return (unsigned long) malloc(THREAD_STACK_SIZE) + THREAD_STACK_SIZE;
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100202}
203
Jens Axboea0a930e2007-02-27 19:47:13 +0100204static void fio_syslet_queued(struct thread_data *td, struct syslet_data *sd)
205{
206 struct syslet_uatom *atom;
207 struct timeval now;
208
209 fio_gettime(&now, NULL);
210
211 atom = sd->head;
212 while (atom) {
Jens Axboe67f5b272007-09-28 08:48:52 +0200213 struct io_u *io_u = ATOM_TO_IOU(atom);
Jens Axboea0a930e2007-02-27 19:47:13 +0100214
215 memcpy(&io_u->issue_time, &now, sizeof(now));
216 io_u_queued(td, io_u);
Jens Axboe67f5b272007-09-28 08:48:52 +0200217 atom = (struct syslet_uatom *) (unsigned long) atom->next;
Jens Axboea0a930e2007-02-27 19:47:13 +0100218 }
219}
220
Jens Axboe9ff9de62007-02-23 13:21:45 +0100221static int fio_syslet_commit(struct thread_data *td)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100222{
223 struct syslet_data *sd = td->io_ops->data;
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100224 struct syslet_uatom *done;
Jens Axboe9ff9de62007-02-23 13:21:45 +0100225
226 if (!sd->head)
227 return 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100228
Jens Axboe5b38ee82007-02-26 10:44:22 +0100229 assert(!sd->tail->next);
230
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100231 if (!sd->ahu.new_thread_stack)
232 sd->ahu.new_thread_stack = thread_stack_alloc();
233
Jens Axboea0a930e2007-02-27 19:47:13 +0100234 fio_syslet_queued(td, sd);
235
Jens Axboe7d44a742007-02-14 17:32:08 +0100236 /*
237 * On sync completion, the atom is returned. So on NULL return
238 * it's queued asynchronously.
239 */
Jens Axboe9ff9de62007-02-23 13:21:45 +0100240 done = async_exec(sd->head, &sd->ahu);
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100241
Jens Axboe76f58b92007-03-29 13:26:17 +0200242 if (done == (void *) -1) {
243 log_err("fio: syslets don't appear to work\n");
244 return -1;
245 }
246
Jens Axboe9ff9de62007-02-23 13:21:45 +0100247 sd->head = sd->tail = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100248
Jens Axboe9ff9de62007-02-23 13:21:45 +0100249 if (done)
250 fio_syslet_complete_atom(td, done);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100251
Jens Axboe9ff9de62007-02-23 13:21:45 +0100252 return 0;
253}
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100254
Jens Axboe9ff9de62007-02-23 13:21:45 +0100255static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
256{
257 struct syslet_data *sd = td->io_ops->data;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100258
Jens Axboe7101d9c2007-09-12 13:12:39 +0200259 fio_ro_check(td, io_u);
260
Jens Axboe9ff9de62007-02-23 13:21:45 +0100261 if (sd->tail) {
Jens Axboe67f5b272007-09-28 08:48:52 +0200262 sd->tail->next = (uint64_t) (unsigned long) &io_u->req.atom;
Jens Axboe9ff9de62007-02-23 13:21:45 +0100263 sd->tail = &io_u->req.atom;
264 } else
Zach Brown1d79a6d2007-09-18 16:28:48 -0700265 sd->head = sd->tail = (struct syslet_uatom *)&io_u->req.atom;
Jens Axboe9ff9de62007-02-23 13:21:45 +0100266
Jens Axboe5b38ee82007-02-26 10:44:22 +0100267 io_u->req.head = sd->head;
Jens Axboe9ff9de62007-02-23 13:21:45 +0100268 return FIO_Q_QUEUED;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100269}
270
Jens Axboedb64e9b2007-02-14 02:10:59 +0100271static int async_head_init(struct syslet_data *sd, unsigned int depth)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100272{
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100273 unsigned long ring_size;
274
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100275 memset(&sd->ahu, 0, sizeof(struct async_head_user));
Jens Axboe2ca50be2007-02-14 08:31:15 +0100276
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100277 ring_size = sizeof(struct syslet_uatom *) * depth;
278 sd->ring = malloc(ring_size);
279 memset(sd->ring, 0, ring_size);
280
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100281 sd->ahu.user_ring_idx = 0;
Jens Axboe67f5b272007-09-28 08:48:52 +0200282 sd->ahu.completion_ring_ptr = (uint64_t) (unsigned long) sd->ring;
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100283 sd->ahu.ring_size_bytes = ring_size;
284 sd->ahu.head_stack = thread_stack_alloc();
Jens Axboe67f5b272007-09-28 08:48:52 +0200285 sd->ahu.head_ip = (uint64_t) (unsigned long) cachemiss_thread_start;
286 sd->ahu.new_thread_ip = (uint64_t) (unsigned long) cachemiss_thread_start;
Zach Brown6a117e02007-09-19 08:42:18 +0200287 sd->ahu.new_thread_stack = thread_stack_alloc();
Jens Axboedb64e9b2007-02-14 02:10:59 +0100288
289 return 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100290}
291
Jens Axboe2ca50be2007-02-14 08:31:15 +0100292static void async_head_exit(struct syslet_data *sd)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100293{
Jens Axboe7f059a72007-02-14 08:53:11 +0100294 free(sd->ring);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100295}
296
Jens Axboe76f58b92007-03-29 13:26:17 +0200297static int check_syslet_support(struct syslet_data *sd)
298{
299 struct syslet_uatom atom;
300 void *ret;
301
302 init_atom(&atom, __NR_getpid, NULL, NULL, NULL, NULL, NULL, 0, NULL);
Zach Brownb4d8dda2007-09-19 08:42:19 +0200303 ret = async_exec(&atom, &sd->ahu);
Jens Axboe76f58b92007-03-29 13:26:17 +0200304 if (ret == (void *) -1)
305 return 1;
306
307 return 0;
308}
309
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100310static void fio_syslet_cleanup(struct thread_data *td)
311{
312 struct syslet_data *sd = td->io_ops->data;
313
314 if (sd) {
Jens Axboe2ca50be2007-02-14 08:31:15 +0100315 async_head_exit(sd);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100316 free(sd->events);
317 free(sd);
318 td->io_ops->data = NULL;
319 }
320}
321
322static int fio_syslet_init(struct thread_data *td)
323{
324 struct syslet_data *sd;
325
326 sd = malloc(sizeof(*sd));
327 memset(sd, 0, sizeof(*sd));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100328 sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth);
329 memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth);
Jens Axboedb64e9b2007-02-14 02:10:59 +0100330
331 /*
332 * This will handily fail for kernels where syslet isn't available
333 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100334 if (async_head_init(sd, td->o.iodepth)) {
Jens Axboedb64e9b2007-02-14 02:10:59 +0100335 free(sd->events);
336 free(sd);
337 return 1;
338 }
339
Jens Axboe76f58b92007-03-29 13:26:17 +0200340 if (check_syslet_support(sd)) {
341 log_err("fio: syslets do not appear to work\n");
342 free(sd->events);
343 free(sd);
344 return 1;
345 }
346
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100347 td->io_ops->data = sd;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100348 return 0;
349}
350
351static struct ioengine_ops ioengine = {
352 .name = "syslet-rw",
353 .version = FIO_IOOPS_VERSION,
354 .init = fio_syslet_init,
355 .prep = fio_syslet_prep,
356 .queue = fio_syslet_queue,
Jens Axboe9ff9de62007-02-23 13:21:45 +0100357 .commit = fio_syslet_commit,
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100358 .getevents = fio_syslet_getevents,
359 .event = fio_syslet_event,
360 .cleanup = fio_syslet_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100361 .open_file = generic_open_file,
362 .close_file = generic_close_file,
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100363};
364
365#else /* FIO_HAVE_SYSLET */
366
367/*
368 * When we have a proper configure system in place, we simply wont build
369 * and install this io engine. For now install a crippled version that
370 * just complains and fails to load.
371 */
372static int fio_syslet_init(struct thread_data fio_unused *td)
373{
374 fprintf(stderr, "fio: syslet not available\n");
375 return 1;
376}
377
378static struct ioengine_ops ioengine = {
379 .name = "syslet-rw",
380 .version = FIO_IOOPS_VERSION,
381 .init = fio_syslet_init,
382};
383
384#endif /* FIO_HAVE_SYSLET */
385
386static void fio_init fio_syslet_register(void)
387{
388 register_ioengine(&ioengine);
389}
390
391static void fio_exit fio_syslet_unregister(void)
392{
393 unregister_ioengine(&ioengine);
394}