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
49 io_u = (struct io_u *)atom->private;
50 atom = io_u->req.head;
53 * now complete in right order
58 io_u = (struct io_u *)atom->private;
59 ret = *(long *)atom->ret_ptr;
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;
71 atom = (struct syslet_uatom *)atom->next;
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 = (uint64_t)ret_ptr;
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;
153 * Use seek atom for sync
155 static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f)
157 init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL,
158 &io_u->req.ret, 0, io_u);
161 static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f)
168 if (io_u->ddir == DDIR_READ)
171 nr = __NR_fio_pwrite;
173 init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf,
174 &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret, 0, io_u);
177 static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u)
179 struct fio_file *f = io_u->file;
181 if (io_u->ddir == DDIR_SYNC)
182 fio_syslet_prep_sync(io_u, f);
184 fio_syslet_prep_rw(io_u, f);
189 static void cachemiss_thread_start(void)
192 async_thread(NULL, NULL);
195 #define THREAD_STACK_SIZE (16384)
197 static unsigned long thread_stack_alloc()
199 return (unsigned long) malloc(THREAD_STACK_SIZE) + THREAD_STACK_SIZE;
202 static void fio_syslet_queued(struct thread_data *td, struct syslet_data *sd)
204 struct syslet_uatom *atom;
207 fio_gettime(&now, NULL);
211 struct io_u *io_u = (struct io_u *)atom->private;
213 memcpy(&io_u->issue_time, &now, sizeof(now));
214 io_u_queued(td, io_u);
215 atom = (struct syslet_uatom *)atom->next;
219 static int fio_syslet_commit(struct thread_data *td)
221 struct syslet_data *sd = td->io_ops->data;
222 struct syslet_uatom *done;
227 assert(!sd->tail->next);
229 if (!sd->ahu.new_thread_stack)
230 sd->ahu.new_thread_stack = thread_stack_alloc();
232 fio_syslet_queued(td, sd);
235 * On sync completion, the atom is returned. So on NULL return
236 * it's queued asynchronously.
238 done = async_exec(sd->head, &sd->ahu);
240 if (done == (void *) -1) {
241 log_err("fio: syslets don't appear to work\n");
245 sd->head = sd->tail = NULL;
248 fio_syslet_complete_atom(td, done);
253 static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
255 struct syslet_data *sd = td->io_ops->data;
257 fio_ro_check(td, io_u);
260 sd->tail->next = (uint64_t)&io_u->req.atom;
261 sd->tail = &io_u->req.atom;
263 sd->head = sd->tail = (struct syslet_uatom *)&io_u->req.atom;
265 io_u->req.head = sd->head;
269 static int async_head_init(struct syslet_data *sd, unsigned int depth)
271 unsigned long ring_size;
273 memset(&sd->ahu, 0, sizeof(struct async_head_user));
275 ring_size = sizeof(struct syslet_uatom *) * depth;
276 sd->ring = malloc(ring_size);
277 memset(sd->ring, 0, ring_size);
279 sd->ahu.user_ring_idx = 0;
280 sd->ahu.completion_ring_ptr = (uint64_t)sd->ring;
281 sd->ahu.ring_size_bytes = ring_size;
282 sd->ahu.head_stack = thread_stack_alloc();
283 sd->ahu.head_ip = (uint64_t)cachemiss_thread_start;
284 sd->ahu.new_thread_ip = (uint64_t)cachemiss_thread_start;
285 sd->ahu.new_thread_stack = thread_stack_alloc();
290 static void async_head_exit(struct syslet_data *sd)
295 static int check_syslet_support(struct syslet_data *sd)
297 struct syslet_uatom atom;
300 init_atom(&atom, __NR_getpid, NULL, NULL, NULL, NULL, NULL, 0, NULL);
301 ret = async_exec(&atom, &sd->ahu);
302 if (ret == (void *) -1)
308 static void fio_syslet_cleanup(struct thread_data *td)
310 struct syslet_data *sd = td->io_ops->data;
316 td->io_ops->data = NULL;
320 static int fio_syslet_init(struct thread_data *td)
322 struct syslet_data *sd;
324 sd = malloc(sizeof(*sd));
325 memset(sd, 0, sizeof(*sd));
326 sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth);
327 memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth);
330 * This will handily fail for kernels where syslet isn't available
332 if (async_head_init(sd, td->o.iodepth)) {
338 if (check_syslet_support(sd)) {
339 log_err("fio: syslets do not appear to work\n");
345 td->io_ops->data = sd;
349 static struct ioengine_ops ioengine = {
351 .version = FIO_IOOPS_VERSION,
352 .init = fio_syslet_init,
353 .prep = fio_syslet_prep,
354 .queue = fio_syslet_queue,
355 .commit = fio_syslet_commit,
356 .getevents = fio_syslet_getevents,
357 .event = fio_syslet_event,
358 .cleanup = fio_syslet_cleanup,
359 .open_file = generic_open_file,
360 .close_file = generic_close_file,
363 #else /* FIO_HAVE_SYSLET */
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.
370 static int fio_syslet_init(struct thread_data fio_unused *td)
372 fprintf(stderr, "fio: syslet not available\n");
376 static struct ioengine_ops ioengine = {
378 .version = FIO_IOOPS_VERSION,
379 .init = fio_syslet_init,
382 #endif /* FIO_HAVE_SYSLET */
384 static void fio_init fio_syslet_register(void)
386 register_ioengine(&ioengine);
389 static void fio_exit fio_syslet_unregister(void)
391 unregister_ioengine(&ioengine);