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