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