engines/exec: Code cleanup to remove leaks
[fio.git] / engines / io_uring.c
... / ...
CommitLineData
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#include "../lib/roundup.h"
21
22#ifdef ARCH_HAVE_IOURING
23
24#include "../lib/types.h"
25#include "../os/linux/io_uring.h"
26
27struct io_sq_ring {
28 unsigned *head;
29 unsigned *tail;
30 unsigned *ring_mask;
31 unsigned *ring_entries;
32 unsigned *flags;
33 unsigned *array;
34};
35
36struct io_cq_ring {
37 unsigned *head;
38 unsigned *tail;
39 unsigned *ring_mask;
40 unsigned *ring_entries;
41 struct io_uring_cqe *cqes;
42};
43
44struct ioring_mmap {
45 void *ptr;
46 size_t len;
47};
48
49struct ioring_data {
50 int ring_fd;
51
52 struct io_u **io_u_index;
53
54 int *fds;
55
56 struct io_sq_ring sq_ring;
57 struct io_uring_sqe *sqes;
58 struct iovec *iovecs;
59 unsigned sq_ring_mask;
60
61 struct io_cq_ring cq_ring;
62 unsigned cq_ring_mask;
63
64 int queued;
65 int cq_ring_off;
66 unsigned iodepth;
67 bool ioprio_class_set;
68 bool ioprio_set;
69 int prepped;
70
71 struct ioring_mmap mmap[3];
72};
73
74struct ioring_options {
75 void *pad;
76 unsigned int hipri;
77 unsigned int cmdprio_percentage;
78 unsigned int fixedbufs;
79 unsigned int registerfiles;
80 unsigned int sqpoll_thread;
81 unsigned int sqpoll_set;
82 unsigned int sqpoll_cpu;
83 unsigned int nonvectored;
84 unsigned int uncached;
85 unsigned int nowait;
86 unsigned int force_async;
87};
88
89static const int ddir_to_op[2][2] = {
90 { IORING_OP_READV, IORING_OP_READ },
91 { IORING_OP_WRITEV, IORING_OP_WRITE }
92};
93
94static const int fixed_ddir_to_op[2] = {
95 IORING_OP_READ_FIXED,
96 IORING_OP_WRITE_FIXED
97};
98
99static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
100{
101 struct ioring_options *o = data;
102
103 o->sqpoll_cpu = *val;
104 o->sqpoll_set = 1;
105 return 0;
106}
107
108static struct fio_option options[] = {
109 {
110 .name = "hipri",
111 .lname = "High Priority",
112 .type = FIO_OPT_STR_SET,
113 .off1 = offsetof(struct ioring_options, hipri),
114 .help = "Use polled IO completions",
115 .category = FIO_OPT_C_ENGINE,
116 .group = FIO_OPT_G_IOURING,
117 },
118#ifdef FIO_HAVE_IOPRIO_CLASS
119 {
120 .name = "cmdprio_percentage",
121 .lname = "high priority percentage",
122 .type = FIO_OPT_INT,
123 .off1 = offsetof(struct ioring_options, cmdprio_percentage),
124 .minval = 1,
125 .maxval = 100,
126 .help = "Send high priority I/O this percentage of the time",
127 .category = FIO_OPT_C_ENGINE,
128 .group = FIO_OPT_G_IOURING,
129 },
130#else
131 {
132 .name = "cmdprio_percentage",
133 .lname = "high priority percentage",
134 .type = FIO_OPT_UNSUPPORTED,
135 .help = "Your platform does not support I/O priority classes",
136 },
137#endif
138 {
139 .name = "fixedbufs",
140 .lname = "Fixed (pre-mapped) IO buffers",
141 .type = FIO_OPT_STR_SET,
142 .off1 = offsetof(struct ioring_options, fixedbufs),
143 .help = "Pre map IO buffers",
144 .category = FIO_OPT_C_ENGINE,
145 .group = FIO_OPT_G_IOURING,
146 },
147 {
148 .name = "registerfiles",
149 .lname = "Register file set",
150 .type = FIO_OPT_STR_SET,
151 .off1 = offsetof(struct ioring_options, registerfiles),
152 .help = "Pre-open/register files",
153 .category = FIO_OPT_C_ENGINE,
154 .group = FIO_OPT_G_IOURING,
155 },
156 {
157 .name = "sqthread_poll",
158 .lname = "Kernel SQ thread polling",
159 .type = FIO_OPT_INT,
160 .off1 = offsetof(struct ioring_options, sqpoll_thread),
161 .help = "Offload submission/completion to kernel thread",
162 .category = FIO_OPT_C_ENGINE,
163 .group = FIO_OPT_G_IOURING,
164 },
165 {
166 .name = "sqthread_poll_cpu",
167 .lname = "SQ Thread Poll CPU",
168 .type = FIO_OPT_INT,
169 .cb = fio_ioring_sqpoll_cb,
170 .help = "What CPU to run SQ thread polling on",
171 .category = FIO_OPT_C_ENGINE,
172 .group = FIO_OPT_G_IOURING,
173 },
174 {
175 .name = "nonvectored",
176 .lname = "Non-vectored",
177 .type = FIO_OPT_INT,
178 .off1 = offsetof(struct ioring_options, nonvectored),
179 .def = "-1",
180 .help = "Use non-vectored read/write commands",
181 .category = FIO_OPT_C_ENGINE,
182 .group = FIO_OPT_G_IOURING,
183 },
184 {
185 .name = "uncached",
186 .lname = "Uncached",
187 .type = FIO_OPT_INT,
188 .off1 = offsetof(struct ioring_options, uncached),
189 .help = "Use RWF_UNCACHED for buffered read/writes",
190 .category = FIO_OPT_C_ENGINE,
191 .group = FIO_OPT_G_IOURING,
192 },
193 {
194 .name = "nowait",
195 .lname = "RWF_NOWAIT",
196 .type = FIO_OPT_BOOL,
197 .off1 = offsetof(struct ioring_options, nowait),
198 .help = "Use RWF_NOWAIT for reads/writes",
199 .category = FIO_OPT_C_ENGINE,
200 .group = FIO_OPT_G_IOURING,
201 },
202 {
203 .name = "force_async",
204 .lname = "Force async",
205 .type = FIO_OPT_INT,
206 .off1 = offsetof(struct ioring_options, force_async),
207 .help = "Set IOSQE_ASYNC every N requests",
208 .category = FIO_OPT_C_ENGINE,
209 .group = FIO_OPT_G_IOURING,
210 },
211 {
212 .name = NULL,
213 },
214};
215
216static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
217 unsigned int min_complete, unsigned int flags)
218{
219 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
220 min_complete, flags, NULL, 0);
221}
222
223static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
224{
225 struct ioring_data *ld = td->io_ops_data;
226 struct ioring_options *o = td->eo;
227 struct fio_file *f = io_u->file;
228 struct io_uring_sqe *sqe;
229
230 sqe = &ld->sqes[io_u->index];
231
232 if (o->registerfiles) {
233 sqe->fd = f->engine_pos;
234 sqe->flags = IOSQE_FIXED_FILE;
235 } else {
236 sqe->fd = f->fd;
237 }
238
239 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
240 if (o->fixedbufs) {
241 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
242 sqe->addr = (unsigned long) io_u->xfer_buf;
243 sqe->len = io_u->xfer_buflen;
244 sqe->buf_index = io_u->index;
245 } else {
246 struct iovec *iov = &ld->iovecs[io_u->index];
247
248 /*
249 * Update based on actual io_u, requeue could have
250 * adjusted these
251 */
252 iov->iov_base = io_u->xfer_buf;
253 iov->iov_len = io_u->xfer_buflen;
254
255 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
256 if (o->nonvectored) {
257 sqe->addr = (unsigned long) iov->iov_base;
258 sqe->len = iov->iov_len;
259 } else {
260 sqe->addr = (unsigned long) iov;
261 sqe->len = 1;
262 }
263 }
264 if (!td->o.odirect && o->uncached)
265 sqe->rw_flags = RWF_UNCACHED;
266 if (o->nowait)
267 sqe->rw_flags |= RWF_NOWAIT;
268 if (ld->ioprio_class_set)
269 sqe->ioprio = td->o.ioprio_class << 13;
270 if (ld->ioprio_set)
271 sqe->ioprio |= td->o.ioprio;
272 sqe->off = io_u->offset;
273 sqe->rw_flags = 0;
274 } else if (ddir_sync(io_u->ddir)) {
275 sqe->ioprio = 0;
276 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
277 sqe->off = f->first_write;
278 sqe->len = f->last_write - f->first_write;
279 sqe->sync_range_flags = td->o.sync_file_range;
280 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
281 } else {
282 sqe->off = 0;
283 sqe->addr = 0;
284 sqe->len = 0;
285 if (io_u->ddir == DDIR_DATASYNC)
286 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
287 sqe->opcode = IORING_OP_FSYNC;
288 }
289 }
290
291 if (o->force_async && ++ld->prepped == o->force_async) {
292 ld->prepped = 0;
293 sqe->flags |= IOSQE_ASYNC;
294 }
295
296 sqe->user_data = (unsigned long) io_u;
297 return 0;
298}
299
300static struct io_u *fio_ioring_event(struct thread_data *td, int event)
301{
302 struct ioring_data *ld = td->io_ops_data;
303 struct io_uring_cqe *cqe;
304 struct io_u *io_u;
305 unsigned index;
306
307 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
308
309 cqe = &ld->cq_ring.cqes[index];
310 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
311
312 if (cqe->res != io_u->xfer_buflen) {
313 if (cqe->res > io_u->xfer_buflen)
314 io_u->error = -cqe->res;
315 else
316 io_u->resid = io_u->xfer_buflen - cqe->res;
317 } else
318 io_u->error = 0;
319
320 return io_u;
321}
322
323static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
324 unsigned int max)
325{
326 struct ioring_data *ld = td->io_ops_data;
327 struct io_cq_ring *ring = &ld->cq_ring;
328 unsigned head, reaped = 0;
329
330 head = *ring->head;
331 do {
332 if (head == atomic_load_acquire(ring->tail))
333 break;
334 reaped++;
335 head++;
336 } while (reaped + events < max);
337
338 if (reaped)
339 atomic_store_release(ring->head, head);
340
341 return reaped;
342}
343
344static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
345 unsigned int max, const struct timespec *t)
346{
347 struct ioring_data *ld = td->io_ops_data;
348 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
349 struct ioring_options *o = td->eo;
350 struct io_cq_ring *ring = &ld->cq_ring;
351 unsigned events = 0;
352 int r;
353
354 ld->cq_ring_off = *ring->head;
355 do {
356 r = fio_ioring_cqring_reap(td, events, max);
357 if (r) {
358 events += r;
359 if (actual_min != 0)
360 actual_min -= r;
361 continue;
362 }
363
364 if (!o->sqpoll_thread) {
365 r = io_uring_enter(ld, 0, actual_min,
366 IORING_ENTER_GETEVENTS);
367 if (r < 0) {
368 if (errno == EAGAIN || errno == EINTR)
369 continue;
370 td_verror(td, errno, "io_uring_enter");
371 break;
372 }
373 }
374 } while (events < min);
375
376 return r < 0 ? r : events;
377}
378
379static void fio_ioring_prio_prep(struct thread_data *td, struct io_u *io_u)
380{
381 struct ioring_options *o = td->eo;
382 struct ioring_data *ld = td->io_ops_data;
383 if (rand_between(&td->prio_state, 0, 99) < o->cmdprio_percentage) {
384 ld->sqes[io_u->index].ioprio = IOPRIO_CLASS_RT << IOPRIO_CLASS_SHIFT;
385 io_u->flags |= IO_U_F_PRIORITY;
386 }
387 return;
388}
389
390static enum fio_q_status fio_ioring_queue(struct thread_data *td,
391 struct io_u *io_u)
392{
393 struct ioring_data *ld = td->io_ops_data;
394 struct io_sq_ring *ring = &ld->sq_ring;
395 struct ioring_options *o = td->eo;
396 unsigned tail, next_tail;
397
398 fio_ro_check(td, io_u);
399
400 if (ld->queued == ld->iodepth)
401 return FIO_Q_BUSY;
402
403 if (io_u->ddir == DDIR_TRIM) {
404 if (ld->queued)
405 return FIO_Q_BUSY;
406
407 do_io_u_trim(td, io_u);
408 io_u_mark_submit(td, 1);
409 io_u_mark_complete(td, 1);
410 return FIO_Q_COMPLETED;
411 }
412
413 tail = *ring->tail;
414 next_tail = tail + 1;
415 if (next_tail == atomic_load_acquire(ring->head))
416 return FIO_Q_BUSY;
417
418 if (o->cmdprio_percentage)
419 fio_ioring_prio_prep(td, io_u);
420 ring->array[tail & ld->sq_ring_mask] = io_u->index;
421 atomic_store_release(ring->tail, next_tail);
422
423 ld->queued++;
424 return FIO_Q_QUEUED;
425}
426
427static void fio_ioring_queued(struct thread_data *td, int start, int nr)
428{
429 struct ioring_data *ld = td->io_ops_data;
430 struct timespec now;
431
432 if (!fio_fill_issue_time(td))
433 return;
434
435 fio_gettime(&now, NULL);
436
437 while (nr--) {
438 struct io_sq_ring *ring = &ld->sq_ring;
439 int index = ring->array[start & ld->sq_ring_mask];
440 struct io_u *io_u = ld->io_u_index[index];
441
442 memcpy(&io_u->issue_time, &now, sizeof(now));
443 io_u_queued(td, io_u);
444
445 start++;
446 }
447}
448
449static int fio_ioring_commit(struct thread_data *td)
450{
451 struct ioring_data *ld = td->io_ops_data;
452 struct ioring_options *o = td->eo;
453 int ret;
454
455 if (!ld->queued)
456 return 0;
457
458 /*
459 * Kernel side does submission. just need to check if the ring is
460 * flagged as needing a kick, if so, call io_uring_enter(). This
461 * only happens if we've been idle too long.
462 */
463 if (o->sqpoll_thread) {
464 struct io_sq_ring *ring = &ld->sq_ring;
465 unsigned flags;
466
467 flags = atomic_load_acquire(ring->flags);
468 if (flags & IORING_SQ_NEED_WAKEUP)
469 io_uring_enter(ld, ld->queued, 0,
470 IORING_ENTER_SQ_WAKEUP);
471 ld->queued = 0;
472 return 0;
473 }
474
475 do {
476 unsigned start = *ld->sq_ring.head;
477 long nr = ld->queued;
478
479 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
480 if (ret > 0) {
481 fio_ioring_queued(td, start, ret);
482 io_u_mark_submit(td, ret);
483
484 ld->queued -= ret;
485 ret = 0;
486 } else if (!ret) {
487 io_u_mark_submit(td, ret);
488 continue;
489 } else {
490 if (errno == EAGAIN || errno == EINTR) {
491 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
492 if (ret)
493 continue;
494 /* Shouldn't happen */
495 usleep(1);
496 continue;
497 }
498 td_verror(td, errno, "io_uring_enter submit");
499 break;
500 }
501 } while (ld->queued);
502
503 return ret;
504}
505
506static void fio_ioring_unmap(struct ioring_data *ld)
507{
508 int i;
509
510 for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++)
511 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
512 close(ld->ring_fd);
513}
514
515static void fio_ioring_cleanup(struct thread_data *td)
516{
517 struct ioring_data *ld = td->io_ops_data;
518
519 if (ld) {
520 if (!(td->flags & TD_F_CHILD))
521 fio_ioring_unmap(ld);
522
523 free(ld->io_u_index);
524 free(ld->iovecs);
525 free(ld->fds);
526 free(ld);
527 }
528}
529
530static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
531{
532 struct io_sq_ring *sring = &ld->sq_ring;
533 struct io_cq_ring *cring = &ld->cq_ring;
534 void *ptr;
535
536 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
537 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
538 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
539 IORING_OFF_SQ_RING);
540 ld->mmap[0].ptr = ptr;
541 sring->head = ptr + p->sq_off.head;
542 sring->tail = ptr + p->sq_off.tail;
543 sring->ring_mask = ptr + p->sq_off.ring_mask;
544 sring->ring_entries = ptr + p->sq_off.ring_entries;
545 sring->flags = ptr + p->sq_off.flags;
546 sring->array = ptr + p->sq_off.array;
547 ld->sq_ring_mask = *sring->ring_mask;
548
549 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
550 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
551 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
552 IORING_OFF_SQES);
553 ld->mmap[1].ptr = ld->sqes;
554
555 ld->mmap[2].len = p->cq_off.cqes +
556 p->cq_entries * sizeof(struct io_uring_cqe);
557 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
558 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
559 IORING_OFF_CQ_RING);
560 ld->mmap[2].ptr = ptr;
561 cring->head = ptr + p->cq_off.head;
562 cring->tail = ptr + p->cq_off.tail;
563 cring->ring_mask = ptr + p->cq_off.ring_mask;
564 cring->ring_entries = ptr + p->cq_off.ring_entries;
565 cring->cqes = ptr + p->cq_off.cqes;
566 ld->cq_ring_mask = *cring->ring_mask;
567 return 0;
568}
569
570static void fio_ioring_probe(struct thread_data *td)
571{
572 struct ioring_data *ld = td->io_ops_data;
573 struct ioring_options *o = td->eo;
574 struct io_uring_probe *p;
575 int ret;
576
577 /* already set by user, don't touch */
578 if (o->nonvectored != -1)
579 return;
580
581 /* default to off, as that's always safe */
582 o->nonvectored = 0;
583
584 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
585 if (!p)
586 return;
587
588 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
589 ret = syscall(__NR_io_uring_register, ld->ring_fd,
590 IORING_REGISTER_PROBE, p, 256);
591 if (ret < 0)
592 goto out;
593
594 if (IORING_OP_WRITE > p->ops_len)
595 goto out;
596
597 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
598 (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
599 o->nonvectored = 1;
600out:
601 free(p);
602}
603
604static int fio_ioring_queue_init(struct thread_data *td)
605{
606 struct ioring_data *ld = td->io_ops_data;
607 struct ioring_options *o = td->eo;
608 int depth = td->o.iodepth;
609 struct io_uring_params p;
610 int ret;
611
612 memset(&p, 0, sizeof(p));
613
614 if (o->hipri)
615 p.flags |= IORING_SETUP_IOPOLL;
616 if (o->sqpoll_thread) {
617 p.flags |= IORING_SETUP_SQPOLL;
618 if (o->sqpoll_set) {
619 p.flags |= IORING_SETUP_SQ_AFF;
620 p.sq_thread_cpu = o->sqpoll_cpu;
621 }
622 }
623
624 ret = syscall(__NR_io_uring_setup, depth, &p);
625 if (ret < 0)
626 return ret;
627
628 ld->ring_fd = ret;
629
630 fio_ioring_probe(td);
631
632 if (o->fixedbufs) {
633 ret = syscall(__NR_io_uring_register, ld->ring_fd,
634 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
635 if (ret < 0)
636 return ret;
637 }
638
639 return fio_ioring_mmap(ld, &p);
640}
641
642static int fio_ioring_register_files(struct thread_data *td)
643{
644 struct ioring_data *ld = td->io_ops_data;
645 struct fio_file *f;
646 unsigned int i;
647 int ret;
648
649 ld->fds = calloc(td->o.nr_files, sizeof(int));
650
651 for_each_file(td, f, i) {
652 ret = generic_open_file(td, f);
653 if (ret)
654 goto err;
655 ld->fds[i] = f->fd;
656 f->engine_pos = i;
657 }
658
659 ret = syscall(__NR_io_uring_register, ld->ring_fd,
660 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
661 if (ret) {
662err:
663 free(ld->fds);
664 ld->fds = NULL;
665 }
666
667 /*
668 * Pretend the file is closed again, and really close it if we hit
669 * an error.
670 */
671 for_each_file(td, f, i) {
672 if (ret) {
673 int fio_unused ret2;
674 ret2 = generic_close_file(td, f);
675 } else
676 f->fd = -1;
677 }
678
679 return ret;
680}
681
682static int fio_ioring_post_init(struct thread_data *td)
683{
684 struct ioring_data *ld = td->io_ops_data;
685 struct ioring_options *o = td->eo;
686 struct io_u *io_u;
687 int err, i;
688
689 for (i = 0; i < td->o.iodepth; i++) {
690 struct iovec *iov = &ld->iovecs[i];
691
692 io_u = ld->io_u_index[i];
693 iov->iov_base = io_u->buf;
694 iov->iov_len = td_max_bs(td);
695 }
696
697 err = fio_ioring_queue_init(td);
698 if (err) {
699 int init_err = errno;
700
701 if (init_err == ENOSYS)
702 log_err("fio: your kernel doesn't support io_uring\n");
703 td_verror(td, init_err, "io_queue_init");
704 return 1;
705 }
706
707 for (i = 0; i < td->o.iodepth; i++) {
708 struct io_uring_sqe *sqe;
709
710 sqe = &ld->sqes[i];
711 memset(sqe, 0, sizeof(*sqe));
712 }
713
714 if (o->registerfiles) {
715 err = fio_ioring_register_files(td);
716 if (err) {
717 td_verror(td, errno, "ioring_register_files");
718 return 1;
719 }
720 }
721
722 return 0;
723}
724
725static int fio_ioring_init(struct thread_data *td)
726{
727 struct ioring_options *o = td->eo;
728 struct ioring_data *ld;
729 struct thread_options *to = &td->o;
730
731 /* sqthread submission requires registered files */
732 if (o->sqpoll_thread)
733 o->registerfiles = 1;
734
735 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
736 log_err("fio: io_uring registered files require nr_files to "
737 "be identical to open_files\n");
738 return 1;
739 }
740
741 ld = calloc(1, sizeof(*ld));
742
743 /* ring depth must be a power-of-2 */
744 ld->iodepth = td->o.iodepth;
745 td->o.iodepth = roundup_pow2(td->o.iodepth);
746
747 /* io_u index */
748 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
749 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
750
751 td->io_ops_data = ld;
752
753 /*
754 * Check for option conflicts
755 */
756 if ((fio_option_is_set(to, ioprio) || fio_option_is_set(to, ioprio_class)) &&
757 o->cmdprio_percentage != 0) {
758 log_err("%s: cmdprio_percentage option and mutually exclusive "
759 "prio or prioclass option is set, exiting\n", to->name);
760 td_verror(td, EINVAL, "fio_io_uring_init");
761 return 1;
762 }
763
764 if (fio_option_is_set(&td->o, ioprio_class))
765 ld->ioprio_class_set = true;
766 if (fio_option_is_set(&td->o, ioprio))
767 ld->ioprio_set = true;
768
769 return 0;
770}
771
772static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
773{
774 struct ioring_data *ld = td->io_ops_data;
775
776 ld->io_u_index[io_u->index] = io_u;
777 return 0;
778}
779
780static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
781{
782 struct ioring_data *ld = td->io_ops_data;
783 struct ioring_options *o = td->eo;
784
785 if (!ld || !o->registerfiles)
786 return generic_open_file(td, f);
787
788 f->fd = ld->fds[f->engine_pos];
789 return 0;
790}
791
792static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
793{
794 struct ioring_data *ld = td->io_ops_data;
795 struct ioring_options *o = td->eo;
796
797 if (!ld || !o->registerfiles)
798 return generic_close_file(td, f);
799
800 f->fd = -1;
801 return 0;
802}
803
804static struct ioengine_ops ioengine = {
805 .name = "io_uring",
806 .version = FIO_IOOPS_VERSION,
807 .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD,
808 .init = fio_ioring_init,
809 .post_init = fio_ioring_post_init,
810 .io_u_init = fio_ioring_io_u_init,
811 .prep = fio_ioring_prep,
812 .queue = fio_ioring_queue,
813 .commit = fio_ioring_commit,
814 .getevents = fio_ioring_getevents,
815 .event = fio_ioring_event,
816 .cleanup = fio_ioring_cleanup,
817 .open_file = fio_ioring_open_file,
818 .close_file = fio_ioring_close_file,
819 .get_file_size = generic_get_file_size,
820 .options = options,
821 .option_struct_size = sizeof(struct ioring_options),
822};
823
824static void fio_init fio_ioring_register(void)
825{
826 register_ioengine(&ioengine);
827}
828
829static void fio_exit fio_ioring_unregister(void)
830{
831 unregister_ioengine(&ioengine);
832}
833#endif