83c9842404ed424fc6512828510cd7d7498de54c
[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 <asm/unistd.h>
14
15 #include "../fio.h"
16
17 #ifdef FIO_HAVE_SYSLET
18
19 #ifdef __NR_pread64
20 #define __NR_fio_pread  __NR_pread64
21 #define __NR_fio_pwrite __NR_pwrite64
22 #else
23 #define __NR_fio_pread  __NR_pread
24 #define __NR_fio_pwrite __NR_pwrite
25 #endif
26
27 struct syslet_data {
28         struct io_u **events;
29         unsigned int nr_events;
30         
31         struct async_head_user ahu;
32         struct syslet_uatom **ring;
33
34         struct syslet_uatom *head, *tail;
35 };
36
37 static void fio_syslet_complete_atom(struct thread_data *td,
38                                      struct syslet_uatom *atom)
39 {
40         struct syslet_data *sd = td->io_ops->data;
41         struct syslet_uatom *last;
42         struct io_u *io_u;
43
44         /*
45          * complete from the beginning of the sequence up to (and
46          * including) this atom
47          */
48         last = atom;
49         io_u = atom->private;
50         atom = io_u->req.head;
51
52         /*
53          * now complete in right order
54          */
55         do {
56                 long ret;
57
58                 io_u = atom->private;
59                 ret = *atom->ret_ptr;
60                 if (ret >= 0)
61                         io_u->resid = io_u->xfer_buflen - ret;
62                 else if (ret < 0)
63                         io_u->error = ret;
64
65                 assert(sd->nr_events < td->o.iodepth);
66                 sd->events[sd->nr_events++] = io_u;
67
68                 if (atom == last)
69                         break;
70
71                 atom = atom->next;
72         } while (1);
73
74         assert(!last->next);
75 }
76
77 /*
78  * Inspect the ring to see if we have completed events
79  */
80 static void fio_syslet_complete(struct thread_data *td)
81 {
82         struct syslet_data *sd = td->io_ops->data;
83
84         do {
85                 struct syslet_uatom *atom;
86
87                 atom = sd->ring[sd->ahu.user_ring_idx];
88                 if (!atom)
89                         break;
90
91                 sd->ring[sd->ahu.user_ring_idx] = NULL;
92                 if (++sd->ahu.user_ring_idx == td->o.iodepth)
93                         sd->ahu.user_ring_idx = 0;
94
95                 fio_syslet_complete_atom(td, atom);
96         } while (1);
97 }
98
99 static int fio_syslet_getevents(struct thread_data *td, int min,
100                                 int fio_unused max,
101                                 struct timespec fio_unused *t)
102 {
103         struct syslet_data *sd = td->io_ops->data;
104         long ret;
105
106         do {
107                 fio_syslet_complete(td);
108
109                 /*
110                  * do we have enough immediate completions?
111                  */
112                 if (sd->nr_events >= (unsigned int) min)
113                         break;
114
115                 /*
116                  * OK, we need to wait for some events...
117                  */
118                 ret = async_wait(1, sd->ahu.user_ring_idx, &sd->ahu);
119                 if (ret < 0)
120                         return -errno;
121         } while (1);
122
123         ret = sd->nr_events;
124         sd->nr_events = 0;
125         return ret;
126 }
127
128 static struct io_u *fio_syslet_event(struct thread_data *td, int event)
129 {
130         struct syslet_data *sd = td->io_ops->data;
131
132         return sd->events[event];
133 }
134
135 static void init_atom(struct syslet_uatom *atom, int nr, void *arg0,
136                       void *arg1, void *arg2, void *arg3, void *ret_ptr,
137                       unsigned long flags, void *priv)
138 {
139         atom->flags = flags;
140         atom->nr = nr;
141         atom->ret_ptr = ret_ptr;
142         atom->next = NULL;
143         atom->arg_ptr[0] = arg0;
144         atom->arg_ptr[1] = arg1;
145         atom->arg_ptr[2] = arg2;
146         atom->arg_ptr[3] = arg3;
147         atom->arg_ptr[4] = atom->arg_ptr[5] = NULL;
148         atom->private = priv;
149 }
150
151 /*
152  * Use seek atom for sync
153  */
154 static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f)
155 {
156         init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL,
157                   &io_u->req.ret, 0, io_u);
158 }
159
160 static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f)
161 {
162         int nr;
163
164         /*
165          * prepare rw
166          */
167         if (io_u->ddir == DDIR_READ)
168                 nr = __NR_fio_pread;
169         else
170                 nr = __NR_fio_pwrite;
171
172         init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf,
173                   &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret, 0, io_u);
174 }
175
176 static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u)
177 {
178         struct fio_file *f = io_u->file;
179
180         if (io_u->ddir == DDIR_SYNC)
181                 fio_syslet_prep_sync(io_u, f);
182         else
183                 fio_syslet_prep_rw(io_u, f);
184
185         return 0;
186 }
187
188 static void cachemiss_thread_start(void)
189 {
190         while (1)
191                 async_thread(NULL, NULL);
192 }
193
194 #define THREAD_STACK_SIZE (16384)
195
196 static unsigned long thread_stack_alloc()
197 {
198         return (unsigned long) malloc(THREAD_STACK_SIZE) + THREAD_STACK_SIZE;
199 }
200
201 static void fio_syslet_queued(struct thread_data *td, struct syslet_data *sd)
202 {
203         struct syslet_uatom *atom;
204         struct timeval now;
205
206         fio_gettime(&now, NULL);
207
208         atom = sd->head;
209         while (atom) {
210                 struct io_u *io_u = atom->private;
211
212                 memcpy(&io_u->issue_time, &now, sizeof(now));
213                 io_u_queued(td, io_u);
214                 atom = atom->next;
215         }
216 }
217
218 static int fio_syslet_commit(struct thread_data *td)
219 {
220         struct syslet_data *sd = td->io_ops->data;
221         struct syslet_uatom *done;
222
223         if (!sd->head)
224                 return 0;
225
226         assert(!sd->tail->next);
227
228         if (!sd->ahu.new_thread_stack)
229                 sd->ahu.new_thread_stack = thread_stack_alloc();
230
231         fio_syslet_queued(td, sd);
232
233         /*
234          * On sync completion, the atom is returned. So on NULL return
235          * it's queued asynchronously.
236          */
237         done = async_exec(sd->head, &sd->ahu);
238
239         if (done == (void *) -1) {
240                 log_err("fio: syslets don't appear to work\n");
241                 return -1;
242         }
243
244         sd->head = sd->tail = NULL;
245
246         if (done)
247                 fio_syslet_complete_atom(td, done);
248
249         return 0;
250 }
251
252 static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
253 {
254         struct syslet_data *sd = td->io_ops->data;
255
256         fio_ro_check(td, io_u);
257
258         if (sd->tail) {
259                 sd->tail->next = &io_u->req.atom;
260                 sd->tail = &io_u->req.atom;
261         } else
262                 sd->head = sd->tail = &io_u->req.atom;
263
264         io_u->req.head = sd->head;
265         return FIO_Q_QUEUED;
266 }
267
268 static int async_head_init(struct syslet_data *sd, unsigned int depth)
269 {
270         unsigned long ring_size;
271
272         memset(&sd->ahu, 0, sizeof(struct async_head_user));
273
274         ring_size = sizeof(struct syslet_uatom *) * depth;
275         sd->ring = malloc(ring_size);
276         memset(sd->ring, 0, ring_size);
277
278         sd->ahu.user_ring_idx = 0;
279         sd->ahu.completion_ring = sd->ring;
280         sd->ahu.ring_size_bytes = ring_size;
281         sd->ahu.head_stack = thread_stack_alloc();
282         sd->ahu.head_eip = (unsigned long) cachemiss_thread_start;
283         sd->ahu.new_thread_eip = (unsigned long) cachemiss_thread_start;
284         sd->ahu.new_thread_stack = thread_stack_alloc();
285
286         return 0;
287 }
288
289 static void async_head_exit(struct syslet_data *sd)
290 {
291         free(sd->ring);
292 }
293
294 static int check_syslet_support(struct syslet_data *sd)
295 {
296         struct syslet_uatom atom;
297         void *ret;
298
299         init_atom(&atom, __NR_getpid, NULL, NULL, NULL, NULL, NULL, 0, NULL);
300         ret = async_exec(&atom, &sd->ahu);
301         if (ret == (void *) -1)
302                 return 1;
303
304         return 0;
305 }
306
307 static void fio_syslet_cleanup(struct thread_data *td)
308 {
309         struct syslet_data *sd = td->io_ops->data;
310
311         if (sd) {
312                 async_head_exit(sd);
313                 free(sd->events);
314                 free(sd);
315                 td->io_ops->data = NULL;
316         }
317 }
318
319 static int fio_syslet_init(struct thread_data *td)
320 {
321         struct syslet_data *sd;
322
323         sd = malloc(sizeof(*sd));
324         memset(sd, 0, sizeof(*sd));
325         sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth);
326         memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth);
327
328         /*
329          * This will handily fail for kernels where syslet isn't available
330          */
331         if (async_head_init(sd, td->o.iodepth)) {
332                 free(sd->events);
333                 free(sd);
334                 return 1;
335         }
336
337         if (check_syslet_support(sd)) {
338                 log_err("fio: syslets do not appear to work\n");
339                 free(sd->events);
340                 free(sd);
341                 return 1;
342         }
343
344         td->io_ops->data = sd;
345         return 0;
346 }
347
348 static struct ioengine_ops ioengine = {
349         .name           = "syslet-rw",
350         .version        = FIO_IOOPS_VERSION,
351         .init           = fio_syslet_init,
352         .prep           = fio_syslet_prep,
353         .queue          = fio_syslet_queue,
354         .commit         = fio_syslet_commit,
355         .getevents      = fio_syslet_getevents,
356         .event          = fio_syslet_event,
357         .cleanup        = fio_syslet_cleanup,
358         .open_file      = generic_open_file,
359         .close_file     = generic_close_file,
360 };
361
362 #else /* FIO_HAVE_SYSLET */
363
364 /*
365  * When we have a proper configure system in place, we simply wont build
366  * and install this io engine. For now install a crippled version that
367  * just complains and fails to load.
368  */
369 static int fio_syslet_init(struct thread_data fio_unused *td)
370 {
371         fprintf(stderr, "fio: syslet not available\n");
372         return 1;
373 }
374
375 static struct ioengine_ops ioengine = {
376         .name           = "syslet-rw",
377         .version        = FIO_IOOPS_VERSION,
378         .init           = fio_syslet_init,
379 };
380
381 #endif /* FIO_HAVE_SYSLET */
382
383 static void fio_init fio_syslet_register(void)
384 {
385         register_ioengine(&ioengine);
386 }
387
388 static void fio_exit fio_syslet_unregister(void)
389 {
390         unregister_ioengine(&ioengine);
391 }