| 1 | /* |
| 2 | * io_uring engine |
| 3 | * |
| 4 | * IO engine using the new native Linux aio io_uring interface. See: |
| 5 | * |
| 6 | * http://git.kernel.dk/cgit/linux-block/log/?h=io_uring |
| 7 | * |
| 8 | */ |
| 9 | #include <stdlib.h> |
| 10 | #include <unistd.h> |
| 11 | #include <errno.h> |
| 12 | #include <sys/time.h> |
| 13 | #include <sys/resource.h> |
| 14 | |
| 15 | #include "../fio.h" |
| 16 | #include "../lib/pow2.h" |
| 17 | #include "../optgroup.h" |
| 18 | #include "../lib/memalign.h" |
| 19 | #include "../lib/fls.h" |
| 20 | |
| 21 | #ifdef ARCH_HAVE_IOURING |
| 22 | |
| 23 | #include "../lib/types.h" |
| 24 | #include "../os/linux/io_uring.h" |
| 25 | |
| 26 | struct io_sq_ring { |
| 27 | unsigned *head; |
| 28 | unsigned *tail; |
| 29 | unsigned *ring_mask; |
| 30 | unsigned *ring_entries; |
| 31 | unsigned *flags; |
| 32 | unsigned *array; |
| 33 | }; |
| 34 | |
| 35 | struct io_cq_ring { |
| 36 | unsigned *head; |
| 37 | unsigned *tail; |
| 38 | unsigned *ring_mask; |
| 39 | unsigned *ring_entries; |
| 40 | struct io_uring_cqe *cqes; |
| 41 | }; |
| 42 | |
| 43 | struct ioring_mmap { |
| 44 | void *ptr; |
| 45 | size_t len; |
| 46 | }; |
| 47 | |
| 48 | struct ioring_data { |
| 49 | int ring_fd; |
| 50 | |
| 51 | struct io_u **io_u_index; |
| 52 | |
| 53 | int *fds; |
| 54 | |
| 55 | struct io_sq_ring sq_ring; |
| 56 | struct io_uring_sqe *sqes; |
| 57 | struct iovec *iovecs; |
| 58 | unsigned sq_ring_mask; |
| 59 | |
| 60 | struct io_cq_ring cq_ring; |
| 61 | unsigned cq_ring_mask; |
| 62 | |
| 63 | int queued; |
| 64 | int cq_ring_off; |
| 65 | unsigned iodepth; |
| 66 | |
| 67 | struct ioring_mmap mmap[3]; |
| 68 | }; |
| 69 | |
| 70 | struct ioring_options { |
| 71 | void *pad; |
| 72 | unsigned int hipri; |
| 73 | unsigned int fixedbufs; |
| 74 | unsigned int registerfiles; |
| 75 | unsigned int sqpoll_thread; |
| 76 | unsigned int sqpoll_set; |
| 77 | unsigned int sqpoll_cpu; |
| 78 | unsigned int nonvectored; |
| 79 | unsigned int uncached; |
| 80 | }; |
| 81 | |
| 82 | static const int ddir_to_op[2][2] = { |
| 83 | { IORING_OP_READV, IORING_OP_READ }, |
| 84 | { IORING_OP_WRITEV, IORING_OP_WRITE } |
| 85 | }; |
| 86 | |
| 87 | static const int fixed_ddir_to_op[2] = { |
| 88 | IORING_OP_READ_FIXED, |
| 89 | IORING_OP_WRITE_FIXED |
| 90 | }; |
| 91 | |
| 92 | static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val) |
| 93 | { |
| 94 | struct ioring_options *o = data; |
| 95 | |
| 96 | o->sqpoll_cpu = *val; |
| 97 | o->sqpoll_set = 1; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static struct fio_option options[] = { |
| 102 | { |
| 103 | .name = "hipri", |
| 104 | .lname = "High Priority", |
| 105 | .type = FIO_OPT_STR_SET, |
| 106 | .off1 = offsetof(struct ioring_options, hipri), |
| 107 | .help = "Use polled IO completions", |
| 108 | .category = FIO_OPT_C_ENGINE, |
| 109 | .group = FIO_OPT_G_IOURING, |
| 110 | }, |
| 111 | { |
| 112 | .name = "fixedbufs", |
| 113 | .lname = "Fixed (pre-mapped) IO buffers", |
| 114 | .type = FIO_OPT_STR_SET, |
| 115 | .off1 = offsetof(struct ioring_options, fixedbufs), |
| 116 | .help = "Pre map IO buffers", |
| 117 | .category = FIO_OPT_C_ENGINE, |
| 118 | .group = FIO_OPT_G_IOURING, |
| 119 | }, |
| 120 | { |
| 121 | .name = "registerfiles", |
| 122 | .lname = "Register file set", |
| 123 | .type = FIO_OPT_STR_SET, |
| 124 | .off1 = offsetof(struct ioring_options, registerfiles), |
| 125 | .help = "Pre-open/register files", |
| 126 | .category = FIO_OPT_C_ENGINE, |
| 127 | .group = FIO_OPT_G_IOURING, |
| 128 | }, |
| 129 | { |
| 130 | .name = "sqthread_poll", |
| 131 | .lname = "Kernel SQ thread polling", |
| 132 | .type = FIO_OPT_INT, |
| 133 | .off1 = offsetof(struct ioring_options, sqpoll_thread), |
| 134 | .help = "Offload submission/completion to kernel thread", |
| 135 | .category = FIO_OPT_C_ENGINE, |
| 136 | .group = FIO_OPT_G_IOURING, |
| 137 | }, |
| 138 | { |
| 139 | .name = "sqthread_poll_cpu", |
| 140 | .lname = "SQ Thread Poll CPU", |
| 141 | .type = FIO_OPT_INT, |
| 142 | .cb = fio_ioring_sqpoll_cb, |
| 143 | .help = "What CPU to run SQ thread polling on", |
| 144 | .category = FIO_OPT_C_ENGINE, |
| 145 | .group = FIO_OPT_G_IOURING, |
| 146 | }, |
| 147 | { |
| 148 | .name = "nonvectored", |
| 149 | .lname = "Non-vectored", |
| 150 | .type = FIO_OPT_INT, |
| 151 | .off1 = offsetof(struct ioring_options, nonvectored), |
| 152 | .help = "Use non-vectored read/write commands", |
| 153 | .category = FIO_OPT_C_ENGINE, |
| 154 | .group = FIO_OPT_G_IOURING, |
| 155 | }, |
| 156 | { |
| 157 | .name = "uncached", |
| 158 | .lname = "Uncached", |
| 159 | .type = FIO_OPT_INT, |
| 160 | .off1 = offsetof(struct ioring_options, uncached), |
| 161 | .help = "Use RWF_UNCACHED for buffered read/writes", |
| 162 | .category = FIO_OPT_C_ENGINE, |
| 163 | .group = FIO_OPT_G_IOURING, |
| 164 | }, |
| 165 | { |
| 166 | .name = NULL, |
| 167 | }, |
| 168 | }; |
| 169 | |
| 170 | static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit, |
| 171 | unsigned int min_complete, unsigned int flags) |
| 172 | { |
| 173 | return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit, |
| 174 | min_complete, flags, NULL, 0); |
| 175 | } |
| 176 | |
| 177 | static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u) |
| 178 | { |
| 179 | struct ioring_data *ld = td->io_ops_data; |
| 180 | struct ioring_options *o = td->eo; |
| 181 | struct fio_file *f = io_u->file; |
| 182 | struct io_uring_sqe *sqe; |
| 183 | |
| 184 | sqe = &ld->sqes[io_u->index]; |
| 185 | |
| 186 | /* zero out fields not used in this submission */ |
| 187 | memset(sqe, 0, sizeof(*sqe)); |
| 188 | |
| 189 | if (o->registerfiles) { |
| 190 | sqe->fd = f->engine_pos; |
| 191 | sqe->flags = IOSQE_FIXED_FILE; |
| 192 | } else { |
| 193 | sqe->fd = f->fd; |
| 194 | } |
| 195 | |
| 196 | if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) { |
| 197 | if (o->fixedbufs) { |
| 198 | sqe->opcode = fixed_ddir_to_op[io_u->ddir]; |
| 199 | sqe->addr = (unsigned long) io_u->xfer_buf; |
| 200 | sqe->len = io_u->xfer_buflen; |
| 201 | sqe->buf_index = io_u->index; |
| 202 | } else { |
| 203 | sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored]; |
| 204 | if (o->nonvectored) { |
| 205 | sqe->addr = (unsigned long) |
| 206 | ld->iovecs[io_u->index].iov_base; |
| 207 | sqe->len = ld->iovecs[io_u->index].iov_len; |
| 208 | } else { |
| 209 | sqe->addr = (unsigned long) &ld->iovecs[io_u->index]; |
| 210 | sqe->len = 1; |
| 211 | } |
| 212 | } |
| 213 | if (!td->o.odirect && o->uncached) |
| 214 | sqe->rw_flags = RWF_UNCACHED; |
| 215 | if (fio_option_is_set(&td->o, ioprio_class)) |
| 216 | sqe->ioprio = td->o.ioprio_class << 13; |
| 217 | if (fio_option_is_set(&td->o, ioprio)) |
| 218 | sqe->ioprio |= td->o.ioprio; |
| 219 | sqe->off = io_u->offset; |
| 220 | } else if (ddir_sync(io_u->ddir)) { |
| 221 | if (io_u->ddir == DDIR_SYNC_FILE_RANGE) { |
| 222 | sqe->off = f->first_write; |
| 223 | sqe->len = f->last_write - f->first_write; |
| 224 | sqe->sync_range_flags = td->o.sync_file_range; |
| 225 | sqe->opcode = IORING_OP_SYNC_FILE_RANGE; |
| 226 | } else { |
| 227 | if (io_u->ddir == DDIR_DATASYNC) |
| 228 | sqe->fsync_flags |= IORING_FSYNC_DATASYNC; |
| 229 | sqe->opcode = IORING_OP_FSYNC; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | sqe->user_data = (unsigned long) io_u; |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static struct io_u *fio_ioring_event(struct thread_data *td, int event) |
| 238 | { |
| 239 | struct ioring_data *ld = td->io_ops_data; |
| 240 | struct io_uring_cqe *cqe; |
| 241 | struct io_u *io_u; |
| 242 | unsigned index; |
| 243 | |
| 244 | index = (event + ld->cq_ring_off) & ld->cq_ring_mask; |
| 245 | |
| 246 | cqe = &ld->cq_ring.cqes[index]; |
| 247 | io_u = (struct io_u *) (uintptr_t) cqe->user_data; |
| 248 | |
| 249 | if (cqe->res != io_u->xfer_buflen) { |
| 250 | if (cqe->res > io_u->xfer_buflen) |
| 251 | io_u->error = -cqe->res; |
| 252 | else |
| 253 | io_u->resid = io_u->xfer_buflen - cqe->res; |
| 254 | } else |
| 255 | io_u->error = 0; |
| 256 | |
| 257 | return io_u; |
| 258 | } |
| 259 | |
| 260 | static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events, |
| 261 | unsigned int max) |
| 262 | { |
| 263 | struct ioring_data *ld = td->io_ops_data; |
| 264 | struct io_cq_ring *ring = &ld->cq_ring; |
| 265 | unsigned head, reaped = 0; |
| 266 | |
| 267 | head = *ring->head; |
| 268 | do { |
| 269 | read_barrier(); |
| 270 | if (head == *ring->tail) |
| 271 | break; |
| 272 | reaped++; |
| 273 | head++; |
| 274 | } while (reaped + events < max); |
| 275 | |
| 276 | *ring->head = head; |
| 277 | write_barrier(); |
| 278 | return reaped; |
| 279 | } |
| 280 | |
| 281 | static int fio_ioring_getevents(struct thread_data *td, unsigned int min, |
| 282 | unsigned int max, const struct timespec *t) |
| 283 | { |
| 284 | struct ioring_data *ld = td->io_ops_data; |
| 285 | unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min; |
| 286 | struct ioring_options *o = td->eo; |
| 287 | struct io_cq_ring *ring = &ld->cq_ring; |
| 288 | unsigned events = 0; |
| 289 | int r; |
| 290 | |
| 291 | ld->cq_ring_off = *ring->head; |
| 292 | do { |
| 293 | r = fio_ioring_cqring_reap(td, events, max); |
| 294 | if (r) { |
| 295 | events += r; |
| 296 | if (actual_min != 0) |
| 297 | actual_min -= r; |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | if (!o->sqpoll_thread) { |
| 302 | r = io_uring_enter(ld, 0, actual_min, |
| 303 | IORING_ENTER_GETEVENTS); |
| 304 | if (r < 0) { |
| 305 | if (errno == EAGAIN || errno == EINTR) |
| 306 | continue; |
| 307 | td_verror(td, errno, "io_uring_enter"); |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | } while (events < min); |
| 312 | |
| 313 | return r < 0 ? r : events; |
| 314 | } |
| 315 | |
| 316 | static enum fio_q_status fio_ioring_queue(struct thread_data *td, |
| 317 | struct io_u *io_u) |
| 318 | { |
| 319 | struct ioring_data *ld = td->io_ops_data; |
| 320 | struct io_sq_ring *ring = &ld->sq_ring; |
| 321 | unsigned tail, next_tail; |
| 322 | |
| 323 | fio_ro_check(td, io_u); |
| 324 | |
| 325 | if (ld->queued == ld->iodepth) |
| 326 | return FIO_Q_BUSY; |
| 327 | |
| 328 | if (io_u->ddir == DDIR_TRIM) { |
| 329 | if (ld->queued) |
| 330 | return FIO_Q_BUSY; |
| 331 | |
| 332 | do_io_u_trim(td, io_u); |
| 333 | io_u_mark_submit(td, 1); |
| 334 | io_u_mark_complete(td, 1); |
| 335 | return FIO_Q_COMPLETED; |
| 336 | } |
| 337 | |
| 338 | tail = *ring->tail; |
| 339 | next_tail = tail + 1; |
| 340 | read_barrier(); |
| 341 | if (next_tail == *ring->head) |
| 342 | return FIO_Q_BUSY; |
| 343 | |
| 344 | /* ensure sqe stores are ordered with tail update */ |
| 345 | write_barrier(); |
| 346 | ring->array[tail & ld->sq_ring_mask] = io_u->index; |
| 347 | *ring->tail = next_tail; |
| 348 | write_barrier(); |
| 349 | |
| 350 | ld->queued++; |
| 351 | return FIO_Q_QUEUED; |
| 352 | } |
| 353 | |
| 354 | static void fio_ioring_queued(struct thread_data *td, int start, int nr) |
| 355 | { |
| 356 | struct ioring_data *ld = td->io_ops_data; |
| 357 | struct timespec now; |
| 358 | |
| 359 | if (!fio_fill_issue_time(td)) |
| 360 | return; |
| 361 | |
| 362 | fio_gettime(&now, NULL); |
| 363 | |
| 364 | while (nr--) { |
| 365 | struct io_sq_ring *ring = &ld->sq_ring; |
| 366 | int index = ring->array[start & ld->sq_ring_mask]; |
| 367 | struct io_u *io_u = ld->io_u_index[index]; |
| 368 | |
| 369 | memcpy(&io_u->issue_time, &now, sizeof(now)); |
| 370 | io_u_queued(td, io_u); |
| 371 | |
| 372 | start++; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | static int fio_ioring_commit(struct thread_data *td) |
| 377 | { |
| 378 | struct ioring_data *ld = td->io_ops_data; |
| 379 | struct ioring_options *o = td->eo; |
| 380 | int ret; |
| 381 | |
| 382 | if (!ld->queued) |
| 383 | return 0; |
| 384 | |
| 385 | /* |
| 386 | * Kernel side does submission. just need to check if the ring is |
| 387 | * flagged as needing a kick, if so, call io_uring_enter(). This |
| 388 | * only happens if we've been idle too long. |
| 389 | */ |
| 390 | if (o->sqpoll_thread) { |
| 391 | struct io_sq_ring *ring = &ld->sq_ring; |
| 392 | |
| 393 | read_barrier(); |
| 394 | if (*ring->flags & IORING_SQ_NEED_WAKEUP) |
| 395 | io_uring_enter(ld, ld->queued, 0, |
| 396 | IORING_ENTER_SQ_WAKEUP); |
| 397 | ld->queued = 0; |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | do { |
| 402 | unsigned start = *ld->sq_ring.head; |
| 403 | long nr = ld->queued; |
| 404 | |
| 405 | ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS); |
| 406 | if (ret > 0) { |
| 407 | fio_ioring_queued(td, start, ret); |
| 408 | io_u_mark_submit(td, ret); |
| 409 | |
| 410 | ld->queued -= ret; |
| 411 | ret = 0; |
| 412 | } else if (!ret) { |
| 413 | io_u_mark_submit(td, ret); |
| 414 | continue; |
| 415 | } else { |
| 416 | if (errno == EAGAIN || errno == EINTR) { |
| 417 | ret = fio_ioring_cqring_reap(td, 0, ld->queued); |
| 418 | if (ret) |
| 419 | continue; |
| 420 | /* Shouldn't happen */ |
| 421 | usleep(1); |
| 422 | continue; |
| 423 | } |
| 424 | td_verror(td, errno, "io_uring_enter submit"); |
| 425 | break; |
| 426 | } |
| 427 | } while (ld->queued); |
| 428 | |
| 429 | return ret; |
| 430 | } |
| 431 | |
| 432 | static void fio_ioring_unmap(struct ioring_data *ld) |
| 433 | { |
| 434 | int i; |
| 435 | |
| 436 | for (i = 0; i < ARRAY_SIZE(ld->mmap); i++) |
| 437 | munmap(ld->mmap[i].ptr, ld->mmap[i].len); |
| 438 | close(ld->ring_fd); |
| 439 | } |
| 440 | |
| 441 | static void fio_ioring_cleanup(struct thread_data *td) |
| 442 | { |
| 443 | struct ioring_data *ld = td->io_ops_data; |
| 444 | |
| 445 | if (ld) { |
| 446 | if (!(td->flags & TD_F_CHILD)) |
| 447 | fio_ioring_unmap(ld); |
| 448 | |
| 449 | free(ld->io_u_index); |
| 450 | free(ld->iovecs); |
| 451 | free(ld->fds); |
| 452 | free(ld); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p) |
| 457 | { |
| 458 | struct io_sq_ring *sring = &ld->sq_ring; |
| 459 | struct io_cq_ring *cring = &ld->cq_ring; |
| 460 | void *ptr; |
| 461 | |
| 462 | ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32); |
| 463 | ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE, |
| 464 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, |
| 465 | IORING_OFF_SQ_RING); |
| 466 | ld->mmap[0].ptr = ptr; |
| 467 | sring->head = ptr + p->sq_off.head; |
| 468 | sring->tail = ptr + p->sq_off.tail; |
| 469 | sring->ring_mask = ptr + p->sq_off.ring_mask; |
| 470 | sring->ring_entries = ptr + p->sq_off.ring_entries; |
| 471 | sring->flags = ptr + p->sq_off.flags; |
| 472 | sring->array = ptr + p->sq_off.array; |
| 473 | ld->sq_ring_mask = *sring->ring_mask; |
| 474 | |
| 475 | ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe); |
| 476 | ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE, |
| 477 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, |
| 478 | IORING_OFF_SQES); |
| 479 | ld->mmap[1].ptr = ld->sqes; |
| 480 | |
| 481 | ld->mmap[2].len = p->cq_off.cqes + |
| 482 | p->cq_entries * sizeof(struct io_uring_cqe); |
| 483 | ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE, |
| 484 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, |
| 485 | IORING_OFF_CQ_RING); |
| 486 | ld->mmap[2].ptr = ptr; |
| 487 | cring->head = ptr + p->cq_off.head; |
| 488 | cring->tail = ptr + p->cq_off.tail; |
| 489 | cring->ring_mask = ptr + p->cq_off.ring_mask; |
| 490 | cring->ring_entries = ptr + p->cq_off.ring_entries; |
| 491 | cring->cqes = ptr + p->cq_off.cqes; |
| 492 | ld->cq_ring_mask = *cring->ring_mask; |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int fio_ioring_queue_init(struct thread_data *td) |
| 497 | { |
| 498 | struct ioring_data *ld = td->io_ops_data; |
| 499 | struct ioring_options *o = td->eo; |
| 500 | int depth = td->o.iodepth; |
| 501 | struct io_uring_params p; |
| 502 | int ret; |
| 503 | |
| 504 | memset(&p, 0, sizeof(p)); |
| 505 | |
| 506 | if (o->hipri) |
| 507 | p.flags |= IORING_SETUP_IOPOLL; |
| 508 | if (o->sqpoll_thread) { |
| 509 | p.flags |= IORING_SETUP_SQPOLL; |
| 510 | if (o->sqpoll_set) { |
| 511 | p.flags |= IORING_SETUP_SQ_AFF; |
| 512 | p.sq_thread_cpu = o->sqpoll_cpu; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | ret = syscall(__NR_sys_io_uring_setup, depth, &p); |
| 517 | if (ret < 0) |
| 518 | return ret; |
| 519 | |
| 520 | ld->ring_fd = ret; |
| 521 | |
| 522 | if (o->fixedbufs) { |
| 523 | struct rlimit rlim = { |
| 524 | .rlim_cur = RLIM_INFINITY, |
| 525 | .rlim_max = RLIM_INFINITY, |
| 526 | }; |
| 527 | |
| 528 | if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) |
| 529 | return -1; |
| 530 | |
| 531 | ret = syscall(__NR_sys_io_uring_register, ld->ring_fd, |
| 532 | IORING_REGISTER_BUFFERS, ld->iovecs, depth); |
| 533 | if (ret < 0) |
| 534 | return ret; |
| 535 | } |
| 536 | |
| 537 | return fio_ioring_mmap(ld, &p); |
| 538 | } |
| 539 | |
| 540 | static int fio_ioring_register_files(struct thread_data *td) |
| 541 | { |
| 542 | struct ioring_data *ld = td->io_ops_data; |
| 543 | struct fio_file *f; |
| 544 | unsigned int i; |
| 545 | int ret; |
| 546 | |
| 547 | ld->fds = calloc(td->o.nr_files, sizeof(int)); |
| 548 | |
| 549 | for_each_file(td, f, i) { |
| 550 | ret = generic_open_file(td, f); |
| 551 | if (ret) |
| 552 | goto err; |
| 553 | ld->fds[i] = f->fd; |
| 554 | f->engine_pos = i; |
| 555 | } |
| 556 | |
| 557 | ret = syscall(__NR_sys_io_uring_register, ld->ring_fd, |
| 558 | IORING_REGISTER_FILES, ld->fds, td->o.nr_files); |
| 559 | if (ret) { |
| 560 | err: |
| 561 | free(ld->fds); |
| 562 | ld->fds = NULL; |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * Pretend the file is closed again, and really close it if we hit |
| 567 | * an error. |
| 568 | */ |
| 569 | for_each_file(td, f, i) { |
| 570 | if (ret) { |
| 571 | int fio_unused ret2; |
| 572 | ret2 = generic_close_file(td, f); |
| 573 | } else |
| 574 | f->fd = -1; |
| 575 | } |
| 576 | |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | static int fio_ioring_post_init(struct thread_data *td) |
| 581 | { |
| 582 | struct ioring_data *ld = td->io_ops_data; |
| 583 | struct ioring_options *o = td->eo; |
| 584 | struct io_u *io_u; |
| 585 | int err, i; |
| 586 | |
| 587 | for (i = 0; i < td->o.iodepth; i++) { |
| 588 | struct iovec *iov = &ld->iovecs[i]; |
| 589 | |
| 590 | io_u = ld->io_u_index[i]; |
| 591 | iov->iov_base = io_u->buf; |
| 592 | iov->iov_len = td_max_bs(td); |
| 593 | } |
| 594 | |
| 595 | err = fio_ioring_queue_init(td); |
| 596 | if (err) { |
| 597 | td_verror(td, errno, "io_queue_init"); |
| 598 | return 1; |
| 599 | } |
| 600 | |
| 601 | if (o->registerfiles) { |
| 602 | err = fio_ioring_register_files(td); |
| 603 | if (err) { |
| 604 | td_verror(td, errno, "ioring_register_files"); |
| 605 | return 1; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static unsigned roundup_pow2(unsigned depth) |
| 613 | { |
| 614 | return 1UL << __fls(depth - 1); |
| 615 | } |
| 616 | |
| 617 | static int fio_ioring_init(struct thread_data *td) |
| 618 | { |
| 619 | struct ioring_options *o = td->eo; |
| 620 | struct ioring_data *ld; |
| 621 | |
| 622 | /* sqthread submission requires registered files */ |
| 623 | if (o->sqpoll_thread) |
| 624 | o->registerfiles = 1; |
| 625 | |
| 626 | if (o->registerfiles && td->o.nr_files != td->o.open_files) { |
| 627 | log_err("fio: io_uring registered files require nr_files to " |
| 628 | "be identical to open_files\n"); |
| 629 | return 1; |
| 630 | } |
| 631 | |
| 632 | ld = calloc(1, sizeof(*ld)); |
| 633 | |
| 634 | /* ring depth must be a power-of-2 */ |
| 635 | ld->iodepth = td->o.iodepth; |
| 636 | td->o.iodepth = roundup_pow2(td->o.iodepth); |
| 637 | |
| 638 | /* io_u index */ |
| 639 | ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *)); |
| 640 | ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec)); |
| 641 | |
| 642 | td->io_ops_data = ld; |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u) |
| 647 | { |
| 648 | struct ioring_data *ld = td->io_ops_data; |
| 649 | |
| 650 | ld->io_u_index[io_u->index] = io_u; |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f) |
| 655 | { |
| 656 | struct ioring_data *ld = td->io_ops_data; |
| 657 | struct ioring_options *o = td->eo; |
| 658 | |
| 659 | if (!ld || !o->registerfiles) |
| 660 | return generic_open_file(td, f); |
| 661 | |
| 662 | f->fd = ld->fds[f->engine_pos]; |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f) |
| 667 | { |
| 668 | struct ioring_data *ld = td->io_ops_data; |
| 669 | struct ioring_options *o = td->eo; |
| 670 | |
| 671 | if (!ld || !o->registerfiles) |
| 672 | return generic_close_file(td, f); |
| 673 | |
| 674 | f->fd = -1; |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | static struct ioengine_ops ioengine = { |
| 679 | .name = "io_uring", |
| 680 | .version = FIO_IOOPS_VERSION, |
| 681 | .flags = FIO_ASYNCIO_SYNC_TRIM, |
| 682 | .init = fio_ioring_init, |
| 683 | .post_init = fio_ioring_post_init, |
| 684 | .io_u_init = fio_ioring_io_u_init, |
| 685 | .prep = fio_ioring_prep, |
| 686 | .queue = fio_ioring_queue, |
| 687 | .commit = fio_ioring_commit, |
| 688 | .getevents = fio_ioring_getevents, |
| 689 | .event = fio_ioring_event, |
| 690 | .cleanup = fio_ioring_cleanup, |
| 691 | .open_file = fio_ioring_open_file, |
| 692 | .close_file = fio_ioring_close_file, |
| 693 | .get_file_size = generic_get_file_size, |
| 694 | .options = options, |
| 695 | .option_struct_size = sizeof(struct ioring_options), |
| 696 | }; |
| 697 | |
| 698 | static void fio_init fio_ioring_register(void) |
| 699 | { |
| 700 | register_ioengine(&ioengine); |
| 701 | } |
| 702 | |
| 703 | static void fio_exit fio_ioring_unregister(void) |
| 704 | { |
| 705 | unregister_ioengine(&ioengine); |
| 706 | } |
| 707 | #endif |