blob: 9a644f6fe4d07d17be9997a7cc94c882c275cd3e [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
2 * native linux aio io engine
3 *
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
Jens Axboe5f350952006-11-07 15:20:59 +010010
11#include "../fio.h"
12#include "../os.h"
Jens Axboe2866c822006-10-09 15:57:48 +020013
Jens Axboe34cfcda2006-11-03 14:00:45 +010014#ifdef FIO_HAVE_LIBAIO
15
Jens Axboe2866c822006-10-09 15:57:48 +020016#define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
17
18struct libaio_data {
19 io_context_t aio_ctx;
20 struct io_event *aio_events;
21};
22
Jens Axboe7a16dd02006-10-18 17:21:58 +020023static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020024{
Jens Axboe53cdc682006-10-18 11:50:58 +020025 struct fio_file *f = io_u->file;
26
Jens Axboe2866c822006-10-09 15:57:48 +020027 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010028 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
Jens Axboe87dc1ab2006-10-24 14:41:26 +020029 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010030 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
Jens Axboe87dc1ab2006-10-24 14:41:26 +020031 else if (io_u->ddir == DDIR_SYNC)
32 io_prep_fsync(&io_u->iocb, f->fd);
33 else
34 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +020035
36 return 0;
37}
38
39static struct io_u *fio_libaio_event(struct thread_data *td, int event)
40{
41 struct libaio_data *ld = td->io_ops->data;
42
43 return ev_to_iou(ld->aio_events + event);
44}
45
46static int fio_libaio_getevents(struct thread_data *td, int min, int max,
47 struct timespec *t)
48{
49 struct libaio_data *ld = td->io_ops->data;
50 long r;
51
52 do {
53 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
Jens Axboeccbb91c2007-01-08 22:27:05 +010054 if (r >= min)
55 break;
56 else if (r == -EAGAIN) {
Jens Axboe2866c822006-10-09 15:57:48 +020057 usleep(100);
58 continue;
59 } else if (r == -EINTR)
60 continue;
Jens Axboe84585002006-10-19 20:26:22 +020061 else if (r != 0)
Jens Axboe2866c822006-10-09 15:57:48 +020062 break;
63 } while (1);
64
Jens Axboeeaf09db2006-10-27 10:45:12 +020065 if (r < 0)
66 r = -r;
67
68 return (int) r;
Jens Axboe2866c822006-10-09 15:57:48 +020069}
70
71static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
72{
73 struct libaio_data *ld = td->io_ops->data;
74 struct iocb *iocb = &io_u->iocb;
75 long ret;
76
77 do {
78 ret = io_submit(ld->aio_ctx, 1, &iocb);
79 if (ret == 1)
80 return 0;
Jens Axboe84585002006-10-19 20:26:22 +020081 else if (ret == -EAGAIN || !ret)
Jens Axboe2866c822006-10-09 15:57:48 +020082 usleep(100);
83 else if (ret == -EINTR)
84 continue;
85 else
86 break;
87 } while (1);
88
Jens Axboe353a7e02006-10-25 09:16:07 +020089 if (ret <= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +010090 io_u->resid = io_u->xfer_buflen;
Jens Axboe353a7e02006-10-25 09:16:07 +020091 io_u->error = -ret;
92 return 1;
93 }
Jens Axboe2866c822006-10-09 15:57:48 +020094
Jens Axboe353a7e02006-10-25 09:16:07 +020095 return 0;
Jens Axboe2866c822006-10-09 15:57:48 +020096}
97
98static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
99{
100 struct libaio_data *ld = td->io_ops->data;
101
102 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
103}
104
105static void fio_libaio_cleanup(struct thread_data *td)
106{
107 struct libaio_data *ld = td->io_ops->data;
108
109 if (ld) {
110 io_destroy(ld->aio_ctx);
111 if (ld->aio_events)
112 free(ld->aio_events);
113
114 free(ld);
115 td->io_ops->data = NULL;
116 }
117}
118
119static int fio_libaio_init(struct thread_data *td)
120{
121 struct libaio_data *ld = malloc(sizeof(*ld));
122
123 memset(ld, 0, sizeof(*ld));
124 if (io_queue_init(td->iodepth, &ld->aio_ctx)) {
125 td_verror(td, errno);
Jens Axboecb781c72006-11-07 14:02:48 +0100126 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200127 return 1;
128 }
129
130 ld->aio_events = malloc(td->iodepth * sizeof(struct io_event));
Jens Axboe84585002006-10-19 20:26:22 +0200131 memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event));
Jens Axboe2866c822006-10-09 15:57:48 +0200132 td->io_ops->data = ld;
133 return 0;
134}
135
Jens Axboe5f350952006-11-07 15:20:59 +0100136static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200137 .name = "libaio",
138 .version = FIO_IOOPS_VERSION,
139 .init = fio_libaio_init,
140 .prep = fio_libaio_prep,
141 .queue = fio_libaio_queue,
142 .cancel = fio_libaio_cancel,
143 .getevents = fio_libaio_getevents,
144 .event = fio_libaio_event,
145 .cleanup = fio_libaio_cleanup,
Jens Axboe2866c822006-10-09 15:57:48 +0200146};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100147
148#else /* FIO_HAVE_LIBAIO */
149
150/*
151 * When we have a proper configure system in place, we simply wont build
152 * and install this io engine. For now install a crippled version that
153 * just complains and fails to load.
154 */
155static int fio_libaio_init(struct thread_data fio_unused *td)
156{
157 fprintf(stderr, "fio: libaio not available\n");
158 return 1;
159}
160
Jens Axboe5f350952006-11-07 15:20:59 +0100161static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100162 .name = "libaio",
163 .version = FIO_IOOPS_VERSION,
164 .init = fio_libaio_init,
165};
166
167#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100168
169static void fio_init fio_libaio_register(void)
170{
171 register_ioengine(&ioengine);
172}
173
174static void fio_exit fio_libaio_unregister(void)
175{
176 unregister_ioengine(&ioengine);
177}