Commit | Line | Data |
---|---|---|
2866c82d JA |
1 | /* |
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> | |
5f350952 JA |
10 | |
11 | #include "../fio.h" | |
12 | #include "../os.h" | |
2866c82d | 13 | |
34cfcdaf JA |
14 | #ifdef FIO_HAVE_LIBAIO |
15 | ||
2866c82d JA |
16 | #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj) |
17 | ||
18 | struct libaio_data { | |
19 | io_context_t aio_ctx; | |
20 | struct io_event *aio_events; | |
21 | }; | |
22 | ||
7a16dd02 | 23 | static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u) |
2866c82d | 24 | { |
53cdc686 JA |
25 | struct fio_file *f = io_u->file; |
26 | ||
2866c82d | 27 | if (io_u->ddir == DDIR_READ) |
cec6b55d | 28 | io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
87dc1ab1 | 29 | else if (io_u->ddir == DDIR_WRITE) |
cec6b55d | 30 | io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
87dc1ab1 JA |
31 | else if (io_u->ddir == DDIR_SYNC) |
32 | io_prep_fsync(&io_u->iocb, f->fd); | |
33 | else | |
34 | return 1; | |
2866c82d JA |
35 | |
36 | return 0; | |
37 | } | |
38 | ||
39 | static 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 | ||
46 | static 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); | |
ccbb91cb JA |
54 | if (r >= min) |
55 | break; | |
56 | else if (r == -EAGAIN) { | |
2866c82d JA |
57 | usleep(100); |
58 | continue; | |
59 | } else if (r == -EINTR) | |
60 | continue; | |
84585003 | 61 | else if (r != 0) |
2866c82d JA |
62 | break; |
63 | } while (1); | |
64 | ||
22819ec2 | 65 | return r; |
2866c82d JA |
66 | } |
67 | ||
68 | static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u) | |
69 | { | |
70 | struct libaio_data *ld = td->io_ops->data; | |
71 | struct iocb *iocb = &io_u->iocb; | |
72 | long ret; | |
73 | ||
74 | do { | |
75 | ret = io_submit(ld->aio_ctx, 1, &iocb); | |
76 | if (ret == 1) | |
36167d82 | 77 | return FIO_Q_QUEUED; |
84585003 | 78 | else if (ret == -EAGAIN || !ret) |
2866c82d JA |
79 | usleep(100); |
80 | else if (ret == -EINTR) | |
81 | continue; | |
e5c40aab JA |
82 | else if (ret == -EINVAL && io_u->ddir == DDIR_SYNC) { |
83 | /* | |
84 | * the async fsync doesn't currently seem to be | |
85 | * supported, so just fsync if we fail with EINVAL | |
86 | * for a sync. since buffered io is also sync | |
87 | * with libaio (still), we don't have pending | |
88 | * requests to flush first. | |
89 | */ | |
311b84ae | 90 | if (fsync(io_u->file->fd) < 0) |
22819ec2 | 91 | ret = -errno; |
36167d82 JA |
92 | else |
93 | ret = FIO_Q_COMPLETED; | |
e5c40aab JA |
94 | break; |
95 | } else | |
2866c82d JA |
96 | break; |
97 | } while (1); | |
98 | ||
353a7e0e | 99 | if (ret <= 0) { |
cec6b55d | 100 | io_u->resid = io_u->xfer_buflen; |
353a7e0e | 101 | io_u->error = -ret; |
95bcd815 | 102 | td_verror(td, io_u->error); |
36167d82 | 103 | return FIO_Q_COMPLETED; |
353a7e0e | 104 | } |
2866c82d | 105 | |
36167d82 | 106 | return ret; |
2866c82d JA |
107 | } |
108 | ||
109 | static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u) | |
110 | { | |
111 | struct libaio_data *ld = td->io_ops->data; | |
112 | ||
113 | return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events); | |
114 | } | |
115 | ||
116 | static void fio_libaio_cleanup(struct thread_data *td) | |
117 | { | |
118 | struct libaio_data *ld = td->io_ops->data; | |
119 | ||
120 | if (ld) { | |
121 | io_destroy(ld->aio_ctx); | |
122 | if (ld->aio_events) | |
123 | free(ld->aio_events); | |
124 | ||
125 | free(ld); | |
126 | td->io_ops->data = NULL; | |
127 | } | |
128 | } | |
129 | ||
130 | static int fio_libaio_init(struct thread_data *td) | |
131 | { | |
132 | struct libaio_data *ld = malloc(sizeof(*ld)); | |
133 | ||
134 | memset(ld, 0, sizeof(*ld)); | |
135 | if (io_queue_init(td->iodepth, &ld->aio_ctx)) { | |
136 | td_verror(td, errno); | |
cb781c75 | 137 | free(ld); |
2866c82d JA |
138 | return 1; |
139 | } | |
140 | ||
141 | ld->aio_events = malloc(td->iodepth * sizeof(struct io_event)); | |
84585003 | 142 | memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event)); |
2866c82d JA |
143 | td->io_ops->data = ld; |
144 | return 0; | |
145 | } | |
146 | ||
5f350952 | 147 | static struct ioengine_ops ioengine = { |
2866c82d JA |
148 | .name = "libaio", |
149 | .version = FIO_IOOPS_VERSION, | |
150 | .init = fio_libaio_init, | |
151 | .prep = fio_libaio_prep, | |
152 | .queue = fio_libaio_queue, | |
153 | .cancel = fio_libaio_cancel, | |
154 | .getevents = fio_libaio_getevents, | |
155 | .event = fio_libaio_event, | |
156 | .cleanup = fio_libaio_cleanup, | |
2866c82d | 157 | }; |
34cfcdaf JA |
158 | |
159 | #else /* FIO_HAVE_LIBAIO */ | |
160 | ||
161 | /* | |
162 | * When we have a proper configure system in place, we simply wont build | |
163 | * and install this io engine. For now install a crippled version that | |
164 | * just complains and fails to load. | |
165 | */ | |
166 | static int fio_libaio_init(struct thread_data fio_unused *td) | |
167 | { | |
168 | fprintf(stderr, "fio: libaio not available\n"); | |
169 | return 1; | |
170 | } | |
171 | ||
5f350952 | 172 | static struct ioengine_ops ioengine = { |
34cfcdaf JA |
173 | .name = "libaio", |
174 | .version = FIO_IOOPS_VERSION, | |
175 | .init = fio_libaio_init, | |
176 | }; | |
177 | ||
178 | #endif | |
5f350952 JA |
179 | |
180 | static void fio_init fio_libaio_register(void) | |
181 | { | |
182 | register_ioengine(&ioengine); | |
183 | } | |
184 | ||
185 | static void fio_exit fio_libaio_unregister(void) | |
186 | { | |
187 | unregister_ioengine(&ioengine); | |
188 | } |