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