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
27 #define ATOM_TO_IOU(p) ((struct io_u *) (unsigned long) (p))
31 unsigned int nr_events;
33 struct async_head_user ahu;
34 struct syslet_uatom **ring;
36 struct syslet_uatom *head, *tail;
39 static void fio_syslet_complete_atom(struct thread_data *td,
40 struct syslet_uatom *atom)
42 struct syslet_data *sd = td->io_ops->data;
43 struct syslet_uatom *last;
47 * complete from the beginning of the sequence up to (and
48 * including) this atom
51 io_u = ATOM_TO_IOU(atom);
52 atom = io_u->req.head;
55 * now complete in right order
60 io_u = ATOM_TO_IOU(atom);
61 ret = *(long *) (unsigned long) atom->ret_ptr;
63 io_u->resid = io_u->xfer_buflen - ret;
67 assert(sd->nr_events < td->o.iodepth);
68 sd->events[sd->nr_events++] = io_u;
73 atom = (struct syslet_uatom *) (unsigned long) atom->next;
80 * Inspect the ring to see if we have completed events
82 static void fio_syslet_complete(struct thread_data *td)
84 struct syslet_data *sd = td->io_ops->data;
87 struct syslet_uatom *atom;
89 atom = sd->ring[sd->ahu.user_ring_idx];
93 sd->ring[sd->ahu.user_ring_idx] = NULL;
94 if (++sd->ahu.user_ring_idx == td->o.iodepth)
95 sd->ahu.user_ring_idx = 0;
97 fio_syslet_complete_atom(td, atom);
101 static int fio_syslet_getevents(struct thread_data *td, int min,
103 struct timespec fio_unused *t)
105 struct syslet_data *sd = td->io_ops->data;
109 fio_syslet_complete(td);
112 * do we have enough immediate completions?
114 if (sd->nr_events >= (unsigned int) min)
118 * OK, we need to wait for some events...
120 ret = async_wait(1, sd->ahu.user_ring_idx, &sd->ahu);
130 static struct io_u *fio_syslet_event(struct thread_data *td, int event)
132 struct syslet_data *sd = td->io_ops->data;
134 return sd->events[event];
137 static void init_atom(struct syslet_uatom *atom, int nr, void *arg0,
138 void *arg1, void *arg2, void *arg3, void *ret_ptr,
139 unsigned long flags, void *priv)
143 atom->ret_ptr = (uint64_t) (unsigned long) ret_ptr;
145 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;
149 atom->arg_ptr[4] = 0;
150 atom->arg_ptr[5] = 0;
151 atom->private = (uint64_t) (unsigned long) priv;
155 * Use seek atom for sync
157 static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f)
159 init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL,
160 &io_u->req.ret, 0, io_u);
163 static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f)
170 if (io_u->ddir == DDIR_READ)
173 nr = __NR_fio_pwrite;
175 init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf,
176 &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret, 0, io_u);
179 static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u)
181 struct fio_file *f = io_u->file;
183 if (io_u->ddir == DDIR_SYNC)
184 fio_syslet_prep_sync(io_u, f);
186 fio_syslet_prep_rw(io_u, f);
191 static void cachemiss_thread_start(void)
194 async_thread(NULL, NULL);
197 #define THREAD_STACK_SIZE (16384)
199 static unsigned long thread_stack_alloc()
201 return (unsigned long) malloc(THREAD_STACK_SIZE) + THREAD_STACK_SIZE;
204 static void fio_syslet_queued(struct thread_data *td, struct syslet_data *sd)
206 struct syslet_uatom *atom;
209 fio_gettime(&now, NULL);
213 struct io_u *io_u = ATOM_TO_IOU(atom);
215 memcpy(&io_u->issue_time, &now, sizeof(now));
216 io_u_queued(td, io_u);
217 atom = (struct syslet_uatom *) (unsigned long) atom->next;
221 static int fio_syslet_commit(struct thread_data *td)
223 struct syslet_data *sd = td->io_ops->data;
224 struct syslet_uatom *done;
229 assert(!sd->tail->next);
231 if (!sd->ahu.new_thread_stack)
232 sd->ahu.new_thread_stack = thread_stack_alloc();
234 fio_syslet_queued(td, sd);
237 * On sync completion, the atom is returned. So on NULL return
238 * it's queued asynchronously.
240 done = async_exec(sd->head, &sd->ahu);
242 if (done == (void *) -1) {
243 log_err("fio: syslets don't appear to work\n");
247 sd->head = sd->tail = NULL;
250 fio_syslet_complete_atom(td, done);
255 static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
257 struct syslet_data *sd = td->io_ops->data;
259 fio_ro_check(td, io_u);
262 sd->tail->next = (uint64_t) (unsigned long) &io_u->req.atom;
263 sd->tail = &io_u->req.atom;
265 sd->head = sd->tail = (struct syslet_uatom *)&io_u->req.atom;
267 io_u->req.head = sd->head;
271 static int async_head_init(struct syslet_data *sd, unsigned int depth)
273 unsigned long ring_size;
275 memset(&sd->ahu, 0, sizeof(struct async_head_user));
277 ring_size = sizeof(struct syslet_uatom *) * depth;
278 sd->ring = malloc(ring_size);
279 memset(sd->ring, 0, ring_size);
281 sd->ahu.user_ring_idx = 0;
282 sd->ahu.completion_ring_ptr = (uint64_t) (unsigned long) sd->ring;
283 sd->ahu.ring_size_bytes = ring_size;
284 sd->ahu.head_stack = thread_stack_alloc();
285 sd->ahu.head_ip = (uint64_t) (unsigned long) cachemiss_thread_start;
286 sd->ahu.new_thread_ip = (uint64_t) (unsigned long) cachemiss_thread_start;
287 sd->ahu.new_thread_stack = thread_stack_alloc();
292 static void async_head_exit(struct syslet_data *sd)
297 static int check_syslet_support(struct syslet_data *sd)
299 struct syslet_uatom atom;
302 init_atom(&atom, __NR_getpid, NULL, NULL, NULL, NULL, NULL, 0, NULL);
303 ret = async_exec(&atom, &sd->ahu);
304 if (ret == (void *) -1)
310 static void fio_syslet_cleanup(struct thread_data *td)
312 struct syslet_data *sd = td->io_ops->data;
318 td->io_ops->data = NULL;
322 static int fio_syslet_init(struct thread_data *td)
324 struct syslet_data *sd;
326 sd = malloc(sizeof(*sd));
327 memset(sd, 0, sizeof(*sd));
328 sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth);
329 memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth);
332 * This will handily fail for kernels where syslet isn't available
334 if (async_head_init(sd, td->o.iodepth)) {
340 if (check_syslet_support(sd)) {
341 log_err("fio: syslets do not appear to work\n");
347 td->io_ops->data = sd;
351 static struct ioengine_ops ioengine = {
353 .version = FIO_IOOPS_VERSION,
354 .init = fio_syslet_init,
355 .prep = fio_syslet_prep,
356 .queue = fio_syslet_queue,
357 .commit = fio_syslet_commit,
358 .getevents = fio_syslet_getevents,
359 .event = fio_syslet_event,
360 .cleanup = fio_syslet_cleanup,
361 .open_file = generic_open_file,
362 .close_file = generic_close_file,
365 #else /* FIO_HAVE_SYSLET */
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.
372 static int fio_syslet_init(struct thread_data fio_unused *td)
374 fprintf(stderr, "fio: syslet not available\n");
378 static struct ioengine_ops ioengine = {
380 .version = FIO_IOOPS_VERSION,
381 .init = fio_syslet_init,
384 #endif /* FIO_HAVE_SYSLET */
386 static void fio_init fio_syslet_register(void)
388 register_ioengine(&ioengine);
391 static void fio_exit fio_syslet_unregister(void)
393 unregister_ioengine(&ioengine);