sync engine: cleanup psync/sync seperation
[fio.git] / engines / syslet-rw.c
1 /*
2  * 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.
6  *
7  */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <assert.h>
13 #include <malloc.h>
14 #include <asm/unistd.h>
15
16 #include "../fio.h"
17 #include "../indirect.h"
18 #include "../syslet.h"
19 #include "../fls.h"
20
21 #ifdef FIO_HAVE_SYSLET
22
23 #ifdef __NR_pread64
24 #define __NR_fio_pread  __NR_pread64
25 #define __NR_fio_pwrite __NR_pwrite64
26 #else
27 #define __NR_fio_pread  __NR_pread
28 #define __NR_fio_pwrite __NR_pwrite
29 #endif
30
31 struct syslet_data {
32         struct io_u **events;
33         unsigned int nr_events;
34         
35         struct syslet_ring *ring;
36         unsigned int ring_mask;
37         void *stack;
38 };
39
40 static void fio_syslet_add_event(struct thread_data *td, struct io_u *io_u)
41 {
42         struct syslet_data *sd = td->io_ops->data;
43
44         assert(sd->nr_events < td->o.iodepth);
45         sd->events[sd->nr_events++] = io_u;
46 }
47
48 static void fio_syslet_add_events(struct thread_data *td, unsigned int nr)
49 {
50         struct syslet_data *sd = td->io_ops->data;
51         unsigned int i, uidx;
52
53         uidx = sd->ring->user_tail;
54         read_barrier();
55
56         for (i = 0; i < nr; i++) {
57                 unsigned int idx = (i + uidx) & sd->ring_mask;
58                 struct syslet_completion *comp = &sd->ring->comp[idx];
59                 struct io_u *io_u = (struct io_u *) (long) comp->caller_data;
60                 long ret;
61
62                 ret = comp->status;
63                 if (ret <= 0) {
64                         io_u->resid = io_u->xfer_buflen;
65                         io_u->error = -ret;
66                 } else {
67                         io_u->resid = io_u->xfer_buflen - ret;
68                         io_u->error = 0;
69                 }
70
71                 fio_syslet_add_event(td, io_u);
72         }
73 }
74
75 static void fio_syslet_wait_for_events(struct thread_data *td)
76 {
77         struct syslet_data *sd = td->io_ops->data;
78         struct syslet_ring *ring = sd->ring;
79
80         do {
81                 unsigned int kh = ring->kernel_head;
82                 int ret;
83
84                 /*
85                  * first reap events that are already completed
86                  */
87                 if (ring->user_tail != kh) {
88                         unsigned int nr = kh - ring->user_tail;
89
90                         fio_syslet_add_events(td, nr);
91                         ring->user_tail = kh;
92                         break;
93                 }
94
95                 /*
96                  * block waiting for at least one event
97                  */
98                 ret = syscall(__NR_syslet_ring_wait, ring, ring->user_tail);
99                 assert(!ret);
100         } while (1);
101 }
102
103 static int fio_syslet_getevents(struct thread_data *td, unsigned int min,
104                                 unsigned int fio_unused max,
105                                 struct timespec fio_unused *t)
106 {
107         struct syslet_data *sd = td->io_ops->data;
108         long ret;
109
110         /*
111          * While we have less events than requested, block waiting for them
112          * (if we have to, there may already be more completed events ready
113          * for us - see fio_syslet_wait_for_events()
114          */
115         while (sd->nr_events < min)
116                 fio_syslet_wait_for_events(td);
117
118         ret = sd->nr_events;
119         sd->nr_events = 0;
120         return ret;
121 }
122
123 static struct io_u *fio_syslet_event(struct thread_data *td, int event)
124 {
125         struct syslet_data *sd = td->io_ops->data;
126
127         return sd->events[event];
128 }
129
130 static void fio_syslet_prep_sync(struct fio_file *f,
131                                  struct indirect_registers *regs)
132 {
133         FILL_IN(*regs, __NR_fsync, (long) f->fd);
134 }
135
136 static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f,
137                                struct indirect_registers *regs)
138 {
139         long nr;
140
141         /*
142          * prepare rw
143          */
144         if (io_u->ddir == DDIR_READ)
145                 nr = __NR_fio_pread;
146         else
147                 nr = __NR_fio_pwrite;
148
149         FILL_IN(*regs, nr, (long) f->fd, (long) io_u->xfer_buf,
150                 (long) io_u->xfer_buflen, (long) io_u->offset);
151 }
152
153 static void fio_syslet_prep(struct io_u *io_u, struct indirect_registers *regs)
154 {
155         struct fio_file *f = io_u->file;
156
157         if (io_u->ddir == DDIR_SYNC)
158                 fio_syslet_prep_sync(f, regs);
159         else
160                 fio_syslet_prep_rw(io_u, f, regs);
161 }
162
163 static void ret_func(void)
164 {
165         syscall(__NR_exit);
166 }
167
168 static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
169 {
170         struct syslet_data *sd = td->io_ops->data;
171         union indirect_params params;
172         struct indirect_registers regs;
173         int ret;
174
175         fio_ro_check(td, io_u);
176
177         memset(&params, 0, sizeof(params));
178         fill_syslet_args(&params.syslet, sd->ring, (long)io_u, ret_func, sd->stack);
179
180         fio_syslet_prep(io_u, &regs);
181
182         ret = syscall(__NR_indirect, &regs, &params, sizeof(params), 0);
183         if (ret == (int) io_u->xfer_buflen) {
184                 /*
185                  * completed sync, account. this also catches fsync().
186                  */
187                 return FIO_Q_COMPLETED;
188         } else if (ret < 0) {
189                 /*
190                  * queued for async execution
191                  */
192                 if (errno == ESYSLETPENDING)
193                         return FIO_Q_QUEUED;
194         }
195
196         io_u->error = errno;
197         td_verror(td, io_u->error, "xfer");
198         return FIO_Q_COMPLETED;
199 }
200
201 static int check_syslet_support(struct syslet_data *sd)
202 {
203         union indirect_params params;
204         struct indirect_registers regs;
205         pid_t pid, my_pid = getpid();
206
207         memset(&params, 0, sizeof(params));
208         fill_syslet_args(&params.syslet, sd->ring, 0, ret_func, sd->stack);
209
210         FILL_IN(regs, __NR_getpid);
211
212         pid = syscall(__NR_indirect, &regs, &params, sizeof(params), 0);
213         if (pid == my_pid)
214                 return 0;
215
216         return 1;
217 }
218
219 static void fio_syslet_cleanup(struct thread_data *td)
220 {
221         struct syslet_data *sd = td->io_ops->data;
222
223         if (sd) {
224                 free(sd->events);
225                 free(sd->ring);
226                 free(sd);
227                 td->io_ops->data = NULL;
228         }
229 }
230
231 static int fio_syslet_init(struct thread_data *td)
232 {
233         struct syslet_data *sd;
234         void *ring = NULL, *stack = NULL;
235         unsigned int ring_size, ring_nr;
236
237         sd = malloc(sizeof(*sd));
238         memset(sd, 0, sizeof(*sd));
239
240         sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth);
241         memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth);
242
243         /*
244          * The ring needs to be a power-of-2, so round it up if we have to
245          */
246         ring_nr = td->o.iodepth;
247         if (ring_nr & (ring_nr - 1))
248                 ring_nr = 1 << fls(ring_nr);
249
250         ring_size = sizeof(struct syslet_ring) +
251                         ring_nr * sizeof(struct syslet_completion);
252         if (posix_memalign(&ring, sizeof(uint64_t), ring_size))
253                 goto err_mem;
254         if (posix_memalign(&stack, page_size, page_size))
255                 goto err_mem;
256
257         sd->ring = ring;
258         sd->ring_mask = ring_nr - 1;
259         sd->stack = stack;
260
261         memset(sd->ring, 0, ring_size);
262         sd->ring->elements = ring_nr;
263
264         if (!check_syslet_support(sd)) {
265                 td->io_ops->data = sd;
266                 return 0;
267         }
268
269         log_err("fio: syslets do not appear to work\n");
270 err_mem:
271         free(sd->events);
272         if (ring)
273                 free(ring);
274         if (stack)
275                 free(stack);
276         free(sd);
277         return 1;
278 }
279
280 static struct ioengine_ops ioengine = {
281         .name           = "syslet-rw",
282         .version        = FIO_IOOPS_VERSION,
283         .init           = fio_syslet_init,
284         .queue          = fio_syslet_queue,
285         .getevents      = fio_syslet_getevents,
286         .event          = fio_syslet_event,
287         .cleanup        = fio_syslet_cleanup,
288         .open_file      = generic_open_file,
289         .close_file     = generic_close_file,
290 };
291
292 #else /* FIO_HAVE_SYSLET */
293
294 /*
295  * When we have a proper configure system in place, we simply wont build
296  * and install this io engine. For now install a crippled version that
297  * just complains and fails to load.
298  */
299 static int fio_syslet_init(struct thread_data fio_unused *td)
300 {
301         fprintf(stderr, "fio: syslet not available\n");
302         return 1;
303 }
304
305 static struct ioengine_ops ioengine = {
306         .name           = "syslet-rw",
307         .version        = FIO_IOOPS_VERSION,
308         .init           = fio_syslet_init,
309 };
310
311 #endif /* FIO_HAVE_SYSLET */
312
313 static void fio_init fio_syslet_register(void)
314 {
315         register_ioengine(&ioengine);
316 }
317
318 static void fio_exit fio_syslet_unregister(void)
319 {
320         unregister_ioengine(&ioengine);
321 }