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;
35 unsigned int ring_mask;
39 static void fio_syslet_add_event(struct thread_data *td, struct io_u *io_u)
41 struct syslet_data *sd = td->io_ops->data;
43 assert(sd->nr_events < td->o.iodepth);
44 sd->events[sd->nr_events++] = io_u;
47 static void fio_syslet_add_events(struct thread_data *td, unsigned int nr)
49 struct syslet_data *sd = td->io_ops->data;
52 uidx = sd->ring->user_tail;
55 for (i = 0; i < nr; i++) {
56 unsigned int idx = (i + uidx) & sd->ring_mask;
57 struct syslet_completion *comp = &sd->ring->comp[idx];
58 struct io_u *io_u = (struct io_u *) (long) comp->caller_data;
63 io_u->resid = io_u->xfer_buflen;
66 io_u->resid = io_u->xfer_buflen - ret;
70 fio_syslet_add_event(td, io_u);
74 static void fio_syslet_wait_for_events(struct thread_data *td)
76 struct syslet_data *sd = td->io_ops->data;
77 struct syslet_ring *ring = sd->ring;
82 unsigned int kh = ring->kernel_head;
86 * first reap events that are already completed
88 if (ring->user_tail != kh) {
89 unsigned int nr = kh - ring->user_tail;
91 fio_syslet_add_events(td, nr);
98 * block waiting for at least one event
100 ret = syscall(__NR_syslet_ring_wait, ring, ring->user_tail);
105 static int fio_syslet_getevents(struct thread_data *td, int min,
107 struct timespec fio_unused *t)
109 struct syslet_data *sd = td->io_ops->data;
114 * do we have enough immediate completions?
116 if (sd->nr_events >= (unsigned int) min)
119 fio_syslet_wait_for_events(td);
127 static struct io_u *fio_syslet_event(struct thread_data *td, int event)
129 struct syslet_data *sd = td->io_ops->data;
131 return sd->events[event];
134 static void fio_syslet_prep_sync(struct fio_file *f,
135 struct indirect_registers *regs)
137 FILL_IN(*regs, __NR_fsync, (long) f->fd);
140 static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f,
141 struct indirect_registers *regs)
148 if (io_u->ddir == DDIR_READ)
151 nr = __NR_fio_pwrite;
153 FILL_IN(*regs, nr, (long) f->fd, (long) io_u->xfer_buf,
154 (long) io_u->xfer_buflen, (long) io_u->offset);
157 static void fio_syslet_prep(struct io_u *io_u, struct indirect_registers *regs)
159 struct fio_file *f = io_u->file;
161 if (io_u->ddir == DDIR_SYNC)
162 fio_syslet_prep_sync(f, regs);
164 fio_syslet_prep_rw(io_u, f, regs);
167 static void ret_func(void)
172 static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
174 struct syslet_data *sd = td->io_ops->data;
175 union indirect_params params;
176 struct indirect_registers regs;
179 fio_ro_check(td, io_u);
181 memset(¶ms, 0, sizeof(params));
182 fill_syslet_args(¶ms.syslet, sd->ring, (long)io_u, ret_func, sd->stack);
184 fio_syslet_prep(io_u, ®s);
186 ret = syscall(__NR_indirect, ®s, ¶ms, sizeof(params), 0);
187 if (ret == (int) io_u->xfer_buflen) {
189 * completed sync, account. this also catches fsync().
191 return FIO_Q_COMPLETED;
192 } else if (ret < 0) {
194 * queued for async execution
196 if (errno == ESYSLETPENDING)
201 td_verror(td, io_u->error, "xfer");
202 return FIO_Q_COMPLETED;
205 static int check_syslet_support(struct syslet_data *sd)
207 union indirect_params params;
208 struct indirect_registers regs;
209 pid_t pid, my_pid = getpid();
211 memset(¶ms, 0, sizeof(params));
212 fill_syslet_args(¶ms.syslet, sd->ring, 0, ret_func, sd->stack);
214 FILL_IN(regs, __NR_getpid);
216 pid = syscall(__NR_indirect, ®s, ¶ms, sizeof(params), 0);
223 static void fio_syslet_cleanup(struct thread_data *td)
225 struct syslet_data *sd = td->io_ops->data;
232 td->io_ops->data = NULL;
236 static int fio_syslet_init(struct thread_data *td)
238 struct syslet_data *sd;
239 void *ring = NULL, *stack = NULL;
240 unsigned int ring_size, ring_nr;
242 sd = malloc(sizeof(*sd));
243 memset(sd, 0, sizeof(*sd));
245 sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth);
246 memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth);
249 * The ring needs to be a power-of-2, so round it up if we have to
251 ring_nr = td->o.iodepth;
252 if (ring_nr & (ring_nr - 1)) {
255 while (ring_nr >>= 1)
261 ring_size = sizeof(struct syslet_ring) +
262 ring_nr * sizeof(struct syslet_completion);
263 if (posix_memalign(&ring, sizeof(uint64_t), ring_size))
265 if (posix_memalign(&stack, page_size, page_size))
269 sd->ring_mask = ring_nr - 1;
272 memset(sd->ring, 0, ring_size);
273 sd->ring->elements = ring_nr;
275 if (!check_syslet_support(sd)) {
276 td->io_ops->data = sd;
280 log_err("fio: syslets do not appear to work\n");
291 static struct ioengine_ops ioengine = {
293 .version = FIO_IOOPS_VERSION,
294 .init = fio_syslet_init,
295 .queue = fio_syslet_queue,
296 .getevents = fio_syslet_getevents,
297 .event = fio_syslet_event,
298 .cleanup = fio_syslet_cleanup,
299 .open_file = generic_open_file,
300 .close_file = generic_close_file,
303 #else /* FIO_HAVE_SYSLET */
306 * When we have a proper configure system in place, we simply wont build
307 * and install this io engine. For now install a crippled version that
308 * just complains and fails to load.
310 static int fio_syslet_init(struct thread_data fio_unused *td)
312 fprintf(stderr, "fio: syslet not available\n");
316 static struct ioengine_ops ioengine = {
318 .version = FIO_IOOPS_VERSION,
319 .init = fio_syslet_init,
322 #endif /* FIO_HAVE_SYSLET */
324 static void fio_init fio_syslet_register(void)
326 register_ioengine(&ioengine);
329 static void fio_exit fio_syslet_unregister(void)
331 unregister_ioengine(&ioengine);