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