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