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