blob: 279884a5d041094774cb7bfcde31266fb642b2fe [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>
13
14#include "../fio.h"
15#include "../os.h"
16
17#ifdef FIO_HAVE_SYSLET
18
19struct syslet_data {
20 struct io_u **events;
21 unsigned int nr_events;
22
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010023 struct async_head_user ahu;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010024 struct syslet_uatom **ring;
Jens Axboe9ff9de62007-02-23 13:21:45 +010025
26 struct syslet_uatom *head, *tail;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010027};
28
Jens Axboe9ff9de62007-02-23 13:21:45 +010029static void fio_syslet_complete_atom(struct thread_data *td,
30 struct syslet_uatom *atom)
31{
32 struct syslet_data *sd = td->io_ops->data;
Jens Axboe5b38ee82007-02-26 10:44:22 +010033 struct syslet_uatom *last;
Jens Axboe9ff9de62007-02-23 13:21:45 +010034 struct io_u *io_u;
Jens Axboe9ff9de62007-02-23 13:21:45 +010035
36 /*
Jens Axboe5b38ee82007-02-26 10:44:22 +010037 * complete from the beginning of the sequence up to (and
38 * including) this atom
Jens Axboe9ff9de62007-02-23 13:21:45 +010039 */
Jens Axboe5b38ee82007-02-26 10:44:22 +010040 last = atom;
41 io_u = atom->private;
42 atom = io_u->req.head;
Jens Axboe9ff9de62007-02-23 13:21:45 +010043
44 /*
45 * now complete in right order
46 */
Jens Axboe5b38ee82007-02-26 10:44:22 +010047 do {
Jens Axboe9ff9de62007-02-23 13:21:45 +010048 long ret;
49
Jens Axboe9ff9de62007-02-23 13:21:45 +010050 io_u = atom->private;
51 ret = *atom->ret_ptr;
Jens Axboee2e67912007-03-12 09:43:05 +010052 if (ret >= 0)
Jens Axboe9ff9de62007-02-23 13:21:45 +010053 io_u->resid = io_u->xfer_buflen - ret;
54 else if (ret < 0)
55 io_u->error = ret;
56
57 assert(sd->nr_events < td->iodepth);
58 sd->events[sd->nr_events++] = io_u;
Jens Axboe9ff9de62007-02-23 13:21:45 +010059
Jens Axboe5b38ee82007-02-26 10:44:22 +010060 if (atom == last)
61 break;
Jens Axboe9ff9de62007-02-23 13:21:45 +010062
Jens Axboe5b38ee82007-02-26 10:44:22 +010063 atom = atom->next;
64 } while (1);
65
66 assert(!last->next);
Jens Axboe9ff9de62007-02-23 13:21:45 +010067}
68
Jens Axboea4f4fdd2007-02-14 01:16:39 +010069/*
70 * Inspect the ring to see if we have completed events
71 */
72static void fio_syslet_complete(struct thread_data *td)
73{
74 struct syslet_data *sd = td->io_ops->data;
75
76 do {
77 struct syslet_uatom *atom;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010078
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010079 atom = sd->ring[sd->ahu.user_ring_idx];
Jens Axboea4f4fdd2007-02-14 01:16:39 +010080 if (!atom)
81 break;
82
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010083 sd->ring[sd->ahu.user_ring_idx] = NULL;
84 if (++sd->ahu.user_ring_idx == td->iodepth)
85 sd->ahu.user_ring_idx = 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010086
Jens Axboe9ff9de62007-02-23 13:21:45 +010087 fio_syslet_complete_atom(td, atom);
Jens Axboea4f4fdd2007-02-14 01:16:39 +010088 } while (1);
89}
90
91static int fio_syslet_getevents(struct thread_data *td, int min,
92 int fio_unused max,
93 struct timespec fio_unused *t)
94{
95 struct syslet_data *sd = td->io_ops->data;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010096 long ret;
97
98 do {
99 fio_syslet_complete(td);
100
101 /*
102 * do we have enough immediate completions?
103 */
104 if (sd->nr_events >= (unsigned int) min)
105 break;
106
107 /*
108 * OK, we need to wait for some events...
109 */
Jens Axboe9ff9de62007-02-23 13:21:45 +0100110 ret = async_wait(1, sd->ahu.user_ring_idx, &sd->ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100111 if (ret < 0)
Jens Axboee49499f2007-02-22 11:08:52 +0100112 return -errno;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100113 } while (1);
114
115 ret = sd->nr_events;
116 sd->nr_events = 0;
117 return ret;
118}
119
120static 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
127static void init_atom(struct syslet_uatom *atom, int nr, void *arg0,
Jens Axboea2e1b082007-02-14 08:06:19 +0100128 void *arg1, void *arg2, void *arg3, void *ret_ptr,
129 unsigned long flags, void *priv)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100130{
131 atom->flags = flags;
132 atom->nr = nr;
133 atom->ret_ptr = ret_ptr;
Jens Axboea2e1b082007-02-14 08:06:19 +0100134 atom->next = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100135 atom->arg_ptr[0] = arg0;
136 atom->arg_ptr[1] = arg1;
137 atom->arg_ptr[2] = arg2;
Jens Axboea2e1b082007-02-14 08:06:19 +0100138 atom->arg_ptr[3] = arg3;
139 atom->arg_ptr[4] = atom->arg_ptr[5] = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100140 atom->private = priv;
141}
142
143/*
144 * Use seek atom for sync
145 */
146static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f)
147{
Jens Axboea2e1b082007-02-14 08:06:19 +0100148 init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL,
Jens Axboe7d44a742007-02-14 17:32:08 +0100149 &io_u->req.ret, 0, io_u);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100150}
151
152static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f)
153{
154 int nr;
155
156 /*
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100157 * prepare rw
158 */
159 if (io_u->ddir == DDIR_READ)
Jens Axboea2e1b082007-02-14 08:06:19 +0100160 nr = __NR_pread64;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100161 else
Jens Axboea2e1b082007-02-14 08:06:19 +0100162 nr = __NR_pwrite64;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100163
Jens Axboea2e1b082007-02-14 08:06:19 +0100164 init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf,
Jens Axboe7d44a742007-02-14 17:32:08 +0100165 &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret, 0, io_u);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100166}
167
168static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u)
169{
170 struct fio_file *f = io_u->file;
171
172 if (io_u->ddir == DDIR_SYNC)
173 fio_syslet_prep_sync(io_u, f);
174 else
175 fio_syslet_prep_rw(io_u, f);
176
177 return 0;
178}
179
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100180static void cachemiss_thread_start(void)
181{
182 while (1)
Jens Axboe7756b0d2007-02-26 10:22:31 +0100183 async_thread(NULL, NULL);
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100184}
185
186#define THREAD_STACK_SIZE (16384)
187
188static unsigned long thread_stack_alloc()
189{
Jens Axboe5b38ee82007-02-26 10:44:22 +0100190 return (unsigned long) malloc(THREAD_STACK_SIZE) + THREAD_STACK_SIZE;
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100191}
192
Jens Axboea0a930e2007-02-27 19:47:13 +0100193static void fio_syslet_queued(struct thread_data *td, struct syslet_data *sd)
194{
195 struct syslet_uatom *atom;
196 struct timeval now;
197
198 fio_gettime(&now, NULL);
199
200 atom = sd->head;
201 while (atom) {
202 struct io_u *io_u = atom->private;
203
204 memcpy(&io_u->issue_time, &now, sizeof(now));
205 io_u_queued(td, io_u);
206 atom = atom->next;
207 }
208}
209
Jens Axboe9ff9de62007-02-23 13:21:45 +0100210static int fio_syslet_commit(struct thread_data *td)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100211{
212 struct syslet_data *sd = td->io_ops->data;
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100213 struct syslet_uatom *done;
Jens Axboe9ff9de62007-02-23 13:21:45 +0100214
215 if (!sd->head)
216 return 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100217
Jens Axboe5b38ee82007-02-26 10:44:22 +0100218 assert(!sd->tail->next);
219
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100220 if (!sd->ahu.new_thread_stack)
221 sd->ahu.new_thread_stack = thread_stack_alloc();
222
Jens Axboea0a930e2007-02-27 19:47:13 +0100223 fio_syslet_queued(td, sd);
224
Jens Axboe7d44a742007-02-14 17:32:08 +0100225 /*
226 * On sync completion, the atom is returned. So on NULL return
227 * it's queued asynchronously.
228 */
Jens Axboe9ff9de62007-02-23 13:21:45 +0100229 done = async_exec(sd->head, &sd->ahu);
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100230
Jens Axboe9ff9de62007-02-23 13:21:45 +0100231 sd->head = sd->tail = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100232
Jens Axboe9ff9de62007-02-23 13:21:45 +0100233 if (done)
234 fio_syslet_complete_atom(td, done);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100235
Jens Axboe9ff9de62007-02-23 13:21:45 +0100236 return 0;
237}
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100238
Jens Axboe9ff9de62007-02-23 13:21:45 +0100239static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
240{
241 struct syslet_data *sd = td->io_ops->data;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100242
Jens Axboe9ff9de62007-02-23 13:21:45 +0100243 if (sd->tail) {
244 sd->tail->next = &io_u->req.atom;
245 sd->tail = &io_u->req.atom;
246 } else
247 sd->head = sd->tail = &io_u->req.atom;
248
Jens Axboe5b38ee82007-02-26 10:44:22 +0100249 io_u->req.head = sd->head;
Jens Axboe9ff9de62007-02-23 13:21:45 +0100250 return FIO_Q_QUEUED;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100251}
252
Jens Axboedb64e9b2007-02-14 02:10:59 +0100253static int async_head_init(struct syslet_data *sd, unsigned int depth)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100254{
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100255 unsigned long ring_size;
256
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100257 memset(&sd->ahu, 0, sizeof(struct async_head_user));
Jens Axboe2ca50be2007-02-14 08:31:15 +0100258
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100259 ring_size = sizeof(struct syslet_uatom *) * depth;
260 sd->ring = malloc(ring_size);
261 memset(sd->ring, 0, ring_size);
262
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100263 sd->ahu.user_ring_idx = 0;
264 sd->ahu.completion_ring = sd->ring;
265 sd->ahu.ring_size_bytes = ring_size;
266 sd->ahu.head_stack = thread_stack_alloc();
Jens Axboe5b38ee82007-02-26 10:44:22 +0100267 sd->ahu.head_eip = (unsigned long) cachemiss_thread_start;
268 sd->ahu.new_thread_eip = (unsigned long) cachemiss_thread_start;
Jens Axboedb64e9b2007-02-14 02:10:59 +0100269
270 return 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100271}
272
Jens Axboe2ca50be2007-02-14 08:31:15 +0100273static void async_head_exit(struct syslet_data *sd)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100274{
Jens Axboe7f059a72007-02-14 08:53:11 +0100275 free(sd->ring);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100276}
277
278static void fio_syslet_cleanup(struct thread_data *td)
279{
280 struct syslet_data *sd = td->io_ops->data;
281
282 if (sd) {
Jens Axboe2ca50be2007-02-14 08:31:15 +0100283 async_head_exit(sd);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100284 free(sd->events);
285 free(sd);
286 td->io_ops->data = NULL;
287 }
288}
289
290static int fio_syslet_init(struct thread_data *td)
291{
292 struct syslet_data *sd;
293
Jens Axboedb64e9b2007-02-14 02:10:59 +0100294
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100295 sd = malloc(sizeof(*sd));
296 memset(sd, 0, sizeof(*sd));
297 sd->events = malloc(sizeof(struct io_u *) * td->iodepth);
298 memset(sd->events, 0, sizeof(struct io_u *) * td->iodepth);
Jens Axboedb64e9b2007-02-14 02:10:59 +0100299
300 /*
301 * This will handily fail for kernels where syslet isn't available
302 */
303 if (async_head_init(sd, td->iodepth)) {
304 free(sd->events);
305 free(sd);
306 return 1;
307 }
308
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100309 td->io_ops->data = sd;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100310 return 0;
311}
312
313static struct ioengine_ops ioengine = {
314 .name = "syslet-rw",
315 .version = FIO_IOOPS_VERSION,
316 .init = fio_syslet_init,
317 .prep = fio_syslet_prep,
318 .queue = fio_syslet_queue,
Jens Axboe9ff9de62007-02-23 13:21:45 +0100319 .commit = fio_syslet_commit,
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100320 .getevents = fio_syslet_getevents,
321 .event = fio_syslet_event,
322 .cleanup = fio_syslet_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100323 .open_file = generic_open_file,
324 .close_file = generic_close_file,
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100325};
326
327#else /* FIO_HAVE_SYSLET */
328
329/*
330 * When we have a proper configure system in place, we simply wont build
331 * and install this io engine. For now install a crippled version that
332 * just complains and fails to load.
333 */
334static int fio_syslet_init(struct thread_data fio_unused *td)
335{
336 fprintf(stderr, "fio: syslet not available\n");
337 return 1;
338}
339
340static struct ioengine_ops ioengine = {
341 .name = "syslet-rw",
342 .version = FIO_IOOPS_VERSION,
343 .init = fio_syslet_init,
344};
345
346#endif /* FIO_HAVE_SYSLET */
347
348static void fio_init fio_syslet_register(void)
349{
350 register_ioengine(&ioengine);
351}
352
353static void fio_exit fio_syslet_unregister(void)
354{
355 unregister_ioengine(&ioengine);
356}