blob: b635f6440d1fd1089bfb9150e892f58c3358cc2a [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>
Jens Axboeb8846352007-03-14 14:29:16 +010013#include <asm/unistd.h>
Jens Axboea4f4fdd2007-02-14 01:16:39 +010014
15#include "../fio.h"
16#include "../os.h"
17
18#ifdef FIO_HAVE_SYSLET
19
20struct syslet_data {
21 struct io_u **events;
22 unsigned int nr_events;
23
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010024 struct async_head_user ahu;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010025 struct syslet_uatom **ring;
Jens Axboe9ff9de62007-02-23 13:21:45 +010026
27 struct syslet_uatom *head, *tail;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010028};
29
Jens Axboe9ff9de62007-02-23 13:21:45 +010030static void fio_syslet_complete_atom(struct thread_data *td,
31 struct syslet_uatom *atom)
32{
33 struct syslet_data *sd = td->io_ops->data;
Jens Axboe5b38ee82007-02-26 10:44:22 +010034 struct syslet_uatom *last;
Jens Axboe9ff9de62007-02-23 13:21:45 +010035 struct io_u *io_u;
Jens Axboe9ff9de62007-02-23 13:21:45 +010036
37 /*
Jens Axboe5b38ee82007-02-26 10:44:22 +010038 * complete from the beginning of the sequence up to (and
39 * including) this atom
Jens Axboe9ff9de62007-02-23 13:21:45 +010040 */
Jens Axboe5b38ee82007-02-26 10:44:22 +010041 last = atom;
42 io_u = atom->private;
43 atom = io_u->req.head;
Jens Axboe9ff9de62007-02-23 13:21:45 +010044
45 /*
46 * now complete in right order
47 */
Jens Axboe5b38ee82007-02-26 10:44:22 +010048 do {
Jens Axboe9ff9de62007-02-23 13:21:45 +010049 long ret;
50
Jens Axboe9ff9de62007-02-23 13:21:45 +010051 io_u = atom->private;
52 ret = *atom->ret_ptr;
Jens Axboee2e67912007-03-12 09:43:05 +010053 if (ret >= 0)
Jens Axboe9ff9de62007-02-23 13:21:45 +010054 io_u->resid = io_u->xfer_buflen - ret;
55 else if (ret < 0)
56 io_u->error = ret;
57
58 assert(sd->nr_events < td->iodepth);
59 sd->events[sd->nr_events++] = io_u;
Jens Axboe9ff9de62007-02-23 13:21:45 +010060
Jens Axboe5b38ee82007-02-26 10:44:22 +010061 if (atom == last)
62 break;
Jens Axboe9ff9de62007-02-23 13:21:45 +010063
Jens Axboe5b38ee82007-02-26 10:44:22 +010064 atom = atom->next;
65 } while (1);
66
67 assert(!last->next);
Jens Axboe9ff9de62007-02-23 13:21:45 +010068}
69
Jens Axboea4f4fdd2007-02-14 01:16:39 +010070/*
71 * Inspect the ring to see if we have completed events
72 */
73static void fio_syslet_complete(struct thread_data *td)
74{
75 struct syslet_data *sd = td->io_ops->data;
76
77 do {
78 struct syslet_uatom *atom;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010079
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010080 atom = sd->ring[sd->ahu.user_ring_idx];
Jens Axboea4f4fdd2007-02-14 01:16:39 +010081 if (!atom)
82 break;
83
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010084 sd->ring[sd->ahu.user_ring_idx] = NULL;
85 if (++sd->ahu.user_ring_idx == td->iodepth)
86 sd->ahu.user_ring_idx = 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010087
Jens Axboe9ff9de62007-02-23 13:21:45 +010088 fio_syslet_complete_atom(td, atom);
Jens Axboea4f4fdd2007-02-14 01:16:39 +010089 } while (1);
90}
91
92static int fio_syslet_getevents(struct thread_data *td, int min,
93 int fio_unused max,
94 struct timespec fio_unused *t)
95{
96 struct syslet_data *sd = td->io_ops->data;
Jens Axboea4f4fdd2007-02-14 01:16:39 +010097 long ret;
98
99 do {
100 fio_syslet_complete(td);
101
102 /*
103 * do we have enough immediate completions?
104 */
105 if (sd->nr_events >= (unsigned int) min)
106 break;
107
108 /*
109 * OK, we need to wait for some events...
110 */
Jens Axboe9ff9de62007-02-23 13:21:45 +0100111 ret = async_wait(1, sd->ahu.user_ring_idx, &sd->ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100112 if (ret < 0)
Jens Axboee49499f2007-02-22 11:08:52 +0100113 return -errno;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100114 } while (1);
115
116 ret = sd->nr_events;
117 sd->nr_events = 0;
118 return ret;
119}
120
121static struct io_u *fio_syslet_event(struct thread_data *td, int event)
122{
123 struct syslet_data *sd = td->io_ops->data;
124
125 return sd->events[event];
126}
127
128static void init_atom(struct syslet_uatom *atom, int nr, void *arg0,
Jens Axboea2e1b082007-02-14 08:06:19 +0100129 void *arg1, void *arg2, void *arg3, void *ret_ptr,
130 unsigned long flags, void *priv)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100131{
132 atom->flags = flags;
133 atom->nr = nr;
134 atom->ret_ptr = ret_ptr;
Jens Axboea2e1b082007-02-14 08:06:19 +0100135 atom->next = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100136 atom->arg_ptr[0] = arg0;
137 atom->arg_ptr[1] = arg1;
138 atom->arg_ptr[2] = arg2;
Jens Axboea2e1b082007-02-14 08:06:19 +0100139 atom->arg_ptr[3] = arg3;
140 atom->arg_ptr[4] = atom->arg_ptr[5] = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100141 atom->private = priv;
142}
143
144/*
145 * Use seek atom for sync
146 */
147static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f)
148{
Jens Axboea2e1b082007-02-14 08:06:19 +0100149 init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL,
Jens Axboe7d44a742007-02-14 17:32:08 +0100150 &io_u->req.ret, 0, io_u);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100151}
152
153static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f)
154{
155 int nr;
156
157 /*
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100158 * prepare rw
159 */
160 if (io_u->ddir == DDIR_READ)
Jens Axboea2e1b082007-02-14 08:06:19 +0100161 nr = __NR_pread64;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100162 else
Jens Axboea2e1b082007-02-14 08:06:19 +0100163 nr = __NR_pwrite64;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100164
Jens Axboea2e1b082007-02-14 08:06:19 +0100165 init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf,
Jens Axboe7d44a742007-02-14 17:32:08 +0100166 &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret, 0, io_u);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100167}
168
169static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u)
170{
171 struct fio_file *f = io_u->file;
172
173 if (io_u->ddir == DDIR_SYNC)
174 fio_syslet_prep_sync(io_u, f);
175 else
176 fio_syslet_prep_rw(io_u, f);
177
178 return 0;
179}
180
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100181static void cachemiss_thread_start(void)
182{
183 while (1)
Jens Axboe7756b0d2007-02-26 10:22:31 +0100184 async_thread(NULL, NULL);
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100185}
186
187#define THREAD_STACK_SIZE (16384)
188
189static unsigned long thread_stack_alloc()
190{
Jens Axboe5b38ee82007-02-26 10:44:22 +0100191 return (unsigned long) malloc(THREAD_STACK_SIZE) + THREAD_STACK_SIZE;
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100192}
193
Jens Axboea0a930e2007-02-27 19:47:13 +0100194static void fio_syslet_queued(struct thread_data *td, struct syslet_data *sd)
195{
196 struct syslet_uatom *atom;
197 struct timeval now;
198
199 fio_gettime(&now, NULL);
200
201 atom = sd->head;
202 while (atom) {
203 struct io_u *io_u = atom->private;
204
205 memcpy(&io_u->issue_time, &now, sizeof(now));
206 io_u_queued(td, io_u);
207 atom = atom->next;
208 }
209}
210
Jens Axboe9ff9de62007-02-23 13:21:45 +0100211static int fio_syslet_commit(struct thread_data *td)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100212{
213 struct syslet_data *sd = td->io_ops->data;
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100214 struct syslet_uatom *done;
Jens Axboe9ff9de62007-02-23 13:21:45 +0100215
216 if (!sd->head)
217 return 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100218
Jens Axboe5b38ee82007-02-26 10:44:22 +0100219 assert(!sd->tail->next);
220
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100221 if (!sd->ahu.new_thread_stack)
222 sd->ahu.new_thread_stack = thread_stack_alloc();
223
Jens Axboea0a930e2007-02-27 19:47:13 +0100224 fio_syslet_queued(td, sd);
225
Jens Axboe7d44a742007-02-14 17:32:08 +0100226 /*
227 * On sync completion, the atom is returned. So on NULL return
228 * it's queued asynchronously.
229 */
Jens Axboe9ff9de62007-02-23 13:21:45 +0100230 done = async_exec(sd->head, &sd->ahu);
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100231
Jens Axboe9ff9de62007-02-23 13:21:45 +0100232 sd->head = sd->tail = NULL;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100233
Jens Axboe9ff9de62007-02-23 13:21:45 +0100234 if (done)
235 fio_syslet_complete_atom(td, done);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100236
Jens Axboe9ff9de62007-02-23 13:21:45 +0100237 return 0;
238}
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100239
Jens Axboe9ff9de62007-02-23 13:21:45 +0100240static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
241{
242 struct syslet_data *sd = td->io_ops->data;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100243
Jens Axboe9ff9de62007-02-23 13:21:45 +0100244 if (sd->tail) {
245 sd->tail->next = &io_u->req.atom;
246 sd->tail = &io_u->req.atom;
247 } else
248 sd->head = sd->tail = &io_u->req.atom;
249
Jens Axboe5b38ee82007-02-26 10:44:22 +0100250 io_u->req.head = sd->head;
Jens Axboe9ff9de62007-02-23 13:21:45 +0100251 return FIO_Q_QUEUED;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100252}
253
Jens Axboedb64e9b2007-02-14 02:10:59 +0100254static int async_head_init(struct syslet_data *sd, unsigned int depth)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100255{
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100256 unsigned long ring_size;
257
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100258 memset(&sd->ahu, 0, sizeof(struct async_head_user));
Jens Axboe2ca50be2007-02-14 08:31:15 +0100259
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100260 ring_size = sizeof(struct syslet_uatom *) * depth;
261 sd->ring = malloc(ring_size);
262 memset(sd->ring, 0, ring_size);
263
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100264 sd->ahu.user_ring_idx = 0;
265 sd->ahu.completion_ring = sd->ring;
266 sd->ahu.ring_size_bytes = ring_size;
267 sd->ahu.head_stack = thread_stack_alloc();
Jens Axboe5b38ee82007-02-26 10:44:22 +0100268 sd->ahu.head_eip = (unsigned long) cachemiss_thread_start;
269 sd->ahu.new_thread_eip = (unsigned long) cachemiss_thread_start;
Jens Axboedb64e9b2007-02-14 02:10:59 +0100270
271 return 0;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100272}
273
Jens Axboe2ca50be2007-02-14 08:31:15 +0100274static void async_head_exit(struct syslet_data *sd)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100275{
Jens Axboe7f059a72007-02-14 08:53:11 +0100276 free(sd->ring);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100277}
278
279static void fio_syslet_cleanup(struct thread_data *td)
280{
281 struct syslet_data *sd = td->io_ops->data;
282
283 if (sd) {
Jens Axboe2ca50be2007-02-14 08:31:15 +0100284 async_head_exit(sd);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100285 free(sd->events);
286 free(sd);
287 td->io_ops->data = NULL;
288 }
289}
290
291static int fio_syslet_init(struct thread_data *td)
292{
293 struct syslet_data *sd;
294
Jens Axboedb64e9b2007-02-14 02:10:59 +0100295
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100296 sd = malloc(sizeof(*sd));
297 memset(sd, 0, sizeof(*sd));
298 sd->events = malloc(sizeof(struct io_u *) * td->iodepth);
299 memset(sd->events, 0, sizeof(struct io_u *) * td->iodepth);
Jens Axboedb64e9b2007-02-14 02:10:59 +0100300
301 /*
302 * This will handily fail for kernels where syslet isn't available
303 */
304 if (async_head_init(sd, td->iodepth)) {
305 free(sd->events);
306 free(sd);
307 return 1;
308 }
309
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100310 td->io_ops->data = sd;
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100311 return 0;
312}
313
314static struct ioengine_ops ioengine = {
315 .name = "syslet-rw",
316 .version = FIO_IOOPS_VERSION,
317 .init = fio_syslet_init,
318 .prep = fio_syslet_prep,
319 .queue = fio_syslet_queue,
Jens Axboe9ff9de62007-02-23 13:21:45 +0100320 .commit = fio_syslet_commit,
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100321 .getevents = fio_syslet_getevents,
322 .event = fio_syslet_event,
323 .cleanup = fio_syslet_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100324 .open_file = generic_open_file,
325 .close_file = generic_close_file,
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100326};
327
328#else /* FIO_HAVE_SYSLET */
329
330/*
331 * When we have a proper configure system in place, we simply wont build
332 * and install this io engine. For now install a crippled version that
333 * just complains and fails to load.
334 */
335static int fio_syslet_init(struct thread_data fio_unused *td)
336{
337 fprintf(stderr, "fio: syslet not available\n");
338 return 1;
339}
340
341static struct ioengine_ops ioengine = {
342 .name = "syslet-rw",
343 .version = FIO_IOOPS_VERSION,
344 .init = fio_syslet_init,
345};
346
347#endif /* FIO_HAVE_SYSLET */
348
349static void fio_init fio_syslet_register(void)
350{
351 register_ioengine(&ioengine);
352}
353
354static void fio_exit fio_syslet_unregister(void)
355{
356 unregister_ioengine(&ioengine);
357}