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 | 228 | .lname = "Kernel SQ thread polling", |
d6f936d1 | 229 | .type = FIO_OPT_STR_SET, |
3d7d00a3 JA |
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 | } |
0ebd3bf6 AG |
436 | if (o->fixedbufs) { |
437 | sqe->uring_cmd_flags = IORING_URING_CMD_FIXED; | |
438 | sqe->buf_index = io_u->index; | |
439 | } | |
3ce6a3de JA |
440 | |
441 | cmd = (struct nvme_uring_cmd *)sqe->cmd; | |
442 | return fio_nvme_uring_cmd_prep(cmd, io_u, | |
443 | o->nonvectored ? NULL : &ld->iovecs[io_u->index]); | |
855dc4d4 AG |
444 | } |
445 | ||
bffad86f | 446 | static struct io_u *fio_ioring_event(struct thread_data *td, int event) |
52885fa2 | 447 | { |
bffad86f | 448 | struct ioring_data *ld = td->io_ops_data; |
f0403f94 | 449 | struct io_uring_cqe *cqe; |
52885fa2 | 450 | struct io_u *io_u; |
b87aa01a | 451 | unsigned index; |
52885fa2 | 452 | |
b87aa01a | 453 | index = (event + ld->cq_ring_off) & ld->cq_ring_mask; |
52885fa2 | 454 | |
f0403f94 | 455 | cqe = &ld->cq_ring.cqes[index]; |
e3466352 | 456 | io_u = (struct io_u *) (uintptr_t) cqe->user_data; |
52885fa2 | 457 | |
f0403f94 JA |
458 | if (cqe->res != io_u->xfer_buflen) { |
459 | if (cqe->res > io_u->xfer_buflen) | |
460 | io_u->error = -cqe->res; | |
52885fa2 | 461 | else |
f0403f94 | 462 | io_u->resid = io_u->xfer_buflen - cqe->res; |
52885fa2 JA |
463 | } else |
464 | io_u->error = 0; | |
465 | ||
466 | return io_u; | |
467 | } | |
468 | ||
855dc4d4 AG |
469 | static struct io_u *fio_ioring_cmd_event(struct thread_data *td, int event) |
470 | { | |
471 | struct ioring_data *ld = td->io_ops_data; | |
472 | struct ioring_options *o = td->eo; | |
473 | struct io_uring_cqe *cqe; | |
474 | struct io_u *io_u; | |
475 | unsigned index; | |
476 | ||
477 | index = (event + ld->cq_ring_off) & ld->cq_ring_mask; | |
478 | if (o->cmd_type == FIO_URING_CMD_NVME) | |
479 | index <<= 1; | |
480 | ||
481 | cqe = &ld->cq_ring.cqes[index]; | |
482 | io_u = (struct io_u *) (uintptr_t) cqe->user_data; | |
483 | ||
484 | if (cqe->res != 0) | |
485 | io_u->error = -cqe->res; | |
486 | else | |
487 | io_u->error = 0; | |
488 | ||
489 | return io_u; | |
490 | } | |
491 | ||
bffad86f | 492 | static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events, |
52885fa2 JA |
493 | unsigned int max) |
494 | { | |
bffad86f JA |
495 | struct ioring_data *ld = td->io_ops_data; |
496 | struct io_cq_ring *ring = &ld->cq_ring; | |
e2239016 | 497 | unsigned head, reaped = 0; |
52885fa2 | 498 | |
9a2d78b3 | 499 | head = *ring->head; |
52885fa2 | 500 | do { |
9e26aff9 | 501 | if (head == atomic_load_acquire(ring->tail)) |
52885fa2 JA |
502 | break; |
503 | reaped++; | |
504 | head++; | |
52885fa2 JA |
505 | } while (reaped + events < max); |
506 | ||
76ce63dd AB |
507 | if (reaped) |
508 | atomic_store_release(ring->head, head); | |
509 | ||
52885fa2 JA |
510 | return reaped; |
511 | } | |
512 | ||
bffad86f JA |
513 | static int fio_ioring_getevents(struct thread_data *td, unsigned int min, |
514 | unsigned int max, const struct timespec *t) | |
52885fa2 | 515 | { |
bffad86f | 516 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 | 517 | unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min; |
bffad86f JA |
518 | struct ioring_options *o = td->eo; |
519 | struct io_cq_ring *ring = &ld->cq_ring; | |
b87aa01a JA |
520 | unsigned events = 0; |
521 | int r; | |
52885fa2 | 522 | |
9a2d78b3 | 523 | ld->cq_ring_off = *ring->head; |
52885fa2 | 524 | do { |
bffad86f | 525 | r = fio_ioring_cqring_reap(td, events, max); |
52885fa2 JA |
526 | if (r) { |
527 | events += r; | |
f7cbbbf8 ST |
528 | if (actual_min != 0) |
529 | actual_min -= r; | |
52885fa2 JA |
530 | continue; |
531 | } | |
532 | ||
3d7d00a3 | 533 | if (!o->sqpoll_thread) { |
9a2d78b3 JA |
534 | r = io_uring_enter(ld, 0, actual_min, |
535 | IORING_ENTER_GETEVENTS); | |
771c9901 | 536 | if (r < 0) { |
f6abd731 | 537 | if (errno == EAGAIN || errno == EINTR) |
771c9901 | 538 | continue; |
1816895b | 539 | r = -errno; |
9a2d78b3 | 540 | td_verror(td, errno, "io_uring_enter"); |
771c9901 JA |
541 | break; |
542 | } | |
52885fa2 JA |
543 | } |
544 | } while (events < min); | |
545 | ||
546 | return r < 0 ? r : events; | |
547 | } | |
548 | ||
127715b6 NC |
549 | static inline void fio_ioring_cmdprio_prep(struct thread_data *td, |
550 | struct io_u *io_u) | |
b2a432bf | 551 | { |
b2a432bf | 552 | struct ioring_data *ld = td->io_ops_data; |
d6cbeab4 | 553 | struct cmdprio *cmdprio = &ld->cmdprio; |
127715b6 NC |
554 | |
555 | if (fio_cmdprio_set_ioprio(td, cmdprio, io_u)) | |
556 | ld->sqes[io_u->index].ioprio = io_u->ioprio; | |
b2a432bf PC |
557 | } |
558 | ||
bffad86f JA |
559 | static enum fio_q_status fio_ioring_queue(struct thread_data *td, |
560 | struct io_u *io_u) | |
52885fa2 | 561 | { |
bffad86f JA |
562 | struct ioring_data *ld = td->io_ops_data; |
563 | struct io_sq_ring *ring = &ld->sq_ring; | |
52885fa2 JA |
564 | unsigned tail, next_tail; |
565 | ||
566 | fio_ro_check(td, io_u); | |
567 | ||
b87aa01a | 568 | if (ld->queued == ld->iodepth) |
52885fa2 JA |
569 | return FIO_Q_BUSY; |
570 | ||
52885fa2 JA |
571 | if (io_u->ddir == DDIR_TRIM) { |
572 | if (ld->queued) | |
573 | return FIO_Q_BUSY; | |
574 | ||
575 | do_io_u_trim(td, io_u); | |
576 | io_u_mark_submit(td, 1); | |
577 | io_u_mark_complete(td, 1); | |
578 | return FIO_Q_COMPLETED; | |
579 | } | |
580 | ||
9a2d78b3 | 581 | tail = *ring->tail; |
52885fa2 | 582 | next_tail = tail + 1; |
9e26aff9 | 583 | if (next_tail == atomic_load_acquire(ring->head)) |
52885fa2 JA |
584 | return FIO_Q_BUSY; |
585 | ||
d6cbeab4 | 586 | if (ld->cmdprio.mode != CMDPRIO_MODE_NONE) |
ff00f247 NC |
587 | fio_ioring_cmdprio_prep(td, io_u); |
588 | ||
b87aa01a | 589 | ring->array[tail & ld->sq_ring_mask] = io_u->index; |
9e26aff9 | 590 | atomic_store_release(ring->tail, next_tail); |
52885fa2 JA |
591 | |
592 | ld->queued++; | |
593 | return FIO_Q_QUEUED; | |
594 | } | |
595 | ||
bffad86f | 596 | static void fio_ioring_queued(struct thread_data *td, int start, int nr) |
52885fa2 | 597 | { |
bffad86f | 598 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 JA |
599 | struct timespec now; |
600 | ||
601 | if (!fio_fill_issue_time(td)) | |
602 | return; | |
603 | ||
604 | fio_gettime(&now, NULL); | |
605 | ||
606 | while (nr--) { | |
bffad86f | 607 | struct io_sq_ring *ring = &ld->sq_ring; |
9a2d78b3 | 608 | int index = ring->array[start & ld->sq_ring_mask]; |
f8289afc | 609 | struct io_u *io_u = ld->io_u_index[index]; |
52885fa2 JA |
610 | |
611 | memcpy(&io_u->issue_time, &now, sizeof(now)); | |
612 | io_u_queued(td, io_u); | |
613 | ||
614 | start++; | |
52885fa2 | 615 | } |
39f56400 VF |
616 | |
617 | /* | |
618 | * only used for iolog | |
619 | */ | |
620 | if (td->o.read_iolog_file) | |
621 | memcpy(&td->last_issue, &now, sizeof(now)); | |
52885fa2 JA |
622 | } |
623 | ||
bffad86f | 624 | static int fio_ioring_commit(struct thread_data *td) |
52885fa2 | 625 | { |
bffad86f JA |
626 | struct ioring_data *ld = td->io_ops_data; |
627 | struct ioring_options *o = td->eo; | |
52885fa2 JA |
628 | int ret; |
629 | ||
630 | if (!ld->queued) | |
631 | return 0; | |
632 | ||
3d7d00a3 JA |
633 | /* |
634 | * Kernel side does submission. just need to check if the ring is | |
635 | * flagged as needing a kick, if so, call io_uring_enter(). This | |
636 | * only happens if we've been idle too long. | |
637 | */ | |
638 | if (o->sqpoll_thread) { | |
bffad86f | 639 | struct io_sq_ring *ring = &ld->sq_ring; |
c011bf12 | 640 | unsigned start = *ld->sq_ring.head; |
2dd96cc4 | 641 | unsigned flags; |
4cdbc048 | 642 | |
2dd96cc4 JA |
643 | flags = atomic_load_acquire(ring->flags); |
644 | if (flags & IORING_SQ_NEED_WAKEUP) | |
b532dd6d JA |
645 | io_uring_enter(ld, ld->queued, 0, |
646 | IORING_ENTER_SQ_WAKEUP); | |
c011bf12 AK |
647 | fio_ioring_queued(td, start, ld->queued); |
648 | io_u_mark_submit(td, ld->queued); | |
649 | ||
771c9901 JA |
650 | ld->queued = 0; |
651 | return 0; | |
652 | } | |
653 | ||
52885fa2 | 654 | do { |
9a2d78b3 | 655 | unsigned start = *ld->sq_ring.head; |
52885fa2 JA |
656 | long nr = ld->queued; |
657 | ||
9a2d78b3 | 658 | ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS); |
52885fa2 | 659 | if (ret > 0) { |
bffad86f | 660 | fio_ioring_queued(td, start, ret); |
52885fa2 JA |
661 | io_u_mark_submit(td, ret); |
662 | ||
663 | ld->queued -= ret; | |
664 | ret = 0; | |
a90cd050 JA |
665 | } else if (!ret) { |
666 | io_u_mark_submit(td, ret); | |
52885fa2 | 667 | continue; |
a90cd050 | 668 | } else { |
f6abd731 | 669 | if (errno == EAGAIN || errno == EINTR) { |
bffad86f | 670 | ret = fio_ioring_cqring_reap(td, 0, ld->queued); |
a90cd050 JA |
671 | if (ret) |
672 | continue; | |
673 | /* Shouldn't happen */ | |
674 | usleep(1); | |
675 | continue; | |
52885fa2 | 676 | } |
1816895b | 677 | ret = -errno; |
9a2d78b3 | 678 | td_verror(td, errno, "io_uring_enter submit"); |
52885fa2 | 679 | break; |
a90cd050 | 680 | } |
52885fa2 JA |
681 | } while (ld->queued); |
682 | ||
683 | return ret; | |
684 | } | |
685 | ||
bffad86f | 686 | static void fio_ioring_unmap(struct ioring_data *ld) |
52885fa2 | 687 | { |
9a2d78b3 | 688 | int i; |
52885fa2 | 689 | |
59f94d26 | 690 | for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++) |
9a2d78b3 JA |
691 | munmap(ld->mmap[i].ptr, ld->mmap[i].len); |
692 | close(ld->ring_fd); | |
b87aa01a JA |
693 | } |
694 | ||
bffad86f | 695 | static void fio_ioring_cleanup(struct thread_data *td) |
52885fa2 | 696 | { |
bffad86f | 697 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 JA |
698 | |
699 | if (ld) { | |
52885fa2 | 700 | if (!(td->flags & TD_F_CHILD)) |
bffad86f | 701 | fio_ioring_unmap(ld); |
9a2d78b3 | 702 | |
d6cbeab4 | 703 | fio_cmdprio_cleanup(&ld->cmdprio); |
52885fa2 | 704 | free(ld->io_u_index); |
9a2d78b3 | 705 | free(ld->iovecs); |
5ffd5626 | 706 | free(ld->fds); |
52885fa2 JA |
707 | free(ld); |
708 | } | |
709 | } | |
710 | ||
bffad86f | 711 | static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p) |
9a2d78b3 | 712 | { |
bffad86f JA |
713 | struct io_sq_ring *sring = &ld->sq_ring; |
714 | struct io_cq_ring *cring = &ld->cq_ring; | |
9a2d78b3 JA |
715 | void *ptr; |
716 | ||
e2239016 | 717 | ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32); |
9a2d78b3 JA |
718 | ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE, |
719 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, | |
720 | IORING_OFF_SQ_RING); | |
721 | ld->mmap[0].ptr = ptr; | |
722 | sring->head = ptr + p->sq_off.head; | |
723 | sring->tail = ptr + p->sq_off.tail; | |
724 | sring->ring_mask = ptr + p->sq_off.ring_mask; | |
725 | sring->ring_entries = ptr + p->sq_off.ring_entries; | |
726 | sring->flags = ptr + p->sq_off.flags; | |
ac122fea | 727 | sring->array = ptr + p->sq_off.array; |
9a2d78b3 JA |
728 | ld->sq_ring_mask = *sring->ring_mask; |
729 | ||
855dc4d4 AG |
730 | if (p->flags & IORING_SETUP_SQE128) |
731 | ld->mmap[1].len = 2 * p->sq_entries * sizeof(struct io_uring_sqe); | |
732 | else | |
733 | ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe); | |
f0403f94 | 734 | ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE, |
9a2d78b3 | 735 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, |
f0403f94 JA |
736 | IORING_OFF_SQES); |
737 | ld->mmap[1].ptr = ld->sqes; | |
9a2d78b3 | 738 | |
855dc4d4 AG |
739 | if (p->flags & IORING_SETUP_CQE32) { |
740 | ld->mmap[2].len = p->cq_off.cqes + | |
741 | 2 * p->cq_entries * sizeof(struct io_uring_cqe); | |
742 | } else { | |
743 | ld->mmap[2].len = p->cq_off.cqes + | |
744 | p->cq_entries * sizeof(struct io_uring_cqe); | |
745 | } | |
9a2d78b3 JA |
746 | ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE, |
747 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, | |
748 | IORING_OFF_CQ_RING); | |
749 | ld->mmap[2].ptr = ptr; | |
750 | cring->head = ptr + p->cq_off.head; | |
751 | cring->tail = ptr + p->cq_off.tail; | |
752 | cring->ring_mask = ptr + p->cq_off.ring_mask; | |
753 | cring->ring_entries = ptr + p->cq_off.ring_entries; | |
f0403f94 | 754 | cring->cqes = ptr + p->cq_off.cqes; |
9a2d78b3 JA |
755 | ld->cq_ring_mask = *cring->ring_mask; |
756 | return 0; | |
757 | } | |
758 | ||
556d8415 JA |
759 | static void fio_ioring_probe(struct thread_data *td) |
760 | { | |
761 | struct ioring_data *ld = td->io_ops_data; | |
762 | struct ioring_options *o = td->eo; | |
763 | struct io_uring_probe *p; | |
764 | int ret; | |
765 | ||
766 | /* already set by user, don't touch */ | |
767 | if (o->nonvectored != -1) | |
768 | return; | |
769 | ||
770 | /* default to off, as that's always safe */ | |
771 | o->nonvectored = 0; | |
772 | ||
773 | p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op)); | |
774 | if (!p) | |
775 | return; | |
776 | ||
777 | memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op)); | |
778 | ret = syscall(__NR_io_uring_register, ld->ring_fd, | |
779 | IORING_REGISTER_PROBE, p, 256); | |
780 | if (ret < 0) | |
781 | goto out; | |
782 | ||
783 | if (IORING_OP_WRITE > p->ops_len) | |
784 | goto out; | |
785 | ||
786 | if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) && | |
787 | (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED)) | |
788 | o->nonvectored = 1; | |
789 | out: | |
790 | free(p); | |
791 | } | |
792 | ||
bffad86f | 793 | static int fio_ioring_queue_init(struct thread_data *td) |
52885fa2 | 794 | { |
bffad86f JA |
795 | struct ioring_data *ld = td->io_ops_data; |
796 | struct ioring_options *o = td->eo; | |
52885fa2 | 797 | int depth = td->o.iodepth; |
bffad86f | 798 | struct io_uring_params p; |
9a2d78b3 JA |
799 | int ret; |
800 | ||
801 | memset(&p, 0, sizeof(p)); | |
52885fa2 JA |
802 | |
803 | if (o->hipri) | |
bffad86f | 804 | p.flags |= IORING_SETUP_IOPOLL; |
3d7d00a3 JA |
805 | if (o->sqpoll_thread) { |
806 | p.flags |= IORING_SETUP_SQPOLL; | |
807 | if (o->sqpoll_set) { | |
808 | p.flags |= IORING_SETUP_SQ_AFF; | |
809 | p.sq_thread_cpu = o->sqpoll_cpu; | |
810 | } | |
c011bf12 AK |
811 | |
812 | /* | |
813 | * Submission latency for sqpoll_thread is just the time it | |
814 | * takes to fill in the SQ ring entries, and any syscall if | |
815 | * IORING_SQ_NEED_WAKEUP is set, we don't need to log that time | |
816 | * separately. | |
817 | */ | |
818 | td->o.disable_slat = 1; | |
f635f1fb | 819 | } |
a90cd050 | 820 | |
1db268db JA |
821 | /* |
822 | * Clamp CQ ring size at our SQ ring size, we don't need more entries | |
823 | * than that. | |
824 | */ | |
825 | p.flags |= IORING_SETUP_CQSIZE; | |
826 | p.cq_entries = depth; | |
827 | ||
4d22c103 JA |
828 | /* |
829 | * Setup COOP_TASKRUN as we don't need to get IPI interrupted for | |
830 | * completing IO operations. | |
831 | */ | |
832 | p.flags |= IORING_SETUP_COOP_TASKRUN; | |
833 | ||
e453f369 JA |
834 | /* |
835 | * io_uring is always a single issuer, and we can defer task_work | |
836 | * runs until we reap events. | |
837 | */ | |
838 | p.flags |= IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN; | |
839 | ||
b5e99df6 | 840 | retry: |
bfed648c | 841 | ret = syscall(__NR_io_uring_setup, depth, &p); |
b5e99df6 | 842 | if (ret < 0) { |
e453f369 JA |
843 | if (errno == EINVAL && p.flags & IORING_SETUP_DEFER_TASKRUN) { |
844 | p.flags &= ~IORING_SETUP_DEFER_TASKRUN; | |
845 | p.flags &= ~IORING_SETUP_SINGLE_ISSUER; | |
846 | goto retry; | |
847 | } | |
4d22c103 JA |
848 | if (errno == EINVAL && p.flags & IORING_SETUP_COOP_TASKRUN) { |
849 | p.flags &= ~IORING_SETUP_COOP_TASKRUN; | |
850 | goto retry; | |
851 | } | |
b5e99df6 JA |
852 | if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) { |
853 | p.flags &= ~IORING_SETUP_CQSIZE; | |
854 | goto retry; | |
855 | } | |
9a2d78b3 | 856 | return ret; |
b5e99df6 | 857 | } |
9a2d78b3 JA |
858 | |
859 | ld->ring_fd = ret; | |
2ea53ca3 | 860 | |
556d8415 JA |
861 | fio_ioring_probe(td); |
862 | ||
2ea53ca3 | 863 | if (o->fixedbufs) { |
bfed648c | 864 | ret = syscall(__NR_io_uring_register, ld->ring_fd, |
919850d2 | 865 | IORING_REGISTER_BUFFERS, ld->iovecs, depth); |
2ea53ca3 JA |
866 | if (ret < 0) |
867 | return ret; | |
868 | } | |
869 | ||
bffad86f | 870 | return fio_ioring_mmap(ld, &p); |
52885fa2 JA |
871 | } |
872 | ||
855dc4d4 AG |
873 | static int fio_ioring_cmd_queue_init(struct thread_data *td) |
874 | { | |
875 | struct ioring_data *ld = td->io_ops_data; | |
876 | struct ioring_options *o = td->eo; | |
877 | int depth = td->o.iodepth; | |
878 | struct io_uring_params p; | |
879 | int ret; | |
880 | ||
881 | memset(&p, 0, sizeof(p)); | |
882 | ||
883 | if (o->hipri) | |
884 | p.flags |= IORING_SETUP_IOPOLL; | |
885 | if (o->sqpoll_thread) { | |
886 | p.flags |= IORING_SETUP_SQPOLL; | |
887 | if (o->sqpoll_set) { | |
888 | p.flags |= IORING_SETUP_SQ_AFF; | |
889 | p.sq_thread_cpu = o->sqpoll_cpu; | |
890 | } | |
c011bf12 AK |
891 | |
892 | /* | |
893 | * Submission latency for sqpoll_thread is just the time it | |
894 | * takes to fill in the SQ ring entries, and any syscall if | |
895 | * IORING_SQ_NEED_WAKEUP is set, we don't need to log that time | |
896 | * separately. | |
897 | */ | |
898 | td->o.disable_slat = 1; | |
855dc4d4 AG |
899 | } |
900 | if (o->cmd_type == FIO_URING_CMD_NVME) { | |
901 | p.flags |= IORING_SETUP_SQE128; | |
902 | p.flags |= IORING_SETUP_CQE32; | |
903 | } | |
904 | ||
905 | /* | |
906 | * Clamp CQ ring size at our SQ ring size, we don't need more entries | |
907 | * than that. | |
908 | */ | |
909 | p.flags |= IORING_SETUP_CQSIZE; | |
910 | p.cq_entries = depth; | |
911 | ||
07f78c37 AK |
912 | /* |
913 | * Setup COOP_TASKRUN as we don't need to get IPI interrupted for | |
914 | * completing IO operations. | |
915 | */ | |
916 | p.flags |= IORING_SETUP_COOP_TASKRUN; | |
917 | ||
918 | /* | |
919 | * io_uring is always a single issuer, and we can defer task_work | |
920 | * runs until we reap events. | |
921 | */ | |
922 | p.flags |= IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN; | |
923 | ||
855dc4d4 AG |
924 | retry: |
925 | ret = syscall(__NR_io_uring_setup, depth, &p); | |
926 | if (ret < 0) { | |
07f78c37 AK |
927 | if (errno == EINVAL && p.flags & IORING_SETUP_DEFER_TASKRUN) { |
928 | p.flags &= ~IORING_SETUP_DEFER_TASKRUN; | |
929 | p.flags &= ~IORING_SETUP_SINGLE_ISSUER; | |
930 | goto retry; | |
931 | } | |
932 | if (errno == EINVAL && p.flags & IORING_SETUP_COOP_TASKRUN) { | |
933 | p.flags &= ~IORING_SETUP_COOP_TASKRUN; | |
934 | goto retry; | |
935 | } | |
855dc4d4 AG |
936 | if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) { |
937 | p.flags &= ~IORING_SETUP_CQSIZE; | |
938 | goto retry; | |
939 | } | |
940 | return ret; | |
941 | } | |
942 | ||
943 | ld->ring_fd = ret; | |
944 | ||
945 | fio_ioring_probe(td); | |
946 | ||
947 | if (o->fixedbufs) { | |
948 | ret = syscall(__NR_io_uring_register, ld->ring_fd, | |
949 | IORING_REGISTER_BUFFERS, ld->iovecs, depth); | |
950 | if (ret < 0) | |
951 | return ret; | |
952 | } | |
953 | ||
954 | return fio_ioring_mmap(ld, &p); | |
955 | } | |
956 | ||
5ffd5626 JA |
957 | static int fio_ioring_register_files(struct thread_data *td) |
958 | { | |
959 | struct ioring_data *ld = td->io_ops_data; | |
960 | struct fio_file *f; | |
961 | unsigned int i; | |
962 | int ret; | |
963 | ||
964 | ld->fds = calloc(td->o.nr_files, sizeof(int)); | |
965 | ||
966 | for_each_file(td, f, i) { | |
967 | ret = generic_open_file(td, f); | |
968 | if (ret) | |
969 | goto err; | |
970 | ld->fds[i] = f->fd; | |
971 | f->engine_pos = i; | |
972 | } | |
973 | ||
bfed648c | 974 | ret = syscall(__NR_io_uring_register, ld->ring_fd, |
5ffd5626 JA |
975 | IORING_REGISTER_FILES, ld->fds, td->o.nr_files); |
976 | if (ret) { | |
977 | err: | |
978 | free(ld->fds); | |
979 | ld->fds = NULL; | |
980 | } | |
981 | ||
982 | /* | |
983 | * Pretend the file is closed again, and really close it if we hit | |
984 | * an error. | |
985 | */ | |
986 | for_each_file(td, f, i) { | |
987 | if (ret) { | |
988 | int fio_unused ret2; | |
989 | ret2 = generic_close_file(td, f); | |
990 | } else | |
991 | f->fd = -1; | |
992 | } | |
993 | ||
994 | return ret; | |
995 | } | |
996 | ||
bffad86f | 997 | static int fio_ioring_post_init(struct thread_data *td) |
52885fa2 | 998 | { |
bffad86f | 999 | struct ioring_data *ld = td->io_ops_data; |
5ffd5626 | 1000 | struct ioring_options *o = td->eo; |
52885fa2 | 1001 | struct io_u *io_u; |
650346e1 | 1002 | int err, i; |
52885fa2 | 1003 | |
650346e1 JA |
1004 | for (i = 0; i < td->o.iodepth; i++) { |
1005 | struct iovec *iov = &ld->iovecs[i]; | |
9a2d78b3 | 1006 | |
650346e1 JA |
1007 | io_u = ld->io_u_index[i]; |
1008 | iov->iov_base = io_u->buf; | |
1009 | iov->iov_len = td_max_bs(td); | |
52885fa2 JA |
1010 | } |
1011 | ||
bffad86f | 1012 | err = fio_ioring_queue_init(td); |
52885fa2 | 1013 | if (err) { |
0442b53f | 1014 | int init_err = errno; |
c4f5c92f | 1015 | |
0442b53f | 1016 | if (init_err == ENOSYS) |
c4f5c92f | 1017 | log_err("fio: your kernel doesn't support io_uring\n"); |
0442b53f | 1018 | td_verror(td, init_err, "io_queue_init"); |
52885fa2 JA |
1019 | return 1; |
1020 | } | |
1021 | ||
7c70f506 JA |
1022 | for (i = 0; i < td->o.iodepth; i++) { |
1023 | struct io_uring_sqe *sqe; | |
1024 | ||
1025 | sqe = &ld->sqes[i]; | |
1026 | memset(sqe, 0, sizeof(*sqe)); | |
1027 | } | |
1028 | ||
5ffd5626 JA |
1029 | if (o->registerfiles) { |
1030 | err = fio_ioring_register_files(td); | |
1031 | if (err) { | |
1032 | td_verror(td, errno, "ioring_register_files"); | |
1033 | return 1; | |
1034 | } | |
1035 | } | |
1036 | ||
52885fa2 JA |
1037 | return 0; |
1038 | } | |
1039 | ||
855dc4d4 AG |
1040 | static int fio_ioring_cmd_post_init(struct thread_data *td) |
1041 | { | |
1042 | struct ioring_data *ld = td->io_ops_data; | |
1043 | struct ioring_options *o = td->eo; | |
1044 | struct io_u *io_u; | |
1045 | int err, i; | |
1046 | ||
1047 | for (i = 0; i < td->o.iodepth; i++) { | |
1048 | struct iovec *iov = &ld->iovecs[i]; | |
1049 | ||
1050 | io_u = ld->io_u_index[i]; | |
1051 | iov->iov_base = io_u->buf; | |
1052 | iov->iov_len = td_max_bs(td); | |
1053 | } | |
1054 | ||
1055 | err = fio_ioring_cmd_queue_init(td); | |
1056 | if (err) { | |
1057 | int init_err = errno; | |
1058 | ||
1059 | td_verror(td, init_err, "io_queue_init"); | |
1060 | return 1; | |
1061 | } | |
1062 | ||
1063 | for (i = 0; i < td->o.iodepth; i++) { | |
1064 | struct io_uring_sqe *sqe; | |
1065 | ||
1066 | if (o->cmd_type == FIO_URING_CMD_NVME) { | |
1067 | sqe = &ld->sqes[i << 1]; | |
1068 | memset(sqe, 0, 2 * sizeof(*sqe)); | |
1069 | } else { | |
1070 | sqe = &ld->sqes[i]; | |
1071 | memset(sqe, 0, sizeof(*sqe)); | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | if (o->registerfiles) { | |
1076 | err = fio_ioring_register_files(td); | |
1077 | if (err) { | |
1078 | td_verror(td, errno, "ioring_register_files"); | |
1079 | return 1; | |
1080 | } | |
1081 | } | |
1082 | ||
1083 | return 0; | |
1084 | } | |
1085 | ||
bffad86f | 1086 | static int fio_ioring_init(struct thread_data *td) |
52885fa2 | 1087 | { |
5ffd5626 | 1088 | struct ioring_options *o = td->eo; |
bffad86f | 1089 | struct ioring_data *ld; |
e9f6567a | 1090 | int ret; |
52885fa2 | 1091 | |
5ffd5626 JA |
1092 | /* sqthread submission requires registered files */ |
1093 | if (o->sqpoll_thread) | |
1094 | o->registerfiles = 1; | |
1095 | ||
1096 | if (o->registerfiles && td->o.nr_files != td->o.open_files) { | |
1097 | log_err("fio: io_uring registered files require nr_files to " | |
1098 | "be identical to open_files\n"); | |
1099 | return 1; | |
1100 | } | |
1101 | ||
52885fa2 JA |
1102 | ld = calloc(1, sizeof(*ld)); |
1103 | ||
b87aa01a JA |
1104 | /* ring depth must be a power-of-2 */ |
1105 | ld->iodepth = td->o.iodepth; | |
1106 | td->o.iodepth = roundup_pow2(td->o.iodepth); | |
1107 | ||
52885fa2 JA |
1108 | /* io_u index */ |
1109 | ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *)); | |
650346e1 | 1110 | ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec)); |
52885fa2 JA |
1111 | |
1112 | td->io_ops_data = ld; | |
b2a432bf | 1113 | |
d6cbeab4 | 1114 | ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options); |
e9f6567a DLM |
1115 | if (ret) { |
1116 | td_verror(td, EINVAL, "fio_ioring_init"); | |
b2a432bf PC |
1117 | return 1; |
1118 | } | |
1af44196 | 1119 | |
52885fa2 JA |
1120 | return 0; |
1121 | } | |
1122 | ||
bffad86f | 1123 | static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u) |
52885fa2 | 1124 | { |
bffad86f | 1125 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 JA |
1126 | |
1127 | ld->io_u_index[io_u->index] = io_u; | |
1128 | return 0; | |
1129 | } | |
1130 | ||
5ffd5626 JA |
1131 | static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f) |
1132 | { | |
1133 | struct ioring_data *ld = td->io_ops_data; | |
1134 | struct ioring_options *o = td->eo; | |
1135 | ||
17318cf6 | 1136 | if (!ld || !o->registerfiles) |
5ffd5626 JA |
1137 | return generic_open_file(td, f); |
1138 | ||
1139 | f->fd = ld->fds[f->engine_pos]; | |
1140 | return 0; | |
1141 | } | |
1142 | ||
855dc4d4 AG |
1143 | static int fio_ioring_cmd_open_file(struct thread_data *td, struct fio_file *f) |
1144 | { | |
1145 | struct ioring_data *ld = td->io_ops_data; | |
1146 | struct ioring_options *o = td->eo; | |
1147 | ||
1148 | if (o->cmd_type == FIO_URING_CMD_NVME) { | |
1149 | struct nvme_data *data = NULL; | |
1150 | unsigned int nsid, lba_size = 0; | |
1151 | unsigned long long nlba = 0; | |
1152 | int ret; | |
1153 | ||
1154 | /* Store the namespace-id and lba size. */ | |
1155 | data = FILE_ENG_DATA(f); | |
1156 | if (data == NULL) { | |
1157 | ret = fio_nvme_get_info(f, &nsid, &lba_size, &nlba); | |
1158 | if (ret) | |
1159 | return ret; | |
1160 | ||
1161 | data = calloc(1, sizeof(struct nvme_data)); | |
1162 | data->nsid = nsid; | |
1163 | data->lba_shift = ilog2(lba_size); | |
1164 | ||
1165 | FILE_SET_ENG_DATA(f, data); | |
1166 | } | |
1167 | } | |
1168 | if (!ld || !o->registerfiles) | |
1169 | return generic_open_file(td, f); | |
1170 | ||
1171 | f->fd = ld->fds[f->engine_pos]; | |
1172 | return 0; | |
1173 | } | |
1174 | ||
5ffd5626 JA |
1175 | static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f) |
1176 | { | |
17318cf6 | 1177 | struct ioring_data *ld = td->io_ops_data; |
5ffd5626 JA |
1178 | struct ioring_options *o = td->eo; |
1179 | ||
17318cf6 | 1180 | if (!ld || !o->registerfiles) |
5ffd5626 JA |
1181 | return generic_close_file(td, f); |
1182 | ||
1183 | f->fd = -1; | |
1184 | return 0; | |
1185 | } | |
1186 | ||
855dc4d4 AG |
1187 | static int fio_ioring_cmd_close_file(struct thread_data *td, |
1188 | struct fio_file *f) | |
1189 | { | |
1190 | struct ioring_data *ld = td->io_ops_data; | |
1191 | struct ioring_options *o = td->eo; | |
1192 | ||
1193 | if (o->cmd_type == FIO_URING_CMD_NVME) { | |
1194 | struct nvme_data *data = FILE_ENG_DATA(f); | |
1195 | ||
1196 | FILE_SET_ENG_DATA(f, NULL); | |
1197 | free(data); | |
1198 | } | |
1199 | if (!ld || !o->registerfiles) | |
1200 | return generic_close_file(td, f); | |
1201 | ||
1202 | f->fd = -1; | |
1203 | return 0; | |
1204 | } | |
1205 | ||
1206 | static int fio_ioring_cmd_get_file_size(struct thread_data *td, | |
1207 | struct fio_file *f) | |
1208 | { | |
1209 | struct ioring_options *o = td->eo; | |
1210 | ||
1211 | if (fio_file_size_known(f)) | |
1212 | return 0; | |
1213 | ||
1214 | if (o->cmd_type == FIO_URING_CMD_NVME) { | |
1215 | struct nvme_data *data = NULL; | |
1216 | unsigned int nsid, lba_size = 0; | |
1217 | unsigned long long nlba = 0; | |
1218 | int ret; | |
1219 | ||
1220 | ret = fio_nvme_get_info(f, &nsid, &lba_size, &nlba); | |
1221 | if (ret) | |
1222 | return ret; | |
1223 | ||
1224 | data = calloc(1, sizeof(struct nvme_data)); | |
1225 | data->nsid = nsid; | |
1226 | data->lba_shift = ilog2(lba_size); | |
1227 | ||
1228 | f->real_file_size = lba_size * nlba; | |
1229 | fio_file_set_size_known(f); | |
1230 | ||
1231 | FILE_SET_ENG_DATA(f, data); | |
1232 | return 0; | |
1233 | } | |
1234 | return generic_get_file_size(td, f); | |
1235 | } | |
1236 | ||
3d05e0ff AK |
1237 | static int fio_ioring_cmd_get_zoned_model(struct thread_data *td, |
1238 | struct fio_file *f, | |
1239 | enum zbd_zoned_model *model) | |
1240 | { | |
1241 | return fio_nvme_get_zoned_model(td, f, model); | |
1242 | } | |
1243 | ||
1244 | static int fio_ioring_cmd_report_zones(struct thread_data *td, | |
1245 | struct fio_file *f, uint64_t offset, | |
1246 | struct zbd_zone *zbdz, | |
1247 | unsigned int nr_zones) | |
1248 | { | |
1249 | return fio_nvme_report_zones(td, f, offset, zbdz, nr_zones); | |
1250 | } | |
1251 | ||
1252 | static int fio_ioring_cmd_reset_wp(struct thread_data *td, struct fio_file *f, | |
1253 | uint64_t offset, uint64_t length) | |
1254 | { | |
1255 | return fio_nvme_reset_wp(td, f, offset, length); | |
1256 | } | |
1257 | ||
1258 | static int fio_ioring_cmd_get_max_open_zones(struct thread_data *td, | |
1259 | struct fio_file *f, | |
1260 | unsigned int *max_open_zones) | |
1261 | { | |
1262 | return fio_nvme_get_max_open_zones(td, f, max_open_zones); | |
1263 | } | |
1264 | ||
855dc4d4 | 1265 | static struct ioengine_ops ioengine_uring = { |
bffad86f | 1266 | .name = "io_uring", |
52885fa2 | 1267 | .version = FIO_IOOPS_VERSION, |
4e7e7898 VF |
1268 | .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD | |
1269 | FIO_ASYNCIO_SETS_ISSUE_TIME, | |
bffad86f JA |
1270 | .init = fio_ioring_init, |
1271 | .post_init = fio_ioring_post_init, | |
1272 | .io_u_init = fio_ioring_io_u_init, | |
1273 | .prep = fio_ioring_prep, | |
1274 | .queue = fio_ioring_queue, | |
1275 | .commit = fio_ioring_commit, | |
1276 | .getevents = fio_ioring_getevents, | |
1277 | .event = fio_ioring_event, | |
1278 | .cleanup = fio_ioring_cleanup, | |
5ffd5626 JA |
1279 | .open_file = fio_ioring_open_file, |
1280 | .close_file = fio_ioring_close_file, | |
52885fa2 JA |
1281 | .get_file_size = generic_get_file_size, |
1282 | .options = options, | |
bffad86f | 1283 | .option_struct_size = sizeof(struct ioring_options), |
52885fa2 JA |
1284 | }; |
1285 | ||
855dc4d4 AG |
1286 | static struct ioengine_ops ioengine_uring_cmd = { |
1287 | .name = "io_uring_cmd", | |
1288 | .version = FIO_IOOPS_VERSION, | |
4e7e7898 VF |
1289 | .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD | |
1290 | FIO_MEMALIGN | FIO_RAWIO | | |
1291 | FIO_ASYNCIO_SETS_ISSUE_TIME, | |
855dc4d4 AG |
1292 | .init = fio_ioring_init, |
1293 | .post_init = fio_ioring_cmd_post_init, | |
1294 | .io_u_init = fio_ioring_io_u_init, | |
1295 | .prep = fio_ioring_cmd_prep, | |
1296 | .queue = fio_ioring_queue, | |
1297 | .commit = fio_ioring_commit, | |
1298 | .getevents = fio_ioring_getevents, | |
1299 | .event = fio_ioring_cmd_event, | |
1300 | .cleanup = fio_ioring_cleanup, | |
1301 | .open_file = fio_ioring_cmd_open_file, | |
1302 | .close_file = fio_ioring_cmd_close_file, | |
1303 | .get_file_size = fio_ioring_cmd_get_file_size, | |
3d05e0ff AK |
1304 | .get_zoned_model = fio_ioring_cmd_get_zoned_model, |
1305 | .report_zones = fio_ioring_cmd_report_zones, | |
1306 | .reset_wp = fio_ioring_cmd_reset_wp, | |
1307 | .get_max_open_zones = fio_ioring_cmd_get_max_open_zones, | |
855dc4d4 AG |
1308 | .options = options, |
1309 | .option_struct_size = sizeof(struct ioring_options), | |
1310 | }; | |
1311 | ||
bffad86f | 1312 | static void fio_init fio_ioring_register(void) |
52885fa2 | 1313 | { |
855dc4d4 AG |
1314 | register_ioengine(&ioengine_uring); |
1315 | register_ioengine(&ioengine_uring_cmd); | |
52885fa2 JA |
1316 | } |
1317 | ||
bffad86f | 1318 | static void fio_exit fio_ioring_unregister(void) |
52885fa2 | 1319 | { |
855dc4d4 AG |
1320 | unregister_ioengine(&ioengine_uring); |
1321 | unregister_ioengine(&ioengine_uring_cmd); | |
52885fa2 | 1322 | } |
1f90e9bb | 1323 | #endif |