lib/pattern: fix formatting
[fio.git] / t / io_uring.c
CommitLineData
c9fb4c5b
JA
1#include <stdio.h>
2#include <errno.h>
3#include <assert.h>
4#include <stdlib.h>
5#include <stddef.h>
6#include <signal.h>
7#include <inttypes.h>
932131c9 8#include <math.h>
c9fb4c5b 9
256714ea
JA
10#ifdef CONFIG_LIBAIO
11#include <libaio.h>
12#endif
13
4b9e13dc
JA
14#ifdef CONFIG_LIBNUMA
15#include <numa.h>
16#endif
17
c9fb4c5b
JA
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <sys/ioctl.h>
21#include <sys/syscall.h>
22#include <sys/resource.h>
c3e2fc25 23#include <sys/mman.h>
e31b8288 24#include <sys/uio.h>
c9fb4c5b
JA
25#include <linux/fs.h>
26#include <fcntl.h>
27#include <unistd.h>
c9fb4c5b
JA
28#include <string.h>
29#include <pthread.h>
30#include <sched.h>
31
74efb029 32#include "../arch/arch.h"
a5a2429e 33#include "../os/os.h"
57fa61f0 34#include "../lib/types.h"
932131c9 35#include "../lib/roundup.h"
9eff5320 36#include "../lib/rand.h"
932131c9 37#include "../minmax.h"
f3e769a4 38#include "../os/linux/io_uring.h"
7d04588a 39#include "../engines/nvme.h"
ac122fea 40
e31b8288 41struct io_sq_ring {
e2239016
JA
42 unsigned *head;
43 unsigned *tail;
44 unsigned *ring_mask;
45 unsigned *ring_entries;
ce1705de 46 unsigned *flags;
e2239016 47 unsigned *array;
c9fb4c5b
JA
48};
49
e31b8288 50struct io_cq_ring {
e2239016
JA
51 unsigned *head;
52 unsigned *tail;
53 unsigned *ring_mask;
54 unsigned *ring_entries;
f0403f94 55 struct io_uring_cqe *cqes;
c9fb4c5b
JA
56};
57
701d1277 58#define DEPTH 128
2e7888ef
JA
59#define BATCH_SUBMIT 32
60#define BATCH_COMPLETE 32
c9fb4c5b
JA
61#define BS 4096
62
a7086591
JA
63#define MAX_FDS 16
64
c3e2fc25 65static unsigned sq_ring_mask, cq_ring_mask;
e39c34dc 66
a7086591
JA
67struct file {
68 unsigned long max_blocks;
beda9d8d
JA
69 unsigned long max_size;
70 unsigned long cur_off;
701d1277 71 unsigned pending_ios;
7d04588a
AG
72 unsigned int nsid; /* nsid field required for nvme-passthrough */
73 unsigned int lba_shift; /* lba_shift field required for nvme-passthrough */
48e698fa
JA
74 int real_fd;
75 int fixed_fd;
932131c9 76 int fileno;
a7086591
JA
77};
78
932131c9
JA
79#define PLAT_BITS 6
80#define PLAT_VAL (1 << PLAT_BITS)
81#define PLAT_GROUP_NR 29
82#define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
83
c9fb4c5b
JA
84struct submitter {
85 pthread_t thread;
f310970e 86 int ring_fd;
ca8c91c5 87 int enter_ring_fd;
54319661 88 int index;
e31b8288 89 struct io_sq_ring sq_ring;
f0403f94 90 struct io_uring_sqe *sqes;
e31b8288 91 struct io_cq_ring cq_ring;
c9fb4c5b 92 int inflight;
932131c9 93 int tid;
c9fb4c5b
JA
94 unsigned long reaps;
95 unsigned long done;
96 unsigned long calls;
97 volatile int finish;
701d1277 98
48e698fa
JA
99 __s32 *fds;
100
9eff5320
JA
101 struct taus258_state rand_state;
102
932131c9
JA
103 unsigned long *clock_batch;
104 int clock_index;
105 unsigned long *plat;
106
256714ea
JA
107#ifdef CONFIG_LIBAIO
108 io_context_t aio_ctx;
109#endif
110
4b9e13dc
JA
111 int numa_node;
112 const char *filename;
113
a7086591
JA
114 struct file files[MAX_FDS];
115 unsigned nr_files;
116 unsigned cur_file;
e39863e3 117 struct iovec iovecs[];
c9fb4c5b
JA
118};
119
e39863e3 120static struct submitter *submitter;
c9fb4c5b 121static volatile int finish;
52479d8b 122static int stats_running;
16d25711 123static unsigned long max_iops;
c409e4c2 124static long t_io_uring_page_size;
c9fb4c5b 125
e39863e3
KB
126static int depth = DEPTH;
127static int batch_submit = BATCH_SUBMIT;
128static int batch_complete = BATCH_COMPLETE;
5bd526f2 129static int bs = BS;
f0403f94 130static int polled = 1; /* use IO polling */
701d1277 131static int fixedbufs = 1; /* use fixed user buffers */
a71ad043 132static int dma_map; /* pre-map DMA buffers */
8c5fa755 133static int register_files = 1; /* use fixed files */
f0403f94 134static int buffered = 0; /* use buffered IO, not O_DIRECT */
3d7d00a3
JA
135static int sq_thread_poll = 0; /* use kernel submission/poller thread */
136static int sq_thread_cpu = -1; /* pin above thread to this CPU */
8025517d 137static int do_nop = 0; /* no-op SQ ring commands */
54319661 138static int nthreads = 1;
932131c9 139static int stats = 0; /* generate IO stats */
256714ea 140static int aio = 0; /* use libaio */
beda9d8d
JA
141static int runtime = 0; /* runtime */
142static int random_io = 1; /* random or sequential IO */
ca8c91c5 143static int register_ring = 1; /* register ring */
379406bc 144static int use_sync = 0; /* use preadv2 */
4b9e13dc 145static int numa_placement = 0; /* set to node of device */
7d04588a 146static int pt = 0; /* passthrough I/O or not */
256714ea 147
932131c9 148static unsigned long tsc_rate;
c9fb4c5b 149
203e4c26
JA
150#define TSC_RATE_FILE "tsc-rate"
151
84106576 152static int vectored = 1;
b3915995 153
932131c9 154static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
ad45a465 155 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
932131c9
JA
156static int plist_len = 17;
157
a71ad043 158#ifndef IORING_REGISTER_MAP_BUFFERS
70eb71e6 159#define IORING_REGISTER_MAP_BUFFERS 26
a71ad043
JA
160struct io_uring_map_buffers {
161 __s32 fd;
162 __u32 buf_start;
163 __u32 buf_end;
702b0be2
JA
164 __u32 flags;
165 __u64 rsvd[2];
a71ad043
JA
166};
167#endif
168
7d04588a
AG
169static int nvme_identify(int fd, __u32 nsid, enum nvme_identify_cns cns,
170 enum nvme_csi csi, void *data)
171{
172 struct nvme_passthru_cmd cmd = {
173 .opcode = nvme_admin_identify,
174 .nsid = nsid,
175 .addr = (__u64)(uintptr_t)data,
176 .data_len = NVME_IDENTIFY_DATA_SIZE,
177 .cdw10 = cns,
178 .cdw11 = csi << NVME_IDENTIFY_CSI_SHIFT,
179 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
180 };
181
182 return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
183}
184
185static int nvme_get_info(int fd, __u32 *nsid, __u32 *lba_sz, __u64 *nlba)
186{
187 struct nvme_id_ns ns;
188 int namespace_id;
189 int err;
190
191 namespace_id = ioctl(fd, NVME_IOCTL_ID);
192 if (namespace_id < 0) {
193 fprintf(stderr, "error failed to fetch namespace-id\n");
194 close(fd);
195 return -errno;
196 }
197
198 /*
199 * Identify namespace to get namespace-id, namespace size in LBA's
200 * and LBA data size.
201 */
202 err = nvme_identify(fd, namespace_id, NVME_IDENTIFY_CNS_NS,
203 NVME_CSI_NVM, &ns);
204 if (err) {
205 fprintf(stderr, "error failed to fetch identify namespace\n");
206 close(fd);
207 return err;
208 }
209
210 *nsid = namespace_id;
211 *lba_sz = 1 << ns.lbaf[(ns.flbas & 0x0f)].ds;
212 *nlba = ns.nsze;
213
214 return 0;
215}
216
932131c9
JA
217static unsigned long cycles_to_nsec(unsigned long cycles)
218{
219 uint64_t val;
220
221 if (!tsc_rate)
222 return cycles;
223
224 val = cycles * 1000000000ULL;
225 return val / tsc_rate;
226}
227
228static unsigned long plat_idx_to_val(unsigned int idx)
229{
230 unsigned int error_bits;
231 unsigned long k, base;
232
233 assert(idx < PLAT_NR);
234
235 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
236 * all bits of the sample as index */
237 if (idx < (PLAT_VAL << 1))
238 return cycles_to_nsec(idx);
239
240 /* Find the group and compute the minimum value of that group */
241 error_bits = (idx >> PLAT_BITS) - 1;
242 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
243
244 /* Find its bucket number of the group */
245 k = idx % PLAT_VAL;
246
247 /* Return the mean of the range of the bucket */
248 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
249}
250
c409e4c2
AG
251unsigned int calculate_clat_percentiles(unsigned long *io_u_plat,
252 unsigned long nr, unsigned long **output,
253 unsigned long *maxv, unsigned long *minv)
932131c9
JA
254{
255 unsigned long sum = 0;
256 unsigned int len = plist_len, i, j = 0;
257 unsigned long *ovals = NULL;
258 bool is_last;
259
bb209d68 260 *minv = -1UL;
932131c9
JA
261 *maxv = 0;
262
263 ovals = malloc(len * sizeof(*ovals));
264 if (!ovals)
265 return 0;
266
267 /*
268 * Calculate bucket values, note down max and min values
269 */
270 is_last = false;
271 for (i = 0; i < PLAT_NR && !is_last; i++) {
272 sum += io_u_plat[i];
273 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
274 assert(plist[j] <= 100.0);
275
276 ovals[j] = plat_idx_to_val(i);
277 if (ovals[j] < *minv)
278 *minv = ovals[j];
279 if (ovals[j] > *maxv)
280 *maxv = ovals[j];
281
282 is_last = (j == len - 1) != 0;
283 if (is_last)
284 break;
285
286 j++;
287 }
288 }
289
290 if (!is_last)
291 fprintf(stderr, "error calculating latency percentiles\n");
292
293 *output = ovals;
294 return len;
295}
296
297static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
298 unsigned int precision)
299{
300 unsigned int divisor, len, i, j = 0;
301 unsigned long minv, maxv;
302 unsigned long *ovals;
303 int per_line, scale_down, time_width;
304 bool is_last;
305 char fmt[32];
306
c409e4c2 307 len = calculate_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
932131c9
JA
308 if (!len || !ovals)
309 goto out;
310
311 if (!tsc_rate) {
312 scale_down = 0;
313 divisor = 1;
314 printf(" percentiles (tsc ticks):\n |");
315 } else if (minv > 2000 && maxv > 99999) {
316 scale_down = 1;
317 divisor = 1000;
318 printf(" percentiles (usec):\n |");
319 } else {
320 scale_down = 0;
321 divisor = 1;
322 printf(" percentiles (nsec):\n |");
323 }
324
325 time_width = max(5, (int) (log10(maxv / divisor) + 1));
326 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
327 precision, time_width);
328 /* fmt will be something like " %5.2fth=[%4llu]%c" */
329 per_line = (80 - 7) / (precision + 10 + time_width);
330
331 for (j = 0; j < len; j++) {
332 /* for formatting */
333 if (j != 0 && (j % per_line) == 0)
334 printf(" |");
335
336 /* end of the list */
337 is_last = (j == len - 1) != 0;
338
339 for (i = 0; i < scale_down; i++)
340 ovals[j] = (ovals[j] + 999) / 1000;
341
342 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
343
344 if (is_last)
345 break;
346
347 if ((j % per_line) == per_line - 1) /* for formatting */
348 printf("\n");
349 }
350
351out:
352 free(ovals);
353}
354
b65c1fc0 355#ifdef ARCH_HAVE_CPU_CLOCK
932131c9
JA
356static unsigned int plat_val_to_idx(unsigned long val)
357{
358 unsigned int msb, error_bits, base, offset, idx;
359
360 /* Find MSB starting from bit 0 */
361 if (val == 0)
362 msb = 0;
363 else
364 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
365
366 /*
367 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
368 * all bits of the sample as index
369 */
370 if (msb <= PLAT_BITS)
371 return val;
372
373 /* Compute the number of error bits to discard*/
374 error_bits = msb - PLAT_BITS;
375
376 /* Compute the number of buckets before the group */
377 base = (error_bits + 1) << PLAT_BITS;
378
379 /*
380 * Discard the error bits and apply the mask to find the
381 * index for the buckets in the group
382 */
383 offset = (PLAT_VAL - 1) & (val >> error_bits);
384
385 /* Make sure the index does not exceed (array size - 1) */
386 idx = (base + offset) < (PLAT_NR - 1) ?
387 (base + offset) : (PLAT_NR - 1);
388
389 return idx;
390}
b65c1fc0 391#endif
932131c9
JA
392
393static void add_stat(struct submitter *s, int clock_index, int nr)
394{
395#ifdef ARCH_HAVE_CPU_CLOCK
396 unsigned long cycles;
397 unsigned int pidx;
398
265697fc
JA
399 if (!s->finish && clock_index) {
400 cycles = get_cpu_clock();
401 cycles -= s->clock_batch[clock_index];
402 pidx = plat_val_to_idx(cycles);
403 s->plat[pidx] += nr;
404 }
932131c9
JA
405#endif
406}
407
a71ad043
JA
408static int io_uring_map_buffers(struct submitter *s)
409{
410 struct io_uring_map_buffers map = {
411 .fd = s->files[0].real_fd,
a71ad043 412 .buf_end = depth,
a71ad043
JA
413 };
414
415 if (do_nop)
416 return 0;
d49885aa
JA
417 if (s->nr_files > 1)
418 fprintf(stdout, "Mapping buffers may not work with multiple files\n");
a71ad043
JA
419
420 return syscall(__NR_io_uring_register, s->ring_fd,
421 IORING_REGISTER_MAP_BUFFERS, &map, 1);
422}
423
2ea53ca3 424static int io_uring_register_buffers(struct submitter *s)
c9fb4c5b 425{
8025517d
JA
426 if (do_nop)
427 return 0;
428
bfed648c 429 return syscall(__NR_io_uring_register, s->ring_fd,
55845033 430 IORING_REGISTER_BUFFERS, s->iovecs, roundup_pow2(depth));
2ea53ca3
JA
431}
432
a7abc9fb
JA
433static int io_uring_register_files(struct submitter *s)
434{
48e698fa 435 int i;
a7abc9fb 436
8025517d
JA
437 if (do_nop)
438 return 0;
439
48e698fa
JA
440 s->fds = calloc(s->nr_files, sizeof(__s32));
441 for (i = 0; i < s->nr_files; i++) {
442 s->fds[i] = s->files[i].real_fd;
443 s->files[i].fixed_fd = i;
444 }
a7abc9fb 445
bfed648c 446 return syscall(__NR_io_uring_register, s->ring_fd,
919850d2 447 IORING_REGISTER_FILES, s->fds, s->nr_files);
a7abc9fb
JA
448}
449
2ea53ca3
JA
450static int io_uring_setup(unsigned entries, struct io_uring_params *p)
451{
2be18f6b
JA
452 int ret;
453
1db268db
JA
454 /*
455 * Clamp CQ ring size at our SQ ring size, we don't need more entries
456 * than that.
457 */
458 p->flags |= IORING_SETUP_CQSIZE;
459 p->cq_entries = entries;
460
2be18f6b
JA
461 p->flags |= IORING_SETUP_COOP_TASKRUN;
462 p->flags |= IORING_SETUP_SINGLE_ISSUER;
463 p->flags |= IORING_SETUP_DEFER_TASKRUN;
464retry:
465 ret = syscall(__NR_io_uring_setup, entries, p);
466 if (!ret)
467 return 0;
468
469 if (errno == EINVAL && p->flags & IORING_SETUP_COOP_TASKRUN) {
470 p->flags &= ~IORING_SETUP_COOP_TASKRUN;
471 goto retry;
472 }
473 if (errno == EINVAL && p->flags & IORING_SETUP_SINGLE_ISSUER) {
474 p->flags &= ~IORING_SETUP_SINGLE_ISSUER;
475 goto retry;
476 }
477 if (errno == EINVAL && p->flags & IORING_SETUP_DEFER_TASKRUN) {
478 p->flags &= ~IORING_SETUP_DEFER_TASKRUN;
479 goto retry;
480 }
481
482 return ret;
c9fb4c5b
JA
483}
484
b3915995
JA
485static void io_uring_probe(int fd)
486{
487 struct io_uring_probe *p;
488 int ret;
489
490 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
491 if (!p)
492 return;
493
494 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
495 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
496 if (ret < 0)
497 goto out;
498
499 if (IORING_OP_READ > p->ops_len)
500 goto out;
501
502 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
84106576 503 vectored = 0;
b3915995
JA
504out:
505 free(p);
506}
507
c3e2fc25
JA
508static int io_uring_enter(struct submitter *s, unsigned int to_submit,
509 unsigned int min_complete, unsigned int flags)
c9fb4c5b 510{
ca8c91c5
JA
511 if (register_ring)
512 flags |= IORING_ENTER_REGISTERED_RING;
c377f4f8 513#ifdef FIO_ARCH_HAS_SYSCALL
ca8c91c5 514 return __do_syscall6(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
c377f4f8
JA
515 min_complete, flags, NULL, 0);
516#else
ca8c91c5
JA
517 return syscall(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
518 min_complete, flags, NULL, 0);
c377f4f8 519#endif
c9fb4c5b
JA
520}
521
701d1277
JA
522static unsigned file_depth(struct submitter *s)
523{
e39863e3 524 return (depth + s->nr_files - 1) / s->nr_files;
701d1277
JA
525}
526
501565a1
JA
527static unsigned long long get_offset(struct submitter *s, struct file *f)
528{
529 unsigned long long offset;
530 long r;
531
532 if (random_io) {
533 r = __rand64(&s->rand_state);
534 offset = (r % (f->max_blocks - 1)) * bs;
535 } else {
536 offset = f->cur_off;
537 f->cur_off += bs;
538 if (f->cur_off + bs > f->max_size)
539 f->cur_off = 0;
540 }
541
542 return offset;
543}
544
a7086591 545static void init_io(struct submitter *s, unsigned index)
c9fb4c5b 546{
f0403f94 547 struct io_uring_sqe *sqe = &s->sqes[index];
a7086591 548 struct file *f;
c9fb4c5b 549
8025517d
JA
550 if (do_nop) {
551 sqe->opcode = IORING_OP_NOP;
552 return;
553 }
554
701d1277
JA
555 if (s->nr_files == 1) {
556 f = &s->files[0];
557 } else {
558 f = &s->files[s->cur_file];
559 if (f->pending_ios >= file_depth(s)) {
560 s->cur_file++;
561 if (s->cur_file == s->nr_files)
562 s->cur_file = 0;
93d1811c 563 f = &s->files[s->cur_file];
701d1277
JA
564 }
565 }
566 f->pending_ios++;
a7086591 567
8c5fa755
JA
568 if (register_files) {
569 sqe->flags = IOSQE_FIXED_FILE;
570 sqe->fd = f->fixed_fd;
571 } else {
572 sqe->flags = 0;
573 sqe->fd = f->real_fd;
574 }
f0403f94 575 if (fixedbufs) {
48e698fa 576 sqe->opcode = IORING_OP_READ_FIXED;
919850d2 577 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
5bd526f2 578 sqe->len = bs;
2ea53ca3 579 sqe->buf_index = index;
84106576 580 } else if (!vectored) {
b3915995
JA
581 sqe->opcode = IORING_OP_READ;
582 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
583 sqe->len = bs;
584 sqe->buf_index = 0;
84106576
JA
585 } else {
586 sqe->opcode = IORING_OP_READV;
587 sqe->addr = (unsigned long) &s->iovecs[index];
588 sqe->len = 1;
589 sqe->buf_index = 0;
f0403f94 590 }
f0403f94 591 sqe->ioprio = 0;
501565a1 592 sqe->off = get_offset(s, f);
932131c9 593 sqe->user_data = (unsigned long) f->fileno;
52479d8b 594 if (stats && stats_running)
bb209d68 595 sqe->user_data |= ((uint64_t)s->clock_index << 32);
c9fb4c5b
JA
596}
597
7d04588a
AG
598static void init_io_pt(struct submitter *s, unsigned index)
599{
600 struct io_uring_sqe *sqe = &s->sqes[index << 1];
601 unsigned long offset;
602 struct file *f;
603 struct nvme_uring_cmd *cmd;
604 unsigned long long slba;
605 unsigned long long nlb;
606 long r;
607
608 if (s->nr_files == 1) {
609 f = &s->files[0];
610 } else {
611 f = &s->files[s->cur_file];
612 if (f->pending_ios >= file_depth(s)) {
613 s->cur_file++;
614 if (s->cur_file == s->nr_files)
615 s->cur_file = 0;
616 f = &s->files[s->cur_file];
617 }
618 }
619 f->pending_ios++;
620
621 if (random_io) {
622 r = __rand64(&s->rand_state);
623 offset = (r % (f->max_blocks - 1)) * bs;
624 } else {
625 offset = f->cur_off;
626 f->cur_off += bs;
627 if (f->cur_off + bs > f->max_size)
628 f->cur_off = 0;
629 }
630
631 if (register_files) {
632 sqe->fd = f->fixed_fd;
633 sqe->flags = IOSQE_FIXED_FILE;
634 } else {
635 sqe->fd = f->real_fd;
636 sqe->flags = 0;
637 }
638 sqe->opcode = IORING_OP_URING_CMD;
639 sqe->user_data = (unsigned long) f->fileno;
640 if (stats)
9ce84fbd 641 sqe->user_data |= ((__u64) s->clock_index << 32ULL);
7d04588a
AG
642 sqe->cmd_op = NVME_URING_CMD_IO;
643 slba = offset >> f->lba_shift;
644 nlb = (bs >> f->lba_shift) - 1;
645 cmd = (struct nvme_uring_cmd *)&sqe->cmd;
646 /* cdw10 and cdw11 represent starting slba*/
647 cmd->cdw10 = slba & 0xffffffff;
648 cmd->cdw11 = slba >> 32;
649 /* cdw12 represent number of lba to be read*/
650 cmd->cdw12 = nlb;
651 cmd->addr = (unsigned long) s->iovecs[index].iov_base;
652 cmd->data_len = bs;
021ce718
JA
653 if (fixedbufs) {
654 sqe->uring_cmd_flags = IORING_URING_CMD_FIXED;
655 sqe->buf_index = index;
656 }
7d04588a
AG
657 cmd->nsid = f->nsid;
658 cmd->opcode = 2;
659}
660
256714ea 661static int prep_more_ios_uring(struct submitter *s, int max_ios)
c9fb4c5b 662{
e31b8288 663 struct io_sq_ring *ring = &s->sq_ring;
c5f0a205
JA
664 unsigned head, index, tail, next_tail, prepped = 0;
665
666 if (sq_thread_poll)
667 head = atomic_load_acquire(ring->head);
668 else
669 head = *ring->head;
c9fb4c5b 670
c3e2fc25 671 next_tail = tail = *ring->tail;
c9fb4c5b
JA
672 do {
673 next_tail++;
6b6f52b9 674 if (next_tail == head)
c9fb4c5b
JA
675 break;
676
e39c34dc 677 index = tail & sq_ring_mask;
7d04588a
AG
678 if (pt)
679 init_io_pt(s, index);
680 else
681 init_io(s, index);
c9fb4c5b
JA
682 prepped++;
683 tail = next_tail;
684 } while (prepped < max_ios);
685
fc2dc21b
JA
686 if (prepped)
687 atomic_store_release(ring->tail, tail);
c9fb4c5b
JA
688 return prepped;
689}
690
a7086591 691static int get_file_size(struct file *f)
c9fb4c5b
JA
692{
693 struct stat st;
694
48e698fa 695 if (fstat(f->real_fd, &st) < 0)
c9fb4c5b 696 return -1;
7d04588a
AG
697 if (pt) {
698 __u64 nlba;
699 __u32 lbs;
700 int ret;
701
702 if (!S_ISCHR(st.st_mode)) {
703 fprintf(stderr, "passthrough works with only nvme-ns "
704 "generic devices (/dev/ngXnY)\n");
705 return -1;
706 }
707 ret = nvme_get_info(f->real_fd, &f->nsid, &lbs, &nlba);
708 if (ret)
709 return -1;
710 if ((bs % lbs) != 0) {
711 printf("error: bs:%d should be a multiple logical_block_size:%d\n",
712 bs, lbs);
713 return -1;
714 }
715 f->max_blocks = nlba / bs;
716 f->max_size = nlba;
717 f->lba_shift = ilog2(lbs);
718 return 0;
719 } else if (S_ISBLK(st.st_mode)) {
c9fb4c5b
JA
720 unsigned long long bytes;
721
48e698fa 722 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
c9fb4c5b
JA
723 return -1;
724
5bd526f2 725 f->max_blocks = bytes / bs;
beda9d8d 726 f->max_size = bytes;
c9fb4c5b
JA
727 return 0;
728 } else if (S_ISREG(st.st_mode)) {
5bd526f2 729 f->max_blocks = st.st_size / bs;
beda9d8d 730 f->max_size = st.st_size;
c9fb4c5b
JA
731 return 0;
732 }
733
734 return -1;
735}
736
256714ea 737static int reap_events_uring(struct submitter *s)
c9fb4c5b 738{
e31b8288 739 struct io_cq_ring *ring = &s->cq_ring;
f0403f94 740 struct io_uring_cqe *cqe;
e2239016 741 unsigned head, reaped = 0;
ab85494f 742 int last_idx = -1, stat_nr = 0;
c9fb4c5b 743
c3e2fc25 744 head = *ring->head;
c9fb4c5b 745 do {
701d1277
JA
746 struct file *f;
747
fc2dc21b 748 if (head == atomic_load_acquire(ring->tail))
c9fb4c5b 749 break;
f0403f94 750 cqe = &ring->cqes[head & cq_ring_mask];
8025517d 751 if (!do_nop) {
932131c9
JA
752 int fileno = cqe->user_data & 0xffffffff;
753
754 f = &s->files[fileno];
8025517d 755 f->pending_ios--;
5bd526f2 756 if (cqe->res != bs) {
8025517d 757 printf("io: unexpected ret=%d\n", cqe->res);
154a9582 758 if (polled && cqe->res == -EOPNOTSUPP)
8066f6b6 759 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
8025517d
JA
760 return -1;
761 }
c9fb4c5b 762 }
932131c9
JA
763 if (stats) {
764 int clock_index = cqe->user_data >> 32;
765
ab85494f
JA
766 if (last_idx != clock_index) {
767 if (last_idx != -1) {
768 add_stat(s, last_idx, stat_nr);
769 stat_nr = 0;
770 }
771 last_idx = clock_index;
d4af2ece
JA
772 }
773 stat_nr++;
932131c9 774 }
c9fb4c5b
JA
775 reaped++;
776 head++;
c9fb4c5b
JA
777 } while (1);
778
ab85494f
JA
779 if (stat_nr)
780 add_stat(s, last_idx, stat_nr);
781
fc2dc21b
JA
782 if (reaped) {
783 s->inflight -= reaped;
784 atomic_store_release(ring->head, head);
785 }
c9fb4c5b
JA
786 return reaped;
787}
788
7d04588a
AG
789static int reap_events_uring_pt(struct submitter *s)
790{
791 struct io_cq_ring *ring = &s->cq_ring;
792 struct io_uring_cqe *cqe;
793 unsigned head, reaped = 0;
794 int last_idx = -1, stat_nr = 0;
795 unsigned index;
796 int fileno;
797
798 head = *ring->head;
799 do {
800 struct file *f;
801
7d04588a
AG
802 if (head == atomic_load_acquire(ring->tail))
803 break;
804 index = head & cq_ring_mask;
805 cqe = &ring->cqes[index << 1];
806 fileno = cqe->user_data & 0xffffffff;
807 f = &s->files[fileno];
808 f->pending_ios--;
809
810 if (cqe->res != 0) {
811 printf("io: unexpected ret=%d\n", cqe->res);
812 if (polled && cqe->res == -EINVAL)
813 printf("passthrough doesn't support polled IO\n");
814 return -1;
815 }
816 if (stats) {
817 int clock_index = cqe->user_data >> 32;
818
819 if (last_idx != clock_index) {
820 if (last_idx != -1) {
821 add_stat(s, last_idx, stat_nr);
822 stat_nr = 0;
823 }
824 last_idx = clock_index;
825 }
826 stat_nr++;
827 }
828 reaped++;
829 head++;
830 } while (1);
831
832 if (stat_nr)
833 add_stat(s, last_idx, stat_nr);
834
835 if (reaped) {
836 s->inflight -= reaped;
837 atomic_store_release(ring->head, head);
838 }
839 return reaped;
840}
841
4b9e13dc
JA
842static void set_affinity(struct submitter *s)
843{
844#ifdef CONFIG_LIBNUMA
845 struct bitmask *mask;
846
847 if (s->numa_node == -1)
848 return;
849
850 numa_set_preferred(s->numa_node);
851
852 mask = numa_allocate_cpumask();
853 numa_node_to_cpus(s->numa_node, mask);
854 numa_sched_setaffinity(s->tid, mask);
855#endif
856}
857
858static int detect_node(struct submitter *s, const char *name)
859{
860#ifdef CONFIG_LIBNUMA
861 const char *base = basename(name);
862 char str[128];
863 int ret, fd, node;
864
cc791a99
JA
865 if (pt)
866 sprintf(str, "/sys/class/nvme-generic/%s/device/numa_node", base);
867 else
868 sprintf(str, "/sys/block/%s/device/numa_node", base);
4b9e13dc
JA
869 fd = open(str, O_RDONLY);
870 if (fd < 0)
871 return -1;
872
873 ret = read(fd, str, sizeof(str));
874 if (ret < 0) {
875 close(fd);
876 return -1;
877 }
878 node = atoi(str);
879 s->numa_node = node;
880 close(fd);
881#else
882 s->numa_node = -1;
883#endif
884 return 0;
885}
886
887static int setup_aio(struct submitter *s)
888{
889#ifdef CONFIG_LIBAIO
890 if (polled) {
891 fprintf(stderr, "aio does not support polled IO\n");
892 polled = 0;
893 }
894 if (sq_thread_poll) {
895 fprintf(stderr, "aio does not support SQPOLL IO\n");
896 sq_thread_poll = 0;
897 }
898 if (do_nop) {
899 fprintf(stderr, "aio does not support polled IO\n");
900 do_nop = 0;
901 }
902 if (fixedbufs || register_files) {
903 fprintf(stderr, "aio does not support registered files or buffers\n");
904 fixedbufs = register_files = 0;
905 }
906
907 return io_queue_init(roundup_pow2(depth), &s->aio_ctx);
908#else
909 fprintf(stderr, "Legacy AIO not available on this system/build\n");
910 errno = EINVAL;
911 return -1;
912#endif
913}
914
915static int setup_ring(struct submitter *s)
916{
917 struct io_sq_ring *sring = &s->sq_ring;
918 struct io_cq_ring *cring = &s->cq_ring;
919 struct io_uring_params p;
6b6f52b9 920 int ret, fd, i;
4b9e13dc 921 void *ptr;
7d04588a 922 size_t len;
4b9e13dc
JA
923
924 memset(&p, 0, sizeof(p));
925
926 if (polled && !do_nop)
927 p.flags |= IORING_SETUP_IOPOLL;
928 if (sq_thread_poll) {
929 p.flags |= IORING_SETUP_SQPOLL;
930 if (sq_thread_cpu != -1) {
931 p.flags |= IORING_SETUP_SQ_AFF;
932 p.sq_thread_cpu = sq_thread_cpu;
933 }
934 }
7d04588a
AG
935 if (pt) {
936 p.flags |= IORING_SETUP_SQE128;
937 p.flags |= IORING_SETUP_CQE32;
938 }
4b9e13dc
JA
939
940 fd = io_uring_setup(depth, &p);
941 if (fd < 0) {
942 perror("io_uring_setup");
943 return 1;
944 }
945 s->ring_fd = s->enter_ring_fd = fd;
946
947 io_uring_probe(fd);
948
949 if (fixedbufs) {
950 struct rlimit rlim;
951
952 rlim.rlim_cur = RLIM_INFINITY;
953 rlim.rlim_max = RLIM_INFINITY;
954 /* ignore potential error, not needed on newer kernels */
955 setrlimit(RLIMIT_MEMLOCK, &rlim);
956
957 ret = io_uring_register_buffers(s);
958 if (ret < 0) {
959 perror("io_uring_register_buffers");
960 return 1;
961 }
962
963 if (dma_map) {
964 ret = io_uring_map_buffers(s);
965 if (ret < 0) {
966 perror("io_uring_map_buffers");
967 return 1;
968 }
969 }
970 }
971
972 if (register_files) {
973 ret = io_uring_register_files(s);
974 if (ret < 0) {
975 perror("io_uring_register_files");
976 return 1;
977 }
978 }
979
980 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
981 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
982 IORING_OFF_SQ_RING);
983 sring->head = ptr + p.sq_off.head;
984 sring->tail = ptr + p.sq_off.tail;
985 sring->ring_mask = ptr + p.sq_off.ring_mask;
986 sring->ring_entries = ptr + p.sq_off.ring_entries;
987 sring->flags = ptr + p.sq_off.flags;
988 sring->array = ptr + p.sq_off.array;
989 sq_ring_mask = *sring->ring_mask;
990
7d04588a
AG
991 if (p.flags & IORING_SETUP_SQE128)
992 len = 2 * p.sq_entries * sizeof(struct io_uring_sqe);
993 else
994 len = p.sq_entries * sizeof(struct io_uring_sqe);
995 s->sqes = mmap(0, len,
4b9e13dc
JA
996 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
997 IORING_OFF_SQES);
998
7d04588a
AG
999 if (p.flags & IORING_SETUP_CQE32) {
1000 len = p.cq_off.cqes +
1001 2 * p.cq_entries * sizeof(struct io_uring_cqe);
1002 } else {
1003 len = p.cq_off.cqes +
1004 p.cq_entries * sizeof(struct io_uring_cqe);
1005 }
1006 ptr = mmap(0, len,
4b9e13dc
JA
1007 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1008 IORING_OFF_CQ_RING);
1009 cring->head = ptr + p.cq_off.head;
1010 cring->tail = ptr + p.cq_off.tail;
1011 cring->ring_mask = ptr + p.cq_off.ring_mask;
1012 cring->ring_entries = ptr + p.cq_off.ring_entries;
1013 cring->cqes = ptr + p.cq_off.cqes;
1014 cq_ring_mask = *cring->ring_mask;
6b6f52b9
JA
1015
1016 for (i = 0; i < p.sq_entries; i++)
1017 sring->array[i] = i;
1018
4b9e13dc
JA
1019 return 0;
1020}
1021
1022static void *allocate_mem(struct submitter *s, int size)
1023{
1024 void *buf;
1025
1026#ifdef CONFIG_LIBNUMA
1027 if (s->numa_node != -1)
1028 return numa_alloc_onnode(size, s->numa_node);
1029#endif
1030
c409e4c2 1031 if (posix_memalign(&buf, t_io_uring_page_size, bs)) {
4b9e13dc
JA
1032 printf("failed alloc\n");
1033 return NULL;
1034 }
1035
1036 return buf;
1037}
1038
256714ea 1039static int submitter_init(struct submitter *s)
c9fb4c5b 1040{
4b9e13dc
JA
1041 int i, nr_batch, err;
1042 static int init_printed;
1043 char buf[80];
932131c9 1044 s->tid = gettid();
4b9e13dc
JA
1045 printf("submitter=%d, tid=%d, file=%s, node=%d\n", s->index, s->tid,
1046 s->filename, s->numa_node);
1047
1048 set_affinity(s);
c9fb4c5b 1049
6243766b
KR
1050 __init_rand64(&s->rand_state, s->tid);
1051 srand48(s->tid);
c9fb4c5b 1052
932131c9
JA
1053 for (i = 0; i < MAX_FDS; i++)
1054 s->files[i].fileno = i;
1055
4b9e13dc
JA
1056 for (i = 0; i < roundup_pow2(depth); i++) {
1057 void *buf;
1058
1059 buf = allocate_mem(s, bs);
1060 if (!buf)
1061 return 1;
1062 s->iovecs[i].iov_base = buf;
1063 s->iovecs[i].iov_len = bs;
1064 }
1065
1066 if (use_sync) {
1067 sprintf(buf, "Engine=preadv2\n");
1068 err = 0;
1069 } else if (!aio) {
1070 err = setup_ring(s);
1071 sprintf(buf, "Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1072 } else {
1073 sprintf(buf, "Engine=aio\n");
1074 err = setup_aio(s);
1075 }
1076 if (err) {
1077 printf("queue setup failed: %s, %d\n", strerror(errno), err);
1078 return 1;
1079 }
1080
1081 if (!init_printed) {
1082 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
1083 printf("%s", buf);
1084 init_printed = 1;
1085 }
1086
932131c9
JA
1087 if (stats) {
1088 nr_batch = roundup_pow2(depth / batch_submit);
d4af2ece
JA
1089 if (nr_batch < 2)
1090 nr_batch = 2;
932131c9 1091 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
52479d8b 1092 s->clock_index = 1;
932131c9
JA
1093
1094 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
1095 } else {
1096 s->clock_batch = NULL;
1097 s->plat = NULL;
1098 nr_batch = 0;
1099 }
7d04588a
AG
1100 /* perform the expensive command initialization part for passthrough here
1101 * rather than in the fast path
1102 */
1103 if (pt) {
1104 for (i = 0; i < roundup_pow2(depth); i++) {
1105 struct io_uring_sqe *sqe = &s->sqes[i << 1];
932131c9 1106
7d04588a
AG
1107 memset(&sqe->cmd, 0, sizeof(struct nvme_uring_cmd));
1108 }
1109 }
256714ea
JA
1110 return nr_batch;
1111}
1112
1113#ifdef CONFIG_LIBAIO
1114static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
1115{
8310c570 1116 uint64_t data;
256714ea
JA
1117 struct file *f;
1118 unsigned index;
256714ea
JA
1119
1120 index = 0;
1121 while (index < max_ios) {
1122 struct iocb *iocb = &iocbs[index];
1123
1124 if (s->nr_files == 1) {
1125 f = &s->files[0];
1126 } else {
1127 f = &s->files[s->cur_file];
1128 if (f->pending_ios >= file_depth(s)) {
1129 s->cur_file++;
1130 if (s->cur_file == s->nr_files)
1131 s->cur_file = 0;
1132 f = &s->files[s->cur_file];
1133 }
1134 }
1135 f->pending_ios++;
1136
256714ea 1137 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
501565a1 1138 s->iovecs[index].iov_len, get_offset(s, f));
256714ea
JA
1139
1140 data = f->fileno;
52479d8b 1141 if (stats && stats_running)
8310c570 1142 data |= (((uint64_t) s->clock_index) << 32);
256714ea
JA
1143 iocb->data = (void *) (uintptr_t) data;
1144 index++;
1145 }
1146 return index;
1147}
1148
1149static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
1150{
1151 int last_idx = -1, stat_nr = 0;
1152 int reaped = 0;
1153
1154 while (evs) {
8310c570 1155 uint64_t data = (uintptr_t) events[reaped].data;
256714ea
JA
1156 struct file *f = &s->files[data & 0xffffffff];
1157
1158 f->pending_ios--;
1159 if (events[reaped].res != bs) {
1160 printf("io: unexpected ret=%ld\n", events[reaped].res);
1161 return -1;
1162 }
1163 if (stats) {
1164 int clock_index = data >> 32;
1165
1166 if (last_idx != clock_index) {
1167 if (last_idx != -1) {
1168 add_stat(s, last_idx, stat_nr);
1169 stat_nr = 0;
1170 }
1171 last_idx = clock_index;
d4af2ece
JA
1172 }
1173 stat_nr++;
256714ea
JA
1174 }
1175 reaped++;
1176 evs--;
1177 }
1178
1179 if (stat_nr)
1180 add_stat(s, last_idx, stat_nr);
1181
1182 s->inflight -= reaped;
1183 s->done += reaped;
1184 return reaped;
1185}
1186
1187static void *submitter_aio_fn(void *data)
1188{
1189 struct submitter *s = data;
71989c1b 1190 int i, ret, prepped;
256714ea
JA
1191 struct iocb **iocbsptr;
1192 struct iocb *iocbs;
1193 struct io_event *events;
71989c1b
JA
1194#ifdef ARCH_HAVE_CPU_CLOCK
1195 int nr_batch = submitter_init(s);
1196#else
1197 submitter_init(s);
1198#endif
256714ea
JA
1199
1200 iocbsptr = calloc(depth, sizeof(struct iocb *));
1201 iocbs = calloc(depth, sizeof(struct iocb));
1202 events = calloc(depth, sizeof(struct io_event));
1203
1204 for (i = 0; i < depth; i++)
1205 iocbsptr[i] = &iocbs[i];
1206
1207 prepped = 0;
1208 do {
1209 int to_wait, to_submit, to_prep;
1210
1211 if (!prepped && s->inflight < depth) {
1212 to_prep = min(depth - s->inflight, batch_submit);
1213 prepped = prep_more_ios_aio(s, to_prep, iocbs);
1214#ifdef ARCH_HAVE_CPU_CLOCK
1215 if (prepped && stats) {
1216 s->clock_batch[s->clock_index] = get_cpu_clock();
1217 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1218 }
1219#endif
1220 }
1221 s->inflight += prepped;
1222 to_submit = prepped;
1223
1224 if (to_submit && (s->inflight + to_submit <= depth))
1225 to_wait = 0;
1226 else
1227 to_wait = min(s->inflight + to_submit, batch_complete);
1228
1229 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
1230 s->calls++;
1231 if (ret < 0) {
1232 perror("io_submit");
1233 break;
1234 } else if (ret != to_submit) {
1235 printf("submitted %d, wanted %d\n", ret, to_submit);
1236 break;
1237 }
1238 prepped = 0;
1239
24a24c12 1240 while (to_wait) {
256714ea
JA
1241 int r;
1242
24a24c12
JA
1243 s->calls++;
1244 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
1245 if (r < 0) {
1246 perror("io_getevents");
1247 break;
1248 } else if (r != to_wait) {
1249 printf("r=%d, wait=%d\n", r, to_wait);
1250 break;
1251 }
1252 r = reap_events_aio(s, events, r);
1253 s->reaps += r;
1254 to_wait -= r;
256714ea
JA
1255 }
1256 } while (!s->finish);
1257
1258 free(iocbsptr);
1259 free(iocbs);
1260 free(events);
1261 finish = 1;
1262 return NULL;
1263}
1264#endif
1265
ca8c91c5
JA
1266static void io_uring_unregister_ring(struct submitter *s)
1267{
1268 struct io_uring_rsrc_update up = {
1269 .offset = s->enter_ring_fd,
1270 };
1271
1272 syscall(__NR_io_uring_register, s->ring_fd, IORING_UNREGISTER_RING_FDS,
1273 &up, 1);
1274}
1275
1276static int io_uring_register_ring(struct submitter *s)
1277{
1278 struct io_uring_rsrc_update up = {
1279 .data = s->ring_fd,
1280 .offset = -1U,
1281 };
1282 int ret;
1283
1284 ret = syscall(__NR_io_uring_register, s->ring_fd,
1285 IORING_REGISTER_RING_FDS, &up, 1);
1286 if (ret == 1) {
1287 s->enter_ring_fd = up.offset;
1288 return 0;
1289 }
1290 register_ring = 0;
1291 return -1;
1292}
1293
256714ea
JA
1294static void *submitter_uring_fn(void *data)
1295{
1296 struct submitter *s = data;
1297 struct io_sq_ring *ring = &s->sq_ring;
b65c1fc0
JA
1298 int ret, prepped;
1299#ifdef ARCH_HAVE_CPU_CLOCK
1300 int nr_batch = submitter_init(s);
1301#else
1302 submitter_init(s);
1303#endif
256714ea 1304
ca8c91c5
JA
1305 if (register_ring)
1306 io_uring_register_ring(s);
1307
c9fb4c5b
JA
1308 prepped = 0;
1309 do {
f310970e 1310 int to_wait, to_submit, this_reap, to_prep;
fc2dc21b 1311 unsigned ring_flags = 0;
c9fb4c5b 1312
e39863e3
KB
1313 if (!prepped && s->inflight < depth) {
1314 to_prep = min(depth - s->inflight, batch_submit);
256714ea 1315 prepped = prep_more_ios_uring(s, to_prep);
932131c9
JA
1316#ifdef ARCH_HAVE_CPU_CLOCK
1317 if (prepped && stats) {
1318 s->clock_batch[s->clock_index] = get_cpu_clock();
1319 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1320 }
1321#endif
f310970e 1322 }
c9fb4c5b
JA
1323 s->inflight += prepped;
1324submit_more:
1325 to_submit = prepped;
1326submit:
e39863e3 1327 if (to_submit && (s->inflight + to_submit <= depth))
c9fb4c5b
JA
1328 to_wait = 0;
1329 else
e39863e3 1330 to_wait = min(s->inflight + to_submit, batch_complete);
c9fb4c5b 1331
ce1705de
JA
1332 /*
1333 * Only need to call io_uring_enter if we're not using SQ thread
1334 * poll, or if IORING_SQ_NEED_WAKEUP is set.
1335 */
fc2dc21b
JA
1336 if (sq_thread_poll)
1337 ring_flags = atomic_load_acquire(ring->flags);
1338 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
e0abe388
JA
1339 unsigned flags = 0;
1340
1341 if (to_wait)
1342 flags = IORING_ENTER_GETEVENTS;
fc2dc21b 1343 if (ring_flags & IORING_SQ_NEED_WAKEUP)
b532dd6d 1344 flags |= IORING_ENTER_SQ_WAKEUP;
e0abe388 1345 ret = io_uring_enter(s, to_submit, to_wait, flags);
ce1705de 1346 s->calls++;
fc2dc21b
JA
1347 } else {
1348 /* for SQPOLL, we submitted it all effectively */
1349 ret = to_submit;
ce1705de 1350 }
c9fb4c5b 1351
ce1705de
JA
1352 /*
1353 * For non SQ thread poll, we already got the events we needed
1354 * through the io_uring_enter() above. For SQ thread poll, we
1355 * need to loop here until we find enough events.
1356 */
1357 this_reap = 0;
1358 do {
1359 int r;
256714ea 1360
7d04588a
AG
1361 if (pt)
1362 r = reap_events_uring_pt(s);
1363 else
1364 r = reap_events_uring(s);
7d1435e6
JA
1365 if (r == -1) {
1366 s->finish = 1;
ce1705de 1367 break;
7d1435e6 1368 } else if (r > 0)
ce1705de
JA
1369 this_reap += r;
1370 } while (sq_thread_poll && this_reap < to_wait);
c9fb4c5b
JA
1371 s->reaps += this_reap;
1372
1373 if (ret >= 0) {
1374 if (!ret) {
1375 to_submit = 0;
1376 if (s->inflight)
1377 goto submit;
1378 continue;
1379 } else if (ret < to_submit) {
1380 int diff = to_submit - ret;
1381
1382 s->done += ret;
1383 prepped -= diff;
1384 goto submit_more;
1385 }
1386 s->done += ret;
1387 prepped = 0;
1388 continue;
1389 } else if (ret < 0) {
ac122fea 1390 if (errno == EAGAIN) {
c9fb4c5b
JA
1391 if (s->finish)
1392 break;
1393 if (this_reap)
1394 goto submit;
c9fb4c5b
JA
1395 to_submit = 0;
1396 goto submit;
1397 }
ac122fea 1398 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
1399 break;
1400 }
1401 } while (!s->finish);
a7086591 1402
ca8c91c5
JA
1403 if (register_ring)
1404 io_uring_unregister_ring(s);
1405
c9fb4c5b
JA
1406 finish = 1;
1407 return NULL;
1408}
1409
a7648136 1410#ifdef CONFIG_PWRITEV2
379406bc
JA
1411static void *submitter_sync_fn(void *data)
1412{
1413 struct submitter *s = data;
1414 int ret;
1415
1416 submitter_init(s);
1417
1418 do {
1419 uint64_t offset;
1420 struct file *f;
379406bc
JA
1421
1422 if (s->nr_files == 1) {
1423 f = &s->files[0];
1424 } else {
1425 f = &s->files[s->cur_file];
1426 if (f->pending_ios >= file_depth(s)) {
1427 s->cur_file++;
1428 if (s->cur_file == s->nr_files)
1429 s->cur_file = 0;
1430 f = &s->files[s->cur_file];
1431 }
1432 }
1433 f->pending_ios++;
1434
379406bc
JA
1435#ifdef ARCH_HAVE_CPU_CLOCK
1436 if (stats)
1437 s->clock_batch[s->clock_index] = get_cpu_clock();
1438#endif
1439
1440 s->inflight++;
1441 s->calls++;
1442
501565a1 1443 offset = get_offset(s, f);
379406bc
JA
1444 if (polled)
1445 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, RWF_HIPRI);
1446 else
1447 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, 0);
1448
1449 if (ret < 0) {
1450 perror("preadv2");
1451 break;
1452 } else if (ret != bs) {
1453 break;
1454 }
1455
1456 s->done++;
1457 s->inflight--;
1458 f->pending_ios--;
1459 if (stats)
1460 add_stat(s, s->clock_index, 1);
1461 } while (!s->finish);
1462
1463 finish = 1;
1464 return NULL;
1465}
a7648136
JA
1466#else
1467static void *submitter_sync_fn(void *data)
1468{
1469 finish = 1;
1470 return NULL;
1471}
1472#endif
379406bc 1473
54319661
JA
1474static struct submitter *get_submitter(int offset)
1475{
1476 void *ret;
1477
1478 ret = submitter;
1479 if (offset)
1480 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
1481 return ret;
1482}
1483
65e1a5e8 1484static void do_finish(const char *reason)
c9fb4c5b 1485{
54319661 1486 int j;
4b9e13dc 1487
65e1a5e8 1488 printf("Exiting on %s\n", reason);
54319661
JA
1489 for (j = 0; j < nthreads; j++) {
1490 struct submitter *s = get_submitter(j);
1491 s->finish = 1;
1492 }
4b9e13dc
JA
1493 if (max_iops > 1000000) {
1494 double miops = (double) max_iops / 1000000.0;
1495 printf("Maximum IOPS=%.2fM\n", miops);
1496 } else if (max_iops > 100000) {
1497 double kiops = (double) max_iops / 1000.0;
1498 printf("Maximum IOPS=%.2fK\n", kiops);
1499 } else {
18b557a0 1500 printf("Maximum IOPS=%lu\n", max_iops);
4b9e13dc 1501 }
c9fb4c5b
JA
1502 finish = 1;
1503}
1504
65e1a5e8
EV
1505static void sig_int(int sig)
1506{
1507 do_finish("signal");
1508}
1509
c9fb4c5b
JA
1510static void arm_sig_int(void)
1511{
1512 struct sigaction act;
1513
1514 memset(&act, 0, sizeof(act));
1515 act.sa_handler = sig_int;
1516 act.sa_flags = SA_RESTART;
1517 sigaction(SIGINT, &act, NULL);
2cf71009
BP
1518
1519 /* Windows uses SIGBREAK as a quit signal from other applications */
1520#ifdef WIN32
1521 sigaction(SIGBREAK, &act, NULL);
1522#endif
c9fb4c5b
JA
1523}
1524
d79ff8c9 1525static void usage(char *argv, int status)
e39863e3 1526{
65e1a5e8
EV
1527 char runtime_str[16];
1528 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
e39863e3 1529 printf("%s [options] -- [filenames]\n"
35268e11
AJ
1530 " -d <int> : IO Depth, default %d\n"
1531 " -s <int> : Batch submit, default %d\n"
1532 " -c <int> : Batch complete, default %d\n"
1533 " -b <int> : Block size, default %d\n"
1534 " -p <bool> : Polled IO, default %d\n"
1535 " -B <bool> : Fixed buffers, default %d\n"
dd1f1ba0 1536 " -D <bool> : DMA map fixed buffers, default %d\n"
35268e11 1537 " -F <bool> : Register files, default %d\n"
0862f718 1538 " -n <int> : Number of threads, default %d\n"
2686fc22 1539 " -O <bool> : Use O_DIRECT, default %d\n"
4a39c524
EV
1540 " -N <bool> : Perform just no-op requests, default %d\n"
1541 " -t <bool> : Track IO latencies, default %d\n"
256714ea 1542 " -T <int> : TSC rate in HZ\n"
beda9d8d
JA
1543 " -r <int> : Runtime in seconds, default %s\n"
1544 " -R <bool> : Use random IO, default %d\n"
ca8c91c5 1545 " -a <bool> : Use legacy aio, default %d\n"
3be2f0ca 1546 " -S <bool> : Use sync IO (preadv2), default %d\n"
4b9e13dc 1547 " -X <bool> : Use registered ring %d\n"
7d04588a
AG
1548 " -P <bool> : Automatically place on device home node %d\n"
1549 " -u <bool> : Use nvme-passthrough I/O, default %d\n",
35268e11 1550 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
a71ad043 1551 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
ca8c91c5 1552 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio,
7d04588a 1553 use_sync, register_ring, numa_placement, pt);
d79ff8c9 1554 exit(status);
e39863e3
KB
1555}
1556
203e4c26
JA
1557static void read_tsc_rate(void)
1558{
1559 char buffer[32];
1560 int fd, ret;
1561
1562 if (tsc_rate)
1563 return;
1564
1565 fd = open(TSC_RATE_FILE, O_RDONLY);
1566 if (fd < 0)
1567 return;
1568
1569 ret = read(fd, buffer, sizeof(buffer));
1570 if (ret < 0) {
1571 close(fd);
1572 return;
1573 }
1574
1575 tsc_rate = strtoul(buffer, NULL, 10);
1576 printf("Using TSC rate %luHz\n", tsc_rate);
1577 close(fd);
1578}
1579
1580static void write_tsc_rate(void)
1581{
1582 char buffer[32];
1583 struct stat sb;
1584 int fd, ret;
1585
1586 if (!stat(TSC_RATE_FILE, &sb))
1587 return;
1588
1589 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1590 if (fd < 0)
1591 return;
1592
1593 memset(buffer, 0, sizeof(buffer));
1594 sprintf(buffer, "%lu", tsc_rate);
1595 ret = write(fd, buffer, strlen(buffer));
1596 if (ret < 0)
1597 perror("write");
1598 close(fd);
1599}
1600
c9fb4c5b
JA
1601int main(int argc, char *argv[])
1602{
e39863e3 1603 struct submitter *s;
05138221 1604 unsigned long done, calls, reap;
4b9e13dc 1605 int i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
d79ff8c9 1606 struct file f;
c3e2fc25 1607 void *ret;
c9fb4c5b 1608
0862f718
JA
1609 if (!do_nop && argc < 2)
1610 usage(argv[0], 1);
c9fb4c5b 1611
7d04588a 1612 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:r:D:R:X:S:P:u:h?")) != -1) {
e39863e3 1613 switch (opt) {
256714ea
JA
1614 case 'a':
1615 aio = !!atoi(optarg);
1616 break;
e39863e3
KB
1617 case 'd':
1618 depth = atoi(optarg);
1619 break;
1620 case 's':
1621 batch_submit = atoi(optarg);
932131c9
JA
1622 if (!batch_submit)
1623 batch_submit = 1;
e39863e3
KB
1624 break;
1625 case 'c':
1626 batch_complete = atoi(optarg);
932131c9
JA
1627 if (!batch_complete)
1628 batch_complete = 1;
e39863e3 1629 break;
5bd526f2
JA
1630 case 'b':
1631 bs = atoi(optarg);
1632 break;
1633 case 'p':
1634 polled = !!atoi(optarg);
1635 break;
6a87c3b0
JA
1636 case 'B':
1637 fixedbufs = !!atoi(optarg);
1638 break;
1639 case 'F':
1640 register_files = !!atoi(optarg);
1641 break;
54319661
JA
1642 case 'n':
1643 nthreads = atoi(optarg);
caf2b9ac
JA
1644 if (!nthreads) {
1645 printf("Threads must be non-zero\n");
1646 usage(argv[0], 1);
1647 }
54319661 1648 break;
0862f718
JA
1649 case 'N':
1650 do_nop = !!atoi(optarg);
1651 break;
2686fc22
JA
1652 case 'O':
1653 buffered = !atoi(optarg);
1654 break;
932131c9
JA
1655 case 't':
1656#ifndef ARCH_HAVE_CPU_CLOCK
1657 fprintf(stderr, "Stats not supported on this CPU\n");
1658 return 1;
1659#endif
1660 stats = !!atoi(optarg);
1661 break;
1662 case 'T':
1663#ifndef ARCH_HAVE_CPU_CLOCK
1664 fprintf(stderr, "Stats not supported on this CPU\n");
1665 return 1;
1666#endif
1667 tsc_rate = strtoul(optarg, NULL, 10);
203e4c26 1668 write_tsc_rate();
932131c9 1669 break;
65e1a5e8
EV
1670 case 'r':
1671 runtime = atoi(optarg);
1672 break;
a71ad043
JA
1673 case 'D':
1674 dma_map = !!atoi(optarg);
1675 break;
beda9d8d
JA
1676 case 'R':
1677 random_io = !!atoi(optarg);
1678 break;
ca8c91c5
JA
1679 case 'X':
1680 register_ring = !!atoi(optarg);
1681 break;
379406bc 1682 case 'S':
a7648136 1683#ifdef CONFIG_PWRITEV2
379406bc 1684 use_sync = !!atoi(optarg);
a7648136
JA
1685#else
1686 fprintf(stderr, "preadv2 not supported\n");
1687 exit(1);
1688#endif
379406bc 1689 break;
4b9e13dc
JA
1690 case 'P':
1691 numa_placement = !!atoi(optarg);
1692 break;
7d04588a
AG
1693 case 'u':
1694 pt = !!atoi(optarg);
1695 break;
e39863e3
KB
1696 case 'h':
1697 case '?':
1698 default:
d79ff8c9 1699 usage(argv[0], 0);
e39863e3
KB
1700 break;
1701 }
1702 }
1703
203e4c26
JA
1704 if (stats)
1705 read_tsc_rate();
1706
c5347611
JA
1707 if (batch_complete > depth)
1708 batch_complete = depth;
1709 if (batch_submit > depth)
1710 batch_submit = depth;
a71ad043
JA
1711 if (!fixedbufs && dma_map)
1712 dma_map = 0;
c5347611 1713
54319661 1714 submitter = calloc(nthreads, sizeof(*submitter) +
55845033 1715 roundup_pow2(depth) * sizeof(struct iovec));
54319661
JA
1716 for (j = 0; j < nthreads; j++) {
1717 s = get_submitter(j);
4b9e13dc 1718 s->numa_node = -1;
54319661
JA
1719 s->index = j;
1720 s->done = s->calls = s->reaps = 0;
1721 }
e39863e3 1722
701d1277 1723 flags = O_RDONLY | O_NOATIME;
a7086591
JA
1724 if (!buffered)
1725 flags |= O_DIRECT;
1726
54319661 1727 j = 0;
e39863e3 1728 i = optind;
d79ff8c9 1729 nfiles = argc - i;
2ac00585
JA
1730 if (!do_nop) {
1731 if (!nfiles) {
1732 printf("No files specified\n");
1733 usage(argv[0], 1);
1734 }
1735 threads_per_f = nthreads / nfiles;
1736 /* make sure each thread gets assigned files */
1737 if (threads_per_f == 0) {
1738 threads_per_f = 1;
1739 } else {
1740 threads_rem = nthreads - threads_per_f * nfiles;
1741 }
d79ff8c9 1742 }
8025517d 1743 while (!do_nop && i < argc) {
d79ff8c9
AJ
1744 int k, limit;
1745
1746 memset(&f, 0, sizeof(f));
a7086591
JA
1747
1748 fd = open(argv[i], flags);
1749 if (fd < 0) {
1750 perror("open");
1751 return 1;
1752 }
d79ff8c9
AJ
1753 f.real_fd = fd;
1754 if (get_file_size(&f)) {
a7086591
JA
1755 printf("failed getting size of device/file\n");
1756 return 1;
1757 }
d79ff8c9 1758 if (f.max_blocks <= 1) {
a7086591
JA
1759 printf("Zero file/device size?\n");
1760 return 1;
1761 }
d79ff8c9
AJ
1762 f.max_blocks--;
1763
1764 limit = threads_per_f;
1765 limit += threads_rem > 0 ? 1 : 0;
1766 for (k = 0; k < limit; k++) {
1767 s = get_submitter((j + k) % nthreads);
a7086591 1768
d79ff8c9
AJ
1769 if (s->nr_files == MAX_FDS) {
1770 printf("Max number of files (%d) reached\n", MAX_FDS);
1771 break;
1772 }
1773
1774 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1775
4b9e13dc
JA
1776 if (numa_placement)
1777 detect_node(s, argv[i]);
1778
1779 s->filename = argv[i];
d79ff8c9
AJ
1780 s->nr_files++;
1781 }
1782 threads_rem--;
a7086591 1783 i++;
d79ff8c9 1784 j += limit;
a7086591
JA
1785 }
1786
c9fb4c5b
JA
1787 arm_sig_int();
1788
c409e4c2
AG
1789 t_io_uring_page_size = sysconf(_SC_PAGESIZE);
1790 if (t_io_uring_page_size < 0)
1791 t_io_uring_page_size = 4096;
a0639afe 1792
54319661
JA
1793 for (j = 0; j < nthreads; j++) {
1794 s = get_submitter(j);
379406bc
JA
1795 if (use_sync)
1796 pthread_create(&s->thread, NULL, submitter_sync_fn, s);
1797 else if (!aio)
256714ea
JA
1798 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1799#ifdef CONFIG_LIBAIO
1800 else
1801 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1802#endif
54319661 1803 }
c9fb4c5b 1804
05138221 1805 reap = calls = done = 0;
c9fb4c5b
JA
1806 do {
1807 unsigned long this_done = 0;
1808 unsigned long this_reap = 0;
1809 unsigned long this_call = 0;
1810 unsigned long rpc = 0, ipc = 0;
f3057d26 1811 unsigned long iops, bw;
c9fb4c5b
JA
1812
1813 sleep(1);
65e1a5e8
EV
1814 if (runtime && !--runtime)
1815 do_finish("timeout");
a1f17100
JA
1816
1817 /* don't print partial run, if interrupted by signal */
1818 if (finish)
1819 break;
52479d8b
JA
1820
1821 /* one second in to the run, enable stats */
1822 if (stats)
1823 stats_running = 1;
1824
54319661 1825 for (j = 0; j < nthreads; j++) {
7d1ce4b7 1826 s = get_submitter(j);
54319661
JA
1827 this_done += s->done;
1828 this_call += s->calls;
1829 this_reap += s->reaps;
1830 }
c9fb4c5b
JA
1831 if (this_call - calls) {
1832 rpc = (this_done - done) / (this_call - calls);
1833 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
1834 } else
1835 rpc = ipc = -1;
22fd3501 1836 iops = this_done - done;
f3057d26
JA
1837 if (bs > 1048576)
1838 bw = iops * (bs / 1048576);
1839 else
1840 bw = iops / (1048576 / bs);
4b9e13dc
JA
1841 if (iops > 1000000) {
1842 double miops = (double) iops / 1000000.0;
1843 printf("IOPS=%.2fM, ", miops);
1844 } else if (iops > 100000) {
1845 double kiops = (double) iops / 1000.0;
1846 printf("IOPS=%.2fK, ", kiops);
1847 } else {
53b5fa1e 1848 printf("IOPS=%lu, ", iops);
4b9e13dc 1849 }
16d25711 1850 max_iops = max(max_iops, iops);
55037c48
JA
1851 if (!do_nop) {
1852 if (bw > 2000) {
1853 double bw_g = (double) bw / 1000.0;
1854
1855 printf("BW=%.2fGiB/s, ", bw_g);
1856 } else {
1857 printf("BW=%luMiB/s, ", bw);
1858 }
1859 }
4b9e13dc 1860 printf("IOS/call=%ld/%ld\n", rpc, ipc);
c9fb4c5b
JA
1861 done = this_done;
1862 calls = this_call;
1863 reap = this_reap;
1864 } while (!finish);
1865
54319661
JA
1866 for (j = 0; j < nthreads; j++) {
1867 s = get_submitter(j);
1868 pthread_join(s->thread, &ret);
1869 close(s->ring_fd);
932131c9
JA
1870
1871 if (stats) {
1872 unsigned long nr;
1873
1874 printf("%d: Latency percentiles:\n", s->tid);
1875 for (i = 0, nr = 0; i < PLAT_NR; i++)
1876 nr += s->plat[i];
1877 show_clat_percentiles(s->plat, nr, 4);
1878 free(s->clock_batch);
1879 free(s->plat);
1880 }
54319661 1881 }
932131c9 1882
54319661 1883 free(submitter);
c9fb4c5b
JA
1884 return 0;
1885}