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