t/io_uring: make submitter_init() return < 0 on error
[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
223decdd 490 p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
b3915995
JA
491 if (!p)
492 return;
493
b3915995
JA
494 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
495 if (ret < 0)
496 goto out;
497
498 if (IORING_OP_READ > p->ops_len)
499 goto out;
500
501 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
84106576 502 vectored = 0;
b3915995
JA
503out:
504 free(p);
505}
506
c3e2fc25
JA
507static int io_uring_enter(struct submitter *s, unsigned int to_submit,
508 unsigned int min_complete, unsigned int flags)
c9fb4c5b 509{
ca8c91c5
JA
510 if (register_ring)
511 flags |= IORING_ENTER_REGISTERED_RING;
c377f4f8 512#ifdef FIO_ARCH_HAS_SYSCALL
ca8c91c5 513 return __do_syscall6(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
c377f4f8
JA
514 min_complete, flags, NULL, 0);
515#else
ca8c91c5
JA
516 return syscall(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
517 min_complete, flags, NULL, 0);
c377f4f8 518#endif
c9fb4c5b
JA
519}
520
701d1277
JA
521static unsigned file_depth(struct submitter *s)
522{
e39863e3 523 return (depth + s->nr_files - 1) / s->nr_files;
701d1277
JA
524}
525
501565a1
JA
526static unsigned long long get_offset(struct submitter *s, struct file *f)
527{
528 unsigned long long offset;
529 long r;
530
531 if (random_io) {
4d9521fd
JA
532 unsigned long long block;
533
501565a1 534 r = __rand64(&s->rand_state);
4d9521fd
JA
535 block = r % f->max_blocks;
536 offset = block * (unsigned long long) bs;
501565a1
JA
537 } else {
538 offset = f->cur_off;
539 f->cur_off += bs;
540 if (f->cur_off + bs > f->max_size)
541 f->cur_off = 0;
542 }
543
544 return offset;
545}
546
4ad09b56 547static struct file *init_new_io(struct submitter *s)
c9fb4c5b 548{
a7086591 549 struct file *f;
c9fb4c5b 550
701d1277
JA
551 if (s->nr_files == 1) {
552 f = &s->files[0];
553 } else {
554 f = &s->files[s->cur_file];
555 if (f->pending_ios >= file_depth(s)) {
556 s->cur_file++;
557 if (s->cur_file == s->nr_files)
558 s->cur_file = 0;
93d1811c 559 f = &s->files[s->cur_file];
701d1277
JA
560 }
561 }
4ad09b56 562
701d1277 563 f->pending_ios++;
4ad09b56
JA
564 return f;
565}
566
567static void init_io(struct submitter *s, unsigned index)
568{
569 struct io_uring_sqe *sqe = &s->sqes[index];
570 struct file *f;
571
572 if (do_nop) {
573 sqe->opcode = IORING_OP_NOP;
574 return;
575 }
576
577 f = init_new_io(s);
a7086591 578
8c5fa755
JA
579 if (register_files) {
580 sqe->flags = IOSQE_FIXED_FILE;
581 sqe->fd = f->fixed_fd;
582 } else {
583 sqe->flags = 0;
584 sqe->fd = f->real_fd;
585 }
f0403f94 586 if (fixedbufs) {
48e698fa 587 sqe->opcode = IORING_OP_READ_FIXED;
919850d2 588 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
5bd526f2 589 sqe->len = bs;
2ea53ca3 590 sqe->buf_index = index;
84106576 591 } else if (!vectored) {
b3915995
JA
592 sqe->opcode = IORING_OP_READ;
593 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
594 sqe->len = bs;
595 sqe->buf_index = 0;
84106576
JA
596 } else {
597 sqe->opcode = IORING_OP_READV;
598 sqe->addr = (unsigned long) &s->iovecs[index];
599 sqe->len = 1;
600 sqe->buf_index = 0;
f0403f94 601 }
f0403f94 602 sqe->ioprio = 0;
501565a1 603 sqe->off = get_offset(s, f);
932131c9 604 sqe->user_data = (unsigned long) f->fileno;
52479d8b 605 if (stats && stats_running)
bb209d68 606 sqe->user_data |= ((uint64_t)s->clock_index << 32);
c9fb4c5b
JA
607}
608
7d04588a
AG
609static void init_io_pt(struct submitter *s, unsigned index)
610{
611 struct io_uring_sqe *sqe = &s->sqes[index << 1];
612 unsigned long offset;
613 struct file *f;
614 struct nvme_uring_cmd *cmd;
615 unsigned long long slba;
616 unsigned long long nlb;
7d04588a 617
4ad09b56 618 f = init_new_io(s);
7d04588a 619
bb2d963c 620 offset = get_offset(s, f);
7d04588a
AG
621
622 if (register_files) {
623 sqe->fd = f->fixed_fd;
624 sqe->flags = IOSQE_FIXED_FILE;
625 } else {
626 sqe->fd = f->real_fd;
627 sqe->flags = 0;
628 }
629 sqe->opcode = IORING_OP_URING_CMD;
630 sqe->user_data = (unsigned long) f->fileno;
631 if (stats)
9ce84fbd 632 sqe->user_data |= ((__u64) s->clock_index << 32ULL);
7d04588a
AG
633 sqe->cmd_op = NVME_URING_CMD_IO;
634 slba = offset >> f->lba_shift;
635 nlb = (bs >> f->lba_shift) - 1;
636 cmd = (struct nvme_uring_cmd *)&sqe->cmd;
637 /* cdw10 and cdw11 represent starting slba*/
638 cmd->cdw10 = slba & 0xffffffff;
639 cmd->cdw11 = slba >> 32;
640 /* cdw12 represent number of lba to be read*/
641 cmd->cdw12 = nlb;
642 cmd->addr = (unsigned long) s->iovecs[index].iov_base;
643 cmd->data_len = bs;
021ce718
JA
644 if (fixedbufs) {
645 sqe->uring_cmd_flags = IORING_URING_CMD_FIXED;
646 sqe->buf_index = index;
647 }
7d04588a
AG
648 cmd->nsid = f->nsid;
649 cmd->opcode = 2;
650}
651
256714ea 652static int prep_more_ios_uring(struct submitter *s, int max_ios)
c9fb4c5b 653{
e31b8288 654 struct io_sq_ring *ring = &s->sq_ring;
c5f0a205
JA
655 unsigned head, index, tail, next_tail, prepped = 0;
656
657 if (sq_thread_poll)
658 head = atomic_load_acquire(ring->head);
659 else
660 head = *ring->head;
c9fb4c5b 661
c3e2fc25 662 next_tail = tail = *ring->tail;
c9fb4c5b
JA
663 do {
664 next_tail++;
6b6f52b9 665 if (next_tail == head)
c9fb4c5b
JA
666 break;
667
e39c34dc 668 index = tail & sq_ring_mask;
7d04588a
AG
669 if (pt)
670 init_io_pt(s, index);
671 else
672 init_io(s, index);
c9fb4c5b
JA
673 prepped++;
674 tail = next_tail;
675 } while (prepped < max_ios);
676
fc2dc21b
JA
677 if (prepped)
678 atomic_store_release(ring->tail, tail);
c9fb4c5b
JA
679 return prepped;
680}
681
a7086591 682static int get_file_size(struct file *f)
c9fb4c5b
JA
683{
684 struct stat st;
685
48e698fa 686 if (fstat(f->real_fd, &st) < 0)
c9fb4c5b 687 return -1;
7d04588a
AG
688 if (pt) {
689 __u64 nlba;
690 __u32 lbs;
691 int ret;
692
693 if (!S_ISCHR(st.st_mode)) {
694 fprintf(stderr, "passthrough works with only nvme-ns "
695 "generic devices (/dev/ngXnY)\n");
696 return -1;
697 }
698 ret = nvme_get_info(f->real_fd, &f->nsid, &lbs, &nlba);
699 if (ret)
700 return -1;
701 if ((bs % lbs) != 0) {
702 printf("error: bs:%d should be a multiple logical_block_size:%d\n",
703 bs, lbs);
704 return -1;
705 }
e2a4a77e 706 f->max_blocks = nlba;
7d04588a
AG
707 f->max_size = nlba;
708 f->lba_shift = ilog2(lbs);
709 return 0;
710 } else if (S_ISBLK(st.st_mode)) {
c9fb4c5b
JA
711 unsigned long long bytes;
712
48e698fa 713 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
c9fb4c5b
JA
714 return -1;
715
5bd526f2 716 f->max_blocks = bytes / bs;
beda9d8d 717 f->max_size = bytes;
c9fb4c5b
JA
718 return 0;
719 } else if (S_ISREG(st.st_mode)) {
5bd526f2 720 f->max_blocks = st.st_size / bs;
beda9d8d 721 f->max_size = st.st_size;
c9fb4c5b
JA
722 return 0;
723 }
724
725 return -1;
726}
727
256714ea 728static int reap_events_uring(struct submitter *s)
c9fb4c5b 729{
e31b8288 730 struct io_cq_ring *ring = &s->cq_ring;
f0403f94 731 struct io_uring_cqe *cqe;
e2239016 732 unsigned head, reaped = 0;
ab85494f 733 int last_idx = -1, stat_nr = 0;
c9fb4c5b 734
c3e2fc25 735 head = *ring->head;
c9fb4c5b 736 do {
701d1277
JA
737 struct file *f;
738
fc2dc21b 739 if (head == atomic_load_acquire(ring->tail))
c9fb4c5b 740 break;
f0403f94 741 cqe = &ring->cqes[head & cq_ring_mask];
8025517d 742 if (!do_nop) {
932131c9
JA
743 int fileno = cqe->user_data & 0xffffffff;
744
745 f = &s->files[fileno];
8025517d 746 f->pending_ios--;
5bd526f2 747 if (cqe->res != bs) {
8025517d 748 printf("io: unexpected ret=%d\n", cqe->res);
154a9582 749 if (polled && cqe->res == -EOPNOTSUPP)
8066f6b6 750 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
8025517d
JA
751 return -1;
752 }
c9fb4c5b 753 }
932131c9
JA
754 if (stats) {
755 int clock_index = cqe->user_data >> 32;
756
ab85494f
JA
757 if (last_idx != clock_index) {
758 if (last_idx != -1) {
759 add_stat(s, last_idx, stat_nr);
760 stat_nr = 0;
761 }
762 last_idx = clock_index;
d4af2ece
JA
763 }
764 stat_nr++;
932131c9 765 }
c9fb4c5b
JA
766 reaped++;
767 head++;
c9fb4c5b
JA
768 } while (1);
769
ab85494f
JA
770 if (stat_nr)
771 add_stat(s, last_idx, stat_nr);
772
fc2dc21b
JA
773 if (reaped) {
774 s->inflight -= reaped;
775 atomic_store_release(ring->head, head);
776 }
c9fb4c5b
JA
777 return reaped;
778}
779
7d04588a
AG
780static int reap_events_uring_pt(struct submitter *s)
781{
782 struct io_cq_ring *ring = &s->cq_ring;
783 struct io_uring_cqe *cqe;
784 unsigned head, reaped = 0;
785 int last_idx = -1, stat_nr = 0;
786 unsigned index;
787 int fileno;
788
789 head = *ring->head;
790 do {
791 struct file *f;
792
7d04588a
AG
793 if (head == atomic_load_acquire(ring->tail))
794 break;
795 index = head & cq_ring_mask;
796 cqe = &ring->cqes[index << 1];
797 fileno = cqe->user_data & 0xffffffff;
798 f = &s->files[fileno];
799 f->pending_ios--;
800
801 if (cqe->res != 0) {
802 printf("io: unexpected ret=%d\n", cqe->res);
803 if (polled && cqe->res == -EINVAL)
804 printf("passthrough doesn't support polled IO\n");
805 return -1;
806 }
807 if (stats) {
808 int clock_index = cqe->user_data >> 32;
809
810 if (last_idx != clock_index) {
811 if (last_idx != -1) {
812 add_stat(s, last_idx, stat_nr);
813 stat_nr = 0;
814 }
815 last_idx = clock_index;
816 }
817 stat_nr++;
818 }
819 reaped++;
820 head++;
821 } while (1);
822
823 if (stat_nr)
824 add_stat(s, last_idx, stat_nr);
825
826 if (reaped) {
827 s->inflight -= reaped;
828 atomic_store_release(ring->head, head);
829 }
830 return reaped;
831}
832
4b9e13dc
JA
833static void set_affinity(struct submitter *s)
834{
835#ifdef CONFIG_LIBNUMA
836 struct bitmask *mask;
837
838 if (s->numa_node == -1)
839 return;
840
841 numa_set_preferred(s->numa_node);
842
843 mask = numa_allocate_cpumask();
844 numa_node_to_cpus(s->numa_node, mask);
845 numa_sched_setaffinity(s->tid, mask);
846#endif
847}
848
849static int detect_node(struct submitter *s, const char *name)
850{
851#ifdef CONFIG_LIBNUMA
852 const char *base = basename(name);
853 char str[128];
854 int ret, fd, node;
855
cc791a99
JA
856 if (pt)
857 sprintf(str, "/sys/class/nvme-generic/%s/device/numa_node", base);
858 else
859 sprintf(str, "/sys/block/%s/device/numa_node", base);
4b9e13dc
JA
860 fd = open(str, O_RDONLY);
861 if (fd < 0)
862 return -1;
863
864 ret = read(fd, str, sizeof(str));
865 if (ret < 0) {
866 close(fd);
867 return -1;
868 }
869 node = atoi(str);
870 s->numa_node = node;
871 close(fd);
872#else
873 s->numa_node = -1;
874#endif
875 return 0;
876}
877
878static int setup_aio(struct submitter *s)
879{
880#ifdef CONFIG_LIBAIO
881 if (polled) {
882 fprintf(stderr, "aio does not support polled IO\n");
883 polled = 0;
884 }
885 if (sq_thread_poll) {
886 fprintf(stderr, "aio does not support SQPOLL IO\n");
887 sq_thread_poll = 0;
888 }
889 if (do_nop) {
890 fprintf(stderr, "aio does not support polled IO\n");
891 do_nop = 0;
892 }
893 if (fixedbufs || register_files) {
894 fprintf(stderr, "aio does not support registered files or buffers\n");
895 fixedbufs = register_files = 0;
896 }
897
898 return io_queue_init(roundup_pow2(depth), &s->aio_ctx);
899#else
900 fprintf(stderr, "Legacy AIO not available on this system/build\n");
901 errno = EINVAL;
902 return -1;
903#endif
904}
905
906static int setup_ring(struct submitter *s)
907{
908 struct io_sq_ring *sring = &s->sq_ring;
909 struct io_cq_ring *cring = &s->cq_ring;
910 struct io_uring_params p;
6b6f52b9 911 int ret, fd, i;
4b9e13dc 912 void *ptr;
7d04588a 913 size_t len;
4b9e13dc
JA
914
915 memset(&p, 0, sizeof(p));
916
917 if (polled && !do_nop)
918 p.flags |= IORING_SETUP_IOPOLL;
919 if (sq_thread_poll) {
920 p.flags |= IORING_SETUP_SQPOLL;
921 if (sq_thread_cpu != -1) {
922 p.flags |= IORING_SETUP_SQ_AFF;
923 p.sq_thread_cpu = sq_thread_cpu;
924 }
925 }
7d04588a
AG
926 if (pt) {
927 p.flags |= IORING_SETUP_SQE128;
928 p.flags |= IORING_SETUP_CQE32;
929 }
4b9e13dc
JA
930
931 fd = io_uring_setup(depth, &p);
932 if (fd < 0) {
933 perror("io_uring_setup");
934 return 1;
935 }
936 s->ring_fd = s->enter_ring_fd = fd;
937
938 io_uring_probe(fd);
939
940 if (fixedbufs) {
941 struct rlimit rlim;
942
943 rlim.rlim_cur = RLIM_INFINITY;
944 rlim.rlim_max = RLIM_INFINITY;
945 /* ignore potential error, not needed on newer kernels */
946 setrlimit(RLIMIT_MEMLOCK, &rlim);
947
948 ret = io_uring_register_buffers(s);
949 if (ret < 0) {
950 perror("io_uring_register_buffers");
951 return 1;
952 }
953
954 if (dma_map) {
955 ret = io_uring_map_buffers(s);
956 if (ret < 0) {
957 perror("io_uring_map_buffers");
958 return 1;
959 }
960 }
961 }
962
963 if (register_files) {
964 ret = io_uring_register_files(s);
965 if (ret < 0) {
966 perror("io_uring_register_files");
967 return 1;
968 }
969 }
970
971 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
972 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
973 IORING_OFF_SQ_RING);
974 sring->head = ptr + p.sq_off.head;
975 sring->tail = ptr + p.sq_off.tail;
976 sring->ring_mask = ptr + p.sq_off.ring_mask;
977 sring->ring_entries = ptr + p.sq_off.ring_entries;
978 sring->flags = ptr + p.sq_off.flags;
979 sring->array = ptr + p.sq_off.array;
980 sq_ring_mask = *sring->ring_mask;
981
7d04588a
AG
982 if (p.flags & IORING_SETUP_SQE128)
983 len = 2 * p.sq_entries * sizeof(struct io_uring_sqe);
984 else
985 len = p.sq_entries * sizeof(struct io_uring_sqe);
986 s->sqes = mmap(0, len,
4b9e13dc
JA
987 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
988 IORING_OFF_SQES);
989
7d04588a
AG
990 if (p.flags & IORING_SETUP_CQE32) {
991 len = p.cq_off.cqes +
992 2 * p.cq_entries * sizeof(struct io_uring_cqe);
993 } else {
994 len = p.cq_off.cqes +
995 p.cq_entries * sizeof(struct io_uring_cqe);
996 }
997 ptr = mmap(0, len,
4b9e13dc
JA
998 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
999 IORING_OFF_CQ_RING);
1000 cring->head = ptr + p.cq_off.head;
1001 cring->tail = ptr + p.cq_off.tail;
1002 cring->ring_mask = ptr + p.cq_off.ring_mask;
1003 cring->ring_entries = ptr + p.cq_off.ring_entries;
1004 cring->cqes = ptr + p.cq_off.cqes;
1005 cq_ring_mask = *cring->ring_mask;
6b6f52b9
JA
1006
1007 for (i = 0; i < p.sq_entries; i++)
1008 sring->array[i] = i;
1009
4b9e13dc
JA
1010 return 0;
1011}
1012
1013static void *allocate_mem(struct submitter *s, int size)
1014{
1015 void *buf;
1016
1017#ifdef CONFIG_LIBNUMA
1018 if (s->numa_node != -1)
1019 return numa_alloc_onnode(size, s->numa_node);
1020#endif
1021
c409e4c2 1022 if (posix_memalign(&buf, t_io_uring_page_size, bs)) {
4b9e13dc
JA
1023 printf("failed alloc\n");
1024 return NULL;
1025 }
1026
1027 return buf;
1028}
1029
256714ea 1030static int submitter_init(struct submitter *s)
c9fb4c5b 1031{
4b9e13dc
JA
1032 int i, nr_batch, err;
1033 static int init_printed;
1034 char buf[80];
932131c9 1035 s->tid = gettid();
4b9e13dc
JA
1036 printf("submitter=%d, tid=%d, file=%s, node=%d\n", s->index, s->tid,
1037 s->filename, s->numa_node);
1038
1039 set_affinity(s);
c9fb4c5b 1040
6243766b
KR
1041 __init_rand64(&s->rand_state, s->tid);
1042 srand48(s->tid);
c9fb4c5b 1043
932131c9
JA
1044 for (i = 0; i < MAX_FDS; i++)
1045 s->files[i].fileno = i;
1046
4b9e13dc
JA
1047 for (i = 0; i < roundup_pow2(depth); i++) {
1048 void *buf;
1049
1050 buf = allocate_mem(s, bs);
1051 if (!buf)
ae7ea7a4 1052 return -1;
4b9e13dc
JA
1053 s->iovecs[i].iov_base = buf;
1054 s->iovecs[i].iov_len = bs;
1055 }
1056
1057 if (use_sync) {
1058 sprintf(buf, "Engine=preadv2\n");
1059 err = 0;
1060 } else if (!aio) {
1061 err = setup_ring(s);
1062 sprintf(buf, "Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1063 } else {
1064 sprintf(buf, "Engine=aio\n");
1065 err = setup_aio(s);
1066 }
1067 if (err) {
1068 printf("queue setup failed: %s, %d\n", strerror(errno), err);
ae7ea7a4 1069 return -1;
4b9e13dc
JA
1070 }
1071
1072 if (!init_printed) {
1073 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
1074 printf("%s", buf);
1075 init_printed = 1;
1076 }
1077
932131c9
JA
1078 if (stats) {
1079 nr_batch = roundup_pow2(depth / batch_submit);
d4af2ece
JA
1080 if (nr_batch < 2)
1081 nr_batch = 2;
932131c9 1082 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
52479d8b 1083 s->clock_index = 1;
932131c9
JA
1084
1085 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
1086 } else {
1087 s->clock_batch = NULL;
1088 s->plat = NULL;
1089 nr_batch = 0;
1090 }
7d04588a
AG
1091 /* perform the expensive command initialization part for passthrough here
1092 * rather than in the fast path
1093 */
1094 if (pt) {
1095 for (i = 0; i < roundup_pow2(depth); i++) {
1096 struct io_uring_sqe *sqe = &s->sqes[i << 1];
932131c9 1097
7d04588a
AG
1098 memset(&sqe->cmd, 0, sizeof(struct nvme_uring_cmd));
1099 }
1100 }
256714ea
JA
1101 return nr_batch;
1102}
1103
1104#ifdef CONFIG_LIBAIO
1105static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
1106{
8310c570 1107 uint64_t data;
256714ea
JA
1108 struct file *f;
1109 unsigned index;
256714ea
JA
1110
1111 index = 0;
1112 while (index < max_ios) {
1113 struct iocb *iocb = &iocbs[index];
1114
4ad09b56 1115 f = init_new_io(s);
256714ea 1116
256714ea 1117 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
501565a1 1118 s->iovecs[index].iov_len, get_offset(s, f));
256714ea
JA
1119
1120 data = f->fileno;
52479d8b 1121 if (stats && stats_running)
8310c570 1122 data |= (((uint64_t) s->clock_index) << 32);
256714ea
JA
1123 iocb->data = (void *) (uintptr_t) data;
1124 index++;
1125 }
1126 return index;
1127}
1128
1129static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
1130{
1131 int last_idx = -1, stat_nr = 0;
1132 int reaped = 0;
1133
1134 while (evs) {
8310c570 1135 uint64_t data = (uintptr_t) events[reaped].data;
256714ea
JA
1136 struct file *f = &s->files[data & 0xffffffff];
1137
1138 f->pending_ios--;
1139 if (events[reaped].res != bs) {
1140 printf("io: unexpected ret=%ld\n", events[reaped].res);
1141 return -1;
1142 }
1143 if (stats) {
1144 int clock_index = data >> 32;
1145
1146 if (last_idx != clock_index) {
1147 if (last_idx != -1) {
1148 add_stat(s, last_idx, stat_nr);
1149 stat_nr = 0;
1150 }
1151 last_idx = clock_index;
d4af2ece
JA
1152 }
1153 stat_nr++;
256714ea
JA
1154 }
1155 reaped++;
1156 evs--;
1157 }
1158
1159 if (stat_nr)
1160 add_stat(s, last_idx, stat_nr);
1161
1162 s->inflight -= reaped;
1163 s->done += reaped;
1164 return reaped;
1165}
1166
1167static void *submitter_aio_fn(void *data)
1168{
1169 struct submitter *s = data;
71989c1b 1170 int i, ret, prepped;
256714ea
JA
1171 struct iocb **iocbsptr;
1172 struct iocb *iocbs;
1173 struct io_event *events;
71989c1b 1174#ifdef ARCH_HAVE_CPU_CLOCK
ae7ea7a4
JA
1175 int nr_batch;
1176#endif
1177
1178 ret = submitter_init(s);
1179 if (ret < 0)
1180 goto done;
1181
1182#ifdef ARCH_HAVE_CPU_CLOCK
1183 nr_batch = ret;
71989c1b 1184#endif
256714ea
JA
1185
1186 iocbsptr = calloc(depth, sizeof(struct iocb *));
1187 iocbs = calloc(depth, sizeof(struct iocb));
1188 events = calloc(depth, sizeof(struct io_event));
1189
1190 for (i = 0; i < depth; i++)
1191 iocbsptr[i] = &iocbs[i];
1192
1193 prepped = 0;
1194 do {
1195 int to_wait, to_submit, to_prep;
1196
1197 if (!prepped && s->inflight < depth) {
1198 to_prep = min(depth - s->inflight, batch_submit);
1199 prepped = prep_more_ios_aio(s, to_prep, iocbs);
1200#ifdef ARCH_HAVE_CPU_CLOCK
1201 if (prepped && stats) {
1202 s->clock_batch[s->clock_index] = get_cpu_clock();
1203 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1204 }
1205#endif
1206 }
1207 s->inflight += prepped;
1208 to_submit = prepped;
1209
1210 if (to_submit && (s->inflight + to_submit <= depth))
1211 to_wait = 0;
1212 else
1213 to_wait = min(s->inflight + to_submit, batch_complete);
1214
1215 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
1216 s->calls++;
1217 if (ret < 0) {
1218 perror("io_submit");
1219 break;
1220 } else if (ret != to_submit) {
1221 printf("submitted %d, wanted %d\n", ret, to_submit);
1222 break;
1223 }
1224 prepped = 0;
1225
24a24c12 1226 while (to_wait) {
256714ea
JA
1227 int r;
1228
24a24c12
JA
1229 s->calls++;
1230 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
1231 if (r < 0) {
1232 perror("io_getevents");
1233 break;
1234 } else if (r != to_wait) {
1235 printf("r=%d, wait=%d\n", r, to_wait);
1236 break;
1237 }
1238 r = reap_events_aio(s, events, r);
1239 s->reaps += r;
1240 to_wait -= r;
256714ea
JA
1241 }
1242 } while (!s->finish);
1243
1244 free(iocbsptr);
1245 free(iocbs);
1246 free(events);
ae7ea7a4 1247done:
256714ea
JA
1248 finish = 1;
1249 return NULL;
1250}
1251#endif
1252
ca8c91c5
JA
1253static void io_uring_unregister_ring(struct submitter *s)
1254{
1255 struct io_uring_rsrc_update up = {
1256 .offset = s->enter_ring_fd,
1257 };
1258
1259 syscall(__NR_io_uring_register, s->ring_fd, IORING_UNREGISTER_RING_FDS,
1260 &up, 1);
1261}
1262
1263static int io_uring_register_ring(struct submitter *s)
1264{
1265 struct io_uring_rsrc_update up = {
1266 .data = s->ring_fd,
1267 .offset = -1U,
1268 };
1269 int ret;
1270
1271 ret = syscall(__NR_io_uring_register, s->ring_fd,
1272 IORING_REGISTER_RING_FDS, &up, 1);
1273 if (ret == 1) {
1274 s->enter_ring_fd = up.offset;
1275 return 0;
1276 }
1277 register_ring = 0;
1278 return -1;
1279}
1280
256714ea
JA
1281static void *submitter_uring_fn(void *data)
1282{
1283 struct submitter *s = data;
1284 struct io_sq_ring *ring = &s->sq_ring;
b65c1fc0
JA
1285 int ret, prepped;
1286#ifdef ARCH_HAVE_CPU_CLOCK
ae7ea7a4
JA
1287 int nr_batch;
1288#endif
1289
1290 ret = submitter_init(s);
1291 if (ret < 0)
1292 goto done;
1293
1294#ifdef ARCH_HAVE_CPU_CLOCK
1295 nr_batch = ret;
b65c1fc0 1296#endif
256714ea 1297
ca8c91c5
JA
1298 if (register_ring)
1299 io_uring_register_ring(s);
1300
c9fb4c5b
JA
1301 prepped = 0;
1302 do {
f310970e 1303 int to_wait, to_submit, this_reap, to_prep;
fc2dc21b 1304 unsigned ring_flags = 0;
c9fb4c5b 1305
e39863e3
KB
1306 if (!prepped && s->inflight < depth) {
1307 to_prep = min(depth - s->inflight, batch_submit);
256714ea 1308 prepped = prep_more_ios_uring(s, to_prep);
932131c9
JA
1309#ifdef ARCH_HAVE_CPU_CLOCK
1310 if (prepped && stats) {
1311 s->clock_batch[s->clock_index] = get_cpu_clock();
1312 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1313 }
1314#endif
f310970e 1315 }
c9fb4c5b
JA
1316 s->inflight += prepped;
1317submit_more:
1318 to_submit = prepped;
1319submit:
e39863e3 1320 if (to_submit && (s->inflight + to_submit <= depth))
c9fb4c5b
JA
1321 to_wait = 0;
1322 else
e39863e3 1323 to_wait = min(s->inflight + to_submit, batch_complete);
c9fb4c5b 1324
ce1705de
JA
1325 /*
1326 * Only need to call io_uring_enter if we're not using SQ thread
1327 * poll, or if IORING_SQ_NEED_WAKEUP is set.
1328 */
fc2dc21b
JA
1329 if (sq_thread_poll)
1330 ring_flags = atomic_load_acquire(ring->flags);
1331 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
e0abe388
JA
1332 unsigned flags = 0;
1333
1334 if (to_wait)
1335 flags = IORING_ENTER_GETEVENTS;
fc2dc21b 1336 if (ring_flags & IORING_SQ_NEED_WAKEUP)
b532dd6d 1337 flags |= IORING_ENTER_SQ_WAKEUP;
e0abe388 1338 ret = io_uring_enter(s, to_submit, to_wait, flags);
ce1705de 1339 s->calls++;
fc2dc21b
JA
1340 } else {
1341 /* for SQPOLL, we submitted it all effectively */
1342 ret = to_submit;
ce1705de 1343 }
c9fb4c5b 1344
ce1705de
JA
1345 /*
1346 * For non SQ thread poll, we already got the events we needed
1347 * through the io_uring_enter() above. For SQ thread poll, we
1348 * need to loop here until we find enough events.
1349 */
1350 this_reap = 0;
1351 do {
1352 int r;
256714ea 1353
7d04588a
AG
1354 if (pt)
1355 r = reap_events_uring_pt(s);
1356 else
1357 r = reap_events_uring(s);
7d1435e6
JA
1358 if (r == -1) {
1359 s->finish = 1;
ce1705de 1360 break;
7d1435e6 1361 } else if (r > 0)
ce1705de
JA
1362 this_reap += r;
1363 } while (sq_thread_poll && this_reap < to_wait);
c9fb4c5b
JA
1364 s->reaps += this_reap;
1365
1366 if (ret >= 0) {
1367 if (!ret) {
1368 to_submit = 0;
1369 if (s->inflight)
1370 goto submit;
1371 continue;
1372 } else if (ret < to_submit) {
1373 int diff = to_submit - ret;
1374
1375 s->done += ret;
1376 prepped -= diff;
1377 goto submit_more;
1378 }
1379 s->done += ret;
1380 prepped = 0;
1381 continue;
1382 } else if (ret < 0) {
ac122fea 1383 if (errno == EAGAIN) {
c9fb4c5b
JA
1384 if (s->finish)
1385 break;
1386 if (this_reap)
1387 goto submit;
c9fb4c5b
JA
1388 to_submit = 0;
1389 goto submit;
1390 }
ac122fea 1391 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
1392 break;
1393 }
1394 } while (!s->finish);
a7086591 1395
ca8c91c5
JA
1396 if (register_ring)
1397 io_uring_unregister_ring(s);
1398
ae7ea7a4 1399done:
c9fb4c5b
JA
1400 finish = 1;
1401 return NULL;
1402}
1403
a7648136 1404#ifdef CONFIG_PWRITEV2
379406bc
JA
1405static void *submitter_sync_fn(void *data)
1406{
1407 struct submitter *s = data;
1408 int ret;
1409
ae7ea7a4
JA
1410 if (submitter_init(s) < 0)
1411 goto done;
379406bc
JA
1412
1413 do {
1414 uint64_t offset;
1415 struct file *f;
379406bc 1416
4ad09b56 1417 f = init_new_io(s);
379406bc 1418
379406bc
JA
1419#ifdef ARCH_HAVE_CPU_CLOCK
1420 if (stats)
1421 s->clock_batch[s->clock_index] = get_cpu_clock();
1422#endif
1423
1424 s->inflight++;
1425 s->calls++;
1426
501565a1 1427 offset = get_offset(s, f);
379406bc
JA
1428 if (polled)
1429 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, RWF_HIPRI);
1430 else
1431 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, 0);
1432
1433 if (ret < 0) {
1434 perror("preadv2");
1435 break;
1436 } else if (ret != bs) {
1437 break;
1438 }
1439
1440 s->done++;
1441 s->inflight--;
1442 f->pending_ios--;
1443 if (stats)
1444 add_stat(s, s->clock_index, 1);
1445 } while (!s->finish);
1446
ae7ea7a4 1447done:
379406bc
JA
1448 finish = 1;
1449 return NULL;
1450}
a7648136
JA
1451#else
1452static void *submitter_sync_fn(void *data)
1453{
1454 finish = 1;
1455 return NULL;
1456}
1457#endif
379406bc 1458
54319661
JA
1459static struct submitter *get_submitter(int offset)
1460{
1461 void *ret;
1462
1463 ret = submitter;
1464 if (offset)
1465 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
1466 return ret;
1467}
1468
65e1a5e8 1469static void do_finish(const char *reason)
c9fb4c5b 1470{
54319661 1471 int j;
4b9e13dc 1472
65e1a5e8 1473 printf("Exiting on %s\n", reason);
54319661
JA
1474 for (j = 0; j < nthreads; j++) {
1475 struct submitter *s = get_submitter(j);
1476 s->finish = 1;
1477 }
4b9e13dc
JA
1478 if (max_iops > 1000000) {
1479 double miops = (double) max_iops / 1000000.0;
1480 printf("Maximum IOPS=%.2fM\n", miops);
1481 } else if (max_iops > 100000) {
1482 double kiops = (double) max_iops / 1000.0;
1483 printf("Maximum IOPS=%.2fK\n", kiops);
1484 } else {
18b557a0 1485 printf("Maximum IOPS=%lu\n", max_iops);
4b9e13dc 1486 }
c9fb4c5b
JA
1487 finish = 1;
1488}
1489
65e1a5e8
EV
1490static void sig_int(int sig)
1491{
1492 do_finish("signal");
1493}
1494
c9fb4c5b
JA
1495static void arm_sig_int(void)
1496{
1497 struct sigaction act;
1498
1499 memset(&act, 0, sizeof(act));
1500 act.sa_handler = sig_int;
1501 act.sa_flags = SA_RESTART;
1502 sigaction(SIGINT, &act, NULL);
2cf71009
BP
1503
1504 /* Windows uses SIGBREAK as a quit signal from other applications */
1505#ifdef WIN32
1506 sigaction(SIGBREAK, &act, NULL);
1507#endif
c9fb4c5b
JA
1508}
1509
d79ff8c9 1510static void usage(char *argv, int status)
e39863e3 1511{
65e1a5e8
EV
1512 char runtime_str[16];
1513 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
e39863e3 1514 printf("%s [options] -- [filenames]\n"
35268e11
AJ
1515 " -d <int> : IO Depth, default %d\n"
1516 " -s <int> : Batch submit, default %d\n"
1517 " -c <int> : Batch complete, default %d\n"
1518 " -b <int> : Block size, default %d\n"
1519 " -p <bool> : Polled IO, default %d\n"
1520 " -B <bool> : Fixed buffers, default %d\n"
dd1f1ba0 1521 " -D <bool> : DMA map fixed buffers, default %d\n"
35268e11 1522 " -F <bool> : Register files, default %d\n"
0862f718 1523 " -n <int> : Number of threads, default %d\n"
2686fc22 1524 " -O <bool> : Use O_DIRECT, default %d\n"
4a39c524
EV
1525 " -N <bool> : Perform just no-op requests, default %d\n"
1526 " -t <bool> : Track IO latencies, default %d\n"
256714ea 1527 " -T <int> : TSC rate in HZ\n"
beda9d8d
JA
1528 " -r <int> : Runtime in seconds, default %s\n"
1529 " -R <bool> : Use random IO, default %d\n"
ca8c91c5 1530 " -a <bool> : Use legacy aio, default %d\n"
3be2f0ca 1531 " -S <bool> : Use sync IO (preadv2), default %d\n"
4b9e13dc 1532 " -X <bool> : Use registered ring %d\n"
7d04588a
AG
1533 " -P <bool> : Automatically place on device home node %d\n"
1534 " -u <bool> : Use nvme-passthrough I/O, default %d\n",
35268e11 1535 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
a71ad043 1536 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
ca8c91c5 1537 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio,
7d04588a 1538 use_sync, register_ring, numa_placement, pt);
d79ff8c9 1539 exit(status);
e39863e3
KB
1540}
1541
203e4c26
JA
1542static void read_tsc_rate(void)
1543{
1544 char buffer[32];
1545 int fd, ret;
1546
1547 if (tsc_rate)
1548 return;
1549
1550 fd = open(TSC_RATE_FILE, O_RDONLY);
1551 if (fd < 0)
1552 return;
1553
1554 ret = read(fd, buffer, sizeof(buffer));
1555 if (ret < 0) {
1556 close(fd);
1557 return;
1558 }
1559
1560 tsc_rate = strtoul(buffer, NULL, 10);
1561 printf("Using TSC rate %luHz\n", tsc_rate);
1562 close(fd);
1563}
1564
1565static void write_tsc_rate(void)
1566{
1567 char buffer[32];
1568 struct stat sb;
1569 int fd, ret;
1570
1571 if (!stat(TSC_RATE_FILE, &sb))
1572 return;
1573
1574 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1575 if (fd < 0)
1576 return;
1577
1578 memset(buffer, 0, sizeof(buffer));
1579 sprintf(buffer, "%lu", tsc_rate);
1580 ret = write(fd, buffer, strlen(buffer));
1581 if (ret < 0)
1582 perror("write");
1583 close(fd);
1584}
1585
c9fb4c5b
JA
1586int main(int argc, char *argv[])
1587{
e39863e3 1588 struct submitter *s;
05138221 1589 unsigned long done, calls, reap;
4b9e13dc 1590 int i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
d79ff8c9 1591 struct file f;
c3e2fc25 1592 void *ret;
c9fb4c5b 1593
0862f718
JA
1594 if (!do_nop && argc < 2)
1595 usage(argv[0], 1);
c9fb4c5b 1596
7d04588a 1597 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 1598 switch (opt) {
256714ea
JA
1599 case 'a':
1600 aio = !!atoi(optarg);
1601 break;
e39863e3
KB
1602 case 'd':
1603 depth = atoi(optarg);
1604 break;
1605 case 's':
1606 batch_submit = atoi(optarg);
932131c9
JA
1607 if (!batch_submit)
1608 batch_submit = 1;
e39863e3
KB
1609 break;
1610 case 'c':
1611 batch_complete = atoi(optarg);
932131c9
JA
1612 if (!batch_complete)
1613 batch_complete = 1;
e39863e3 1614 break;
5bd526f2
JA
1615 case 'b':
1616 bs = atoi(optarg);
1617 break;
1618 case 'p':
1619 polled = !!atoi(optarg);
1620 break;
6a87c3b0
JA
1621 case 'B':
1622 fixedbufs = !!atoi(optarg);
1623 break;
1624 case 'F':
1625 register_files = !!atoi(optarg);
1626 break;
54319661
JA
1627 case 'n':
1628 nthreads = atoi(optarg);
caf2b9ac
JA
1629 if (!nthreads) {
1630 printf("Threads must be non-zero\n");
1631 usage(argv[0], 1);
1632 }
54319661 1633 break;
0862f718
JA
1634 case 'N':
1635 do_nop = !!atoi(optarg);
1636 break;
2686fc22
JA
1637 case 'O':
1638 buffered = !atoi(optarg);
1639 break;
932131c9
JA
1640 case 't':
1641#ifndef ARCH_HAVE_CPU_CLOCK
1642 fprintf(stderr, "Stats not supported on this CPU\n");
1643 return 1;
1644#endif
1645 stats = !!atoi(optarg);
1646 break;
1647 case 'T':
1648#ifndef ARCH_HAVE_CPU_CLOCK
1649 fprintf(stderr, "Stats not supported on this CPU\n");
1650 return 1;
1651#endif
1652 tsc_rate = strtoul(optarg, NULL, 10);
203e4c26 1653 write_tsc_rate();
932131c9 1654 break;
65e1a5e8
EV
1655 case 'r':
1656 runtime = atoi(optarg);
1657 break;
a71ad043
JA
1658 case 'D':
1659 dma_map = !!atoi(optarg);
1660 break;
beda9d8d
JA
1661 case 'R':
1662 random_io = !!atoi(optarg);
1663 break;
ca8c91c5
JA
1664 case 'X':
1665 register_ring = !!atoi(optarg);
1666 break;
379406bc 1667 case 'S':
a7648136 1668#ifdef CONFIG_PWRITEV2
379406bc 1669 use_sync = !!atoi(optarg);
a7648136
JA
1670#else
1671 fprintf(stderr, "preadv2 not supported\n");
1672 exit(1);
1673#endif
379406bc 1674 break;
4b9e13dc
JA
1675 case 'P':
1676 numa_placement = !!atoi(optarg);
1677 break;
7d04588a
AG
1678 case 'u':
1679 pt = !!atoi(optarg);
1680 break;
e39863e3
KB
1681 case 'h':
1682 case '?':
1683 default:
d79ff8c9 1684 usage(argv[0], 0);
e39863e3
KB
1685 break;
1686 }
1687 }
1688
203e4c26
JA
1689 if (stats)
1690 read_tsc_rate();
1691
c5347611
JA
1692 if (batch_complete > depth)
1693 batch_complete = depth;
1694 if (batch_submit > depth)
1695 batch_submit = depth;
a71ad043
JA
1696 if (!fixedbufs && dma_map)
1697 dma_map = 0;
c5347611 1698
54319661 1699 submitter = calloc(nthreads, sizeof(*submitter) +
55845033 1700 roundup_pow2(depth) * sizeof(struct iovec));
54319661
JA
1701 for (j = 0; j < nthreads; j++) {
1702 s = get_submitter(j);
4b9e13dc 1703 s->numa_node = -1;
54319661
JA
1704 s->index = j;
1705 s->done = s->calls = s->reaps = 0;
1706 }
e39863e3 1707
701d1277 1708 flags = O_RDONLY | O_NOATIME;
a7086591
JA
1709 if (!buffered)
1710 flags |= O_DIRECT;
1711
54319661 1712 j = 0;
e39863e3 1713 i = optind;
d79ff8c9 1714 nfiles = argc - i;
2ac00585
JA
1715 if (!do_nop) {
1716 if (!nfiles) {
1717 printf("No files specified\n");
1718 usage(argv[0], 1);
1719 }
1720 threads_per_f = nthreads / nfiles;
1721 /* make sure each thread gets assigned files */
1722 if (threads_per_f == 0) {
1723 threads_per_f = 1;
1724 } else {
1725 threads_rem = nthreads - threads_per_f * nfiles;
1726 }
d79ff8c9 1727 }
8025517d 1728 while (!do_nop && i < argc) {
d79ff8c9
AJ
1729 int k, limit;
1730
1731 memset(&f, 0, sizeof(f));
a7086591
JA
1732
1733 fd = open(argv[i], flags);
1734 if (fd < 0) {
1735 perror("open");
1736 return 1;
1737 }
d79ff8c9
AJ
1738 f.real_fd = fd;
1739 if (get_file_size(&f)) {
a7086591
JA
1740 printf("failed getting size of device/file\n");
1741 return 1;
1742 }
d79ff8c9 1743 if (f.max_blocks <= 1) {
a7086591
JA
1744 printf("Zero file/device size?\n");
1745 return 1;
1746 }
d79ff8c9
AJ
1747 f.max_blocks--;
1748
1749 limit = threads_per_f;
1750 limit += threads_rem > 0 ? 1 : 0;
1751 for (k = 0; k < limit; k++) {
1752 s = get_submitter((j + k) % nthreads);
a7086591 1753
d79ff8c9
AJ
1754 if (s->nr_files == MAX_FDS) {
1755 printf("Max number of files (%d) reached\n", MAX_FDS);
1756 break;
1757 }
1758
1759 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1760
4b9e13dc
JA
1761 if (numa_placement)
1762 detect_node(s, argv[i]);
1763
1764 s->filename = argv[i];
d79ff8c9
AJ
1765 s->nr_files++;
1766 }
1767 threads_rem--;
a7086591 1768 i++;
d79ff8c9 1769 j += limit;
a7086591
JA
1770 }
1771
c9fb4c5b
JA
1772 arm_sig_int();
1773
c409e4c2
AG
1774 t_io_uring_page_size = sysconf(_SC_PAGESIZE);
1775 if (t_io_uring_page_size < 0)
1776 t_io_uring_page_size = 4096;
a0639afe 1777
54319661
JA
1778 for (j = 0; j < nthreads; j++) {
1779 s = get_submitter(j);
379406bc
JA
1780 if (use_sync)
1781 pthread_create(&s->thread, NULL, submitter_sync_fn, s);
1782 else if (!aio)
256714ea
JA
1783 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1784#ifdef CONFIG_LIBAIO
1785 else
1786 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1787#endif
54319661 1788 }
c9fb4c5b 1789
05138221 1790 reap = calls = done = 0;
c9fb4c5b
JA
1791 do {
1792 unsigned long this_done = 0;
1793 unsigned long this_reap = 0;
1794 unsigned long this_call = 0;
1795 unsigned long rpc = 0, ipc = 0;
f3057d26 1796 unsigned long iops, bw;
c9fb4c5b
JA
1797
1798 sleep(1);
65e1a5e8
EV
1799 if (runtime && !--runtime)
1800 do_finish("timeout");
a1f17100
JA
1801
1802 /* don't print partial run, if interrupted by signal */
1803 if (finish)
1804 break;
52479d8b
JA
1805
1806 /* one second in to the run, enable stats */
1807 if (stats)
1808 stats_running = 1;
1809
54319661 1810 for (j = 0; j < nthreads; j++) {
7d1ce4b7 1811 s = get_submitter(j);
54319661
JA
1812 this_done += s->done;
1813 this_call += s->calls;
1814 this_reap += s->reaps;
1815 }
c9fb4c5b
JA
1816 if (this_call - calls) {
1817 rpc = (this_done - done) / (this_call - calls);
1818 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
1819 } else
1820 rpc = ipc = -1;
22fd3501 1821 iops = this_done - done;
f3057d26
JA
1822 if (bs > 1048576)
1823 bw = iops * (bs / 1048576);
1824 else
1825 bw = iops / (1048576 / bs);
4b9e13dc
JA
1826 if (iops > 1000000) {
1827 double miops = (double) iops / 1000000.0;
1828 printf("IOPS=%.2fM, ", miops);
1829 } else if (iops > 100000) {
1830 double kiops = (double) iops / 1000.0;
1831 printf("IOPS=%.2fK, ", kiops);
1832 } else {
53b5fa1e 1833 printf("IOPS=%lu, ", iops);
4b9e13dc 1834 }
16d25711 1835 max_iops = max(max_iops, iops);
55037c48
JA
1836 if (!do_nop) {
1837 if (bw > 2000) {
1838 double bw_g = (double) bw / 1000.0;
1839
1840 printf("BW=%.2fGiB/s, ", bw_g);
1841 } else {
1842 printf("BW=%luMiB/s, ", bw);
1843 }
1844 }
4b9e13dc 1845 printf("IOS/call=%ld/%ld\n", rpc, ipc);
c9fb4c5b
JA
1846 done = this_done;
1847 calls = this_call;
1848 reap = this_reap;
1849 } while (!finish);
1850
54319661
JA
1851 for (j = 0; j < nthreads; j++) {
1852 s = get_submitter(j);
1853 pthread_join(s->thread, &ret);
1854 close(s->ring_fd);
932131c9
JA
1855
1856 if (stats) {
1857 unsigned long nr;
1858
1859 printf("%d: Latency percentiles:\n", s->tid);
1860 for (i = 0, nr = 0; i < PLAT_NR; i++)
1861 nr += s->plat[i];
1862 show_clat_percentiles(s->plat, nr, 4);
1863 free(s->clock_batch);
1864 free(s->plat);
1865 }
54319661 1866 }
932131c9 1867
54319661 1868 free(submitter);
c9fb4c5b
JA
1869 return 0;
1870}