| 1 | /* |
| 2 | * libaio engine |
| 3 | * |
| 4 | * IO engine using the Linux native aio interface. |
| 5 | * |
| 6 | */ |
| 7 | #include <stdlib.h> |
| 8 | #include <unistd.h> |
| 9 | #include <errno.h> |
| 10 | #include <libaio.h> |
| 11 | |
| 12 | #include "../fio.h" |
| 13 | #include "../lib/pow2.h" |
| 14 | #include "../optgroup.h" |
| 15 | |
| 16 | #ifndef IOCB_FLAG_HIPRI |
| 17 | #define IOCB_FLAG_HIPRI (1 << 2) |
| 18 | #endif |
| 19 | #ifndef IOCTX_FLAG_IOPOLL |
| 20 | #define IOCTX_FLAG_IOPOLL (1 << 0) |
| 21 | #endif |
| 22 | |
| 23 | static int fio_libaio_commit(struct thread_data *td); |
| 24 | |
| 25 | struct libaio_data { |
| 26 | io_context_t aio_ctx; |
| 27 | struct io_event *aio_events; |
| 28 | struct iocb **iocbs; |
| 29 | struct io_u **io_us; |
| 30 | |
| 31 | /* |
| 32 | * Basic ring buffer. 'head' is incremented in _queue(), and |
| 33 | * 'tail' is incremented in _commit(). We keep 'queued' so |
| 34 | * that we know if the ring is full or empty, when |
| 35 | * 'head' == 'tail'. 'entries' is the ring size, and |
| 36 | * 'is_pow2' is just an optimization to use AND instead of |
| 37 | * modulus to get the remainder on ring increment. |
| 38 | */ |
| 39 | int is_pow2; |
| 40 | unsigned int entries; |
| 41 | unsigned int queued; |
| 42 | unsigned int head; |
| 43 | unsigned int tail; |
| 44 | }; |
| 45 | |
| 46 | struct libaio_options { |
| 47 | void *pad; |
| 48 | unsigned int userspace_reap; |
| 49 | unsigned int hipri; |
| 50 | }; |
| 51 | |
| 52 | static struct fio_option options[] = { |
| 53 | { |
| 54 | .name = "userspace_reap", |
| 55 | .lname = "Libaio userspace reaping", |
| 56 | .type = FIO_OPT_STR_SET, |
| 57 | .off1 = offsetof(struct libaio_options, userspace_reap), |
| 58 | .help = "Use alternative user-space reap implementation", |
| 59 | .category = FIO_OPT_C_ENGINE, |
| 60 | .group = FIO_OPT_G_LIBAIO, |
| 61 | }, |
| 62 | { |
| 63 | .name = "hipri", |
| 64 | .lname = "High Priority", |
| 65 | .type = FIO_OPT_STR_SET, |
| 66 | .off1 = offsetof(struct libaio_options, hipri), |
| 67 | .help = "Use polled IO completions", |
| 68 | .category = FIO_OPT_C_ENGINE, |
| 69 | .group = FIO_OPT_G_LIBAIO, |
| 70 | }, |
| 71 | { |
| 72 | .name = NULL, |
| 73 | }, |
| 74 | }; |
| 75 | |
| 76 | static inline void ring_inc(struct libaio_data *ld, unsigned int *val, |
| 77 | unsigned int add) |
| 78 | { |
| 79 | if (ld->is_pow2) |
| 80 | *val = (*val + add) & (ld->entries - 1); |
| 81 | else |
| 82 | *val = (*val + add) % ld->entries; |
| 83 | } |
| 84 | |
| 85 | static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u) |
| 86 | { |
| 87 | struct fio_file *f = io_u->file; |
| 88 | struct libaio_options *o = td->eo; |
| 89 | |
| 90 | if (io_u->ddir == DDIR_READ) { |
| 91 | io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
| 92 | if (o->hipri) |
| 93 | io_u->iocb.u.c.flags |= IOCB_FLAG_HIPRI; |
| 94 | } else if (io_u->ddir == DDIR_WRITE) { |
| 95 | io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
| 96 | if (o->hipri) |
| 97 | io_u->iocb.u.c.flags |= IOCB_FLAG_HIPRI; |
| 98 | } else if (ddir_sync(io_u->ddir)) |
| 99 | io_prep_fsync(&io_u->iocb, f->fd); |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static struct io_u *fio_libaio_event(struct thread_data *td, int event) |
| 105 | { |
| 106 | struct libaio_data *ld = td->io_ops_data; |
| 107 | struct io_event *ev; |
| 108 | struct io_u *io_u; |
| 109 | |
| 110 | ev = ld->aio_events + event; |
| 111 | io_u = container_of(ev->obj, struct io_u, iocb); |
| 112 | |
| 113 | if (ev->res != io_u->xfer_buflen) { |
| 114 | if (ev->res > io_u->xfer_buflen) |
| 115 | io_u->error = -ev->res; |
| 116 | else |
| 117 | io_u->resid = io_u->xfer_buflen - ev->res; |
| 118 | } else |
| 119 | io_u->error = 0; |
| 120 | |
| 121 | return io_u; |
| 122 | } |
| 123 | |
| 124 | struct aio_ring { |
| 125 | unsigned id; /** kernel internal index number */ |
| 126 | unsigned nr; /** number of io_events */ |
| 127 | unsigned head; |
| 128 | unsigned tail; |
| 129 | |
| 130 | unsigned magic; |
| 131 | unsigned compat_features; |
| 132 | unsigned incompat_features; |
| 133 | unsigned header_length; /** size of aio_ring */ |
| 134 | |
| 135 | struct io_event events[0]; |
| 136 | }; |
| 137 | |
| 138 | #define AIO_RING_MAGIC 0xa10a10a1 |
| 139 | |
| 140 | static int user_io_getevents(io_context_t aio_ctx, unsigned int max, |
| 141 | struct io_event *events) |
| 142 | { |
| 143 | long i = 0; |
| 144 | unsigned head; |
| 145 | struct aio_ring *ring = (struct aio_ring*) aio_ctx; |
| 146 | |
| 147 | while (i < max) { |
| 148 | head = ring->head; |
| 149 | |
| 150 | if (head == ring->tail) { |
| 151 | /* There are no more completions */ |
| 152 | break; |
| 153 | } else { |
| 154 | /* There is another completion to reap */ |
| 155 | events[i] = ring->events[head]; |
| 156 | read_barrier(); |
| 157 | ring->head = (head + 1) % ring->nr; |
| 158 | i++; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return i; |
| 163 | } |
| 164 | |
| 165 | static int fio_libaio_getevents(struct thread_data *td, unsigned int min, |
| 166 | unsigned int max, const struct timespec *t) |
| 167 | { |
| 168 | struct libaio_data *ld = td->io_ops_data; |
| 169 | struct libaio_options *o = td->eo; |
| 170 | unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min; |
| 171 | struct timespec __lt, *lt = NULL; |
| 172 | int r, events = 0; |
| 173 | |
| 174 | if (t) { |
| 175 | __lt = *t; |
| 176 | lt = &__lt; |
| 177 | } |
| 178 | |
| 179 | do { |
| 180 | if (o->userspace_reap == 1 |
| 181 | && actual_min == 0 |
| 182 | && ((struct aio_ring *)(ld->aio_ctx))->magic |
| 183 | == AIO_RING_MAGIC) { |
| 184 | r = user_io_getevents(ld->aio_ctx, max, |
| 185 | ld->aio_events + events); |
| 186 | } else { |
| 187 | r = io_getevents(ld->aio_ctx, actual_min, |
| 188 | max, ld->aio_events + events, lt); |
| 189 | } |
| 190 | if (r > 0) |
| 191 | events += r; |
| 192 | else if ((min && r == 0) || r == -EAGAIN) { |
| 193 | fio_libaio_commit(td); |
| 194 | if (actual_min) |
| 195 | usleep(10); |
| 196 | } else if (r != -EINTR) |
| 197 | break; |
| 198 | } while (events < min); |
| 199 | |
| 200 | return r < 0 ? r : events; |
| 201 | } |
| 202 | |
| 203 | static enum fio_q_status fio_libaio_queue(struct thread_data *td, |
| 204 | struct io_u *io_u) |
| 205 | { |
| 206 | struct libaio_data *ld = td->io_ops_data; |
| 207 | |
| 208 | fio_ro_check(td, io_u); |
| 209 | |
| 210 | if (ld->queued == td->o.iodepth) |
| 211 | return FIO_Q_BUSY; |
| 212 | |
| 213 | /* |
| 214 | * fsync is tricky, since it can fail and we need to do it |
| 215 | * serialized with other io. the reason is that linux doesn't |
| 216 | * support aio fsync yet. So return busy for the case where we |
| 217 | * have pending io, to let fio complete those first. |
| 218 | */ |
| 219 | if (ddir_sync(io_u->ddir)) { |
| 220 | if (ld->queued) |
| 221 | return FIO_Q_BUSY; |
| 222 | |
| 223 | do_io_u_sync(td, io_u); |
| 224 | return FIO_Q_COMPLETED; |
| 225 | } |
| 226 | |
| 227 | if (io_u->ddir == DDIR_TRIM) { |
| 228 | if (ld->queued) |
| 229 | return FIO_Q_BUSY; |
| 230 | |
| 231 | do_io_u_trim(td, io_u); |
| 232 | io_u_mark_submit(td, 1); |
| 233 | io_u_mark_complete(td, 1); |
| 234 | return FIO_Q_COMPLETED; |
| 235 | } |
| 236 | |
| 237 | ld->iocbs[ld->head] = &io_u->iocb; |
| 238 | ld->io_us[ld->head] = io_u; |
| 239 | ring_inc(ld, &ld->head, 1); |
| 240 | ld->queued++; |
| 241 | return FIO_Q_QUEUED; |
| 242 | } |
| 243 | |
| 244 | static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us, |
| 245 | unsigned int nr) |
| 246 | { |
| 247 | struct timespec now; |
| 248 | unsigned int i; |
| 249 | |
| 250 | if (!fio_fill_issue_time(td)) |
| 251 | return; |
| 252 | |
| 253 | fio_gettime(&now, NULL); |
| 254 | |
| 255 | for (i = 0; i < nr; i++) { |
| 256 | struct io_u *io_u = io_us[i]; |
| 257 | |
| 258 | memcpy(&io_u->issue_time, &now, sizeof(now)); |
| 259 | io_u_queued(td, io_u); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | static int fio_libaio_commit(struct thread_data *td) |
| 264 | { |
| 265 | struct libaio_data *ld = td->io_ops_data; |
| 266 | struct iocb **iocbs; |
| 267 | struct io_u **io_us; |
| 268 | struct timespec ts; |
| 269 | int ret, wait_start = 0; |
| 270 | |
| 271 | if (!ld->queued) |
| 272 | return 0; |
| 273 | |
| 274 | do { |
| 275 | long nr = ld->queued; |
| 276 | |
| 277 | nr = min((unsigned int) nr, ld->entries - ld->tail); |
| 278 | io_us = ld->io_us + ld->tail; |
| 279 | iocbs = ld->iocbs + ld->tail; |
| 280 | |
| 281 | ret = io_submit(ld->aio_ctx, nr, iocbs); |
| 282 | if (ret > 0) { |
| 283 | fio_libaio_queued(td, io_us, ret); |
| 284 | io_u_mark_submit(td, ret); |
| 285 | |
| 286 | ld->queued -= ret; |
| 287 | ring_inc(ld, &ld->tail, ret); |
| 288 | ret = 0; |
| 289 | wait_start = 0; |
| 290 | } else if (ret == -EINTR || !ret) { |
| 291 | if (!ret) |
| 292 | io_u_mark_submit(td, ret); |
| 293 | wait_start = 0; |
| 294 | continue; |
| 295 | } else if (ret == -EAGAIN) { |
| 296 | /* |
| 297 | * If we get EAGAIN, we should break out without |
| 298 | * error and let the upper layer reap some |
| 299 | * events for us. If we have no queued IO, we |
| 300 | * must loop here. If we loop for more than 30s, |
| 301 | * just error out, something must be buggy in the |
| 302 | * IO path. |
| 303 | */ |
| 304 | if (ld->queued) { |
| 305 | ret = 0; |
| 306 | break; |
| 307 | } |
| 308 | if (!wait_start) { |
| 309 | fio_gettime(&ts, NULL); |
| 310 | wait_start = 1; |
| 311 | } else if (mtime_since_now(&ts) > 30000) { |
| 312 | log_err("fio: aio appears to be stalled, giving up\n"); |
| 313 | break; |
| 314 | } |
| 315 | usleep(1); |
| 316 | continue; |
| 317 | } else if (ret == -ENOMEM) { |
| 318 | /* |
| 319 | * If we get -ENOMEM, reap events if we can. If |
| 320 | * we cannot, treat it as a fatal event since there's |
| 321 | * nothing we can do about it. |
| 322 | */ |
| 323 | if (ld->queued) |
| 324 | ret = 0; |
| 325 | break; |
| 326 | } else |
| 327 | break; |
| 328 | } while (ld->queued); |
| 329 | |
| 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u) |
| 334 | { |
| 335 | struct libaio_data *ld = td->io_ops_data; |
| 336 | |
| 337 | return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events); |
| 338 | } |
| 339 | |
| 340 | static void fio_libaio_cleanup(struct thread_data *td) |
| 341 | { |
| 342 | struct libaio_data *ld = td->io_ops_data; |
| 343 | |
| 344 | if (ld) { |
| 345 | /* |
| 346 | * Work-around to avoid huge RCU stalls at exit time. If we |
| 347 | * don't do this here, then it'll be torn down by exit_aio(). |
| 348 | * But for that case we can parallellize the freeing, thus |
| 349 | * speeding it up a lot. |
| 350 | */ |
| 351 | if (!(td->flags & TD_F_CHILD)) |
| 352 | io_destroy(ld->aio_ctx); |
| 353 | free(ld->aio_events); |
| 354 | free(ld->iocbs); |
| 355 | free(ld->io_us); |
| 356 | free(ld); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | static int fio_libaio_queue_init(struct libaio_data *ld, unsigned int depth, |
| 361 | bool hipri) |
| 362 | { |
| 363 | #ifdef __NR_sys_io_setup2 |
| 364 | int flags = 0; |
| 365 | |
| 366 | if (hipri) |
| 367 | flags = IOCTX_FLAG_IOPOLL; |
| 368 | |
| 369 | return syscall(__NR_sys_io_setup2, depth, flags, &ld->aio_ctx); |
| 370 | #else |
| 371 | if (hipri) { |
| 372 | log_err("fio: polled aio not available on your platform\n"); |
| 373 | return 1; |
| 374 | } |
| 375 | return io_queue_init(depth, &ld->aio_ctx); |
| 376 | #endif |
| 377 | } |
| 378 | |
| 379 | static int fio_libaio_init(struct thread_data *td) |
| 380 | { |
| 381 | struct libaio_options *o = td->eo; |
| 382 | struct libaio_data *ld; |
| 383 | int err = 0; |
| 384 | |
| 385 | ld = calloc(1, sizeof(*ld)); |
| 386 | |
| 387 | /* |
| 388 | * First try passing in 0 for queue depth, since we don't |
| 389 | * care about the user ring. If that fails, the kernel is too old |
| 390 | * and we need the right depth. |
| 391 | */ |
| 392 | err = fio_libaio_queue_init(ld, td->o.iodepth, o->hipri); |
| 393 | if (err) { |
| 394 | td_verror(td, -err, "io_queue_init"); |
| 395 | log_err("fio: check /proc/sys/fs/aio-max-nr\n"); |
| 396 | free(ld); |
| 397 | return 1; |
| 398 | } |
| 399 | |
| 400 | ld->entries = td->o.iodepth; |
| 401 | ld->is_pow2 = is_power_of_2(ld->entries); |
| 402 | ld->aio_events = calloc(ld->entries, sizeof(struct io_event)); |
| 403 | ld->iocbs = calloc(ld->entries, sizeof(struct iocb *)); |
| 404 | ld->io_us = calloc(ld->entries, sizeof(struct io_u *)); |
| 405 | |
| 406 | td->io_ops_data = ld; |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static struct ioengine_ops ioengine = { |
| 411 | .name = "libaio", |
| 412 | .version = FIO_IOOPS_VERSION, |
| 413 | .init = fio_libaio_init, |
| 414 | .prep = fio_libaio_prep, |
| 415 | .queue = fio_libaio_queue, |
| 416 | .commit = fio_libaio_commit, |
| 417 | .cancel = fio_libaio_cancel, |
| 418 | .getevents = fio_libaio_getevents, |
| 419 | .event = fio_libaio_event, |
| 420 | .cleanup = fio_libaio_cleanup, |
| 421 | .open_file = generic_open_file, |
| 422 | .close_file = generic_close_file, |
| 423 | .get_file_size = generic_get_file_size, |
| 424 | .options = options, |
| 425 | .option_struct_size = sizeof(struct libaio_options), |
| 426 | }; |
| 427 | |
| 428 | static void fio_init fio_libaio_register(void) |
| 429 | { |
| 430 | register_ioengine(&ioengine); |
| 431 | } |
| 432 | |
| 433 | static void fio_exit fio_libaio_unregister(void) |
| 434 | { |
| 435 | unregister_ioengine(&ioengine); |
| 436 | } |