| 1 | /* |
| 2 | * libaio engine |
| 3 | * |
| 4 | * IO engine using the Linux native aio interface. |
| 5 | * |
| 6 | */ |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <unistd.h> |
| 10 | #include <errno.h> |
| 11 | #include <assert.h> |
| 12 | |
| 13 | #include "../fio.h" |
| 14 | |
| 15 | #ifdef FIO_HAVE_LIBAIO |
| 16 | |
| 17 | #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj) |
| 18 | |
| 19 | struct libaio_data { |
| 20 | io_context_t aio_ctx; |
| 21 | struct io_event *aio_events; |
| 22 | struct iocb **iocbs; |
| 23 | struct io_u **io_us; |
| 24 | int iocbs_nr; |
| 25 | }; |
| 26 | |
| 27 | static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u) |
| 28 | { |
| 29 | struct fio_file *f = io_u->file; |
| 30 | |
| 31 | if (io_u->ddir == DDIR_READ) |
| 32 | io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
| 33 | else if (io_u->ddir == DDIR_WRITE) |
| 34 | io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
| 35 | else if (ddir_sync(io_u->ddir)) |
| 36 | io_prep_fsync(&io_u->iocb, f->fd); |
| 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static struct io_u *fio_libaio_event(struct thread_data *td, int event) |
| 42 | { |
| 43 | struct libaio_data *ld = td->io_ops->data; |
| 44 | struct io_event *ev; |
| 45 | struct io_u *io_u; |
| 46 | |
| 47 | ev = ld->aio_events + event; |
| 48 | io_u = ev_to_iou(ev); |
| 49 | |
| 50 | if (ev->res != io_u->xfer_buflen) { |
| 51 | if (ev->res > io_u->xfer_buflen) |
| 52 | io_u->error = -ev->res; |
| 53 | else |
| 54 | io_u->resid = io_u->xfer_buflen - ev->res; |
| 55 | } else |
| 56 | io_u->error = 0; |
| 57 | |
| 58 | return io_u; |
| 59 | } |
| 60 | |
| 61 | struct aio_ring { |
| 62 | unsigned id; /** kernel internal index number */ |
| 63 | unsigned nr; /** number of io_events */ |
| 64 | unsigned head; |
| 65 | unsigned tail; |
| 66 | |
| 67 | unsigned magic; |
| 68 | unsigned compat_features; |
| 69 | unsigned incompat_features; |
| 70 | unsigned header_length; /** size of aio_ring */ |
| 71 | |
| 72 | struct io_event events[0]; |
| 73 | }; |
| 74 | |
| 75 | #define AIO_RING_MAGIC 0xa10a10a1 |
| 76 | |
| 77 | static int user_io_getevents(io_context_t aio_ctx, unsigned int max, |
| 78 | struct io_event *events) |
| 79 | { |
| 80 | long i = 0; |
| 81 | unsigned head; |
| 82 | struct aio_ring *ring = (struct aio_ring*) aio_ctx; |
| 83 | |
| 84 | while (i < max) { |
| 85 | head = ring->head; |
| 86 | |
| 87 | if (head == ring->tail) { |
| 88 | /* There are no more completions */ |
| 89 | break; |
| 90 | } else { |
| 91 | /* There is another completion to reap */ |
| 92 | events[i] = ring->events[head]; |
| 93 | read_barrier(); |
| 94 | ring->head = (head + 1) % ring->nr; |
| 95 | i++; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return i; |
| 100 | } |
| 101 | |
| 102 | static int fio_libaio_getevents(struct thread_data *td, unsigned int min, |
| 103 | unsigned int max, struct timespec *t) |
| 104 | { |
| 105 | struct libaio_data *ld = td->io_ops->data; |
| 106 | unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min; |
| 107 | int r, events = 0; |
| 108 | |
| 109 | do { |
| 110 | if (td->o.userspace_libaio_reap == 1 |
| 111 | && actual_min == 0 |
| 112 | && ((struct aio_ring *)(ld->aio_ctx))->magic |
| 113 | == AIO_RING_MAGIC) { |
| 114 | r = user_io_getevents(ld->aio_ctx, max, |
| 115 | ld->aio_events + events); |
| 116 | } else { |
| 117 | r = io_getevents(ld->aio_ctx, actual_min, |
| 118 | max, ld->aio_events + events, t); |
| 119 | } |
| 120 | if (r >= 0) |
| 121 | events += r; |
| 122 | else if (r == -EAGAIN) |
| 123 | usleep(100); |
| 124 | } while (events < min); |
| 125 | |
| 126 | return r < 0 ? r : events; |
| 127 | } |
| 128 | |
| 129 | static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u) |
| 130 | { |
| 131 | struct libaio_data *ld = td->io_ops->data; |
| 132 | |
| 133 | fio_ro_check(td, io_u); |
| 134 | |
| 135 | if (ld->iocbs_nr == (int) td->o.iodepth) |
| 136 | return FIO_Q_BUSY; |
| 137 | |
| 138 | /* |
| 139 | * fsync is tricky, since it can fail and we need to do it |
| 140 | * serialized with other io. the reason is that linux doesn't |
| 141 | * support aio fsync yet. So return busy for the case where we |
| 142 | * have pending io, to let fio complete those first. |
| 143 | */ |
| 144 | if (ddir_sync(io_u->ddir)) { |
| 145 | if (ld->iocbs_nr) |
| 146 | return FIO_Q_BUSY; |
| 147 | |
| 148 | do_io_u_sync(td, io_u); |
| 149 | return FIO_Q_COMPLETED; |
| 150 | } |
| 151 | |
| 152 | if (io_u->ddir == DDIR_TRIM) { |
| 153 | if (ld->iocbs_nr) |
| 154 | return FIO_Q_BUSY; |
| 155 | |
| 156 | do_io_u_trim(td, io_u); |
| 157 | return FIO_Q_COMPLETED; |
| 158 | } |
| 159 | |
| 160 | ld->iocbs[ld->iocbs_nr] = &io_u->iocb; |
| 161 | ld->io_us[ld->iocbs_nr] = io_u; |
| 162 | ld->iocbs_nr++; |
| 163 | return FIO_Q_QUEUED; |
| 164 | } |
| 165 | |
| 166 | static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us, |
| 167 | unsigned int nr) |
| 168 | { |
| 169 | struct timeval now; |
| 170 | unsigned int i; |
| 171 | |
| 172 | if (!fio_fill_issue_time(td)) |
| 173 | return; |
| 174 | |
| 175 | fio_gettime(&now, NULL); |
| 176 | |
| 177 | for (i = 0; i < nr; i++) { |
| 178 | struct io_u *io_u = io_us[i]; |
| 179 | |
| 180 | memcpy(&io_u->issue_time, &now, sizeof(now)); |
| 181 | io_u_queued(td, io_u); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | static int fio_libaio_commit(struct thread_data *td) |
| 186 | { |
| 187 | struct libaio_data *ld = td->io_ops->data; |
| 188 | struct iocb **iocbs; |
| 189 | struct io_u **io_us; |
| 190 | int ret; |
| 191 | |
| 192 | if (!ld->iocbs_nr) |
| 193 | return 0; |
| 194 | |
| 195 | io_us = ld->io_us; |
| 196 | iocbs = ld->iocbs; |
| 197 | do { |
| 198 | ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs); |
| 199 | if (ret > 0) { |
| 200 | fio_libaio_queued(td, io_us, ret); |
| 201 | io_u_mark_submit(td, ret); |
| 202 | ld->iocbs_nr -= ret; |
| 203 | io_us += ret; |
| 204 | iocbs += ret; |
| 205 | ret = 0; |
| 206 | } else if (!ret || ret == -EAGAIN || ret == -EINTR) { |
| 207 | if (!ret) |
| 208 | io_u_mark_submit(td, ret); |
| 209 | continue; |
| 210 | } else |
| 211 | break; |
| 212 | } while (ld->iocbs_nr); |
| 213 | |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u) |
| 218 | { |
| 219 | struct libaio_data *ld = td->io_ops->data; |
| 220 | |
| 221 | return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events); |
| 222 | } |
| 223 | |
| 224 | static void fio_libaio_cleanup(struct thread_data *td) |
| 225 | { |
| 226 | struct libaio_data *ld = td->io_ops->data; |
| 227 | |
| 228 | if (ld) { |
| 229 | io_destroy(ld->aio_ctx); |
| 230 | free(ld->aio_events); |
| 231 | free(ld->iocbs); |
| 232 | free(ld->io_us); |
| 233 | free(ld); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | static int fio_libaio_init(struct thread_data *td) |
| 238 | { |
| 239 | struct libaio_data *ld = malloc(sizeof(*ld)); |
| 240 | int err; |
| 241 | |
| 242 | memset(ld, 0, sizeof(*ld)); |
| 243 | |
| 244 | err = io_queue_init(td->o.iodepth, &ld->aio_ctx); |
| 245 | if (err) { |
| 246 | td_verror(td, -err, "io_queue_init"); |
| 247 | log_err("fio: check /proc/sys/fs/aio-max-nr\n"); |
| 248 | free(ld); |
| 249 | return 1; |
| 250 | } |
| 251 | |
| 252 | ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event)); |
| 253 | memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event)); |
| 254 | ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *)); |
| 255 | memset(ld->iocbs, 0, sizeof(struct iocb *)); |
| 256 | ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *)); |
| 257 | memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *)); |
| 258 | ld->iocbs_nr = 0; |
| 259 | |
| 260 | td->io_ops->data = ld; |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static struct ioengine_ops ioengine = { |
| 265 | .name = "libaio", |
| 266 | .version = FIO_IOOPS_VERSION, |
| 267 | .init = fio_libaio_init, |
| 268 | .prep = fio_libaio_prep, |
| 269 | .queue = fio_libaio_queue, |
| 270 | .commit = fio_libaio_commit, |
| 271 | .cancel = fio_libaio_cancel, |
| 272 | .getevents = fio_libaio_getevents, |
| 273 | .event = fio_libaio_event, |
| 274 | .cleanup = fio_libaio_cleanup, |
| 275 | .open_file = generic_open_file, |
| 276 | .close_file = generic_close_file, |
| 277 | .get_file_size = generic_get_file_size, |
| 278 | }; |
| 279 | |
| 280 | #else /* FIO_HAVE_LIBAIO */ |
| 281 | |
| 282 | /* |
| 283 | * When we have a proper configure system in place, we simply wont build |
| 284 | * and install this io engine. For now install a crippled version that |
| 285 | * just complains and fails to load. |
| 286 | */ |
| 287 | static int fio_libaio_init(struct thread_data fio_unused *td) |
| 288 | { |
| 289 | log_err("fio: libaio not available\n"); |
| 290 | return 1; |
| 291 | } |
| 292 | |
| 293 | static struct ioengine_ops ioengine = { |
| 294 | .name = "libaio", |
| 295 | .version = FIO_IOOPS_VERSION, |
| 296 | .init = fio_libaio_init, |
| 297 | }; |
| 298 | |
| 299 | #endif |
| 300 | |
| 301 | static void fio_init fio_libaio_register(void) |
| 302 | { |
| 303 | register_ioengine(&ioengine); |
| 304 | } |
| 305 | |
| 306 | static void fio_exit fio_libaio_unregister(void) |
| 307 | { |
| 308 | unregister_ioengine(&ioengine); |
| 309 | } |