4 * IO engine that does regular pread(2)/pwrite(2) to transfer data, but
5 * with syslets to make the execution async.
13 #include <asm/unistd.h>
17 #ifdef FIO_HAVE_SYSLET
20 #define __NR_fio_pread __NR_pread64
21 #define __NR_fio_pwrite __NR_pwrite64
23 #define __NR_fio_pread __NR_pread
24 #define __NR_fio_pwrite __NR_pwrite
29 unsigned int nr_events;
31 struct async_head_user ahu;
32 struct syslet_uatom **ring;
34 struct syslet_uatom *head, *tail;
37 static void fio_syslet_complete_atom(struct thread_data *td,
38 struct syslet_uatom *atom)
40 struct syslet_data *sd = td->io_ops->data;
41 struct syslet_uatom *last;
45 * complete from the beginning of the sequence up to (and
46 * including) this atom
50 atom = io_u->req.head;
53 * now complete in right order
61 io_u->resid = io_u->xfer_buflen - ret;
65 assert(sd->nr_events < td->o.iodepth);
66 sd->events[sd->nr_events++] = io_u;
78 * Inspect the ring to see if we have completed events
80 static void fio_syslet_complete(struct thread_data *td)
82 struct syslet_data *sd = td->io_ops->data;
85 struct syslet_uatom *atom;
87 atom = sd->ring[sd->ahu.user_ring_idx];
91 sd->ring[sd->ahu.user_ring_idx] = NULL;
92 if (++sd->ahu.user_ring_idx == td->o.iodepth)
93 sd->ahu.user_ring_idx = 0;
95 fio_syslet_complete_atom(td, atom);
99 static int fio_syslet_getevents(struct thread_data *td, int min,
101 struct timespec fio_unused *t)
103 struct syslet_data *sd = td->io_ops->data;
107 fio_syslet_complete(td);
110 * do we have enough immediate completions?
112 if (sd->nr_events >= (unsigned int) min)
116 * OK, we need to wait for some events...
118 ret = async_wait(1, sd->ahu.user_ring_idx, &sd->ahu);
128 static struct io_u *fio_syslet_event(struct thread_data *td, int event)
130 struct syslet_data *sd = td->io_ops->data;
132 return sd->events[event];
135 static void init_atom(struct syslet_uatom *atom, int nr, void *arg0,
136 void *arg1, void *arg2, void *arg3, void *ret_ptr,
137 unsigned long flags, void *priv)
141 atom->ret_ptr = ret_ptr;
143 atom->arg_ptr[0] = arg0;
144 atom->arg_ptr[1] = arg1;
145 atom->arg_ptr[2] = arg2;
146 atom->arg_ptr[3] = arg3;
147 atom->arg_ptr[4] = atom->arg_ptr[5] = NULL;
148 atom->private = priv;
152 * Use seek atom for sync
154 static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f)
156 init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL,
157 &io_u->req.ret, 0, io_u);
160 static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f)
167 if (io_u->ddir == DDIR_READ)
170 nr = __NR_fio_pwrite;
172 init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf,
173 &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret, 0, io_u);
176 static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u)
178 struct fio_file *f = io_u->file;
180 if (io_u->ddir == DDIR_SYNC)
181 fio_syslet_prep_sync(io_u, f);
183 fio_syslet_prep_rw(io_u, f);
188 static void cachemiss_thread_start(void)
191 async_thread(NULL, NULL);
194 #define THREAD_STACK_SIZE (16384)
196 static unsigned long thread_stack_alloc()
198 return (unsigned long) malloc(THREAD_STACK_SIZE) + THREAD_STACK_SIZE;
201 static void fio_syslet_queued(struct thread_data *td, struct syslet_data *sd)
203 struct syslet_uatom *atom;
206 fio_gettime(&now, NULL);
210 struct io_u *io_u = atom->private;
212 memcpy(&io_u->issue_time, &now, sizeof(now));
213 io_u_queued(td, io_u);
218 static int fio_syslet_commit(struct thread_data *td)
220 struct syslet_data *sd = td->io_ops->data;
221 struct syslet_uatom *done;
226 assert(!sd->tail->next);
228 if (!sd->ahu.new_thread_stack)
229 sd->ahu.new_thread_stack = thread_stack_alloc();
231 fio_syslet_queued(td, sd);
234 * On sync completion, the atom is returned. So on NULL return
235 * it's queued asynchronously.
237 done = async_exec(sd->head, &sd->ahu);
239 if (done == (void *) -1) {
240 log_err("fio: syslets don't appear to work\n");
244 sd->head = sd->tail = NULL;
247 fio_syslet_complete_atom(td, done);
252 static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
254 struct syslet_data *sd = td->io_ops->data;
257 sd->tail->next = &io_u->req.atom;
258 sd->tail = &io_u->req.atom;
260 sd->head = sd->tail = &io_u->req.atom;
262 io_u->req.head = sd->head;
266 static int async_head_init(struct syslet_data *sd, unsigned int depth)
268 unsigned long ring_size;
270 memset(&sd->ahu, 0, sizeof(struct async_head_user));
272 ring_size = sizeof(struct syslet_uatom *) * depth;
273 sd->ring = malloc(ring_size);
274 memset(sd->ring, 0, ring_size);
276 sd->ahu.user_ring_idx = 0;
277 sd->ahu.completion_ring = sd->ring;
278 sd->ahu.ring_size_bytes = ring_size;
279 sd->ahu.head_stack = thread_stack_alloc();
280 sd->ahu.head_eip = (unsigned long) cachemiss_thread_start;
281 sd->ahu.new_thread_eip = (unsigned long) cachemiss_thread_start;
286 static void async_head_exit(struct syslet_data *sd)
291 static int check_syslet_support(struct syslet_data *sd)
293 struct syslet_uatom atom;
296 init_atom(&atom, __NR_getpid, NULL, NULL, NULL, NULL, NULL, 0, NULL);
297 ret = async_exec(sd->head, &sd->ahu);
298 if (ret == (void *) -1)
304 static void fio_syslet_cleanup(struct thread_data *td)
306 struct syslet_data *sd = td->io_ops->data;
312 td->io_ops->data = NULL;
316 static int fio_syslet_init(struct thread_data *td)
318 struct syslet_data *sd;
320 sd = malloc(sizeof(*sd));
321 memset(sd, 0, sizeof(*sd));
322 sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth);
323 memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth);
326 * This will handily fail for kernels where syslet isn't available
328 if (async_head_init(sd, td->o.iodepth)) {
334 if (check_syslet_support(sd)) {
335 log_err("fio: syslets do not appear to work\n");
341 td->io_ops->data = sd;
345 static struct ioengine_ops ioengine = {
347 .version = FIO_IOOPS_VERSION,
348 .init = fio_syslet_init,
349 .prep = fio_syslet_prep,
350 .queue = fio_syslet_queue,
351 .commit = fio_syslet_commit,
352 .getevents = fio_syslet_getevents,
353 .event = fio_syslet_event,
354 .cleanup = fio_syslet_cleanup,
355 .open_file = generic_open_file,
356 .close_file = generic_close_file,
359 #else /* FIO_HAVE_SYSLET */
362 * When we have a proper configure system in place, we simply wont build
363 * and install this io engine. For now install a crippled version that
364 * just complains and fails to load.
366 static int fio_syslet_init(struct thread_data fio_unused *td)
368 fprintf(stderr, "fio: syslet not available\n");
372 static struct ioengine_ops ioengine = {
374 .version = FIO_IOOPS_VERSION,
375 .init = fio_syslet_init,
378 #endif /* FIO_HAVE_SYSLET */
380 static void fio_init fio_syslet_register(void)
382 register_ioengine(&ioengine);
385 static void fio_exit fio_syslet_unregister(void)
387 unregister_ioengine(&ioengine);