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 "../lib/fls.h"
19 #ifdef FIO_HAVE_SYSLET
22 #define __NR_fio_pread __NR_pread64
23 #define __NR_fio_pwrite __NR_pwrite64
25 #define __NR_fio_pread __NR_pread
26 #define __NR_fio_pwrite __NR_pwrite
31 unsigned int nr_events;
33 struct syslet_ring *ring;
34 unsigned int ring_mask;
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 uidx = sd->ring->user_tail;
54 for (i = 0; i < nr; i++) {
55 unsigned int idx = (i + uidx) & sd->ring_mask;
56 struct syslet_completion *comp = &sd->ring->comp[idx];
57 struct io_u *io_u = (struct io_u *) (long) comp->caller_data;
62 io_u->resid = io_u->xfer_buflen;
65 io_u->resid = io_u->xfer_buflen - ret;
69 fio_syslet_add_event(td, io_u);
73 static void fio_syslet_wait_for_events(struct thread_data *td)
75 struct syslet_data *sd = td->io_ops->data;
76 struct syslet_ring *ring = sd->ring;
79 unsigned int kh = ring->kernel_head;
83 * first reap events that are already completed
85 if (ring->user_tail != kh) {
86 unsigned int nr = kh - ring->user_tail;
88 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, unsigned int min,
102 unsigned int fio_unused max,
103 struct timespec fio_unused *t)
105 struct syslet_data *sd = td->io_ops->data;
109 * While we have less events than requested, block waiting for them
110 * (if we have to, there may already be more completed events ready
111 * for us - see fio_syslet_wait_for_events()
113 while (sd->nr_events < min)
114 fio_syslet_wait_for_events(td);
121 static struct io_u *fio_syslet_event(struct thread_data *td, int event)
123 struct syslet_data *sd = td->io_ops->data;
125 return sd->events[event];
128 static void fio_syslet_prep_sync(struct fio_file *f,
129 struct indirect_registers *regs)
131 FILL_IN(*regs, __NR_fsync, (long) f->fd);
134 static void fio_syslet_prep_datasync(struct fio_file *f,
135 struct indirect_registers *regs)
137 FILL_IN(*regs, __NR_fdatasync, (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);
163 else if (io_u->ddir == DDIR_DATASYNC)
164 fio_syslet_prep_datasync(f, regs);
166 fio_syslet_prep_rw(io_u, f, regs);
169 static void ret_func(void)
174 static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
176 struct syslet_data *sd = td->io_ops->data;
177 union indirect_params params;
178 struct indirect_registers regs;
181 fio_ro_check(td, io_u);
183 memset(¶ms, 0, sizeof(params));
184 fill_syslet_args(¶ms.syslet, sd->ring, (long)io_u, ret_func, sd->stack);
186 fio_syslet_prep(io_u, ®s);
188 ret = syscall(__NR_indirect, ®s, ¶ms, sizeof(params), 0);
189 if (ret == (int) io_u->xfer_buflen) {
191 * completed sync, account. this also catches fsync().
193 return FIO_Q_COMPLETED;
194 } else if (ret < 0) {
196 * queued for async execution
198 if (errno == ESYSLETPENDING)
203 td_verror(td, io_u->error, "xfer");
204 return FIO_Q_COMPLETED;
207 static int check_syslet_support(struct syslet_data *sd)
209 union indirect_params params;
210 struct indirect_registers regs;
211 pid_t pid, my_pid = getpid();
213 memset(¶ms, 0, sizeof(params));
214 fill_syslet_args(¶ms.syslet, sd->ring, 0, ret_func, sd->stack);
216 FILL_IN(regs, __NR_getpid);
218 pid = syscall(__NR_indirect, ®s, ¶ms, sizeof(params), 0);
225 static void fio_syslet_cleanup(struct thread_data *td)
227 struct syslet_data *sd = td->io_ops->data;
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))
253 ring_nr = 1 << __fls(ring_nr);
255 ring_size = sizeof(struct syslet_ring) +
256 ring_nr * sizeof(struct syslet_completion);
257 if (posix_memalign(&ring, sizeof(uint64_t), ring_size))
259 if (posix_memalign(&stack, page_size, page_size))
263 sd->ring_mask = ring_nr - 1;
266 memset(sd->ring, 0, ring_size);
267 sd->ring->elements = ring_nr;
269 if (!check_syslet_support(sd)) {
270 td->io_ops->data = sd;
274 log_err("fio: syslets do not appear to work\n");
285 static struct ioengine_ops ioengine = {
287 .version = FIO_IOOPS_VERSION,
288 .init = fio_syslet_init,
289 .queue = fio_syslet_queue,
290 .getevents = fio_syslet_getevents,
291 .event = fio_syslet_event,
292 .cleanup = fio_syslet_cleanup,
293 .open_file = generic_open_file,
294 .close_file = generic_close_file,
295 .get_file_size = generic_get_file_size,
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 log_err("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);