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" |
e9f6567a | 26 | #include "cmdprio.h" |
855dc4d4 AG |
27 | #include "nvme.h" |
28 | ||
29 | #include <sys/stat.h> | |
30 | ||
31 | enum uring_cmd_type { | |
32 | FIO_URING_CMD_NVME = 1, | |
33 | }; | |
9a2d78b3 | 34 | |
bffad86f | 35 | struct io_sq_ring { |
e2239016 JA |
36 | unsigned *head; |
37 | unsigned *tail; | |
38 | unsigned *ring_mask; | |
39 | unsigned *ring_entries; | |
40 | unsigned *flags; | |
41 | unsigned *array; | |
52885fa2 JA |
42 | }; |
43 | ||
bffad86f | 44 | struct io_cq_ring { |
e2239016 JA |
45 | unsigned *head; |
46 | unsigned *tail; | |
47 | unsigned *ring_mask; | |
48 | unsigned *ring_entries; | |
f0403f94 | 49 | struct io_uring_cqe *cqes; |
9a2d78b3 JA |
50 | }; |
51 | ||
bffad86f | 52 | struct ioring_mmap { |
9a2d78b3 JA |
53 | void *ptr; |
54 | size_t len; | |
52885fa2 JA |
55 | }; |
56 | ||
bffad86f | 57 | struct ioring_data { |
9a2d78b3 JA |
58 | int ring_fd; |
59 | ||
52885fa2 JA |
60 | struct io_u **io_u_index; |
61 | ||
5ffd5626 JA |
62 | int *fds; |
63 | ||
bffad86f | 64 | struct io_sq_ring sq_ring; |
f0403f94 | 65 | struct io_uring_sqe *sqes; |
9a2d78b3 | 66 | struct iovec *iovecs; |
b87aa01a | 67 | unsigned sq_ring_mask; |
52885fa2 | 68 | |
bffad86f | 69 | struct io_cq_ring cq_ring; |
b87aa01a | 70 | unsigned cq_ring_mask; |
52885fa2 JA |
71 | |
72 | int queued; | |
73 | int cq_ring_off; | |
b87aa01a | 74 | unsigned iodepth; |
5a59a81d | 75 | int prepped; |
96563db9 | 76 | |
bffad86f | 77 | struct ioring_mmap mmap[3]; |
d6cbeab4 NC |
78 | |
79 | struct cmdprio cmdprio; | |
52885fa2 JA |
80 | }; |
81 | ||
bffad86f | 82 | struct ioring_options { |
a48f0cc7 | 83 | struct thread_data *td; |
52885fa2 | 84 | unsigned int hipri; |
d6cbeab4 | 85 | struct cmdprio_options cmdprio_options; |
52885fa2 | 86 | unsigned int fixedbufs; |
5ffd5626 | 87 | unsigned int registerfiles; |
3d7d00a3 | 88 | unsigned int sqpoll_thread; |
2ea53ca3 JA |
89 | unsigned int sqpoll_set; |
90 | unsigned int sqpoll_cpu; | |
b10b1e70 | 91 | unsigned int nonvectored; |
4a87b584 | 92 | unsigned int uncached; |
7d42e66e | 93 | unsigned int nowait; |
5a59a81d | 94 | unsigned int force_async; |
855dc4d4 | 95 | enum uring_cmd_type cmd_type; |
52885fa2 JA |
96 | }; |
97 | ||
b10b1e70 JA |
98 | static const int ddir_to_op[2][2] = { |
99 | { IORING_OP_READV, IORING_OP_READ }, | |
100 | { IORING_OP_WRITEV, IORING_OP_WRITE } | |
101 | }; | |
102 | ||
3f1e3af7 KB |
103 | static const int fixed_ddir_to_op[2] = { |
104 | IORING_OP_READ_FIXED, | |
105 | IORING_OP_WRITE_FIXED | |
106 | }; | |
107 | ||
2ea53ca3 | 108 | static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val) |
a90cd050 | 109 | { |
bffad86f | 110 | struct ioring_options *o = data; |
a90cd050 | 111 | |
2ea53ca3 JA |
112 | o->sqpoll_cpu = *val; |
113 | o->sqpoll_set = 1; | |
a90cd050 JA |
114 | return 0; |
115 | } | |
116 | ||
52885fa2 JA |
117 | static struct fio_option options[] = { |
118 | { | |
119 | .name = "hipri", | |
120 | .lname = "High Priority", | |
121 | .type = FIO_OPT_STR_SET, | |
bffad86f | 122 | .off1 = offsetof(struct ioring_options, hipri), |
52885fa2 JA |
123 | .help = "Use polled IO completions", |
124 | .category = FIO_OPT_C_ENGINE, | |
27f436d9 | 125 | .group = FIO_OPT_G_IOURING, |
52885fa2 | 126 | }, |
b2a432bf PC |
127 | #ifdef FIO_HAVE_IOPRIO_CLASS |
128 | { | |
129 | .name = "cmdprio_percentage", | |
130 | .lname = "high priority percentage", | |
131 | .type = FIO_OPT_INT, | |
e9f6567a | 132 | .off1 = offsetof(struct ioring_options, |
d6cbeab4 | 133 | cmdprio_options.percentage[DDIR_READ]), |
e9f6567a | 134 | .off2 = offsetof(struct ioring_options, |
d6cbeab4 | 135 | cmdprio_options.percentage[DDIR_WRITE]), |
e9f6567a | 136 | .minval = 0, |
b2a432bf PC |
137 | .maxval = 100, |
138 | .help = "Send high priority I/O this percentage of the time", | |
139 | .category = FIO_OPT_C_ENGINE, | |
140 | .group = FIO_OPT_G_IOURING, | |
141 | }, | |
12f9d54a DLM |
142 | { |
143 | .name = "cmdprio_class", | |
144 | .lname = "Asynchronous I/O priority class", | |
145 | .type = FIO_OPT_INT, | |
146 | .off1 = offsetof(struct ioring_options, | |
d6cbeab4 | 147 | cmdprio_options.class[DDIR_READ]), |
12f9d54a | 148 | .off2 = offsetof(struct ioring_options, |
d6cbeab4 | 149 | cmdprio_options.class[DDIR_WRITE]), |
12f9d54a DLM |
150 | .help = "Set asynchronous IO priority class", |
151 | .minval = IOPRIO_MIN_PRIO_CLASS + 1, | |
152 | .maxval = IOPRIO_MAX_PRIO_CLASS, | |
153 | .interval = 1, | |
154 | .category = FIO_OPT_C_ENGINE, | |
155 | .group = FIO_OPT_G_IOURING, | |
156 | }, | |
157 | { | |
158 | .name = "cmdprio", | |
159 | .lname = "Asynchronous I/O priority level", | |
160 | .type = FIO_OPT_INT, | |
161 | .off1 = offsetof(struct ioring_options, | |
d6cbeab4 | 162 | cmdprio_options.level[DDIR_READ]), |
12f9d54a | 163 | .off2 = offsetof(struct ioring_options, |
d6cbeab4 | 164 | cmdprio_options.level[DDIR_WRITE]), |
12f9d54a DLM |
165 | .help = "Set asynchronous IO priority level", |
166 | .minval = IOPRIO_MIN_PRIO, | |
167 | .maxval = IOPRIO_MAX_PRIO, | |
168 | .interval = 1, | |
169 | .category = FIO_OPT_C_ENGINE, | |
170 | .group = FIO_OPT_G_IOURING, | |
171 | }, | |
a48f0cc7 DLM |
172 | { |
173 | .name = "cmdprio_bssplit", | |
174 | .lname = "Priority percentage block size split", | |
d6cbeab4 NC |
175 | .type = FIO_OPT_STR_STORE, |
176 | .off1 = offsetof(struct ioring_options, | |
177 | cmdprio_options.bssplit_str), | |
a48f0cc7 DLM |
178 | .help = "Set priority percentages for different block sizes", |
179 | .category = FIO_OPT_C_ENGINE, | |
180 | .group = FIO_OPT_G_IOURING, | |
181 | }, | |
b2a432bf PC |
182 | #else |
183 | { | |
184 | .name = "cmdprio_percentage", | |
185 | .lname = "high priority percentage", | |
186 | .type = FIO_OPT_UNSUPPORTED, | |
187 | .help = "Your platform does not support I/O priority classes", | |
188 | }, | |
12f9d54a DLM |
189 | { |
190 | .name = "cmdprio_class", | |
191 | .lname = "Asynchronous I/O priority class", | |
192 | .type = FIO_OPT_UNSUPPORTED, | |
193 | .help = "Your platform does not support I/O priority classes", | |
194 | }, | |
195 | { | |
196 | .name = "cmdprio", | |
197 | .lname = "Asynchronous I/O priority level", | |
198 | .type = FIO_OPT_UNSUPPORTED, | |
199 | .help = "Your platform does not support I/O priority classes", | |
200 | }, | |
a48f0cc7 DLM |
201 | { |
202 | .name = "cmdprio_bssplit", | |
203 | .lname = "Priority percentage block size split", | |
204 | .type = FIO_OPT_UNSUPPORTED, | |
205 | .help = "Your platform does not support I/O priority classes", | |
206 | }, | |
b2a432bf | 207 | #endif |
52885fa2 JA |
208 | { |
209 | .name = "fixedbufs", | |
210 | .lname = "Fixed (pre-mapped) IO buffers", | |
211 | .type = FIO_OPT_STR_SET, | |
bffad86f | 212 | .off1 = offsetof(struct ioring_options, fixedbufs), |
52885fa2 JA |
213 | .help = "Pre map IO buffers", |
214 | .category = FIO_OPT_C_ENGINE, | |
27f436d9 | 215 | .group = FIO_OPT_G_IOURING, |
52885fa2 | 216 | }, |
5ffd5626 JA |
217 | { |
218 | .name = "registerfiles", | |
219 | .lname = "Register file set", | |
220 | .type = FIO_OPT_STR_SET, | |
221 | .off1 = offsetof(struct ioring_options, registerfiles), | |
222 | .help = "Pre-open/register files", | |
223 | .category = FIO_OPT_C_ENGINE, | |
27f436d9 | 224 | .group = FIO_OPT_G_IOURING, |
5ffd5626 | 225 | }, |
771c9901 JA |
226 | { |
227 | .name = "sqthread_poll", | |
3d7d00a3 JA |
228 | .lname = "Kernel SQ thread polling", |
229 | .type = FIO_OPT_INT, | |
230 | .off1 = offsetof(struct ioring_options, sqpoll_thread), | |
231 | .help = "Offload submission/completion to kernel thread", | |
232 | .category = FIO_OPT_C_ENGINE, | |
27f436d9 | 233 | .group = FIO_OPT_G_IOURING, |
3d7d00a3 JA |
234 | }, |
235 | { | |
236 | .name = "sqthread_poll_cpu", | |
237 | .lname = "SQ Thread Poll CPU", | |
2ea53ca3 JA |
238 | .type = FIO_OPT_INT, |
239 | .cb = fio_ioring_sqpoll_cb, | |
3d7d00a3 | 240 | .help = "What CPU to run SQ thread polling on", |
a90cd050 | 241 | .category = FIO_OPT_C_ENGINE, |
27f436d9 | 242 | .group = FIO_OPT_G_IOURING, |
a90cd050 | 243 | }, |
b10b1e70 JA |
244 | { |
245 | .name = "nonvectored", | |
246 | .lname = "Non-vectored", | |
247 | .type = FIO_OPT_INT, | |
248 | .off1 = offsetof(struct ioring_options, nonvectored), | |
556d8415 | 249 | .def = "-1", |
b10b1e70 JA |
250 | .help = "Use non-vectored read/write commands", |
251 | .category = FIO_OPT_C_ENGINE, | |
252 | .group = FIO_OPT_G_IOURING, | |
253 | }, | |
4a87b584 JA |
254 | { |
255 | .name = "uncached", | |
256 | .lname = "Uncached", | |
257 | .type = FIO_OPT_INT, | |
258 | .off1 = offsetof(struct ioring_options, uncached), | |
259 | .help = "Use RWF_UNCACHED for buffered read/writes", | |
260 | .category = FIO_OPT_C_ENGINE, | |
261 | .group = FIO_OPT_G_IOURING, | |
262 | }, | |
7d42e66e KK |
263 | { |
264 | .name = "nowait", | |
265 | .lname = "RWF_NOWAIT", | |
266 | .type = FIO_OPT_BOOL, | |
267 | .off1 = offsetof(struct ioring_options, nowait), | |
268 | .help = "Use RWF_NOWAIT for reads/writes", | |
269 | .category = FIO_OPT_C_ENGINE, | |
270 | .group = FIO_OPT_G_IOURING, | |
271 | }, | |
5a59a81d JA |
272 | { |
273 | .name = "force_async", | |
274 | .lname = "Force async", | |
275 | .type = FIO_OPT_INT, | |
276 | .off1 = offsetof(struct ioring_options, force_async), | |
277 | .help = "Set IOSQE_ASYNC every N requests", | |
278 | .category = FIO_OPT_C_ENGINE, | |
279 | .group = FIO_OPT_G_IOURING, | |
280 | }, | |
855dc4d4 AG |
281 | { |
282 | .name = "cmd_type", | |
283 | .lname = "Uring cmd type", | |
284 | .type = FIO_OPT_STR, | |
285 | .off1 = offsetof(struct ioring_options, cmd_type), | |
286 | .help = "Specify uring-cmd type", | |
287 | .def = "nvme", | |
288 | .posval = { | |
289 | { .ival = "nvme", | |
290 | .oval = FIO_URING_CMD_NVME, | |
291 | .help = "Issue nvme-uring-cmd", | |
292 | }, | |
293 | }, | |
294 | .category = FIO_OPT_C_ENGINE, | |
295 | .group = FIO_OPT_G_IOURING, | |
296 | }, | |
52885fa2 JA |
297 | { |
298 | .name = NULL, | |
299 | }, | |
300 | }; | |
301 | ||
bffad86f | 302 | static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit, |
52885fa2 JA |
303 | unsigned int min_complete, unsigned int flags) |
304 | { | |
c377f4f8 JA |
305 | #ifdef FIO_ARCH_HAS_SYSCALL |
306 | return __do_syscall6(__NR_io_uring_enter, ld->ring_fd, to_submit, | |
307 | min_complete, flags, NULL, 0); | |
308 | #else | |
bfed648c | 309 | return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit, |
521164fa | 310 | min_complete, flags, NULL, 0); |
c377f4f8 | 311 | #endif |
52885fa2 JA |
312 | } |
313 | ||
bffad86f | 314 | static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u) |
52885fa2 | 315 | { |
bffad86f | 316 | struct ioring_data *ld = td->io_ops_data; |
cfcc8564 | 317 | struct ioring_options *o = td->eo; |
52885fa2 | 318 | struct fio_file *f = io_u->file; |
f0403f94 | 319 | struct io_uring_sqe *sqe; |
52885fa2 | 320 | |
f0403f94 | 321 | sqe = &ld->sqes[io_u->index]; |
34d6090e | 322 | |
5ffd5626 JA |
323 | if (o->registerfiles) { |
324 | sqe->fd = f->engine_pos; | |
325 | sqe->flags = IOSQE_FIXED_FILE; | |
326 | } else { | |
327 | sqe->fd = f->fd; | |
87b69ef2 | 328 | sqe->flags = 0; |
5ffd5626 | 329 | } |
52885fa2 | 330 | |
e3970057 | 331 | if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) { |
f0403f94 | 332 | if (o->fixedbufs) { |
3f1e3af7 | 333 | sqe->opcode = fixed_ddir_to_op[io_u->ddir]; |
919850d2 | 334 | sqe->addr = (unsigned long) io_u->xfer_buf; |
f0403f94 | 335 | sqe->len = io_u->xfer_buflen; |
2ea53ca3 | 336 | sqe->buf_index = io_u->index; |
cfcc8564 | 337 | } else { |
832faaaf JA |
338 | struct iovec *iov = &ld->iovecs[io_u->index]; |
339 | ||
340 | /* | |
341 | * Update based on actual io_u, requeue could have | |
342 | * adjusted these | |
343 | */ | |
344 | iov->iov_base = io_u->xfer_buf; | |
345 | iov->iov_len = io_u->xfer_buflen; | |
346 | ||
3f1e3af7 | 347 | sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored]; |
b10b1e70 | 348 | if (o->nonvectored) { |
832faaaf JA |
349 | sqe->addr = (unsigned long) iov->iov_base; |
350 | sqe->len = iov->iov_len; | |
b10b1e70 | 351 | } else { |
832faaaf | 352 | sqe->addr = (unsigned long) iov; |
b10b1e70 JA |
353 | sqe->len = 1; |
354 | } | |
cfcc8564 | 355 | } |
fd70e361 | 356 | sqe->rw_flags = 0; |
4a87b584 | 357 | if (!td->o.odirect && o->uncached) |
fd70e361 | 358 | sqe->rw_flags |= RWF_UNCACHED; |
7d42e66e KK |
359 | if (o->nowait) |
360 | sqe->rw_flags |= RWF_NOWAIT; | |
8ff6b289 NC |
361 | |
362 | /* | |
363 | * Since io_uring can have a submission context (sqthread_poll) | |
364 | * that is different from the process context, we cannot rely on | |
365 | * the IO priority set by ioprio_set() (option prio/prioclass) | |
366 | * to be inherited. | |
367 | * td->ioprio will have the value of the "default prio", so set | |
368 | * this unconditionally. This value might get overridden by | |
ff00f247 | 369 | * fio_ioring_cmdprio_prep() if the option cmdprio_percentage or |
8ff6b289 NC |
370 | * cmdprio_bssplit is used. |
371 | */ | |
372 | sqe->ioprio = td->ioprio; | |
f0403f94 | 373 | sqe->off = io_u->offset; |
48e698fa | 374 | } else if (ddir_sync(io_u->ddir)) { |
7c70f506 | 375 | sqe->ioprio = 0; |
01387bfe AF |
376 | if (io_u->ddir == DDIR_SYNC_FILE_RANGE) { |
377 | sqe->off = f->first_write; | |
378 | sqe->len = f->last_write - f->first_write; | |
379 | sqe->sync_range_flags = td->o.sync_file_range; | |
380 | sqe->opcode = IORING_OP_SYNC_FILE_RANGE; | |
381 | } else { | |
7c70f506 JA |
382 | sqe->off = 0; |
383 | sqe->addr = 0; | |
384 | sqe->len = 0; | |
01387bfe AF |
385 | if (io_u->ddir == DDIR_DATASYNC) |
386 | sqe->fsync_flags |= IORING_FSYNC_DATASYNC; | |
387 | sqe->opcode = IORING_OP_FSYNC; | |
388 | } | |
48e698fa | 389 | } |
52885fa2 | 390 | |
5a59a81d JA |
391 | if (o->force_async && ++ld->prepped == o->force_async) { |
392 | ld->prepped = 0; | |
393 | sqe->flags |= IOSQE_ASYNC; | |
394 | } | |
395 | ||
48e698fa | 396 | sqe->user_data = (unsigned long) io_u; |
52885fa2 JA |
397 | return 0; |
398 | } | |
399 | ||
855dc4d4 AG |
400 | static int fio_ioring_cmd_prep(struct thread_data *td, struct io_u *io_u) |
401 | { | |
402 | struct ioring_data *ld = td->io_ops_data; | |
403 | struct ioring_options *o = td->eo; | |
404 | struct fio_file *f = io_u->file; | |
3ce6a3de | 405 | struct nvme_uring_cmd *cmd; |
855dc4d4 | 406 | struct io_uring_sqe *sqe; |
855dc4d4 | 407 | |
3ce6a3de JA |
408 | /* only supports nvme_uring_cmd */ |
409 | if (o->cmd_type != FIO_URING_CMD_NVME) | |
410 | return -EINVAL; | |
855dc4d4 | 411 | |
3ce6a3de | 412 | sqe = &ld->sqes[(io_u->index) << 1]; |
855dc4d4 | 413 | |
3ce6a3de JA |
414 | if (o->registerfiles) { |
415 | sqe->fd = f->engine_pos; | |
416 | sqe->flags = IOSQE_FIXED_FILE; | |
417 | } else { | |
418 | sqe->fd = f->fd; | |
419 | } | |
420 | sqe->rw_flags = 0; | |
421 | if (!td->o.odirect && o->uncached) | |
422 | sqe->rw_flags |= RWF_UNCACHED; | |
423 | if (o->nowait) | |
424 | sqe->rw_flags |= RWF_NOWAIT; | |
855dc4d4 | 425 | |
3ce6a3de JA |
426 | sqe->opcode = IORING_OP_URING_CMD; |
427 | sqe->user_data = (unsigned long) io_u; | |
428 | if (o->nonvectored) | |
429 | sqe->cmd_op = NVME_URING_CMD_IO; | |
430 | else | |
431 | sqe->cmd_op = NVME_URING_CMD_IO_VEC; | |
432 | if (o->force_async && ++ld->prepped == o->force_async) { | |
433 | ld->prepped = 0; | |
434 | sqe->flags |= IOSQE_ASYNC; | |
855dc4d4 | 435 | } |
3ce6a3de JA |
436 | |
437 | cmd = (struct nvme_uring_cmd *)sqe->cmd; | |
438 | return fio_nvme_uring_cmd_prep(cmd, io_u, | |
439 | o->nonvectored ? NULL : &ld->iovecs[io_u->index]); | |
855dc4d4 AG |
440 | } |
441 | ||
bffad86f | 442 | static struct io_u *fio_ioring_event(struct thread_data *td, int event) |
52885fa2 | 443 | { |
bffad86f | 444 | struct ioring_data *ld = td->io_ops_data; |
f0403f94 | 445 | struct io_uring_cqe *cqe; |
52885fa2 | 446 | struct io_u *io_u; |
b87aa01a | 447 | unsigned index; |
1816895b | 448 | static int eio; |
52885fa2 | 449 | |
b87aa01a | 450 | index = (event + ld->cq_ring_off) & ld->cq_ring_mask; |
52885fa2 | 451 | |
f0403f94 | 452 | cqe = &ld->cq_ring.cqes[index]; |
e3466352 | 453 | io_u = (struct io_u *) (uintptr_t) cqe->user_data; |
52885fa2 | 454 | |
1816895b JA |
455 | if (eio++ == 5) { |
456 | printf("mark EIO\n"); | |
457 | cqe->res = -EIO; | |
458 | } | |
459 | ||
f0403f94 JA |
460 | if (cqe->res != io_u->xfer_buflen) { |
461 | if (cqe->res > io_u->xfer_buflen) | |
462 | io_u->error = -cqe->res; | |
52885fa2 | 463 | else |
f0403f94 | 464 | io_u->resid = io_u->xfer_buflen - cqe->res; |
52885fa2 JA |
465 | } else |
466 | io_u->error = 0; | |
467 | ||
468 | return io_u; | |
469 | } | |
470 | ||
855dc4d4 AG |
471 | static struct io_u *fio_ioring_cmd_event(struct thread_data *td, int event) |
472 | { | |
473 | struct ioring_data *ld = td->io_ops_data; | |
474 | struct ioring_options *o = td->eo; | |
475 | struct io_uring_cqe *cqe; | |
476 | struct io_u *io_u; | |
477 | unsigned index; | |
478 | ||
479 | index = (event + ld->cq_ring_off) & ld->cq_ring_mask; | |
480 | if (o->cmd_type == FIO_URING_CMD_NVME) | |
481 | index <<= 1; | |
482 | ||
483 | cqe = &ld->cq_ring.cqes[index]; | |
484 | io_u = (struct io_u *) (uintptr_t) cqe->user_data; | |
485 | ||
486 | if (cqe->res != 0) | |
487 | io_u->error = -cqe->res; | |
488 | else | |
489 | io_u->error = 0; | |
490 | ||
491 | return io_u; | |
492 | } | |
493 | ||
bffad86f | 494 | static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events, |
52885fa2 JA |
495 | unsigned int max) |
496 | { | |
bffad86f JA |
497 | struct ioring_data *ld = td->io_ops_data; |
498 | struct io_cq_ring *ring = &ld->cq_ring; | |
e2239016 | 499 | unsigned head, reaped = 0; |
52885fa2 | 500 | |
9a2d78b3 | 501 | head = *ring->head; |
52885fa2 | 502 | do { |
9e26aff9 | 503 | if (head == atomic_load_acquire(ring->tail)) |
52885fa2 JA |
504 | break; |
505 | reaped++; | |
506 | head++; | |
52885fa2 JA |
507 | } while (reaped + events < max); |
508 | ||
76ce63dd AB |
509 | if (reaped) |
510 | atomic_store_release(ring->head, head); | |
511 | ||
52885fa2 JA |
512 | return reaped; |
513 | } | |
514 | ||
bffad86f JA |
515 | static int fio_ioring_getevents(struct thread_data *td, unsigned int min, |
516 | unsigned int max, const struct timespec *t) | |
52885fa2 | 517 | { |
bffad86f | 518 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 | 519 | unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min; |
bffad86f JA |
520 | struct ioring_options *o = td->eo; |
521 | struct io_cq_ring *ring = &ld->cq_ring; | |
b87aa01a JA |
522 | unsigned events = 0; |
523 | int r; | |
52885fa2 | 524 | |
9a2d78b3 | 525 | ld->cq_ring_off = *ring->head; |
52885fa2 | 526 | do { |
bffad86f | 527 | r = fio_ioring_cqring_reap(td, events, max); |
52885fa2 JA |
528 | if (r) { |
529 | events += r; | |
f7cbbbf8 ST |
530 | if (actual_min != 0) |
531 | actual_min -= r; | |
52885fa2 JA |
532 | continue; |
533 | } | |
534 | ||
3d7d00a3 | 535 | if (!o->sqpoll_thread) { |
9a2d78b3 JA |
536 | r = io_uring_enter(ld, 0, actual_min, |
537 | IORING_ENTER_GETEVENTS); | |
771c9901 | 538 | if (r < 0) { |
f6abd731 | 539 | if (errno == EAGAIN || errno == EINTR) |
771c9901 | 540 | continue; |
1816895b | 541 | r = -errno; |
9a2d78b3 | 542 | td_verror(td, errno, "io_uring_enter"); |
771c9901 JA |
543 | break; |
544 | } | |
52885fa2 JA |
545 | } |
546 | } while (events < min); | |
547 | ||
548 | return r < 0 ? r : events; | |
549 | } | |
550 | ||
127715b6 NC |
551 | static inline void fio_ioring_cmdprio_prep(struct thread_data *td, |
552 | struct io_u *io_u) | |
b2a432bf | 553 | { |
b2a432bf | 554 | struct ioring_data *ld = td->io_ops_data; |
d6cbeab4 | 555 | struct cmdprio *cmdprio = &ld->cmdprio; |
127715b6 NC |
556 | |
557 | if (fio_cmdprio_set_ioprio(td, cmdprio, io_u)) | |
558 | ld->sqes[io_u->index].ioprio = io_u->ioprio; | |
b2a432bf PC |
559 | } |
560 | ||
bffad86f JA |
561 | static enum fio_q_status fio_ioring_queue(struct thread_data *td, |
562 | struct io_u *io_u) | |
52885fa2 | 563 | { |
bffad86f JA |
564 | struct ioring_data *ld = td->io_ops_data; |
565 | struct io_sq_ring *ring = &ld->sq_ring; | |
52885fa2 JA |
566 | unsigned tail, next_tail; |
567 | ||
568 | fio_ro_check(td, io_u); | |
569 | ||
b87aa01a | 570 | if (ld->queued == ld->iodepth) |
52885fa2 JA |
571 | return FIO_Q_BUSY; |
572 | ||
52885fa2 JA |
573 | if (io_u->ddir == DDIR_TRIM) { |
574 | if (ld->queued) | |
575 | return FIO_Q_BUSY; | |
576 | ||
577 | do_io_u_trim(td, io_u); | |
578 | io_u_mark_submit(td, 1); | |
579 | io_u_mark_complete(td, 1); | |
580 | return FIO_Q_COMPLETED; | |
581 | } | |
582 | ||
9a2d78b3 | 583 | tail = *ring->tail; |
52885fa2 | 584 | next_tail = tail + 1; |
9e26aff9 | 585 | if (next_tail == atomic_load_acquire(ring->head)) |
52885fa2 JA |
586 | return FIO_Q_BUSY; |
587 | ||
d6cbeab4 | 588 | if (ld->cmdprio.mode != CMDPRIO_MODE_NONE) |
ff00f247 NC |
589 | fio_ioring_cmdprio_prep(td, io_u); |
590 | ||
b87aa01a | 591 | ring->array[tail & ld->sq_ring_mask] = io_u->index; |
9e26aff9 | 592 | atomic_store_release(ring->tail, next_tail); |
52885fa2 JA |
593 | |
594 | ld->queued++; | |
595 | return FIO_Q_QUEUED; | |
596 | } | |
597 | ||
bffad86f | 598 | static void fio_ioring_queued(struct thread_data *td, int start, int nr) |
52885fa2 | 599 | { |
bffad86f | 600 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 JA |
601 | struct timespec now; |
602 | ||
603 | if (!fio_fill_issue_time(td)) | |
604 | return; | |
605 | ||
606 | fio_gettime(&now, NULL); | |
607 | ||
608 | while (nr--) { | |
bffad86f | 609 | struct io_sq_ring *ring = &ld->sq_ring; |
9a2d78b3 | 610 | int index = ring->array[start & ld->sq_ring_mask]; |
f8289afc | 611 | struct io_u *io_u = ld->io_u_index[index]; |
52885fa2 JA |
612 | |
613 | memcpy(&io_u->issue_time, &now, sizeof(now)); | |
614 | io_u_queued(td, io_u); | |
615 | ||
616 | start++; | |
52885fa2 | 617 | } |
39f56400 VF |
618 | |
619 | /* | |
620 | * only used for iolog | |
621 | */ | |
622 | if (td->o.read_iolog_file) | |
623 | memcpy(&td->last_issue, &now, sizeof(now)); | |
52885fa2 JA |
624 | } |
625 | ||
bffad86f | 626 | static int fio_ioring_commit(struct thread_data *td) |
52885fa2 | 627 | { |
bffad86f JA |
628 | struct ioring_data *ld = td->io_ops_data; |
629 | struct ioring_options *o = td->eo; | |
52885fa2 JA |
630 | int ret; |
631 | ||
632 | if (!ld->queued) | |
633 | return 0; | |
634 | ||
3d7d00a3 JA |
635 | /* |
636 | * Kernel side does submission. just need to check if the ring is | |
637 | * flagged as needing a kick, if so, call io_uring_enter(). This | |
638 | * only happens if we've been idle too long. | |
639 | */ | |
640 | if (o->sqpoll_thread) { | |
bffad86f | 641 | struct io_sq_ring *ring = &ld->sq_ring; |
2dd96cc4 | 642 | unsigned flags; |
4cdbc048 | 643 | |
2dd96cc4 JA |
644 | flags = atomic_load_acquire(ring->flags); |
645 | if (flags & IORING_SQ_NEED_WAKEUP) | |
b532dd6d JA |
646 | io_uring_enter(ld, ld->queued, 0, |
647 | IORING_ENTER_SQ_WAKEUP); | |
771c9901 JA |
648 | ld->queued = 0; |
649 | return 0; | |
650 | } | |
651 | ||
52885fa2 | 652 | do { |
9a2d78b3 | 653 | unsigned start = *ld->sq_ring.head; |
52885fa2 JA |
654 | long nr = ld->queued; |
655 | ||
9a2d78b3 | 656 | ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS); |
52885fa2 | 657 | if (ret > 0) { |
bffad86f | 658 | fio_ioring_queued(td, start, ret); |
52885fa2 JA |
659 | io_u_mark_submit(td, ret); |
660 | ||
661 | ld->queued -= ret; | |
662 | ret = 0; | |
a90cd050 JA |
663 | } else if (!ret) { |
664 | io_u_mark_submit(td, ret); | |
52885fa2 | 665 | continue; |
a90cd050 | 666 | } else { |
f6abd731 | 667 | if (errno == EAGAIN || errno == EINTR) { |
bffad86f | 668 | ret = fio_ioring_cqring_reap(td, 0, ld->queued); |
a90cd050 JA |
669 | if (ret) |
670 | continue; | |
671 | /* Shouldn't happen */ | |
672 | usleep(1); | |
673 | continue; | |
52885fa2 | 674 | } |
1816895b | 675 | ret = -errno; |
9a2d78b3 | 676 | td_verror(td, errno, "io_uring_enter submit"); |
52885fa2 | 677 | break; |
a90cd050 | 678 | } |
52885fa2 JA |
679 | } while (ld->queued); |
680 | ||
681 | return ret; | |
682 | } | |
683 | ||
bffad86f | 684 | static void fio_ioring_unmap(struct ioring_data *ld) |
52885fa2 | 685 | { |
9a2d78b3 | 686 | int i; |
52885fa2 | 687 | |
59f94d26 | 688 | for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++) |
9a2d78b3 JA |
689 | munmap(ld->mmap[i].ptr, ld->mmap[i].len); |
690 | close(ld->ring_fd); | |
b87aa01a JA |
691 | } |
692 | ||
bffad86f | 693 | static void fio_ioring_cleanup(struct thread_data *td) |
52885fa2 | 694 | { |
bffad86f | 695 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 JA |
696 | |
697 | if (ld) { | |
52885fa2 | 698 | if (!(td->flags & TD_F_CHILD)) |
bffad86f | 699 | fio_ioring_unmap(ld); |
9a2d78b3 | 700 | |
d6cbeab4 | 701 | fio_cmdprio_cleanup(&ld->cmdprio); |
52885fa2 | 702 | free(ld->io_u_index); |
9a2d78b3 | 703 | free(ld->iovecs); |
5ffd5626 | 704 | free(ld->fds); |
52885fa2 JA |
705 | free(ld); |
706 | } | |
707 | } | |
708 | ||
bffad86f | 709 | static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p) |
9a2d78b3 | 710 | { |
bffad86f JA |
711 | struct io_sq_ring *sring = &ld->sq_ring; |
712 | struct io_cq_ring *cring = &ld->cq_ring; | |
9a2d78b3 JA |
713 | void *ptr; |
714 | ||
e2239016 | 715 | ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32); |
9a2d78b3 JA |
716 | ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE, |
717 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, | |
718 | IORING_OFF_SQ_RING); | |
719 | ld->mmap[0].ptr = ptr; | |
720 | sring->head = ptr + p->sq_off.head; | |
721 | sring->tail = ptr + p->sq_off.tail; | |
722 | sring->ring_mask = ptr + p->sq_off.ring_mask; | |
723 | sring->ring_entries = ptr + p->sq_off.ring_entries; | |
724 | sring->flags = ptr + p->sq_off.flags; | |
ac122fea | 725 | sring->array = ptr + p->sq_off.array; |
9a2d78b3 JA |
726 | ld->sq_ring_mask = *sring->ring_mask; |
727 | ||
855dc4d4 AG |
728 | if (p->flags & IORING_SETUP_SQE128) |
729 | ld->mmap[1].len = 2 * p->sq_entries * sizeof(struct io_uring_sqe); | |
730 | else | |
731 | ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe); | |
f0403f94 | 732 | ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE, |
9a2d78b3 | 733 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, |
f0403f94 JA |
734 | IORING_OFF_SQES); |
735 | ld->mmap[1].ptr = ld->sqes; | |
9a2d78b3 | 736 | |
855dc4d4 AG |
737 | if (p->flags & IORING_SETUP_CQE32) { |
738 | ld->mmap[2].len = p->cq_off.cqes + | |
739 | 2 * p->cq_entries * sizeof(struct io_uring_cqe); | |
740 | } else { | |
741 | ld->mmap[2].len = p->cq_off.cqes + | |
742 | p->cq_entries * sizeof(struct io_uring_cqe); | |
743 | } | |
9a2d78b3 JA |
744 | ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE, |
745 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, | |
746 | IORING_OFF_CQ_RING); | |
747 | ld->mmap[2].ptr = ptr; | |
748 | cring->head = ptr + p->cq_off.head; | |
749 | cring->tail = ptr + p->cq_off.tail; | |
750 | cring->ring_mask = ptr + p->cq_off.ring_mask; | |
751 | cring->ring_entries = ptr + p->cq_off.ring_entries; | |
f0403f94 | 752 | cring->cqes = ptr + p->cq_off.cqes; |
9a2d78b3 JA |
753 | ld->cq_ring_mask = *cring->ring_mask; |
754 | return 0; | |
755 | } | |
756 | ||
556d8415 JA |
757 | static void fio_ioring_probe(struct thread_data *td) |
758 | { | |
759 | struct ioring_data *ld = td->io_ops_data; | |
760 | struct ioring_options *o = td->eo; | |
761 | struct io_uring_probe *p; | |
762 | int ret; | |
763 | ||
764 | /* already set by user, don't touch */ | |
765 | if (o->nonvectored != -1) | |
766 | return; | |
767 | ||
768 | /* default to off, as that's always safe */ | |
769 | o->nonvectored = 0; | |
770 | ||
771 | p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op)); | |
772 | if (!p) | |
773 | return; | |
774 | ||
775 | memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op)); | |
776 | ret = syscall(__NR_io_uring_register, ld->ring_fd, | |
777 | IORING_REGISTER_PROBE, p, 256); | |
778 | if (ret < 0) | |
779 | goto out; | |
780 | ||
781 | if (IORING_OP_WRITE > p->ops_len) | |
782 | goto out; | |
783 | ||
784 | if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) && | |
785 | (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED)) | |
786 | o->nonvectored = 1; | |
787 | out: | |
788 | free(p); | |
789 | } | |
790 | ||
bffad86f | 791 | static int fio_ioring_queue_init(struct thread_data *td) |
52885fa2 | 792 | { |
bffad86f JA |
793 | struct ioring_data *ld = td->io_ops_data; |
794 | struct ioring_options *o = td->eo; | |
52885fa2 | 795 | int depth = td->o.iodepth; |
bffad86f | 796 | struct io_uring_params p; |
9a2d78b3 JA |
797 | int ret; |
798 | ||
799 | memset(&p, 0, sizeof(p)); | |
52885fa2 JA |
800 | |
801 | if (o->hipri) | |
bffad86f | 802 | p.flags |= IORING_SETUP_IOPOLL; |
3d7d00a3 JA |
803 | if (o->sqpoll_thread) { |
804 | p.flags |= IORING_SETUP_SQPOLL; | |
805 | if (o->sqpoll_set) { | |
806 | p.flags |= IORING_SETUP_SQ_AFF; | |
807 | p.sq_thread_cpu = o->sqpoll_cpu; | |
808 | } | |
f635f1fb | 809 | } |
a90cd050 | 810 | |
1db268db JA |
811 | /* |
812 | * Clamp CQ ring size at our SQ ring size, we don't need more entries | |
813 | * than that. | |
814 | */ | |
815 | p.flags |= IORING_SETUP_CQSIZE; | |
816 | p.cq_entries = depth; | |
817 | ||
b5e99df6 | 818 | retry: |
bfed648c | 819 | ret = syscall(__NR_io_uring_setup, depth, &p); |
b5e99df6 JA |
820 | if (ret < 0) { |
821 | if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) { | |
822 | p.flags &= ~IORING_SETUP_CQSIZE; | |
823 | goto retry; | |
824 | } | |
9a2d78b3 | 825 | return ret; |
b5e99df6 | 826 | } |
9a2d78b3 JA |
827 | |
828 | ld->ring_fd = ret; | |
2ea53ca3 | 829 | |
556d8415 JA |
830 | fio_ioring_probe(td); |
831 | ||
2ea53ca3 | 832 | if (o->fixedbufs) { |
bfed648c | 833 | ret = syscall(__NR_io_uring_register, ld->ring_fd, |
919850d2 | 834 | IORING_REGISTER_BUFFERS, ld->iovecs, depth); |
2ea53ca3 JA |
835 | if (ret < 0) |
836 | return ret; | |
837 | } | |
838 | ||
bffad86f | 839 | return fio_ioring_mmap(ld, &p); |
52885fa2 JA |
840 | } |
841 | ||
855dc4d4 AG |
842 | static int fio_ioring_cmd_queue_init(struct thread_data *td) |
843 | { | |
844 | struct ioring_data *ld = td->io_ops_data; | |
845 | struct ioring_options *o = td->eo; | |
846 | int depth = td->o.iodepth; | |
847 | struct io_uring_params p; | |
848 | int ret; | |
849 | ||
850 | memset(&p, 0, sizeof(p)); | |
851 | ||
852 | if (o->hipri) | |
853 | p.flags |= IORING_SETUP_IOPOLL; | |
854 | if (o->sqpoll_thread) { | |
855 | p.flags |= IORING_SETUP_SQPOLL; | |
856 | if (o->sqpoll_set) { | |
857 | p.flags |= IORING_SETUP_SQ_AFF; | |
858 | p.sq_thread_cpu = o->sqpoll_cpu; | |
859 | } | |
860 | } | |
861 | if (o->cmd_type == FIO_URING_CMD_NVME) { | |
862 | p.flags |= IORING_SETUP_SQE128; | |
863 | p.flags |= IORING_SETUP_CQE32; | |
864 | } | |
865 | ||
866 | /* | |
867 | * Clamp CQ ring size at our SQ ring size, we don't need more entries | |
868 | * than that. | |
869 | */ | |
870 | p.flags |= IORING_SETUP_CQSIZE; | |
871 | p.cq_entries = depth; | |
872 | ||
873 | retry: | |
874 | ret = syscall(__NR_io_uring_setup, depth, &p); | |
875 | if (ret < 0) { | |
876 | if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) { | |
877 | p.flags &= ~IORING_SETUP_CQSIZE; | |
878 | goto retry; | |
879 | } | |
880 | return ret; | |
881 | } | |
882 | ||
883 | ld->ring_fd = ret; | |
884 | ||
885 | fio_ioring_probe(td); | |
886 | ||
887 | if (o->fixedbufs) { | |
888 | ret = syscall(__NR_io_uring_register, ld->ring_fd, | |
889 | IORING_REGISTER_BUFFERS, ld->iovecs, depth); | |
890 | if (ret < 0) | |
891 | return ret; | |
892 | } | |
893 | ||
894 | return fio_ioring_mmap(ld, &p); | |
895 | } | |
896 | ||
5ffd5626 JA |
897 | static int fio_ioring_register_files(struct thread_data *td) |
898 | { | |
899 | struct ioring_data *ld = td->io_ops_data; | |
900 | struct fio_file *f; | |
901 | unsigned int i; | |
902 | int ret; | |
903 | ||
904 | ld->fds = calloc(td->o.nr_files, sizeof(int)); | |
905 | ||
906 | for_each_file(td, f, i) { | |
907 | ret = generic_open_file(td, f); | |
908 | if (ret) | |
909 | goto err; | |
910 | ld->fds[i] = f->fd; | |
911 | f->engine_pos = i; | |
912 | } | |
913 | ||
bfed648c | 914 | ret = syscall(__NR_io_uring_register, ld->ring_fd, |
5ffd5626 JA |
915 | IORING_REGISTER_FILES, ld->fds, td->o.nr_files); |
916 | if (ret) { | |
917 | err: | |
918 | free(ld->fds); | |
919 | ld->fds = NULL; | |
920 | } | |
921 | ||
922 | /* | |
923 | * Pretend the file is closed again, and really close it if we hit | |
924 | * an error. | |
925 | */ | |
926 | for_each_file(td, f, i) { | |
927 | if (ret) { | |
928 | int fio_unused ret2; | |
929 | ret2 = generic_close_file(td, f); | |
930 | } else | |
931 | f->fd = -1; | |
932 | } | |
933 | ||
934 | return ret; | |
935 | } | |
936 | ||
bffad86f | 937 | static int fio_ioring_post_init(struct thread_data *td) |
52885fa2 | 938 | { |
bffad86f | 939 | struct ioring_data *ld = td->io_ops_data; |
5ffd5626 | 940 | struct ioring_options *o = td->eo; |
52885fa2 | 941 | struct io_u *io_u; |
650346e1 | 942 | int err, i; |
52885fa2 | 943 | |
650346e1 JA |
944 | for (i = 0; i < td->o.iodepth; i++) { |
945 | struct iovec *iov = &ld->iovecs[i]; | |
9a2d78b3 | 946 | |
650346e1 JA |
947 | io_u = ld->io_u_index[i]; |
948 | iov->iov_base = io_u->buf; | |
949 | iov->iov_len = td_max_bs(td); | |
52885fa2 JA |
950 | } |
951 | ||
bffad86f | 952 | err = fio_ioring_queue_init(td); |
52885fa2 | 953 | if (err) { |
0442b53f | 954 | int init_err = errno; |
c4f5c92f | 955 | |
0442b53f | 956 | if (init_err == ENOSYS) |
c4f5c92f | 957 | log_err("fio: your kernel doesn't support io_uring\n"); |
0442b53f | 958 | td_verror(td, init_err, "io_queue_init"); |
52885fa2 JA |
959 | return 1; |
960 | } | |
961 | ||
7c70f506 JA |
962 | for (i = 0; i < td->o.iodepth; i++) { |
963 | struct io_uring_sqe *sqe; | |
964 | ||
965 | sqe = &ld->sqes[i]; | |
966 | memset(sqe, 0, sizeof(*sqe)); | |
967 | } | |
968 | ||
5ffd5626 JA |
969 | if (o->registerfiles) { |
970 | err = fio_ioring_register_files(td); | |
971 | if (err) { | |
972 | td_verror(td, errno, "ioring_register_files"); | |
973 | return 1; | |
974 | } | |
975 | } | |
976 | ||
52885fa2 JA |
977 | return 0; |
978 | } | |
979 | ||
855dc4d4 AG |
980 | static int fio_ioring_cmd_post_init(struct thread_data *td) |
981 | { | |
982 | struct ioring_data *ld = td->io_ops_data; | |
983 | struct ioring_options *o = td->eo; | |
984 | struct io_u *io_u; | |
985 | int err, i; | |
986 | ||
987 | for (i = 0; i < td->o.iodepth; i++) { | |
988 | struct iovec *iov = &ld->iovecs[i]; | |
989 | ||
990 | io_u = ld->io_u_index[i]; | |
991 | iov->iov_base = io_u->buf; | |
992 | iov->iov_len = td_max_bs(td); | |
993 | } | |
994 | ||
995 | err = fio_ioring_cmd_queue_init(td); | |
996 | if (err) { | |
997 | int init_err = errno; | |
998 | ||
999 | td_verror(td, init_err, "io_queue_init"); | |
1000 | return 1; | |
1001 | } | |
1002 | ||
1003 | for (i = 0; i < td->o.iodepth; i++) { | |
1004 | struct io_uring_sqe *sqe; | |
1005 | ||
1006 | if (o->cmd_type == FIO_URING_CMD_NVME) { | |
1007 | sqe = &ld->sqes[i << 1]; | |
1008 | memset(sqe, 0, 2 * sizeof(*sqe)); | |
1009 | } else { | |
1010 | sqe = &ld->sqes[i]; | |
1011 | memset(sqe, 0, sizeof(*sqe)); | |
1012 | } | |
1013 | } | |
1014 | ||
1015 | if (o->registerfiles) { | |
1016 | err = fio_ioring_register_files(td); | |
1017 | if (err) { | |
1018 | td_verror(td, errno, "ioring_register_files"); | |
1019 | return 1; | |
1020 | } | |
1021 | } | |
1022 | ||
1023 | return 0; | |
1024 | } | |
1025 | ||
bffad86f | 1026 | static int fio_ioring_init(struct thread_data *td) |
52885fa2 | 1027 | { |
5ffd5626 | 1028 | struct ioring_options *o = td->eo; |
bffad86f | 1029 | struct ioring_data *ld; |
e9f6567a | 1030 | int ret; |
52885fa2 | 1031 | |
5ffd5626 JA |
1032 | /* sqthread submission requires registered files */ |
1033 | if (o->sqpoll_thread) | |
1034 | o->registerfiles = 1; | |
1035 | ||
1036 | if (o->registerfiles && td->o.nr_files != td->o.open_files) { | |
1037 | log_err("fio: io_uring registered files require nr_files to " | |
1038 | "be identical to open_files\n"); | |
1039 | return 1; | |
1040 | } | |
1041 | ||
52885fa2 JA |
1042 | ld = calloc(1, sizeof(*ld)); |
1043 | ||
b87aa01a JA |
1044 | /* ring depth must be a power-of-2 */ |
1045 | ld->iodepth = td->o.iodepth; | |
1046 | td->o.iodepth = roundup_pow2(td->o.iodepth); | |
1047 | ||
52885fa2 JA |
1048 | /* io_u index */ |
1049 | ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *)); | |
650346e1 | 1050 | ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec)); |
52885fa2 JA |
1051 | |
1052 | td->io_ops_data = ld; | |
b2a432bf | 1053 | |
d6cbeab4 | 1054 | ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options); |
e9f6567a DLM |
1055 | if (ret) { |
1056 | td_verror(td, EINVAL, "fio_ioring_init"); | |
b2a432bf PC |
1057 | return 1; |
1058 | } | |
1af44196 | 1059 | |
52885fa2 JA |
1060 | return 0; |
1061 | } | |
1062 | ||
bffad86f | 1063 | static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u) |
52885fa2 | 1064 | { |
bffad86f | 1065 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 JA |
1066 | |
1067 | ld->io_u_index[io_u->index] = io_u; | |
1068 | return 0; | |
1069 | } | |
1070 | ||
5ffd5626 JA |
1071 | static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f) |
1072 | { | |
1073 | struct ioring_data *ld = td->io_ops_data; | |
1074 | struct ioring_options *o = td->eo; | |
1075 | ||
17318cf6 | 1076 | if (!ld || !o->registerfiles) |
5ffd5626 JA |
1077 | return generic_open_file(td, f); |
1078 | ||
1079 | f->fd = ld->fds[f->engine_pos]; | |
1080 | return 0; | |
1081 | } | |
1082 | ||
855dc4d4 AG |
1083 | static int fio_ioring_cmd_open_file(struct thread_data *td, struct fio_file *f) |
1084 | { | |
1085 | struct ioring_data *ld = td->io_ops_data; | |
1086 | struct ioring_options *o = td->eo; | |
1087 | ||
1088 | if (o->cmd_type == FIO_URING_CMD_NVME) { | |
1089 | struct nvme_data *data = NULL; | |
1090 | unsigned int nsid, lba_size = 0; | |
1091 | unsigned long long nlba = 0; | |
1092 | int ret; | |
1093 | ||
1094 | /* Store the namespace-id and lba size. */ | |
1095 | data = FILE_ENG_DATA(f); | |
1096 | if (data == NULL) { | |
1097 | ret = fio_nvme_get_info(f, &nsid, &lba_size, &nlba); | |
1098 | if (ret) | |
1099 | return ret; | |
1100 | ||
1101 | data = calloc(1, sizeof(struct nvme_data)); | |
1102 | data->nsid = nsid; | |
1103 | data->lba_shift = ilog2(lba_size); | |
1104 | ||
1105 | FILE_SET_ENG_DATA(f, data); | |
1106 | } | |
1107 | } | |
1108 | if (!ld || !o->registerfiles) | |
1109 | return generic_open_file(td, f); | |
1110 | ||
1111 | f->fd = ld->fds[f->engine_pos]; | |
1112 | return 0; | |
1113 | } | |
1114 | ||
5ffd5626 JA |
1115 | static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f) |
1116 | { | |
17318cf6 | 1117 | struct ioring_data *ld = td->io_ops_data; |
5ffd5626 JA |
1118 | struct ioring_options *o = td->eo; |
1119 | ||
17318cf6 | 1120 | if (!ld || !o->registerfiles) |
5ffd5626 JA |
1121 | return generic_close_file(td, f); |
1122 | ||
1123 | f->fd = -1; | |
1124 | return 0; | |
1125 | } | |
1126 | ||
855dc4d4 AG |
1127 | static int fio_ioring_cmd_close_file(struct thread_data *td, |
1128 | struct fio_file *f) | |
1129 | { | |
1130 | struct ioring_data *ld = td->io_ops_data; | |
1131 | struct ioring_options *o = td->eo; | |
1132 | ||
1133 | if (o->cmd_type == FIO_URING_CMD_NVME) { | |
1134 | struct nvme_data *data = FILE_ENG_DATA(f); | |
1135 | ||
1136 | FILE_SET_ENG_DATA(f, NULL); | |
1137 | free(data); | |
1138 | } | |
1139 | if (!ld || !o->registerfiles) | |
1140 | return generic_close_file(td, f); | |
1141 | ||
1142 | f->fd = -1; | |
1143 | return 0; | |
1144 | } | |
1145 | ||
1146 | static int fio_ioring_cmd_get_file_size(struct thread_data *td, | |
1147 | struct fio_file *f) | |
1148 | { | |
1149 | struct ioring_options *o = td->eo; | |
1150 | ||
1151 | if (fio_file_size_known(f)) | |
1152 | return 0; | |
1153 | ||
1154 | if (o->cmd_type == FIO_URING_CMD_NVME) { | |
1155 | struct nvme_data *data = NULL; | |
1156 | unsigned int nsid, lba_size = 0; | |
1157 | unsigned long long nlba = 0; | |
1158 | int ret; | |
1159 | ||
1160 | ret = fio_nvme_get_info(f, &nsid, &lba_size, &nlba); | |
1161 | if (ret) | |
1162 | return ret; | |
1163 | ||
1164 | data = calloc(1, sizeof(struct nvme_data)); | |
1165 | data->nsid = nsid; | |
1166 | data->lba_shift = ilog2(lba_size); | |
1167 | ||
1168 | f->real_file_size = lba_size * nlba; | |
1169 | fio_file_set_size_known(f); | |
1170 | ||
1171 | FILE_SET_ENG_DATA(f, data); | |
1172 | return 0; | |
1173 | } | |
1174 | return generic_get_file_size(td, f); | |
1175 | } | |
1176 | ||
3d05e0ff AK |
1177 | static int fio_ioring_cmd_get_zoned_model(struct thread_data *td, |
1178 | struct fio_file *f, | |
1179 | enum zbd_zoned_model *model) | |
1180 | { | |
1181 | return fio_nvme_get_zoned_model(td, f, model); | |
1182 | } | |
1183 | ||
1184 | static int fio_ioring_cmd_report_zones(struct thread_data *td, | |
1185 | struct fio_file *f, uint64_t offset, | |
1186 | struct zbd_zone *zbdz, | |
1187 | unsigned int nr_zones) | |
1188 | { | |
1189 | return fio_nvme_report_zones(td, f, offset, zbdz, nr_zones); | |
1190 | } | |
1191 | ||
1192 | static int fio_ioring_cmd_reset_wp(struct thread_data *td, struct fio_file *f, | |
1193 | uint64_t offset, uint64_t length) | |
1194 | { | |
1195 | return fio_nvme_reset_wp(td, f, offset, length); | |
1196 | } | |
1197 | ||
1198 | static int fio_ioring_cmd_get_max_open_zones(struct thread_data *td, | |
1199 | struct fio_file *f, | |
1200 | unsigned int *max_open_zones) | |
1201 | { | |
1202 | return fio_nvme_get_max_open_zones(td, f, max_open_zones); | |
1203 | } | |
1204 | ||
855dc4d4 | 1205 | static struct ioengine_ops ioengine_uring = { |
bffad86f | 1206 | .name = "io_uring", |
52885fa2 | 1207 | .version = FIO_IOOPS_VERSION, |
4e7e7898 VF |
1208 | .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD | |
1209 | FIO_ASYNCIO_SETS_ISSUE_TIME, | |
bffad86f JA |
1210 | .init = fio_ioring_init, |
1211 | .post_init = fio_ioring_post_init, | |
1212 | .io_u_init = fio_ioring_io_u_init, | |
1213 | .prep = fio_ioring_prep, | |
1214 | .queue = fio_ioring_queue, | |
1215 | .commit = fio_ioring_commit, | |
1216 | .getevents = fio_ioring_getevents, | |
1217 | .event = fio_ioring_event, | |
1218 | .cleanup = fio_ioring_cleanup, | |
5ffd5626 JA |
1219 | .open_file = fio_ioring_open_file, |
1220 | .close_file = fio_ioring_close_file, | |
52885fa2 JA |
1221 | .get_file_size = generic_get_file_size, |
1222 | .options = options, | |
bffad86f | 1223 | .option_struct_size = sizeof(struct ioring_options), |
52885fa2 JA |
1224 | }; |
1225 | ||
855dc4d4 AG |
1226 | static struct ioengine_ops ioengine_uring_cmd = { |
1227 | .name = "io_uring_cmd", | |
1228 | .version = FIO_IOOPS_VERSION, | |
4e7e7898 VF |
1229 | .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD | |
1230 | FIO_MEMALIGN | FIO_RAWIO | | |
1231 | FIO_ASYNCIO_SETS_ISSUE_TIME, | |
855dc4d4 AG |
1232 | .init = fio_ioring_init, |
1233 | .post_init = fio_ioring_cmd_post_init, | |
1234 | .io_u_init = fio_ioring_io_u_init, | |
1235 | .prep = fio_ioring_cmd_prep, | |
1236 | .queue = fio_ioring_queue, | |
1237 | .commit = fio_ioring_commit, | |
1238 | .getevents = fio_ioring_getevents, | |
1239 | .event = fio_ioring_cmd_event, | |
1240 | .cleanup = fio_ioring_cleanup, | |
1241 | .open_file = fio_ioring_cmd_open_file, | |
1242 | .close_file = fio_ioring_cmd_close_file, | |
1243 | .get_file_size = fio_ioring_cmd_get_file_size, | |
3d05e0ff AK |
1244 | .get_zoned_model = fio_ioring_cmd_get_zoned_model, |
1245 | .report_zones = fio_ioring_cmd_report_zones, | |
1246 | .reset_wp = fio_ioring_cmd_reset_wp, | |
1247 | .get_max_open_zones = fio_ioring_cmd_get_max_open_zones, | |
855dc4d4 AG |
1248 | .options = options, |
1249 | .option_struct_size = sizeof(struct ioring_options), | |
1250 | }; | |
1251 | ||
bffad86f | 1252 | static void fio_init fio_ioring_register(void) |
52885fa2 | 1253 | { |
855dc4d4 AG |
1254 | register_ioengine(&ioengine_uring); |
1255 | register_ioengine(&ioengine_uring_cmd); | |
52885fa2 JA |
1256 | } |
1257 | ||
bffad86f | 1258 | static void fio_exit fio_ioring_unregister(void) |
52885fa2 | 1259 | { |
855dc4d4 AG |
1260 | unregister_ioengine(&ioengine_uring); |
1261 | unregister_ioengine(&ioengine_uring_cmd); | |
52885fa2 | 1262 | } |
1f90e9bb | 1263 | #endif |