18 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/syscall.h>
22 #include <sys/resource.h>
32 #include "../arch/arch.h"
34 #include "../lib/types.h"
35 #include "../lib/roundup.h"
36 #include "../lib/rand.h"
37 #include "../minmax.h"
38 #include "../os/linux/io_uring.h"
39 #include "../engines/nvme.h"
45 unsigned *ring_entries;
54 unsigned *ring_entries;
55 struct io_uring_cqe *cqes;
59 #define BATCH_SUBMIT 32
60 #define BATCH_COMPLETE 32
65 static unsigned sq_ring_mask, cq_ring_mask;
68 unsigned long max_blocks;
69 unsigned long max_size;
70 unsigned long cur_off;
72 unsigned int nsid; /* nsid field required for nvme-passthrough */
73 unsigned int lba_shift; /* lba_shift field required for nvme-passthrough */
80 #define PLAT_VAL (1 << PLAT_BITS)
81 #define PLAT_GROUP_NR 29
82 #define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
89 struct io_sq_ring sq_ring;
90 struct io_uring_sqe *sqes;
91 struct io_cq_ring cq_ring;
101 struct taus258_state rand_state;
103 unsigned long *clock_batch;
108 io_context_t aio_ctx;
112 const char *filename;
114 struct file files[MAX_FDS];
117 struct iovec iovecs[];
120 static struct submitter *submitter;
121 static volatile int finish;
122 static int stats_running;
123 static unsigned long max_iops;
124 static long t_io_uring_page_size;
126 static int depth = DEPTH;
127 static int batch_submit = BATCH_SUBMIT;
128 static int batch_complete = BATCH_COMPLETE;
130 static int polled = 1; /* use IO polling */
131 static int fixedbufs = 1; /* use fixed user buffers */
132 static int register_files = 1; /* use fixed files */
133 static int buffered = 0; /* use buffered IO, not O_DIRECT */
134 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
135 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
136 static int do_nop = 0; /* no-op SQ ring commands */
137 static int nthreads = 1;
138 static int stats = 0; /* generate IO stats */
139 static int aio = 0; /* use libaio */
140 static int runtime = 0; /* runtime */
141 static int random_io = 1; /* random or sequential IO */
142 static int register_ring = 1; /* register ring */
143 static int use_sync = 0; /* use preadv2 */
144 static int numa_placement = 0; /* set to node of device */
145 static int pt = 0; /* passthrough I/O or not */
147 static unsigned long tsc_rate;
149 #define TSC_RATE_FILE "tsc-rate"
151 static int vectored = 1;
153 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
154 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
155 static int plist_len = 17;
157 static int nvme_identify(int fd, __u32 nsid, enum nvme_identify_cns cns,
158 enum nvme_csi csi, void *data)
160 struct nvme_passthru_cmd cmd = {
161 .opcode = nvme_admin_identify,
163 .addr = (__u64)(uintptr_t)data,
164 .data_len = NVME_IDENTIFY_DATA_SIZE,
166 .cdw11 = csi << NVME_IDENTIFY_CSI_SHIFT,
167 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
170 return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
173 static int nvme_get_info(int fd, __u32 *nsid, __u32 *lba_sz, __u64 *nlba)
175 struct nvme_id_ns ns;
179 namespace_id = ioctl(fd, NVME_IOCTL_ID);
180 if (namespace_id < 0) {
181 fprintf(stderr, "error failed to fetch namespace-id\n");
187 * Identify namespace to get namespace-id, namespace size in LBA's
190 err = nvme_identify(fd, namespace_id, NVME_IDENTIFY_CNS_NS,
193 fprintf(stderr, "error failed to fetch identify namespace\n");
198 *nsid = namespace_id;
199 *lba_sz = 1 << ns.lbaf[(ns.flbas & 0x0f)].ds;
205 static unsigned long cycles_to_nsec(unsigned long cycles)
212 val = cycles * 1000000000ULL;
213 return val / tsc_rate;
216 static unsigned long plat_idx_to_val(unsigned int idx)
218 unsigned int error_bits;
219 unsigned long k, base;
221 assert(idx < PLAT_NR);
223 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
224 * all bits of the sample as index */
225 if (idx < (PLAT_VAL << 1))
226 return cycles_to_nsec(idx);
228 /* Find the group and compute the minimum value of that group */
229 error_bits = (idx >> PLAT_BITS) - 1;
230 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
232 /* Find its bucket number of the group */
235 /* Return the mean of the range of the bucket */
236 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
239 unsigned int calculate_clat_percentiles(unsigned long *io_u_plat,
240 unsigned long nr, unsigned long **output,
241 unsigned long *maxv, unsigned long *minv)
243 unsigned long sum = 0;
244 unsigned int len = plist_len, i, j = 0;
245 unsigned long *ovals = NULL;
251 ovals = malloc(len * sizeof(*ovals));
256 * Calculate bucket values, note down max and min values
259 for (i = 0; i < PLAT_NR && !is_last; i++) {
261 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
262 assert(plist[j] <= 100.0);
264 ovals[j] = plat_idx_to_val(i);
265 if (ovals[j] < *minv)
267 if (ovals[j] > *maxv)
270 is_last = (j == len - 1) != 0;
279 fprintf(stderr, "error calculating latency percentiles\n");
285 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
286 unsigned int precision)
288 unsigned int divisor, len, i, j = 0;
289 unsigned long minv, maxv;
290 unsigned long *ovals;
291 int per_line, scale_down, time_width;
295 len = calculate_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
302 printf(" percentiles (tsc ticks):\n |");
303 } else if (minv > 2000 && maxv > 99999) {
306 printf(" percentiles (usec):\n |");
310 printf(" percentiles (nsec):\n |");
313 time_width = max(5, (int) (log10(maxv / divisor) + 1));
314 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
315 precision, time_width);
316 /* fmt will be something like " %5.2fth=[%4llu]%c" */
317 per_line = (80 - 7) / (precision + 10 + time_width);
319 for (j = 0; j < len; j++) {
321 if (j != 0 && (j % per_line) == 0)
324 /* end of the list */
325 is_last = (j == len - 1) != 0;
327 for (i = 0; i < scale_down; i++)
328 ovals[j] = (ovals[j] + 999) / 1000;
330 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
335 if ((j % per_line) == per_line - 1) /* for formatting */
343 #ifdef ARCH_HAVE_CPU_CLOCK
344 static unsigned int plat_val_to_idx(unsigned long val)
346 unsigned int msb, error_bits, base, offset, idx;
348 /* Find MSB starting from bit 0 */
352 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
355 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
356 * all bits of the sample as index
358 if (msb <= PLAT_BITS)
361 /* Compute the number of error bits to discard*/
362 error_bits = msb - PLAT_BITS;
364 /* Compute the number of buckets before the group */
365 base = (error_bits + 1) << PLAT_BITS;
368 * Discard the error bits and apply the mask to find the
369 * index for the buckets in the group
371 offset = (PLAT_VAL - 1) & (val >> error_bits);
373 /* Make sure the index does not exceed (array size - 1) */
374 idx = (base + offset) < (PLAT_NR - 1) ?
375 (base + offset) : (PLAT_NR - 1);
381 static void add_stat(struct submitter *s, int clock_index, int nr)
383 #ifdef ARCH_HAVE_CPU_CLOCK
384 unsigned long cycles;
387 if (!s->finish && clock_index) {
388 cycles = get_cpu_clock();
389 cycles -= s->clock_batch[clock_index];
390 pidx = plat_val_to_idx(cycles);
396 static int io_uring_register_buffers(struct submitter *s)
401 return syscall(__NR_io_uring_register, s->ring_fd,
402 IORING_REGISTER_BUFFERS, s->iovecs, roundup_pow2(depth));
405 static int io_uring_register_files(struct submitter *s)
412 s->fds = calloc(s->nr_files, sizeof(__s32));
413 for (i = 0; i < s->nr_files; i++) {
414 s->fds[i] = s->files[i].real_fd;
415 s->files[i].fixed_fd = i;
418 return syscall(__NR_io_uring_register, s->ring_fd,
419 IORING_REGISTER_FILES, s->fds, s->nr_files);
422 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
427 * Clamp CQ ring size at our SQ ring size, we don't need more entries
430 p->flags |= IORING_SETUP_CQSIZE;
431 p->cq_entries = entries;
433 p->flags |= IORING_SETUP_COOP_TASKRUN;
434 p->flags |= IORING_SETUP_SINGLE_ISSUER;
435 p->flags |= IORING_SETUP_DEFER_TASKRUN;
437 ret = syscall(__NR_io_uring_setup, entries, p);
441 if (errno == EINVAL && p->flags & IORING_SETUP_COOP_TASKRUN) {
442 p->flags &= ~IORING_SETUP_COOP_TASKRUN;
445 if (errno == EINVAL && p->flags & IORING_SETUP_SINGLE_ISSUER) {
446 p->flags &= ~IORING_SETUP_SINGLE_ISSUER;
449 if (errno == EINVAL && p->flags & IORING_SETUP_DEFER_TASKRUN) {
450 p->flags &= ~IORING_SETUP_DEFER_TASKRUN;
457 static void io_uring_probe(int fd)
459 struct io_uring_probe *p;
462 p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
466 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
470 if (IORING_OP_READ > p->ops_len)
473 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
479 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
480 unsigned int min_complete, unsigned int flags)
483 flags |= IORING_ENTER_REGISTERED_RING;
484 #ifdef FIO_ARCH_HAS_SYSCALL
485 return __do_syscall6(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
486 min_complete, flags, NULL, 0);
488 return syscall(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
489 min_complete, flags, NULL, 0);
493 static unsigned file_depth(struct submitter *s)
495 return (depth + s->nr_files - 1) / s->nr_files;
498 static unsigned long long get_offset(struct submitter *s, struct file *f)
500 unsigned long long offset;
504 unsigned long long block;
506 r = __rand64(&s->rand_state);
507 block = r % f->max_blocks;
508 offset = block * (unsigned long long) bs;
512 if (f->cur_off + bs > f->max_size)
519 static struct file *init_new_io(struct submitter *s)
523 if (s->nr_files == 1) {
526 f = &s->files[s->cur_file];
527 if (f->pending_ios >= file_depth(s)) {
529 if (s->cur_file == s->nr_files)
531 f = &s->files[s->cur_file];
539 static void init_io(struct submitter *s, unsigned index)
541 struct io_uring_sqe *sqe = &s->sqes[index];
545 sqe->opcode = IORING_OP_NOP;
551 if (register_files) {
552 sqe->flags = IOSQE_FIXED_FILE;
553 sqe->fd = f->fixed_fd;
556 sqe->fd = f->real_fd;
559 sqe->opcode = IORING_OP_READ_FIXED;
560 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
562 sqe->buf_index = index;
563 } else if (!vectored) {
564 sqe->opcode = IORING_OP_READ;
565 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
569 sqe->opcode = IORING_OP_READV;
570 sqe->addr = (unsigned long) &s->iovecs[index];
575 sqe->off = get_offset(s, f);
576 sqe->user_data = (unsigned long) f->fileno;
577 if (stats && stats_running)
578 sqe->user_data |= ((uint64_t)s->clock_index << 32);
581 static void init_io_pt(struct submitter *s, unsigned index)
583 struct io_uring_sqe *sqe = &s->sqes[index << 1];
584 unsigned long offset;
586 struct nvme_uring_cmd *cmd;
587 unsigned long long slba;
588 unsigned long long nlb;
592 offset = get_offset(s, f);
594 if (register_files) {
595 sqe->fd = f->fixed_fd;
596 sqe->flags = IOSQE_FIXED_FILE;
598 sqe->fd = f->real_fd;
601 sqe->opcode = IORING_OP_URING_CMD;
602 sqe->user_data = (unsigned long) f->fileno;
604 sqe->user_data |= ((__u64) s->clock_index << 32ULL);
605 sqe->cmd_op = NVME_URING_CMD_IO;
606 slba = offset >> f->lba_shift;
607 nlb = (bs >> f->lba_shift) - 1;
608 cmd = (struct nvme_uring_cmd *)&sqe->cmd;
609 /* cdw10 and cdw11 represent starting slba*/
610 cmd->cdw10 = slba & 0xffffffff;
611 cmd->cdw11 = slba >> 32;
612 /* cdw12 represent number of lba to be read*/
614 cmd->addr = (unsigned long) s->iovecs[index].iov_base;
617 sqe->uring_cmd_flags = IORING_URING_CMD_FIXED;
618 sqe->buf_index = index;
624 static int prep_more_ios_uring(struct submitter *s, int max_ios)
626 struct io_sq_ring *ring = &s->sq_ring;
627 unsigned head, index, tail, next_tail, prepped = 0;
630 head = atomic_load_acquire(ring->head);
634 next_tail = tail = *ring->tail;
637 if (next_tail == head)
640 index = tail & sq_ring_mask;
642 init_io_pt(s, index);
647 } while (prepped < max_ios);
650 atomic_store_release(ring->tail, tail);
654 static int get_file_size(struct file *f)
658 if (fstat(f->real_fd, &st) < 0)
665 if (!S_ISCHR(st.st_mode)) {
666 fprintf(stderr, "passthrough works with only nvme-ns "
667 "generic devices (/dev/ngXnY)\n");
670 ret = nvme_get_info(f->real_fd, &f->nsid, &lbs, &nlba);
673 if ((bs % lbs) != 0) {
674 printf("error: bs:%d should be a multiple logical_block_size:%d\n",
678 f->max_blocks = nlba;
680 f->lba_shift = ilog2(lbs);
682 } else if (S_ISBLK(st.st_mode)) {
683 unsigned long long bytes;
685 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
688 f->max_blocks = bytes / bs;
691 } else if (S_ISREG(st.st_mode)) {
692 f->max_blocks = st.st_size / bs;
693 f->max_size = st.st_size;
700 static int reap_events_uring(struct submitter *s)
702 struct io_cq_ring *ring = &s->cq_ring;
703 struct io_uring_cqe *cqe;
704 unsigned head, reaped = 0;
705 int last_idx = -1, stat_nr = 0;
711 if (head == atomic_load_acquire(ring->tail))
713 cqe = &ring->cqes[head & cq_ring_mask];
715 int fileno = cqe->user_data & 0xffffffff;
717 f = &s->files[fileno];
719 if (cqe->res != bs) {
720 printf("io: unexpected ret=%d\n", cqe->res);
721 if (polled && cqe->res == -EOPNOTSUPP)
722 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
727 int clock_index = cqe->user_data >> 32;
729 if (last_idx != clock_index) {
730 if (last_idx != -1) {
731 add_stat(s, last_idx, stat_nr);
734 last_idx = clock_index;
743 add_stat(s, last_idx, stat_nr);
746 s->inflight -= reaped;
747 atomic_store_release(ring->head, head);
752 static int reap_events_uring_pt(struct submitter *s)
754 struct io_cq_ring *ring = &s->cq_ring;
755 struct io_uring_cqe *cqe;
756 unsigned head, reaped = 0;
757 int last_idx = -1, stat_nr = 0;
765 if (head == atomic_load_acquire(ring->tail))
767 index = head & cq_ring_mask;
768 cqe = &ring->cqes[index << 1];
769 fileno = cqe->user_data & 0xffffffff;
770 f = &s->files[fileno];
774 printf("io: unexpected ret=%d\n", cqe->res);
775 if (polled && cqe->res == -EINVAL)
776 printf("passthrough doesn't support polled IO\n");
780 int clock_index = cqe->user_data >> 32;
782 if (last_idx != clock_index) {
783 if (last_idx != -1) {
784 add_stat(s, last_idx, stat_nr);
787 last_idx = clock_index;
796 add_stat(s, last_idx, stat_nr);
799 s->inflight -= reaped;
800 atomic_store_release(ring->head, head);
805 static void set_affinity(struct submitter *s)
807 #ifdef CONFIG_LIBNUMA
808 struct bitmask *mask;
810 if (s->numa_node == -1)
813 numa_set_preferred(s->numa_node);
815 mask = numa_allocate_cpumask();
816 numa_node_to_cpus(s->numa_node, mask);
817 numa_sched_setaffinity(s->tid, mask);
821 static int detect_node(struct submitter *s, const char *name)
823 #ifdef CONFIG_LIBNUMA
824 const char *base = basename(name);
829 sprintf(str, "/sys/class/nvme-generic/%s/device/numa_node", base);
831 sprintf(str, "/sys/block/%s/device/numa_node", base);
832 fd = open(str, O_RDONLY);
836 ret = read(fd, str, sizeof(str));
850 static int setup_aio(struct submitter *s)
854 fprintf(stderr, "aio does not support polled IO\n");
857 if (sq_thread_poll) {
858 fprintf(stderr, "aio does not support SQPOLL IO\n");
862 fprintf(stderr, "aio does not support polled IO\n");
865 if (fixedbufs || register_files) {
866 fprintf(stderr, "aio does not support registered files or buffers\n");
867 fixedbufs = register_files = 0;
870 return io_queue_init(roundup_pow2(depth), &s->aio_ctx);
872 fprintf(stderr, "Legacy AIO not available on this system/build\n");
878 static int setup_ring(struct submitter *s)
880 struct io_sq_ring *sring = &s->sq_ring;
881 struct io_cq_ring *cring = &s->cq_ring;
882 struct io_uring_params p;
887 memset(&p, 0, sizeof(p));
889 if (polled && !do_nop)
890 p.flags |= IORING_SETUP_IOPOLL;
891 if (sq_thread_poll) {
892 p.flags |= IORING_SETUP_SQPOLL;
893 if (sq_thread_cpu != -1) {
894 p.flags |= IORING_SETUP_SQ_AFF;
895 p.sq_thread_cpu = sq_thread_cpu;
899 p.flags |= IORING_SETUP_SQE128;
900 p.flags |= IORING_SETUP_CQE32;
903 fd = io_uring_setup(depth, &p);
905 perror("io_uring_setup");
908 s->ring_fd = s->enter_ring_fd = fd;
915 rlim.rlim_cur = RLIM_INFINITY;
916 rlim.rlim_max = RLIM_INFINITY;
917 /* ignore potential error, not needed on newer kernels */
918 setrlimit(RLIMIT_MEMLOCK, &rlim);
920 ret = io_uring_register_buffers(s);
922 perror("io_uring_register_buffers");
927 if (register_files) {
928 ret = io_uring_register_files(s);
930 perror("io_uring_register_files");
935 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
936 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
938 sring->head = ptr + p.sq_off.head;
939 sring->tail = ptr + p.sq_off.tail;
940 sring->ring_mask = ptr + p.sq_off.ring_mask;
941 sring->ring_entries = ptr + p.sq_off.ring_entries;
942 sring->flags = ptr + p.sq_off.flags;
943 sring->array = ptr + p.sq_off.array;
944 sq_ring_mask = *sring->ring_mask;
946 if (p.flags & IORING_SETUP_SQE128)
947 len = 2 * p.sq_entries * sizeof(struct io_uring_sqe);
949 len = p.sq_entries * sizeof(struct io_uring_sqe);
950 s->sqes = mmap(0, len,
951 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
954 if (p.flags & IORING_SETUP_CQE32) {
955 len = p.cq_off.cqes +
956 2 * p.cq_entries * sizeof(struct io_uring_cqe);
958 len = p.cq_off.cqes +
959 p.cq_entries * sizeof(struct io_uring_cqe);
962 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
964 cring->head = ptr + p.cq_off.head;
965 cring->tail = ptr + p.cq_off.tail;
966 cring->ring_mask = ptr + p.cq_off.ring_mask;
967 cring->ring_entries = ptr + p.cq_off.ring_entries;
968 cring->cqes = ptr + p.cq_off.cqes;
969 cq_ring_mask = *cring->ring_mask;
971 for (i = 0; i < p.sq_entries; i++)
977 static void *allocate_mem(struct submitter *s, int size)
981 #ifdef CONFIG_LIBNUMA
982 if (s->numa_node != -1)
983 return numa_alloc_onnode(size, s->numa_node);
986 if (posix_memalign(&buf, t_io_uring_page_size, bs)) {
987 printf("failed alloc\n");
994 static int submitter_init(struct submitter *s)
996 int i, nr_batch, err;
997 static int init_printed;
1000 printf("submitter=%d, tid=%d, file=%s, node=%d\n", s->index, s->tid,
1001 s->filename, s->numa_node);
1005 __init_rand64(&s->rand_state, s->tid);
1008 for (i = 0; i < MAX_FDS; i++)
1009 s->files[i].fileno = i;
1011 for (i = 0; i < roundup_pow2(depth); i++) {
1014 buf = allocate_mem(s, bs);
1017 s->iovecs[i].iov_base = buf;
1018 s->iovecs[i].iov_len = bs;
1022 sprintf(buf, "Engine=preadv2\n");
1025 err = setup_ring(s);
1027 sprintf(buf, "Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1029 sprintf(buf, "Engine=aio\n");
1033 printf("queue setup failed: %s, %d\n", strerror(errno), err);
1037 if (!init_printed) {
1038 printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, register_files, buffered, depth);
1044 nr_batch = roundup_pow2(depth / batch_submit);
1047 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
1050 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
1052 s->clock_batch = NULL;
1056 /* perform the expensive command initialization part for passthrough here
1057 * rather than in the fast path
1060 for (i = 0; i < roundup_pow2(depth); i++) {
1061 struct io_uring_sqe *sqe = &s->sqes[i << 1];
1063 memset(&sqe->cmd, 0, sizeof(struct nvme_uring_cmd));
1069 #ifdef CONFIG_LIBAIO
1070 static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
1077 while (index < max_ios) {
1078 struct iocb *iocb = &iocbs[index];
1082 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
1083 s->iovecs[index].iov_len, get_offset(s, f));
1086 if (stats && stats_running)
1087 data |= (((uint64_t) s->clock_index) << 32);
1088 iocb->data = (void *) (uintptr_t) data;
1094 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
1096 int last_idx = -1, stat_nr = 0;
1100 uint64_t data = (uintptr_t) events[reaped].data;
1101 struct file *f = &s->files[data & 0xffffffff];
1104 if (events[reaped].res != bs) {
1105 printf("io: unexpected ret=%ld\n", events[reaped].res);
1109 int clock_index = data >> 32;
1111 if (last_idx != clock_index) {
1112 if (last_idx != -1) {
1113 add_stat(s, last_idx, stat_nr);
1116 last_idx = clock_index;
1125 add_stat(s, last_idx, stat_nr);
1127 s->inflight -= reaped;
1132 static void *submitter_aio_fn(void *data)
1134 struct submitter *s = data;
1135 int i, ret, prepped;
1136 struct iocb **iocbsptr;
1138 struct io_event *events;
1139 #ifdef ARCH_HAVE_CPU_CLOCK
1143 ret = submitter_init(s);
1147 #ifdef ARCH_HAVE_CPU_CLOCK
1151 iocbsptr = calloc(depth, sizeof(struct iocb *));
1152 iocbs = calloc(depth, sizeof(struct iocb));
1153 events = calloc(depth, sizeof(struct io_event));
1155 for (i = 0; i < depth; i++)
1156 iocbsptr[i] = &iocbs[i];
1160 int to_wait, to_submit, to_prep;
1162 if (!prepped && s->inflight < depth) {
1163 to_prep = min(depth - s->inflight, batch_submit);
1164 prepped = prep_more_ios_aio(s, to_prep, iocbs);
1165 #ifdef ARCH_HAVE_CPU_CLOCK
1166 if (prepped && stats) {
1167 s->clock_batch[s->clock_index] = get_cpu_clock();
1168 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1172 s->inflight += prepped;
1173 to_submit = prepped;
1175 if (to_submit && (s->inflight + to_submit <= depth))
1178 to_wait = min(s->inflight + to_submit, batch_complete);
1180 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
1183 perror("io_submit");
1185 } else if (ret != to_submit) {
1186 printf("submitted %d, wanted %d\n", ret, to_submit);
1195 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
1197 perror("io_getevents");
1199 } else if (r != to_wait) {
1200 printf("r=%d, wait=%d\n", r, to_wait);
1203 r = reap_events_aio(s, events, r);
1207 } while (!s->finish);
1218 static void io_uring_unregister_ring(struct submitter *s)
1220 struct io_uring_rsrc_update up = {
1221 .offset = s->enter_ring_fd,
1224 syscall(__NR_io_uring_register, s->ring_fd, IORING_UNREGISTER_RING_FDS,
1228 static int io_uring_register_ring(struct submitter *s)
1230 struct io_uring_rsrc_update up = {
1236 ret = syscall(__NR_io_uring_register, s->ring_fd,
1237 IORING_REGISTER_RING_FDS, &up, 1);
1239 s->enter_ring_fd = up.offset;
1246 static void *submitter_uring_fn(void *data)
1248 struct submitter *s = data;
1249 struct io_sq_ring *ring = &s->sq_ring;
1251 #ifdef ARCH_HAVE_CPU_CLOCK
1255 ret = submitter_init(s);
1259 #ifdef ARCH_HAVE_CPU_CLOCK
1264 io_uring_register_ring(s);
1268 int to_wait, to_submit, this_reap, to_prep;
1269 unsigned ring_flags = 0;
1271 if (!prepped && s->inflight < depth) {
1272 to_prep = min(depth - s->inflight, batch_submit);
1273 prepped = prep_more_ios_uring(s, to_prep);
1274 #ifdef ARCH_HAVE_CPU_CLOCK
1275 if (prepped && stats) {
1276 s->clock_batch[s->clock_index] = get_cpu_clock();
1277 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1281 s->inflight += prepped;
1283 to_submit = prepped;
1285 if (to_submit && (s->inflight + to_submit <= depth))
1288 to_wait = min(s->inflight + to_submit, batch_complete);
1291 * Only need to call io_uring_enter if we're not using SQ thread
1292 * poll, or if IORING_SQ_NEED_WAKEUP is set.
1295 ring_flags = atomic_load_acquire(ring->flags);
1296 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
1300 flags = IORING_ENTER_GETEVENTS;
1301 if (ring_flags & IORING_SQ_NEED_WAKEUP)
1302 flags |= IORING_ENTER_SQ_WAKEUP;
1303 ret = io_uring_enter(s, to_submit, to_wait, flags);
1306 /* for SQPOLL, we submitted it all effectively */
1311 * For non SQ thread poll, we already got the events we needed
1312 * through the io_uring_enter() above. For SQ thread poll, we
1313 * need to loop here until we find enough events.
1320 r = reap_events_uring_pt(s);
1322 r = reap_events_uring(s);
1328 } while (sq_thread_poll && this_reap < to_wait);
1329 s->reaps += this_reap;
1337 } else if (ret < to_submit) {
1338 int diff = to_submit - ret;
1347 } else if (ret < 0) {
1348 if (errno == EAGAIN) {
1356 printf("io_submit: %s\n", strerror(errno));
1359 } while (!s->finish);
1362 io_uring_unregister_ring(s);
1369 #ifdef CONFIG_PWRITEV2
1370 static void *submitter_sync_fn(void *data)
1372 struct submitter *s = data;
1375 if (submitter_init(s) < 0)
1384 #ifdef ARCH_HAVE_CPU_CLOCK
1386 s->clock_batch[s->clock_index] = get_cpu_clock();
1392 offset = get_offset(s, f);
1394 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, RWF_HIPRI);
1396 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, 0);
1401 } else if (ret != bs) {
1409 add_stat(s, s->clock_index, 1);
1410 } while (!s->finish);
1417 static void *submitter_sync_fn(void *data)
1424 static struct submitter *get_submitter(int offset)
1430 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
1434 static void do_finish(const char *reason)
1438 printf("Exiting on %s\n", reason);
1439 for (j = 0; j < nthreads; j++) {
1440 struct submitter *s = get_submitter(j);
1443 if (max_iops > 1000000) {
1444 double miops = (double) max_iops / 1000000.0;
1445 printf("Maximum IOPS=%.2fM\n", miops);
1446 } else if (max_iops > 100000) {
1447 double kiops = (double) max_iops / 1000.0;
1448 printf("Maximum IOPS=%.2fK\n", kiops);
1450 printf("Maximum IOPS=%lu\n", max_iops);
1455 static void sig_int(int sig)
1457 do_finish("signal");
1460 static void arm_sig_int(void)
1462 struct sigaction act;
1464 memset(&act, 0, sizeof(act));
1465 act.sa_handler = sig_int;
1466 act.sa_flags = SA_RESTART;
1467 sigaction(SIGINT, &act, NULL);
1469 /* Windows uses SIGBREAK as a quit signal from other applications */
1471 sigaction(SIGBREAK, &act, NULL);
1475 static void usage(char *argv, int status)
1477 char runtime_str[16];
1478 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
1479 printf("%s [options] -- [filenames]\n"
1480 " -d <int> : IO Depth, default %d\n"
1481 " -s <int> : Batch submit, default %d\n"
1482 " -c <int> : Batch complete, default %d\n"
1483 " -b <int> : Block size, default %d\n"
1484 " -p <bool> : Polled IO, default %d\n"
1485 " -B <bool> : Fixed buffers, default %d\n"
1486 " -F <bool> : Register files, default %d\n"
1487 " -n <int> : Number of threads, default %d\n"
1488 " -O <bool> : Use O_DIRECT, default %d\n"
1489 " -N <bool> : Perform just no-op requests, default %d\n"
1490 " -t <bool> : Track IO latencies, default %d\n"
1491 " -T <int> : TSC rate in HZ\n"
1492 " -r <int> : Runtime in seconds, default %s\n"
1493 " -R <bool> : Use random IO, default %d\n"
1494 " -a <bool> : Use legacy aio, default %d\n"
1495 " -S <bool> : Use sync IO (preadv2), default %d\n"
1496 " -X <bool> : Use registered ring %d\n"
1497 " -P <bool> : Automatically place on device home node %d\n"
1498 " -u <bool> : Use nvme-passthrough I/O, default %d\n",
1499 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1500 fixedbufs, register_files, nthreads, !buffered, do_nop,
1501 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio,
1502 use_sync, register_ring, numa_placement, pt);
1506 static void read_tsc_rate(void)
1514 fd = open(TSC_RATE_FILE, O_RDONLY);
1518 ret = read(fd, buffer, sizeof(buffer));
1524 tsc_rate = strtoul(buffer, NULL, 10);
1525 printf("Using TSC rate %luHz\n", tsc_rate);
1529 static void write_tsc_rate(void)
1535 if (!stat(TSC_RATE_FILE, &sb))
1538 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1542 memset(buffer, 0, sizeof(buffer));
1543 sprintf(buffer, "%lu", tsc_rate);
1544 ret = write(fd, buffer, strlen(buffer));
1550 int main(int argc, char *argv[])
1552 struct submitter *s;
1553 unsigned long done, calls, reap;
1554 int i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1558 if (!do_nop && argc < 2)
1561 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) {
1564 aio = !!atoi(optarg);
1567 depth = atoi(optarg);
1570 batch_submit = atoi(optarg);
1575 batch_complete = atoi(optarg);
1576 if (!batch_complete)
1583 polled = !!atoi(optarg);
1586 fixedbufs = !!atoi(optarg);
1589 register_files = !!atoi(optarg);
1592 nthreads = atoi(optarg);
1594 printf("Threads must be non-zero\n");
1599 do_nop = !!atoi(optarg);
1602 buffered = !atoi(optarg);
1605 #ifndef ARCH_HAVE_CPU_CLOCK
1606 fprintf(stderr, "Stats not supported on this CPU\n");
1609 stats = !!atoi(optarg);
1612 #ifndef ARCH_HAVE_CPU_CLOCK
1613 fprintf(stderr, "Stats not supported on this CPU\n");
1616 tsc_rate = strtoul(optarg, NULL, 10);
1620 runtime = atoi(optarg);
1623 random_io = !!atoi(optarg);
1626 register_ring = !!atoi(optarg);
1629 #ifdef CONFIG_PWRITEV2
1630 use_sync = !!atoi(optarg);
1632 fprintf(stderr, "preadv2 not supported\n");
1637 numa_placement = !!atoi(optarg);
1640 pt = !!atoi(optarg);
1653 if (batch_complete > depth)
1654 batch_complete = depth;
1655 if (batch_submit > depth)
1656 batch_submit = depth;
1658 submitter = calloc(nthreads, sizeof(*submitter) +
1659 roundup_pow2(depth) * sizeof(struct iovec));
1660 for (j = 0; j < nthreads; j++) {
1661 s = get_submitter(j);
1664 s->done = s->calls = s->reaps = 0;
1667 flags = O_RDONLY | O_NOATIME;
1676 printf("No files specified\n");
1679 threads_per_f = nthreads / nfiles;
1680 /* make sure each thread gets assigned files */
1681 if (threads_per_f == 0) {
1684 threads_rem = nthreads - threads_per_f * nfiles;
1687 while (!do_nop && i < argc) {
1690 memset(&f, 0, sizeof(f));
1692 fd = open(argv[i], flags);
1698 if (get_file_size(&f)) {
1699 printf("failed getting size of device/file\n");
1702 if (f.max_blocks <= 1) {
1703 printf("Zero file/device size?\n");
1708 limit = threads_per_f;
1709 limit += threads_rem > 0 ? 1 : 0;
1710 for (k = 0; k < limit; k++) {
1711 s = get_submitter((j + k) % nthreads);
1713 if (s->nr_files == MAX_FDS) {
1714 printf("Max number of files (%d) reached\n", MAX_FDS);
1718 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1721 detect_node(s, argv[i]);
1723 s->filename = argv[i];
1733 t_io_uring_page_size = sysconf(_SC_PAGESIZE);
1734 if (t_io_uring_page_size < 0)
1735 t_io_uring_page_size = 4096;
1737 for (j = 0; j < nthreads; j++) {
1738 s = get_submitter(j);
1740 pthread_create(&s->thread, NULL, submitter_sync_fn, s);
1742 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1743 #ifdef CONFIG_LIBAIO
1745 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1749 reap = calls = done = 0;
1751 unsigned long this_done = 0;
1752 unsigned long this_reap = 0;
1753 unsigned long this_call = 0;
1754 unsigned long rpc = 0, ipc = 0;
1755 unsigned long iops, bw;
1758 if (runtime && !--runtime)
1759 do_finish("timeout");
1761 /* don't print partial run, if interrupted by signal */
1765 /* one second in to the run, enable stats */
1769 for (j = 0; j < nthreads; j++) {
1770 s = get_submitter(j);
1771 this_done += s->done;
1772 this_call += s->calls;
1773 this_reap += s->reaps;
1775 if (this_call - calls) {
1776 rpc = (this_done - done) / (this_call - calls);
1777 ipc = (this_reap - reap) / (this_call - calls);
1780 iops = this_done - done;
1782 bw = iops * (bs / 1048576);
1784 bw = iops / (1048576 / bs);
1785 if (iops > 1000000) {
1786 double miops = (double) iops / 1000000.0;
1787 printf("IOPS=%.2fM, ", miops);
1788 } else if (iops > 100000) {
1789 double kiops = (double) iops / 1000.0;
1790 printf("IOPS=%.2fK, ", kiops);
1792 printf("IOPS=%lu, ", iops);
1794 max_iops = max(max_iops, iops);
1797 double bw_g = (double) bw / 1000.0;
1799 printf("BW=%.2fGiB/s, ", bw_g);
1801 printf("BW=%luMiB/s, ", bw);
1804 printf("IOS/call=%ld/%ld\n", rpc, ipc);
1810 for (j = 0; j < nthreads; j++) {
1811 s = get_submitter(j);
1812 pthread_join(s->thread, &ret);
1818 printf("%d: Latency percentiles:\n", s->tid);
1819 for (i = 0, nr = 0; i < PLAT_NR; i++)
1821 show_clat_percentiles(s->plat, nr, 4);
1822 free(s->clock_batch);