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