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