blob: 8a1ab4f0c253edcc16f8aa1dc638c59c11cbe58a [file] [log] [blame]
Jens Axboea4f4fdd2007-02-14 01:16:39 +01001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * syslet engine
3 *
4 * IO engine that does regular pread(2)/pwrite(2) to transfer data, but
5 * with syslets to make the execution async.
Jens Axboea4f4fdd2007-02-14 01:16:39 +01006 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
12#include <assert.h>
Jens Axboe12cbb462007-12-10 20:24:44 +010013#include <malloc.h>
Jens Axboeb8846352007-03-14 14:29:16 +010014#include <asm/unistd.h>
Jens Axboea4f4fdd2007-02-14 01:16:39 +010015
16#include "../fio.h"
Jens Axboe12cbb462007-12-10 20:24:44 +010017#include "../indirect.h"
18#include "../syslet.h"
Jens Axboea4f4fdd2007-02-14 01:16:39 +010019
20#ifdef FIO_HAVE_SYSLET
21
Jens Axboe1760e672007-03-14 20:41:42 +010022#ifdef __NR_pread64
23#define __NR_fio_pread __NR_pread64
24#define __NR_fio_pwrite __NR_pwrite64
25#else
26#define __NR_fio_pread __NR_pread
27#define __NR_fio_pwrite __NR_pwrite
28#endif
29
Jens Axboea4f4fdd2007-02-14 01:16:39 +010030struct syslet_data {
31 struct io_u **events;
32 unsigned int nr_events;
33
Jens Axboe12cbb462007-12-10 20:24:44 +010034 struct syslet_ring *ring;
35 void *stack;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010036};
37
Jens Axboe12cbb462007-12-10 20:24:44 +010038static void fio_syslet_complete(struct thread_data *td, struct io_u *io_u)
Jens Axboe9ff9de62007-02-23 13:21:45 +010039{
40 struct syslet_data *sd = td->io_ops->data;
Jens Axboe9ff9de62007-02-23 13:21:45 +010041
Jens Axboe12cbb462007-12-10 20:24:44 +010042 assert(sd->nr_events < td->o.iodepth);
43 sd->events[sd->nr_events++] = io_u;
Jens Axboe9ff9de62007-02-23 13:21:45 +010044}
45
Jens Axboe12cbb462007-12-10 20:24:44 +010046static void syslet_complete_nr(struct thread_data *td, unsigned int nr)
Jens Axboea4f4fdd2007-02-14 01:16:39 +010047{
48 struct syslet_data *sd = td->io_ops->data;
Jens Axboe12cbb462007-12-10 20:24:44 +010049 unsigned int i;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010050
Jens Axboe12cbb462007-12-10 20:24:44 +010051 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;
55
56 io_u->resid = io_u->xfer_buflen - comp->status;
57 fio_syslet_complete(td, io_u);
58 }
59}
60
61
62static void fio_syslet_wait_for_events(struct thread_data *td)
63{
64 struct syslet_data *sd = td->io_ops->data;
65 struct syslet_ring *ring = sd->ring;
66 unsigned int events;
67
68 events = 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010069 do {
Jens Axboe12cbb462007-12-10 20:24:44 +010070 unsigned int kh = ring->kernel_head;
71 int ret;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010072
Jens Axboe12cbb462007-12-10 20:24:44 +010073 /*
74 * first reap events that are already completed
75 */
76 if (ring->user_tail != kh) {
77 unsigned int nr = kh - ring->user_tail;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010078
Jens Axboe12cbb462007-12-10 20:24:44 +010079 syslet_complete_nr(td, nr);
80 events += nr;
81 ring->user_tail = kh;
82 continue;
83 }
Jens Axboea4f4fdd2007-02-14 01:16:39 +010084
Jens Axboe12cbb462007-12-10 20:24:44 +010085 /*
86 * block waiting for at least one event
87 */
88 ret = syscall(__NR_syslet_ring_wait, ring, ring->user_tail);
89 assert(!ret);
90 } while (!events);
Jens Axboea4f4fdd2007-02-14 01:16:39 +010091}
92
93static int fio_syslet_getevents(struct thread_data *td, int min,
94 int fio_unused max,
95 struct timespec fio_unused *t)
96{
97 struct syslet_data *sd = td->io_ops->data;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010098 long ret;
99
100 do {
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100101 /*
102 * do we have enough immediate completions?
103 */
104 if (sd->nr_events >= (unsigned int) min)
105 break;
106
Jens Axboe12cbb462007-12-10 20:24:44 +0100107 fio_syslet_wait_for_events(td);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100108 } while (1);
109
110 ret = sd->nr_events;
111 sd->nr_events = 0;
112 return ret;
113}
114
115static struct io_u *fio_syslet_event(struct thread_data *td, int event)
116{
117 struct syslet_data *sd = td->io_ops->data;
118
119 return sd->events[event];
120}
121
Jens Axboe12cbb462007-12-10 20:24:44 +0100122static void fio_syslet_prep_sync(struct fio_file *f,
123 struct indirect_registers *regs)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100124{
Jens Axboe12cbb462007-12-10 20:24:44 +0100125 FILL_IN(*regs, __NR_fsync, (long) f->fd);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100126}
127
Jens Axboe12cbb462007-12-10 20:24:44 +0100128static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f,
129 struct indirect_registers *regs)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100130{
Jens Axboe12cbb462007-12-10 20:24:44 +0100131 long nr;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100132
133 /*
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100134 * prepare rw
135 */
136 if (io_u->ddir == DDIR_READ)
Jens Axboe1760e672007-03-14 20:41:42 +0100137 nr = __NR_fio_pread;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100138 else
Jens Axboe1760e672007-03-14 20:41:42 +0100139 nr = __NR_fio_pwrite;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100140
Jens Axboe12cbb462007-12-10 20:24:44 +0100141 FILL_IN(*regs, nr, (long) f->fd, (long) io_u->xfer_buf,
142 (long) io_u->xfer_buflen, (long) io_u->offset);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100143}
144
Jens Axboe12cbb462007-12-10 20:24:44 +0100145static void fio_syslet_prep(struct io_u *io_u, struct indirect_registers *regs)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100146{
147 struct fio_file *f = io_u->file;
148
149 if (io_u->ddir == DDIR_SYNC)
Jens Axboe12cbb462007-12-10 20:24:44 +0100150 fio_syslet_prep_sync(f, regs);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100151 else
Jens Axboe12cbb462007-12-10 20:24:44 +0100152 fio_syslet_prep_rw(io_u, f, regs);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100153}
154
Jens Axboe12cbb462007-12-10 20:24:44 +0100155static void ret_func(void)
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100156{
Jens Axboe12cbb462007-12-10 20:24:44 +0100157 syscall(__NR_exit);
Jens Axboe9ff9de62007-02-23 13:21:45 +0100158}
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100159
Jens Axboe9ff9de62007-02-23 13:21:45 +0100160static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
161{
162 struct syslet_data *sd = td->io_ops->data;
Jens Axboe12cbb462007-12-10 20:24:44 +0100163 union indirect_params params;
164 struct indirect_registers regs;
165 int ret;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100166
Jens Axboe7101d9c2007-09-12 13:12:39 +0200167 fio_ro_check(td, io_u);
168
Jens Axboe12cbb462007-12-10 20:24:44 +0100169 memset(&params, 0, sizeof(params));
170 fill_syslet_args(&params.syslet, sd->ring, (long)io_u, ret_func, sd->stack);
Jens Axboe9ff9de62007-02-23 13:21:45 +0100171
Jens Axboe12cbb462007-12-10 20:24:44 +0100172 fio_syslet_prep(io_u, &regs);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100173
Jens Axboe12cbb462007-12-10 20:24:44 +0100174 ret = syscall(__NR_indirect, &regs, &params, sizeof(params), 0);
175 if (ret == (int) io_u->xfer_buflen) {
176 /*
177 * completed sync, account. this also catches fsync().
178 */
179 return FIO_Q_COMPLETED;
180 } else if (ret < 0) {
181 /*
182 * queued for async execution
183 */
184 if (errno == ESYSLETPENDING)
185 return FIO_Q_QUEUED;
186 }
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100187
Jens Axboe12cbb462007-12-10 20:24:44 +0100188 io_u->error = errno;
189 td_verror(td, io_u->error, "xfer");
190 return FIO_Q_COMPLETED;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100191}
192
Jens Axboe76f58b92007-03-29 13:26:17 +0200193static int check_syslet_support(struct syslet_data *sd)
194{
Jens Axboe12cbb462007-12-10 20:24:44 +0100195 union indirect_params params;
196 struct indirect_registers regs;
197 pid_t pid, my_pid = getpid();
Jens Axboe76f58b92007-03-29 13:26:17 +0200198
Jens Axboe12cbb462007-12-10 20:24:44 +0100199 memset(&params, 0, sizeof(params));
200 fill_syslet_args(&params.syslet, sd->ring, 0, ret_func, sd->stack);
Jens Axboe76f58b92007-03-29 13:26:17 +0200201
Jens Axboe12cbb462007-12-10 20:24:44 +0100202 FILL_IN(regs, __NR_getpid);
203
204 pid = syscall(__NR_indirect, &regs, &params, sizeof(params), 0);
205 if (pid == my_pid)
206 return 0;
207
208 return 1;
Jens Axboe76f58b92007-03-29 13:26:17 +0200209}
210
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100211static void fio_syslet_cleanup(struct thread_data *td)
212{
213 struct syslet_data *sd = td->io_ops->data;
214
215 if (sd) {
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100216 free(sd->events);
Jens Axboe12cbb462007-12-10 20:24:44 +0100217 free(sd->ring);
218 free(sd->stack);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100219 free(sd);
220 td->io_ops->data = NULL;
221 }
222}
223
224static int fio_syslet_init(struct thread_data *td)
225{
226 struct syslet_data *sd;
Jens Axboe12cbb462007-12-10 20:24:44 +0100227 void *ring, *stack;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100228
229 sd = malloc(sizeof(*sd));
230 memset(sd, 0, sizeof(*sd));
Jens Axboe12cbb462007-12-10 20:24:44 +0100231
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100232 sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth);
233 memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth);
Jens Axboe12cbb462007-12-10 20:24:44 +0100234 if (posix_memalign(&ring, sizeof(uint64_t), sizeof(struct syslet_ring)))
Jens Axboedb64e9b2007-02-14 02:10:59 +0100235 return 1;
Jens Axboe12cbb462007-12-10 20:24:44 +0100236 if (posix_memalign(&stack, page_size, page_size))
237 return 1;
238
239 sd->ring = ring;
240 sd->stack = stack;
241
242 memset(sd->ring, 0, sizeof(*sd->ring));
243 sd->ring->elements = td->o.iodepth;
Jens Axboedb64e9b2007-02-14 02:10:59 +0100244
Jens Axboe76f58b92007-03-29 13:26:17 +0200245 if (check_syslet_support(sd)) {
246 log_err("fio: syslets do not appear to work\n");
247 free(sd->events);
Jens Axboe12cbb462007-12-10 20:24:44 +0100248 free(sd->ring);
249 free(sd->stack);
Jens Axboe76f58b92007-03-29 13:26:17 +0200250 free(sd);
251 return 1;
252 }
253
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100254 td->io_ops->data = sd;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100255 return 0;
256}
257
258static struct ioengine_ops ioengine = {
259 .name = "syslet-rw",
260 .version = FIO_IOOPS_VERSION,
261 .init = fio_syslet_init,
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100262 .queue = fio_syslet_queue,
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100263 .getevents = fio_syslet_getevents,
264 .event = fio_syslet_event,
265 .cleanup = fio_syslet_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100266 .open_file = generic_open_file,
267 .close_file = generic_close_file,
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100268};
269
270#else /* FIO_HAVE_SYSLET */
271
272/*
273 * When we have a proper configure system in place, we simply wont build
274 * and install this io engine. For now install a crippled version that
275 * just complains and fails to load.
276 */
277static int fio_syslet_init(struct thread_data fio_unused *td)
278{
279 fprintf(stderr, "fio: syslet not available\n");
280 return 1;
281}
282
283static struct ioengine_ops ioengine = {
284 .name = "syslet-rw",
285 .version = FIO_IOOPS_VERSION,
286 .init = fio_syslet_init,
287};
288
289#endif /* FIO_HAVE_SYSLET */
290
291static void fio_init fio_syslet_register(void)
292{
293 register_ioengine(&ioengine);
294}
295
296static void fio_exit fio_syslet_unregister(void)
297{
298 unregister_ioengine(&ioengine);
299}