blob: 4b1b263691f5825ef247b38a564a8c66f6d4f3cb [file] [log] [blame]
Jens Axboea4f4fdd2007-02-14 01:16:39 +01001/*
2 * read/write() engine that uses syslet to be async
3 *
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
10
11#include "../fio.h"
12#include "../os.h"
13
14#ifdef FIO_HAVE_SYSLET
15
16struct syslet_data {
17 struct io_u **events;
18 unsigned int nr_events;
19
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010020 struct async_head_user ahu;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010021 struct syslet_uatom **ring;
Jens Axboe9ff9de62007-02-23 13:21:45 +010022
23 struct syslet_uatom *head, *tail;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010024};
25
Jens Axboe9ff9de62007-02-23 13:21:45 +010026static void fio_syslet_complete_atom(struct thread_data *td,
27 struct syslet_uatom *atom)
28{
29 struct syslet_data *sd = td->io_ops->data;
Jens Axboe5b38ee82007-02-26 10:44:22 +010030 struct syslet_uatom *last;
Jens Axboe9ff9de62007-02-23 13:21:45 +010031 struct io_u *io_u;
Jens Axboe9ff9de62007-02-23 13:21:45 +010032
33 /*
Jens Axboe5b38ee82007-02-26 10:44:22 +010034 * complete from the beginning of the sequence up to (and
35 * including) this atom
Jens Axboe9ff9de62007-02-23 13:21:45 +010036 */
Jens Axboe5b38ee82007-02-26 10:44:22 +010037 last = atom;
38 io_u = atom->private;
39 atom = io_u->req.head;
Jens Axboe9ff9de62007-02-23 13:21:45 +010040
41 /*
42 * now complete in right order
43 */
Jens Axboe5b38ee82007-02-26 10:44:22 +010044 do {
Jens Axboe9ff9de62007-02-23 13:21:45 +010045 long ret;
46
Jens Axboe9ff9de62007-02-23 13:21:45 +010047 io_u = atom->private;
48 ret = *atom->ret_ptr;
49 if (ret > 0)
50 io_u->resid = io_u->xfer_buflen - ret;
51 else if (ret < 0)
52 io_u->error = ret;
53
54 assert(sd->nr_events < td->iodepth);
55 sd->events[sd->nr_events++] = io_u;
Jens Axboe9ff9de62007-02-23 13:21:45 +010056
Jens Axboe5b38ee82007-02-26 10:44:22 +010057 if (atom == last)
58 break;
Jens Axboe9ff9de62007-02-23 13:21:45 +010059
Jens Axboe5b38ee82007-02-26 10:44:22 +010060 atom = atom->next;
61 } while (1);
62
63 assert(!last->next);
Jens Axboe9ff9de62007-02-23 13:21:45 +010064}
65
Jens Axboea4f4fdd2007-02-14 01:16:39 +010066/*
67 * Inspect the ring to see if we have completed events
68 */
69static void fio_syslet_complete(struct thread_data *td)
70{
71 struct syslet_data *sd = td->io_ops->data;
72
73 do {
74 struct syslet_uatom *atom;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010075
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010076 atom = sd->ring[sd->ahu.user_ring_idx];
Jens Axboea4f4fdd2007-02-14 01:16:39 +010077 if (!atom)
78 break;
79
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010080 sd->ring[sd->ahu.user_ring_idx] = NULL;
81 if (++sd->ahu.user_ring_idx == td->iodepth)
82 sd->ahu.user_ring_idx = 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010083
Jens Axboe9ff9de62007-02-23 13:21:45 +010084 fio_syslet_complete_atom(td, atom);
Jens Axboea4f4fdd2007-02-14 01:16:39 +010085 } while (1);
86}
87
88static int fio_syslet_getevents(struct thread_data *td, int min,
89 int fio_unused max,
90 struct timespec fio_unused *t)
91{
92 struct syslet_data *sd = td->io_ops->data;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010093 long ret;
94
95 do {
96 fio_syslet_complete(td);
97
98 /*
99 * do we have enough immediate completions?
100 */
101 if (sd->nr_events >= (unsigned int) min)
102 break;
103
104 /*
105 * OK, we need to wait for some events...
106 */
Jens Axboe9ff9de62007-02-23 13:21:45 +0100107 ret = async_wait(1, sd->ahu.user_ring_idx, &sd->ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100108 if (ret < 0)
Jens Axboee49499f2007-02-22 11:08:52 +0100109 return -errno;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100110 } while (1);
111
112 ret = sd->nr_events;
113 sd->nr_events = 0;
114 return ret;
115}
116
117static struct io_u *fio_syslet_event(struct thread_data *td, int event)
118{
119 struct syslet_data *sd = td->io_ops->data;
120
121 return sd->events[event];
122}
123
124static void init_atom(struct syslet_uatom *atom, int nr, void *arg0,
Jens Axboea2e1b082007-02-14 08:06:19 +0100125 void *arg1, void *arg2, void *arg3, void *ret_ptr,
126 unsigned long flags, void *priv)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100127{
128 atom->flags = flags;
129 atom->nr = nr;
130 atom->ret_ptr = ret_ptr;
Jens Axboea2e1b082007-02-14 08:06:19 +0100131 atom->next = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100132 atom->arg_ptr[0] = arg0;
133 atom->arg_ptr[1] = arg1;
134 atom->arg_ptr[2] = arg2;
Jens Axboea2e1b082007-02-14 08:06:19 +0100135 atom->arg_ptr[3] = arg3;
136 atom->arg_ptr[4] = atom->arg_ptr[5] = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100137 atom->private = priv;
138}
139
140/*
141 * Use seek atom for sync
142 */
143static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f)
144{
Jens Axboea2e1b082007-02-14 08:06:19 +0100145 init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL,
Jens Axboe7d44a742007-02-14 17:32:08 +0100146 &io_u->req.ret, 0, io_u);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100147}
148
149static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f)
150{
151 int nr;
152
153 /*
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100154 * prepare rw
155 */
156 if (io_u->ddir == DDIR_READ)
Jens Axboea2e1b082007-02-14 08:06:19 +0100157 nr = __NR_pread64;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100158 else
Jens Axboea2e1b082007-02-14 08:06:19 +0100159 nr = __NR_pwrite64;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100160
Jens Axboea2e1b082007-02-14 08:06:19 +0100161 init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf,
Jens Axboe7d44a742007-02-14 17:32:08 +0100162 &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret, 0, io_u);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100163}
164
165static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u)
166{
167 struct fio_file *f = io_u->file;
168
169 if (io_u->ddir == DDIR_SYNC)
170 fio_syslet_prep_sync(io_u, f);
171 else
172 fio_syslet_prep_rw(io_u, f);
173
174 return 0;
175}
176
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100177static void cachemiss_thread_start(void)
178{
179 while (1)
Jens Axboe7756b0d2007-02-26 10:22:31 +0100180 async_thread(NULL, NULL);
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100181}
182
183#define THREAD_STACK_SIZE (16384)
184
185static unsigned long thread_stack_alloc()
186{
Jens Axboe5b38ee82007-02-26 10:44:22 +0100187 return (unsigned long) malloc(THREAD_STACK_SIZE) + THREAD_STACK_SIZE;
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100188}
189
Jens Axboe9ff9de62007-02-23 13:21:45 +0100190static int fio_syslet_commit(struct thread_data *td)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100191{
192 struct syslet_data *sd = td->io_ops->data;
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100193 struct syslet_uatom *done;
Jens Axboe9ff9de62007-02-23 13:21:45 +0100194
195 if (!sd->head)
196 return 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100197
Jens Axboe5b38ee82007-02-26 10:44:22 +0100198 assert(!sd->tail->next);
199
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100200 if (!sd->ahu.new_thread_stack)
201 sd->ahu.new_thread_stack = thread_stack_alloc();
202
Jens Axboe7d44a742007-02-14 17:32:08 +0100203 /*
204 * On sync completion, the atom is returned. So on NULL return
205 * it's queued asynchronously.
206 */
Jens Axboe9ff9de62007-02-23 13:21:45 +0100207 done = async_exec(sd->head, &sd->ahu);
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100208
Jens Axboe9ff9de62007-02-23 13:21:45 +0100209 sd->head = sd->tail = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100210
Jens Axboe9ff9de62007-02-23 13:21:45 +0100211 if (done)
212 fio_syslet_complete_atom(td, done);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100213
Jens Axboe9ff9de62007-02-23 13:21:45 +0100214 return 0;
215}
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100216
Jens Axboe9ff9de62007-02-23 13:21:45 +0100217static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
218{
219 struct syslet_data *sd = td->io_ops->data;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100220
Jens Axboe9ff9de62007-02-23 13:21:45 +0100221 if (sd->tail) {
222 sd->tail->next = &io_u->req.atom;
223 sd->tail = &io_u->req.atom;
224 } else
225 sd->head = sd->tail = &io_u->req.atom;
226
Jens Axboe5b38ee82007-02-26 10:44:22 +0100227 io_u->req.head = sd->head;
Jens Axboe9ff9de62007-02-23 13:21:45 +0100228 return FIO_Q_QUEUED;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100229}
230
Jens Axboedb64e9b2007-02-14 02:10:59 +0100231static int async_head_init(struct syslet_data *sd, unsigned int depth)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100232{
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100233 unsigned long ring_size;
234
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100235 memset(&sd->ahu, 0, sizeof(struct async_head_user));
Jens Axboe2ca50be2007-02-14 08:31:15 +0100236
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100237 ring_size = sizeof(struct syslet_uatom *) * depth;
238 sd->ring = malloc(ring_size);
239 memset(sd->ring, 0, ring_size);
240
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100241 sd->ahu.user_ring_idx = 0;
242 sd->ahu.completion_ring = sd->ring;
243 sd->ahu.ring_size_bytes = ring_size;
244 sd->ahu.head_stack = thread_stack_alloc();
Jens Axboe5b38ee82007-02-26 10:44:22 +0100245 sd->ahu.head_eip = (unsigned long) cachemiss_thread_start;
246 sd->ahu.new_thread_eip = (unsigned long) cachemiss_thread_start;
Jens Axboedb64e9b2007-02-14 02:10:59 +0100247
248 return 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100249}
250
Jens Axboe2ca50be2007-02-14 08:31:15 +0100251static void async_head_exit(struct syslet_data *sd)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100252{
Jens Axboe7f059a72007-02-14 08:53:11 +0100253 free(sd->ring);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100254}
255
256static void fio_syslet_cleanup(struct thread_data *td)
257{
258 struct syslet_data *sd = td->io_ops->data;
259
260 if (sd) {
Jens Axboe2ca50be2007-02-14 08:31:15 +0100261 async_head_exit(sd);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100262 free(sd->events);
263 free(sd);
264 td->io_ops->data = NULL;
265 }
266}
267
268static int fio_syslet_init(struct thread_data *td)
269{
270 struct syslet_data *sd;
271
Jens Axboedb64e9b2007-02-14 02:10:59 +0100272
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100273 sd = malloc(sizeof(*sd));
274 memset(sd, 0, sizeof(*sd));
275 sd->events = malloc(sizeof(struct io_u *) * td->iodepth);
276 memset(sd->events, 0, sizeof(struct io_u *) * td->iodepth);
Jens Axboedb64e9b2007-02-14 02:10:59 +0100277
278 /*
279 * This will handily fail for kernels where syslet isn't available
280 */
281 if (async_head_init(sd, td->iodepth)) {
282 free(sd->events);
283 free(sd);
284 return 1;
285 }
286
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100287 td->io_ops->data = sd;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100288 return 0;
289}
290
291static struct ioengine_ops ioengine = {
292 .name = "syslet-rw",
293 .version = FIO_IOOPS_VERSION,
294 .init = fio_syslet_init,
295 .prep = fio_syslet_prep,
296 .queue = fio_syslet_queue,
Jens Axboe9ff9de62007-02-23 13:21:45 +0100297 .commit = fio_syslet_commit,
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100298 .getevents = fio_syslet_getevents,
299 .event = fio_syslet_event,
300 .cleanup = fio_syslet_cleanup,
301};
302
303#else /* FIO_HAVE_SYSLET */
304
305/*
306 * When we have a proper configure system in place, we simply wont build
307 * and install this io engine. For now install a crippled version that
308 * just complains and fails to load.
309 */
310static int fio_syslet_init(struct thread_data fio_unused *td)
311{
312 fprintf(stderr, "fio: syslet not available\n");
313 return 1;
314}
315
316static struct ioengine_ops ioengine = {
317 .name = "syslet-rw",
318 .version = FIO_IOOPS_VERSION,
319 .init = fio_syslet_init,
320};
321
322#endif /* FIO_HAVE_SYSLET */
323
324static void fio_init fio_syslet_register(void)
325{
326 register_ioengine(&ioengine);
327}
328
329static void fio_exit fio_syslet_unregister(void)
330{
331 unregister_ioengine(&ioengine);
332}