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;
97 unsigned long io_errors;
102 struct taus258_state rand_state;
104 unsigned long *clock_batch;
109 io_context_t aio_ctx;
113 const char *filename;
115 struct file files[MAX_FDS];
118 struct iovec iovecs[];
121 static struct submitter *submitter;
122 static volatile int finish;
123 static int stats_running;
124 static unsigned long max_iops;
125 static long t_io_uring_page_size;
127 static int depth = DEPTH;
128 static int batch_submit = BATCH_SUBMIT;
129 static int batch_complete = BATCH_COMPLETE;
131 static int polled = 1; /* use IO polling */
132 static int fixedbufs = 1; /* use fixed user buffers */
133 static int register_files = 1; /* use fixed files */
134 static int buffered = 0; /* use buffered IO, not O_DIRECT */
135 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
136 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
137 static int do_nop = 0; /* no-op SQ ring commands */
138 static int nthreads = 1;
139 static int stats = 0; /* generate IO stats */
140 static int aio = 0; /* use libaio */
141 static int runtime = 0; /* runtime */
142 static int random_io = 1; /* random or sequential IO */
143 static int register_ring = 1; /* register ring */
144 static int use_sync = 0; /* use preadv2 */
145 static int numa_placement = 0; /* set to node of device */
146 static int pt = 0; /* passthrough I/O or not */
148 static unsigned long tsc_rate;
150 #define TSC_RATE_FILE "tsc-rate"
152 static int vectored = 1;
154 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
155 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
156 static int plist_len = 17;
158 static int nvme_identify(int fd, __u32 nsid, enum nvme_identify_cns cns,
159 enum nvme_csi csi, void *data)
161 struct nvme_passthru_cmd cmd = {
162 .opcode = nvme_admin_identify,
164 .addr = (__u64)(uintptr_t)data,
165 .data_len = NVME_IDENTIFY_DATA_SIZE,
167 .cdw11 = csi << NVME_IDENTIFY_CSI_SHIFT,
168 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
171 return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
174 static int nvme_get_info(int fd, __u32 *nsid, __u32 *lba_sz, __u64 *nlba)
176 struct nvme_id_ns ns;
180 namespace_id = ioctl(fd, NVME_IOCTL_ID);
181 if (namespace_id < 0) {
182 fprintf(stderr, "error failed to fetch namespace-id\n");
188 * Identify namespace to get namespace-id, namespace size in LBA's
191 err = nvme_identify(fd, namespace_id, NVME_IDENTIFY_CNS_NS,
194 fprintf(stderr, "error failed to fetch identify namespace\n");
199 *nsid = namespace_id;
200 *lba_sz = 1 << ns.lbaf[(ns.flbas & 0x0f)].ds;
206 static unsigned long cycles_to_nsec(unsigned long cycles)
213 val = cycles * 1000000000ULL;
214 return val / tsc_rate;
217 static unsigned long plat_idx_to_val(unsigned int idx)
219 unsigned int error_bits;
220 unsigned long k, base;
222 assert(idx < PLAT_NR);
224 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
225 * all bits of the sample as index */
226 if (idx < (PLAT_VAL << 1))
227 return cycles_to_nsec(idx);
229 /* Find the group and compute the minimum value of that group */
230 error_bits = (idx >> PLAT_BITS) - 1;
231 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
233 /* Find its bucket number of the group */
236 /* Return the mean of the range of the bucket */
237 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
240 unsigned int calculate_clat_percentiles(unsigned long *io_u_plat,
241 unsigned long nr, unsigned long **output,
242 unsigned long *maxv, unsigned long *minv)
244 unsigned long sum = 0;
245 unsigned int len = plist_len, i, j = 0;
246 unsigned long *ovals = NULL;
252 ovals = malloc(len * sizeof(*ovals));
257 * Calculate bucket values, note down max and min values
260 for (i = 0; i < PLAT_NR && !is_last; i++) {
262 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
263 assert(plist[j] <= 100.0);
265 ovals[j] = plat_idx_to_val(i);
266 if (ovals[j] < *minv)
268 if (ovals[j] > *maxv)
271 is_last = (j == len - 1) != 0;
280 fprintf(stderr, "error calculating latency percentiles\n");
286 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
287 unsigned int precision)
289 unsigned int divisor, len, i, j = 0;
290 unsigned long minv, maxv;
291 unsigned long *ovals;
292 int per_line, scale_down, time_width;
296 len = calculate_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
303 printf(" percentiles (tsc ticks):\n |");
304 } else if (minv > 2000 && maxv > 99999) {
307 printf(" percentiles (usec):\n |");
311 printf(" percentiles (nsec):\n |");
314 time_width = max(5, (int) (log10(maxv / divisor) + 1));
315 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
316 precision, time_width);
317 /* fmt will be something like " %5.2fth=[%4llu]%c" */
318 per_line = (80 - 7) / (precision + 10 + time_width);
320 for (j = 0; j < len; j++) {
322 if (j != 0 && (j % per_line) == 0)
325 /* end of the list */
326 is_last = (j == len - 1) != 0;
328 for (i = 0; i < scale_down; i++)
329 ovals[j] = (ovals[j] + 999) / 1000;
331 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
336 if ((j % per_line) == per_line - 1) /* for formatting */
344 #ifdef ARCH_HAVE_CPU_CLOCK
345 static unsigned int plat_val_to_idx(unsigned long val)
347 unsigned int msb, error_bits, base, offset, idx;
349 /* Find MSB starting from bit 0 */
353 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
356 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
357 * all bits of the sample as index
359 if (msb <= PLAT_BITS)
362 /* Compute the number of error bits to discard*/
363 error_bits = msb - PLAT_BITS;
365 /* Compute the number of buckets before the group */
366 base = (error_bits + 1) << PLAT_BITS;
369 * Discard the error bits and apply the mask to find the
370 * index for the buckets in the group
372 offset = (PLAT_VAL - 1) & (val >> error_bits);
374 /* Make sure the index does not exceed (array size - 1) */
375 idx = (base + offset) < (PLAT_NR - 1) ?
376 (base + offset) : (PLAT_NR - 1);
382 static void add_stat(struct submitter *s, int clock_index, int nr)
384 #ifdef ARCH_HAVE_CPU_CLOCK
385 unsigned long cycles;
388 if (!s->finish && clock_index) {
389 cycles = get_cpu_clock();
390 cycles -= s->clock_batch[clock_index];
391 pidx = plat_val_to_idx(cycles);
397 static int io_uring_register_buffers(struct submitter *s)
402 return syscall(__NR_io_uring_register, s->ring_fd,
403 IORING_REGISTER_BUFFERS, s->iovecs, roundup_pow2(depth));
406 static int io_uring_register_files(struct submitter *s)
413 s->fds = calloc(s->nr_files, sizeof(__s32));
414 for (i = 0; i < s->nr_files; i++) {
415 s->fds[i] = s->files[i].real_fd;
416 s->files[i].fixed_fd = i;
419 return syscall(__NR_io_uring_register, s->ring_fd,
420 IORING_REGISTER_FILES, s->fds, s->nr_files);
423 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
428 * Clamp CQ ring size at our SQ ring size, we don't need more entries
431 p->flags |= IORING_SETUP_CQSIZE;
432 p->cq_entries = entries;
434 p->flags |= IORING_SETUP_COOP_TASKRUN;
435 p->flags |= IORING_SETUP_SINGLE_ISSUER;
436 p->flags |= IORING_SETUP_DEFER_TASKRUN;
438 ret = syscall(__NR_io_uring_setup, entries, p);
442 if (errno == EINVAL && p->flags & IORING_SETUP_COOP_TASKRUN) {
443 p->flags &= ~IORING_SETUP_COOP_TASKRUN;
446 if (errno == EINVAL && p->flags & IORING_SETUP_SINGLE_ISSUER) {
447 p->flags &= ~IORING_SETUP_SINGLE_ISSUER;
450 if (errno == EINVAL && p->flags & IORING_SETUP_DEFER_TASKRUN) {
451 p->flags &= ~IORING_SETUP_DEFER_TASKRUN;
458 static void io_uring_probe(int fd)
460 struct io_uring_probe *p;
463 p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
467 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
471 if (IORING_OP_READ > p->ops_len)
474 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
480 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
481 unsigned int min_complete, unsigned int flags)
484 flags |= IORING_ENTER_REGISTERED_RING;
485 #ifdef FIO_ARCH_HAS_SYSCALL
486 return __do_syscall6(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
487 min_complete, flags, NULL, 0);
489 return syscall(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
490 min_complete, flags, NULL, 0);
494 static unsigned file_depth(struct submitter *s)
496 return (depth + s->nr_files - 1) / s->nr_files;
499 static unsigned long long get_offset(struct submitter *s, struct file *f)
501 unsigned long long offset;
505 unsigned long long block;
507 r = __rand64(&s->rand_state);
508 block = r % f->max_blocks;
509 offset = block * (unsigned long long) bs;
513 if (f->cur_off + bs > f->max_size)
520 static struct file *init_new_io(struct submitter *s)
524 if (s->nr_files == 1) {
527 f = &s->files[s->cur_file];
528 if (f->pending_ios >= file_depth(s)) {
530 if (s->cur_file == s->nr_files)
532 f = &s->files[s->cur_file];
540 static void init_io(struct submitter *s, unsigned index)
542 struct io_uring_sqe *sqe = &s->sqes[index];
546 sqe->opcode = IORING_OP_NOP;
552 if (register_files) {
553 sqe->flags = IOSQE_FIXED_FILE;
554 sqe->fd = f->fixed_fd;
557 sqe->fd = f->real_fd;
560 sqe->opcode = IORING_OP_READ_FIXED;
561 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
563 sqe->buf_index = index;
564 } else if (!vectored) {
565 sqe->opcode = IORING_OP_READ;
566 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
570 sqe->opcode = IORING_OP_READV;
571 sqe->addr = (unsigned long) &s->iovecs[index];
576 sqe->off = get_offset(s, f);
577 sqe->user_data = (unsigned long) f->fileno;
578 if (stats && stats_running)
579 sqe->user_data |= ((uint64_t)s->clock_index << 32);
582 static void init_io_pt(struct submitter *s, unsigned index)
584 struct io_uring_sqe *sqe = &s->sqes[index << 1];
585 unsigned long offset;
587 struct nvme_uring_cmd *cmd;
588 unsigned long long slba;
589 unsigned long long nlb;
593 offset = get_offset(s, f);
595 if (register_files) {
596 sqe->fd = f->fixed_fd;
597 sqe->flags = IOSQE_FIXED_FILE;
599 sqe->fd = f->real_fd;
602 sqe->opcode = IORING_OP_URING_CMD;
603 sqe->user_data = (unsigned long) f->fileno;
605 sqe->user_data |= ((__u64) s->clock_index << 32ULL);
606 sqe->cmd_op = NVME_URING_CMD_IO;
607 slba = offset >> f->lba_shift;
608 nlb = (bs >> f->lba_shift) - 1;
609 cmd = (struct nvme_uring_cmd *)&sqe->cmd;
610 /* cdw10 and cdw11 represent starting slba*/
611 cmd->cdw10 = slba & 0xffffffff;
612 cmd->cdw11 = slba >> 32;
613 /* cdw12 represent number of lba to be read*/
615 cmd->addr = (unsigned long) s->iovecs[index].iov_base;
618 sqe->uring_cmd_flags = IORING_URING_CMD_FIXED;
619 sqe->buf_index = index;
625 static int prep_more_ios_uring(struct submitter *s, int max_ios)
627 struct io_sq_ring *ring = &s->sq_ring;
628 unsigned head, index, tail, next_tail, prepped = 0;
631 head = atomic_load_acquire(ring->head);
635 next_tail = tail = *ring->tail;
638 if (next_tail == head)
641 index = tail & sq_ring_mask;
643 init_io_pt(s, index);
648 } while (prepped < max_ios);
651 atomic_store_release(ring->tail, tail);
655 static int get_file_size(struct file *f)
659 if (fstat(f->real_fd, &st) < 0)
666 if (!S_ISCHR(st.st_mode)) {
667 fprintf(stderr, "passthrough works with only nvme-ns "
668 "generic devices (/dev/ngXnY)\n");
671 ret = nvme_get_info(f->real_fd, &f->nsid, &lbs, &nlba);
674 if ((bs % lbs) != 0) {
675 printf("error: bs:%d should be a multiple logical_block_size:%d\n",
679 f->max_blocks = nlba;
681 f->lba_shift = ilog2(lbs);
683 } else if (S_ISBLK(st.st_mode)) {
684 unsigned long long bytes;
686 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
689 f->max_blocks = bytes / bs;
692 } else if (S_ISREG(st.st_mode)) {
693 f->max_blocks = st.st_size / bs;
694 f->max_size = st.st_size;
701 static int reap_events_uring(struct submitter *s)
703 struct io_cq_ring *ring = &s->cq_ring;
704 struct io_uring_cqe *cqe;
705 unsigned head, reaped = 0;
706 int last_idx = -1, stat_nr = 0;
712 if (head == atomic_load_acquire(ring->tail))
714 cqe = &ring->cqes[head & cq_ring_mask];
716 int fileno = cqe->user_data & 0xffffffff;
718 f = &s->files[fileno];
720 if (cqe->res != bs) {
721 if (cqe->res == -ENODATA || cqe->res == -EIO) {
724 printf("io: unexpected ret=%d\n", cqe->res);
725 if (polled && cqe->res == -EOPNOTSUPP)
726 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
732 int clock_index = cqe->user_data >> 32;
734 if (last_idx != clock_index) {
735 if (last_idx != -1) {
736 add_stat(s, last_idx, stat_nr);
739 last_idx = clock_index;
748 add_stat(s, last_idx, stat_nr);
751 s->inflight -= reaped;
752 atomic_store_release(ring->head, head);
757 static int reap_events_uring_pt(struct submitter *s)
759 struct io_cq_ring *ring = &s->cq_ring;
760 struct io_uring_cqe *cqe;
761 unsigned head, reaped = 0;
762 int last_idx = -1, stat_nr = 0;
770 if (head == atomic_load_acquire(ring->tail))
772 index = head & cq_ring_mask;
773 cqe = &ring->cqes[index << 1];
774 fileno = cqe->user_data & 0xffffffff;
775 f = &s->files[fileno];
779 printf("io: unexpected ret=%d\n", cqe->res);
780 if (polled && cqe->res == -EINVAL)
781 printf("passthrough doesn't support polled IO\n");
785 int clock_index = cqe->user_data >> 32;
787 if (last_idx != clock_index) {
788 if (last_idx != -1) {
789 add_stat(s, last_idx, stat_nr);
792 last_idx = clock_index;
801 add_stat(s, last_idx, stat_nr);
804 s->inflight -= reaped;
805 atomic_store_release(ring->head, head);
810 static void set_affinity(struct submitter *s)
812 #ifdef CONFIG_LIBNUMA
813 struct bitmask *mask;
815 if (s->numa_node == -1)
818 numa_set_preferred(s->numa_node);
820 mask = numa_allocate_cpumask();
821 numa_node_to_cpus(s->numa_node, mask);
822 numa_sched_setaffinity(s->tid, mask);
826 static int detect_node(struct submitter *s, const char *name)
828 #ifdef CONFIG_LIBNUMA
829 const char *base = basename(name);
834 sprintf(str, "/sys/class/nvme-generic/%s/device/numa_node", base);
836 sprintf(str, "/sys/block/%s/device/numa_node", base);
837 fd = open(str, O_RDONLY);
841 ret = read(fd, str, sizeof(str));
855 static int setup_aio(struct submitter *s)
859 fprintf(stderr, "aio does not support polled IO\n");
862 if (sq_thread_poll) {
863 fprintf(stderr, "aio does not support SQPOLL IO\n");
867 fprintf(stderr, "aio does not support polled IO\n");
870 if (fixedbufs || register_files) {
871 fprintf(stderr, "aio does not support registered files or buffers\n");
872 fixedbufs = register_files = 0;
875 return io_queue_init(roundup_pow2(depth), &s->aio_ctx);
877 fprintf(stderr, "Legacy AIO not available on this system/build\n");
883 static int setup_ring(struct submitter *s)
885 struct io_sq_ring *sring = &s->sq_ring;
886 struct io_cq_ring *cring = &s->cq_ring;
887 struct io_uring_params p;
892 memset(&p, 0, sizeof(p));
894 if (polled && !do_nop)
895 p.flags |= IORING_SETUP_IOPOLL;
896 if (sq_thread_poll) {
897 p.flags |= IORING_SETUP_SQPOLL;
898 if (sq_thread_cpu != -1) {
899 p.flags |= IORING_SETUP_SQ_AFF;
900 p.sq_thread_cpu = sq_thread_cpu;
904 p.flags |= IORING_SETUP_SQE128;
905 p.flags |= IORING_SETUP_CQE32;
908 fd = io_uring_setup(depth, &p);
910 perror("io_uring_setup");
913 s->ring_fd = s->enter_ring_fd = fd;
920 rlim.rlim_cur = RLIM_INFINITY;
921 rlim.rlim_max = RLIM_INFINITY;
922 /* ignore potential error, not needed on newer kernels */
923 setrlimit(RLIMIT_MEMLOCK, &rlim);
925 ret = io_uring_register_buffers(s);
927 perror("io_uring_register_buffers");
932 if (register_files) {
933 ret = io_uring_register_files(s);
935 perror("io_uring_register_files");
940 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
941 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
943 sring->head = ptr + p.sq_off.head;
944 sring->tail = ptr + p.sq_off.tail;
945 sring->ring_mask = ptr + p.sq_off.ring_mask;
946 sring->ring_entries = ptr + p.sq_off.ring_entries;
947 sring->flags = ptr + p.sq_off.flags;
948 sring->array = ptr + p.sq_off.array;
949 sq_ring_mask = *sring->ring_mask;
951 if (p.flags & IORING_SETUP_SQE128)
952 len = 2 * p.sq_entries * sizeof(struct io_uring_sqe);
954 len = p.sq_entries * sizeof(struct io_uring_sqe);
955 s->sqes = mmap(0, len,
956 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
959 if (p.flags & IORING_SETUP_CQE32) {
960 len = p.cq_off.cqes +
961 2 * p.cq_entries * sizeof(struct io_uring_cqe);
963 len = p.cq_off.cqes +
964 p.cq_entries * sizeof(struct io_uring_cqe);
967 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
969 cring->head = ptr + p.cq_off.head;
970 cring->tail = ptr + p.cq_off.tail;
971 cring->ring_mask = ptr + p.cq_off.ring_mask;
972 cring->ring_entries = ptr + p.cq_off.ring_entries;
973 cring->cqes = ptr + p.cq_off.cqes;
974 cq_ring_mask = *cring->ring_mask;
976 for (i = 0; i < p.sq_entries; i++)
982 static void *allocate_mem(struct submitter *s, int size)
986 #ifdef CONFIG_LIBNUMA
987 if (s->numa_node != -1)
988 return numa_alloc_onnode(size, s->numa_node);
991 if (posix_memalign(&buf, t_io_uring_page_size, bs)) {
992 printf("failed alloc\n");
999 static int submitter_init(struct submitter *s)
1001 int i, nr_batch, err;
1002 static int init_printed;
1005 printf("submitter=%d, tid=%d, file=%s, node=%d\n", s->index, s->tid,
1006 s->filename, s->numa_node);
1010 __init_rand64(&s->rand_state, s->tid);
1013 for (i = 0; i < MAX_FDS; i++)
1014 s->files[i].fileno = i;
1016 for (i = 0; i < roundup_pow2(depth); i++) {
1019 buf = allocate_mem(s, bs);
1022 s->iovecs[i].iov_base = buf;
1023 s->iovecs[i].iov_len = bs;
1027 sprintf(buf, "Engine=preadv2\n");
1030 err = setup_ring(s);
1032 sprintf(buf, "Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1034 sprintf(buf, "Engine=aio\n");
1038 printf("queue setup failed: %s, %d\n", strerror(errno), err);
1042 if (!init_printed) {
1043 printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, register_files, buffered, depth);
1049 nr_batch = roundup_pow2(depth / batch_submit);
1052 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
1055 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
1057 s->clock_batch = NULL;
1061 /* perform the expensive command initialization part for passthrough here
1062 * rather than in the fast path
1065 for (i = 0; i < roundup_pow2(depth); i++) {
1066 struct io_uring_sqe *sqe = &s->sqes[i << 1];
1068 memset(&sqe->cmd, 0, sizeof(struct nvme_uring_cmd));
1074 #ifdef CONFIG_LIBAIO
1075 static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
1082 while (index < max_ios) {
1083 struct iocb *iocb = &iocbs[index];
1087 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
1088 s->iovecs[index].iov_len, get_offset(s, f));
1091 if (stats && stats_running)
1092 data |= (((uint64_t) s->clock_index) << 32);
1093 iocb->data = (void *) (uintptr_t) data;
1099 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
1101 int last_idx = -1, stat_nr = 0;
1105 uint64_t data = (uintptr_t) events[reaped].data;
1106 struct file *f = &s->files[data & 0xffffffff];
1109 if (events[reaped].res != bs) {
1110 if (events[reaped].res == -ENODATA ||
1111 events[reaped].res == -EIO) {
1114 printf("io: unexpected ret=%ld\n", events[reaped].res);
1118 int clock_index = data >> 32;
1120 if (last_idx != clock_index) {
1121 if (last_idx != -1) {
1122 add_stat(s, last_idx, stat_nr);
1125 last_idx = clock_index;
1134 add_stat(s, last_idx, stat_nr);
1136 s->inflight -= reaped;
1141 static void *submitter_aio_fn(void *data)
1143 struct submitter *s = data;
1144 int i, ret, prepped;
1145 struct iocb **iocbsptr;
1147 struct io_event *events;
1148 #ifdef ARCH_HAVE_CPU_CLOCK
1152 ret = submitter_init(s);
1156 #ifdef ARCH_HAVE_CPU_CLOCK
1160 iocbsptr = calloc(depth, sizeof(struct iocb *));
1161 iocbs = calloc(depth, sizeof(struct iocb));
1162 events = calloc(depth, sizeof(struct io_event));
1164 for (i = 0; i < depth; i++)
1165 iocbsptr[i] = &iocbs[i];
1169 int to_wait, to_submit, to_prep;
1171 if (!prepped && s->inflight < depth) {
1172 to_prep = min(depth - s->inflight, batch_submit);
1173 prepped = prep_more_ios_aio(s, to_prep, iocbs);
1174 #ifdef ARCH_HAVE_CPU_CLOCK
1175 if (prepped && stats) {
1176 s->clock_batch[s->clock_index] = get_cpu_clock();
1177 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1181 s->inflight += prepped;
1182 to_submit = prepped;
1184 if (to_submit && (s->inflight + to_submit <= depth))
1187 to_wait = min(s->inflight + to_submit, batch_complete);
1189 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
1192 perror("io_submit");
1194 } else if (ret != to_submit) {
1195 printf("submitted %d, wanted %d\n", ret, to_submit);
1204 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
1206 perror("io_getevents");
1208 } else if (r != to_wait) {
1209 printf("r=%d, wait=%d\n", r, to_wait);
1212 r = reap_events_aio(s, events, r);
1216 } while (!s->finish);
1227 static void io_uring_unregister_ring(struct submitter *s)
1229 struct io_uring_rsrc_update up = {
1230 .offset = s->enter_ring_fd,
1233 syscall(__NR_io_uring_register, s->ring_fd, IORING_UNREGISTER_RING_FDS,
1237 static int io_uring_register_ring(struct submitter *s)
1239 struct io_uring_rsrc_update up = {
1245 ret = syscall(__NR_io_uring_register, s->ring_fd,
1246 IORING_REGISTER_RING_FDS, &up, 1);
1248 s->enter_ring_fd = up.offset;
1255 static void *submitter_uring_fn(void *data)
1257 struct submitter *s = data;
1258 struct io_sq_ring *ring = &s->sq_ring;
1260 #ifdef ARCH_HAVE_CPU_CLOCK
1264 ret = submitter_init(s);
1268 #ifdef ARCH_HAVE_CPU_CLOCK
1273 io_uring_register_ring(s);
1277 int to_wait, to_submit, this_reap, to_prep;
1278 unsigned ring_flags = 0;
1280 if (!prepped && s->inflight < depth) {
1281 to_prep = min(depth - s->inflight, batch_submit);
1282 prepped = prep_more_ios_uring(s, to_prep);
1283 #ifdef ARCH_HAVE_CPU_CLOCK
1284 if (prepped && stats) {
1285 s->clock_batch[s->clock_index] = get_cpu_clock();
1286 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1290 s->inflight += prepped;
1292 to_submit = prepped;
1294 if (to_submit && (s->inflight + to_submit <= depth))
1297 to_wait = min(s->inflight + to_submit, batch_complete);
1300 * Only need to call io_uring_enter if we're not using SQ thread
1301 * poll, or if IORING_SQ_NEED_WAKEUP is set.
1304 ring_flags = atomic_load_acquire(ring->flags);
1305 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
1309 flags = IORING_ENTER_GETEVENTS;
1310 if (ring_flags & IORING_SQ_NEED_WAKEUP)
1311 flags |= IORING_ENTER_SQ_WAKEUP;
1312 ret = io_uring_enter(s, to_submit, to_wait, flags);
1315 /* for SQPOLL, we submitted it all effectively */
1320 * For non SQ thread poll, we already got the events we needed
1321 * through the io_uring_enter() above. For SQ thread poll, we
1322 * need to loop here until we find enough events.
1329 r = reap_events_uring_pt(s);
1331 r = reap_events_uring(s);
1337 } while (sq_thread_poll && this_reap < to_wait);
1338 s->reaps += this_reap;
1346 } else if (ret < to_submit) {
1347 int diff = to_submit - ret;
1356 } else if (ret < 0) {
1357 if (errno == EAGAIN) {
1365 printf("io_submit: %s\n", strerror(errno));
1368 } while (!s->finish);
1371 io_uring_unregister_ring(s);
1378 #ifdef CONFIG_PWRITEV2
1379 static void *submitter_sync_fn(void *data)
1381 struct submitter *s = data;
1384 if (submitter_init(s) < 0)
1393 #ifdef ARCH_HAVE_CPU_CLOCK
1395 s->clock_batch[s->clock_index] = get_cpu_clock();
1401 offset = get_offset(s, f);
1403 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, RWF_HIPRI);
1405 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, 0);
1410 } else if (ret != bs) {
1418 add_stat(s, s->clock_index, 1);
1419 } while (!s->finish);
1426 static void *submitter_sync_fn(void *data)
1433 static struct submitter *get_submitter(int offset)
1439 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
1443 static void do_finish(const char *reason)
1447 printf("Exiting on %s\n", reason);
1448 for (j = 0; j < nthreads; j++) {
1449 struct submitter *s = get_submitter(j);
1452 if (max_iops > 1000000) {
1453 double miops = (double) max_iops / 1000000.0;
1454 printf("Maximum IOPS=%.2fM\n", miops);
1455 } else if (max_iops > 100000) {
1456 double kiops = (double) max_iops / 1000.0;
1457 printf("Maximum IOPS=%.2fK\n", kiops);
1459 printf("Maximum IOPS=%lu\n", max_iops);
1464 static void sig_int(int sig)
1466 do_finish("signal");
1469 static void arm_sig_int(void)
1471 struct sigaction act;
1473 memset(&act, 0, sizeof(act));
1474 act.sa_handler = sig_int;
1475 act.sa_flags = SA_RESTART;
1476 sigaction(SIGINT, &act, NULL);
1478 /* Windows uses SIGBREAK as a quit signal from other applications */
1480 sigaction(SIGBREAK, &act, NULL);
1484 static void usage(char *argv, int status)
1486 char runtime_str[16];
1487 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
1488 printf("%s [options] -- [filenames]\n"
1489 " -d <int> : IO Depth, default %d\n"
1490 " -s <int> : Batch submit, default %d\n"
1491 " -c <int> : Batch complete, default %d\n"
1492 " -b <int> : Block size, default %d\n"
1493 " -p <bool> : Polled IO, default %d\n"
1494 " -B <bool> : Fixed buffers, default %d\n"
1495 " -F <bool> : Register files, default %d\n"
1496 " -n <int> : Number of threads, default %d\n"
1497 " -O <bool> : Use O_DIRECT, default %d\n"
1498 " -N <bool> : Perform just no-op requests, default %d\n"
1499 " -t <bool> : Track IO latencies, default %d\n"
1500 " -T <int> : TSC rate in HZ\n"
1501 " -r <int> : Runtime in seconds, default %s\n"
1502 " -R <bool> : Use random IO, default %d\n"
1503 " -a <bool> : Use legacy aio, default %d\n"
1504 " -S <bool> : Use sync IO (preadv2), default %d\n"
1505 " -X <bool> : Use registered ring %d\n"
1506 " -P <bool> : Automatically place on device home node %d\n"
1507 " -u <bool> : Use nvme-passthrough I/O, default %d\n",
1508 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1509 fixedbufs, register_files, nthreads, !buffered, do_nop,
1510 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio,
1511 use_sync, register_ring, numa_placement, pt);
1515 static void read_tsc_rate(void)
1523 fd = open(TSC_RATE_FILE, O_RDONLY);
1527 ret = read(fd, buffer, sizeof(buffer));
1533 tsc_rate = strtoul(buffer, NULL, 10);
1534 printf("Using TSC rate %luHz\n", tsc_rate);
1538 static void write_tsc_rate(void)
1544 if (!stat(TSC_RATE_FILE, &sb))
1547 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1551 memset(buffer, 0, sizeof(buffer));
1552 sprintf(buffer, "%lu", tsc_rate);
1553 ret = write(fd, buffer, strlen(buffer));
1559 int main(int argc, char *argv[])
1561 struct submitter *s;
1562 unsigned long done, calls, reap, io_errors;
1563 int i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1567 if (!do_nop && argc < 2)
1570 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) {
1573 aio = !!atoi(optarg);
1576 depth = atoi(optarg);
1579 batch_submit = atoi(optarg);
1584 batch_complete = atoi(optarg);
1585 if (!batch_complete)
1592 polled = !!atoi(optarg);
1595 fixedbufs = !!atoi(optarg);
1598 register_files = !!atoi(optarg);
1601 nthreads = atoi(optarg);
1603 printf("Threads must be non-zero\n");
1608 do_nop = !!atoi(optarg);
1611 buffered = !atoi(optarg);
1614 #ifndef ARCH_HAVE_CPU_CLOCK
1615 fprintf(stderr, "Stats not supported on this CPU\n");
1618 stats = !!atoi(optarg);
1621 #ifndef ARCH_HAVE_CPU_CLOCK
1622 fprintf(stderr, "Stats not supported on this CPU\n");
1625 tsc_rate = strtoul(optarg, NULL, 10);
1629 runtime = atoi(optarg);
1632 random_io = !!atoi(optarg);
1635 register_ring = !!atoi(optarg);
1638 #ifdef CONFIG_PWRITEV2
1639 use_sync = !!atoi(optarg);
1641 fprintf(stderr, "preadv2 not supported\n");
1646 numa_placement = !!atoi(optarg);
1649 pt = !!atoi(optarg);
1662 if (batch_complete > depth)
1663 batch_complete = depth;
1664 if (batch_submit > depth)
1665 batch_submit = depth;
1667 submitter = calloc(nthreads, sizeof(*submitter) +
1668 roundup_pow2(depth) * sizeof(struct iovec));
1669 for (j = 0; j < nthreads; j++) {
1670 s = get_submitter(j);
1673 s->done = s->calls = s->reaps = s->io_errors = 0;
1676 flags = O_RDONLY | O_NOATIME;
1685 printf("No files specified\n");
1688 threads_per_f = nthreads / nfiles;
1689 /* make sure each thread gets assigned files */
1690 if (threads_per_f == 0) {
1693 threads_rem = nthreads - threads_per_f * nfiles;
1696 while (!do_nop && i < argc) {
1699 memset(&f, 0, sizeof(f));
1701 fd = open(argv[i], flags);
1707 if (get_file_size(&f)) {
1708 printf("failed getting size of device/file\n");
1711 if (f.max_blocks <= 1) {
1712 printf("Zero file/device size?\n");
1717 limit = threads_per_f;
1718 limit += threads_rem > 0 ? 1 : 0;
1719 for (k = 0; k < limit; k++) {
1720 s = get_submitter((j + k) % nthreads);
1722 if (s->nr_files == MAX_FDS) {
1723 printf("Max number of files (%d) reached\n", MAX_FDS);
1727 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1730 detect_node(s, argv[i]);
1732 s->filename = argv[i];
1742 t_io_uring_page_size = sysconf(_SC_PAGESIZE);
1743 if (t_io_uring_page_size < 0)
1744 t_io_uring_page_size = 4096;
1746 for (j = 0; j < nthreads; j++) {
1747 s = get_submitter(j);
1749 pthread_create(&s->thread, NULL, submitter_sync_fn, s);
1751 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1752 #ifdef CONFIG_LIBAIO
1754 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1758 reap = calls = done = io_errors = 0;
1760 unsigned long this_done = 0;
1761 unsigned long this_reap = 0;
1762 unsigned long this_call = 0;
1763 unsigned long this_io_errors = 0;
1764 unsigned long rpc = 0, ipc = 0;
1765 unsigned long iops, bw;
1768 if (runtime && !--runtime)
1769 do_finish("timeout");
1771 /* don't print partial run, if interrupted by signal */
1775 /* one second in to the run, enable stats */
1779 for (j = 0; j < nthreads; j++) {
1780 s = get_submitter(j);
1781 this_done += s->done;
1782 this_call += s->calls;
1783 this_reap += s->reaps;
1784 this_io_errors += s->io_errors;
1786 if (this_call - calls) {
1787 rpc = (this_done - done) / (this_call - calls);
1788 ipc = (this_reap - reap) / (this_call - calls);
1791 iops = this_done - done;
1792 iops -= this_io_errors - io_errors;
1794 bw = iops * (bs / 1048576);
1796 bw = iops / (1048576 / bs);
1797 if (iops > 1000000) {
1798 double miops = (double) iops / 1000000.0;
1799 printf("IOPS=%.2fM, ", miops);
1800 } else if (iops > 100000) {
1801 double kiops = (double) iops / 1000.0;
1802 printf("IOPS=%.2fK, ", kiops);
1804 printf("IOPS=%lu, ", iops);
1806 max_iops = max(max_iops, iops);
1809 double bw_g = (double) bw / 1000.0;
1811 printf("BW=%.2fGiB/s, ", bw_g);
1813 printf("BW=%luMiB/s, ", bw);
1816 printf("IOS/call=%ld/%ld\n", rpc, ipc);
1820 io_errors = this_io_errors;
1823 for (j = 0; j < nthreads; j++) {
1824 s = get_submitter(j);
1825 pthread_join(s->thread, &ret);
1829 printf("%d: %lu IO errors\n", s->tid, s->io_errors);
1834 printf("%d: Latency percentiles:\n", s->tid);
1835 for (i = 0, nr = 0; i < PLAT_NR; i++)
1837 show_clat_percentiles(s->plat, nr, 4);
1838 free(s->clock_batch);