4 * IO engine that does regular pread(2)/pwrite(2) to transfer data, but
5 * with syslets to make the execution async.
14 #include <asm/unistd.h>
17 #include "../indirect.h"
18 #include "../syslet.h"
20 #ifdef FIO_HAVE_SYSLET
23 #define __NR_fio_pread __NR_pread64
24 #define __NR_fio_pwrite __NR_pwrite64
26 #define __NR_fio_pread __NR_pread
27 #define __NR_fio_pwrite __NR_pwrite
32 unsigned int nr_events;
34 struct syslet_ring *ring;
38 static void fio_syslet_add_event(struct thread_data *td, struct io_u *io_u)
40 struct syslet_data *sd = td->io_ops->data;
42 assert(sd->nr_events < td->o.iodepth);
43 sd->events[sd->nr_events++] = io_u;
46 static void fio_syslet_add_events(struct thread_data *td, unsigned int nr)
48 struct syslet_data *sd = td->io_ops->data;
51 for (i = 0; i < nr; i++) {
52 unsigned int idx = (i + sd->ring->user_tail) % td->o.iodepth;
53 struct syslet_completion *comp = &sd->ring->comp[idx];
54 struct io_u *io_u = (struct io_u *) (long) comp->caller_data;
59 io_u->resid = io_u->xfer_buflen;
62 io_u->resid = io_u->xfer_buflen - ret;
66 fio_syslet_add_event(td, io_u);
70 static void fio_syslet_wait_for_events(struct thread_data *td)
72 struct syslet_data *sd = td->io_ops->data;
73 struct syslet_ring *ring = sd->ring;
78 unsigned int kh = ring->kernel_head;
82 * first reap events that are already completed
84 if (ring->user_tail != kh) {
85 unsigned int nr = kh - ring->user_tail;
87 fio_syslet_add_events(td, nr);
94 * block waiting for at least one event
96 ret = syscall(__NR_syslet_ring_wait, ring, ring->user_tail);
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;
110 * do we have enough immediate completions?
112 if (sd->nr_events >= (unsigned int) min)
115 fio_syslet_wait_for_events(td);
123 static struct io_u *fio_syslet_event(struct thread_data *td, int event)
125 struct syslet_data *sd = td->io_ops->data;
127 return sd->events[event];
130 static void fio_syslet_prep_sync(struct fio_file *f,
131 struct indirect_registers *regs)
133 FILL_IN(*regs, __NR_fsync, (long) f->fd);
136 static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f,
137 struct indirect_registers *regs)
144 if (io_u->ddir == DDIR_READ)
147 nr = __NR_fio_pwrite;
149 FILL_IN(*regs, nr, (long) f->fd, (long) io_u->xfer_buf,
150 (long) io_u->xfer_buflen, (long) io_u->offset);
153 static void fio_syslet_prep(struct io_u *io_u, struct indirect_registers *regs)
155 struct fio_file *f = io_u->file;
157 if (io_u->ddir == DDIR_SYNC)
158 fio_syslet_prep_sync(f, regs);
160 fio_syslet_prep_rw(io_u, f, regs);
163 static void ret_func(void)
168 static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
170 struct syslet_data *sd = td->io_ops->data;
171 union indirect_params params;
172 struct indirect_registers regs;
175 fio_ro_check(td, io_u);
177 memset(¶ms, 0, sizeof(params));
178 fill_syslet_args(¶ms.syslet, sd->ring, (long)io_u, ret_func, sd->stack);
180 fio_syslet_prep(io_u, ®s);
182 ret = syscall(__NR_indirect, ®s, ¶ms, sizeof(params), 0);
183 if (ret == (int) io_u->xfer_buflen) {
185 * completed sync, account. this also catches fsync().
187 return FIO_Q_COMPLETED;
188 } else if (ret < 0) {
190 * queued for async execution
192 if (errno == ESYSLETPENDING)
197 td_verror(td, io_u->error, "xfer");
198 return FIO_Q_COMPLETED;
201 static int check_syslet_support(struct syslet_data *sd)
203 union indirect_params params;
204 struct indirect_registers regs;
205 pid_t pid, my_pid = getpid();
207 memset(¶ms, 0, sizeof(params));
208 fill_syslet_args(¶ms.syslet, sd->ring, 0, ret_func, sd->stack);
210 FILL_IN(regs, __NR_getpid);
212 pid = syscall(__NR_indirect, ®s, ¶ms, sizeof(params), 0);
219 static void fio_syslet_cleanup(struct thread_data *td)
221 struct syslet_data *sd = td->io_ops->data;
228 td->io_ops->data = NULL;
232 static int fio_syslet_init(struct thread_data *td)
234 struct syslet_data *sd;
235 void *ring = NULL, *stack = NULL;
236 unsigned int ring_size, ring_nr;
238 sd = malloc(sizeof(*sd));
239 memset(sd, 0, sizeof(*sd));
241 sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth);
242 memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth);
245 * The ring needs to be a power-of-2, so round it up if we have to
247 ring_nr = td->o.iodepth;
248 if (ring_nr & (ring_nr - 1)) {
251 while (ring_nr >>= 1)
257 ring_size = sizeof(struct syslet_ring) +
258 ring_nr * sizeof(struct syslet_completion);
259 if (posix_memalign(&ring, sizeof(uint64_t), ring_size))
261 if (posix_memalign(&stack, page_size, page_size))
267 memset(sd->ring, 0, ring_size);
268 sd->ring->elements = ring_nr;
270 if (!check_syslet_support(sd)) {
271 td->io_ops->data = sd;
275 log_err("fio: syslets do not appear to work\n");
286 static struct ioengine_ops ioengine = {
288 .version = FIO_IOOPS_VERSION,
289 .init = fio_syslet_init,
290 .queue = fio_syslet_queue,
291 .getevents = fio_syslet_getevents,
292 .event = fio_syslet_event,
293 .cleanup = fio_syslet_cleanup,
294 .open_file = generic_open_file,
295 .close_file = generic_close_file,
298 #else /* FIO_HAVE_SYSLET */
301 * When we have a proper configure system in place, we simply wont build
302 * and install this io engine. For now install a crippled version that
303 * just complains and fails to load.
305 static int fio_syslet_init(struct thread_data fio_unused *td)
307 fprintf(stderr, "fio: syslet not available\n");
311 static struct ioengine_ops ioengine = {
313 .version = FIO_IOOPS_VERSION,
314 .init = fio_syslet_init,
317 #endif /* FIO_HAVE_SYSLET */
319 static void fio_init fio_syslet_register(void)
321 register_ioengine(&ioengine);
324 static void fio_exit fio_syslet_unregister(void)
326 unregister_ioengine(&ioengine);