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