18 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/syscall.h>
22 #include <sys/resource.h>
33 #include "../arch/arch.h"
35 #include "../lib/types.h"
36 #include "../lib/roundup.h"
37 #include "../lib/rand.h"
38 #include "../minmax.h"
39 #include "../os/linux/io_uring.h"
40 #include "../engines/nvme.h"
46 unsigned *ring_entries;
55 unsigned *ring_entries;
56 struct io_uring_cqe *cqes;
60 #define BATCH_SUBMIT 32
61 #define BATCH_COMPLETE 32
66 static unsigned sq_ring_mask, cq_ring_mask;
69 unsigned long max_blocks;
70 unsigned long max_size;
71 unsigned long cur_off;
73 unsigned int nsid; /* nsid field required for nvme-passthrough */
74 unsigned int lba_shift; /* lba_shift field required for nvme-passthrough */
81 #define PLAT_VAL (1 << PLAT_BITS)
82 #define PLAT_GROUP_NR 29
83 #define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
90 struct io_sq_ring sq_ring;
91 struct io_uring_sqe *sqes;
92 struct io_cq_ring cq_ring;
98 unsigned long io_errors;
103 struct taus258_state rand_state;
105 unsigned long *clock_batch;
110 io_context_t aio_ctx;
115 const char *filename;
117 struct file files[MAX_FDS];
120 struct iovec iovecs[];
123 static struct submitter *submitter;
124 static volatile int finish;
125 static int stats_running;
126 static unsigned long max_iops;
127 static long t_io_uring_page_size;
129 static int depth = DEPTH;
130 static int batch_submit = BATCH_SUBMIT;
131 static int batch_complete = BATCH_COMPLETE;
133 static int polled = 1; /* use IO polling */
134 static int fixedbufs = 1; /* use fixed user buffers */
135 static int register_files = 1; /* use fixed files */
136 static int buffered = 0; /* use buffered IO, not O_DIRECT */
137 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
138 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
139 static int do_nop = 0; /* no-op SQ ring commands */
140 static int nthreads = 1;
141 static int stats = 0; /* generate IO stats */
142 static int aio = 0; /* use libaio */
143 static int runtime = 0; /* runtime */
144 static int random_io = 1; /* random or sequential IO */
145 static int register_ring = 1; /* register ring */
146 static int use_sync = 0; /* use preadv2 */
147 static int numa_placement = 0; /* set to node of device */
148 static int pt = 0; /* passthrough I/O or not */
150 static unsigned long tsc_rate;
152 #define TSC_RATE_FILE "tsc-rate"
154 static int vectored = 1;
156 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
157 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
158 static int plist_len = 17;
160 static int nvme_identify(int fd, __u32 nsid, enum nvme_identify_cns cns,
161 enum nvme_csi csi, void *data)
163 struct nvme_passthru_cmd cmd = {
164 .opcode = nvme_admin_identify,
166 .addr = (__u64)(uintptr_t)data,
167 .data_len = NVME_IDENTIFY_DATA_SIZE,
169 .cdw11 = csi << NVME_IDENTIFY_CSI_SHIFT,
170 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
173 return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
176 static int nvme_get_info(int fd, __u32 *nsid, __u32 *lba_sz, __u64 *nlba)
178 struct nvme_id_ns ns;
182 namespace_id = ioctl(fd, NVME_IOCTL_ID);
183 if (namespace_id < 0) {
184 fprintf(stderr, "error failed to fetch namespace-id\n");
190 * Identify namespace to get namespace-id, namespace size in LBA's
193 err = nvme_identify(fd, namespace_id, NVME_IDENTIFY_CNS_NS,
196 fprintf(stderr, "error failed to fetch identify namespace\n");
201 *nsid = namespace_id;
202 *lba_sz = 1 << ns.lbaf[(ns.flbas & 0x0f)].ds;
208 static unsigned long cycles_to_nsec(unsigned long cycles)
215 val = cycles * 1000000000ULL;
216 return val / tsc_rate;
219 static unsigned long plat_idx_to_val(unsigned int idx)
221 unsigned int error_bits;
222 unsigned long k, base;
224 assert(idx < PLAT_NR);
226 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
227 * all bits of the sample as index */
228 if (idx < (PLAT_VAL << 1))
229 return cycles_to_nsec(idx);
231 /* Find the group and compute the minimum value of that group */
232 error_bits = (idx >> PLAT_BITS) - 1;
233 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
235 /* Find its bucket number of the group */
238 /* Return the mean of the range of the bucket */
239 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
242 unsigned int calculate_clat_percentiles(unsigned long *io_u_plat,
243 unsigned long nr, unsigned long **output,
244 unsigned long *maxv, unsigned long *minv)
246 unsigned long sum = 0;
247 unsigned int len = plist_len, i, j = 0;
248 unsigned long *ovals = NULL;
254 ovals = malloc(len * sizeof(*ovals));
259 * Calculate bucket values, note down max and min values
262 for (i = 0; i < PLAT_NR && !is_last; i++) {
264 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
265 assert(plist[j] <= 100.0);
267 ovals[j] = plat_idx_to_val(i);
268 if (ovals[j] < *minv)
270 if (ovals[j] > *maxv)
273 is_last = (j == len - 1) != 0;
282 fprintf(stderr, "error calculating latency percentiles\n");
288 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
289 unsigned int precision)
291 unsigned int divisor, len, i, j = 0;
292 unsigned long minv, maxv;
293 unsigned long *ovals;
294 int per_line, scale_down, time_width;
298 len = calculate_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
305 printf(" percentiles (tsc ticks):\n |");
306 } else if (minv > 2000 && maxv > 99999) {
309 printf(" percentiles (usec):\n |");
313 printf(" percentiles (nsec):\n |");
316 time_width = max(5, (int) (log10(maxv / divisor) + 1));
317 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
318 precision, time_width);
319 /* fmt will be something like " %5.2fth=[%4llu]%c" */
320 per_line = (80 - 7) / (precision + 10 + time_width);
322 for (j = 0; j < len; j++) {
324 if (j != 0 && (j % per_line) == 0)
327 /* end of the list */
328 is_last = (j == len - 1) != 0;
330 for (i = 0; i < scale_down; i++)
331 ovals[j] = (ovals[j] + 999) / 1000;
333 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
338 if ((j % per_line) == per_line - 1) /* for formatting */
346 #ifdef ARCH_HAVE_CPU_CLOCK
347 static unsigned int plat_val_to_idx(unsigned long val)
349 unsigned int msb, error_bits, base, offset, idx;
351 /* Find MSB starting from bit 0 */
355 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
358 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
359 * all bits of the sample as index
361 if (msb <= PLAT_BITS)
364 /* Compute the number of error bits to discard*/
365 error_bits = msb - PLAT_BITS;
367 /* Compute the number of buckets before the group */
368 base = (error_bits + 1) << PLAT_BITS;
371 * Discard the error bits and apply the mask to find the
372 * index for the buckets in the group
374 offset = (PLAT_VAL - 1) & (val >> error_bits);
376 /* Make sure the index does not exceed (array size - 1) */
377 idx = (base + offset) < (PLAT_NR - 1) ?
378 (base + offset) : (PLAT_NR - 1);
384 static void add_stat(struct submitter *s, int clock_index, int nr)
386 #ifdef ARCH_HAVE_CPU_CLOCK
387 unsigned long cycles;
390 if (!s->finish && clock_index) {
391 cycles = get_cpu_clock();
392 cycles -= s->clock_batch[clock_index];
393 pidx = plat_val_to_idx(cycles);
399 static int io_uring_register_buffers(struct submitter *s)
404 return syscall(__NR_io_uring_register, s->ring_fd,
405 IORING_REGISTER_BUFFERS, s->iovecs, roundup_pow2(depth));
408 static int io_uring_register_files(struct submitter *s)
415 s->fds = calloc(s->nr_files, sizeof(__s32));
416 for (i = 0; i < s->nr_files; i++) {
417 s->fds[i] = s->files[i].real_fd;
418 s->files[i].fixed_fd = i;
421 return syscall(__NR_io_uring_register, s->ring_fd,
422 IORING_REGISTER_FILES, s->fds, s->nr_files);
425 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
430 * Clamp CQ ring size at our SQ ring size, we don't need more entries
433 p->flags |= IORING_SETUP_CQSIZE;
434 p->cq_entries = entries;
436 p->flags |= IORING_SETUP_COOP_TASKRUN;
437 p->flags |= IORING_SETUP_SINGLE_ISSUER;
438 p->flags |= IORING_SETUP_DEFER_TASKRUN;
440 ret = syscall(__NR_io_uring_setup, entries, p);
444 if (errno == EINVAL && p->flags & IORING_SETUP_COOP_TASKRUN) {
445 p->flags &= ~IORING_SETUP_COOP_TASKRUN;
448 if (errno == EINVAL && p->flags & IORING_SETUP_SINGLE_ISSUER) {
449 p->flags &= ~IORING_SETUP_SINGLE_ISSUER;
452 if (errno == EINVAL && p->flags & IORING_SETUP_DEFER_TASKRUN) {
453 p->flags &= ~IORING_SETUP_DEFER_TASKRUN;
460 static void io_uring_probe(int fd)
462 struct io_uring_probe *p;
465 p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
469 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
473 if (IORING_OP_READ > p->ops_len)
476 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
482 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
483 unsigned int min_complete, unsigned int flags)
486 flags |= IORING_ENTER_REGISTERED_RING;
487 #ifdef FIO_ARCH_HAS_SYSCALL
488 return __do_syscall6(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
489 min_complete, flags, NULL, 0);
491 return syscall(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
492 min_complete, flags, NULL, 0);
496 static unsigned long long get_offset(struct submitter *s, struct file *f)
498 unsigned long long offset;
502 unsigned long long block;
504 r = __rand64(&s->rand_state);
505 block = r % f->max_blocks;
506 offset = block * (unsigned long long) bs;
510 if (f->cur_off + bs > f->max_size)
517 static struct file *get_next_file(struct submitter *s)
521 if (s->nr_files == 1) {
524 f = &s->files[s->cur_file];
525 if (f->pending_ios >= s->per_file_depth) {
527 if (s->cur_file == s->nr_files)
529 f = &s->files[s->cur_file];
537 static void init_io(struct submitter *s, unsigned index)
539 struct io_uring_sqe *sqe = &s->sqes[index];
543 sqe->opcode = IORING_OP_NOP;
547 f = get_next_file(s);
549 if (register_files) {
550 sqe->flags = IOSQE_FIXED_FILE;
551 sqe->fd = f->fixed_fd;
554 sqe->fd = f->real_fd;
557 sqe->opcode = IORING_OP_READ_FIXED;
558 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
560 sqe->buf_index = index;
561 } else if (!vectored) {
562 sqe->opcode = IORING_OP_READ;
563 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
567 sqe->opcode = IORING_OP_READV;
568 sqe->addr = (unsigned long) &s->iovecs[index];
573 sqe->off = get_offset(s, f);
574 sqe->user_data = (unsigned long) f->fileno;
575 if (stats && stats_running)
576 sqe->user_data |= ((uint64_t)s->clock_index << 32);
579 static void init_io_pt(struct submitter *s, unsigned index)
581 struct io_uring_sqe *sqe = &s->sqes[index << 1];
582 unsigned long offset;
584 struct nvme_uring_cmd *cmd;
585 unsigned long long slba;
586 unsigned long long nlb;
588 f = get_next_file(s);
590 offset = get_offset(s, f);
592 if (register_files) {
593 sqe->fd = f->fixed_fd;
594 sqe->flags = IOSQE_FIXED_FILE;
596 sqe->fd = f->real_fd;
599 sqe->opcode = IORING_OP_URING_CMD;
600 sqe->user_data = (unsigned long) f->fileno;
602 sqe->user_data |= ((__u64) s->clock_index << 32ULL);
603 sqe->cmd_op = NVME_URING_CMD_IO;
604 slba = offset >> f->lba_shift;
605 nlb = (bs >> f->lba_shift) - 1;
606 cmd = (struct nvme_uring_cmd *)&sqe->cmd;
607 /* cdw10 and cdw11 represent starting slba*/
608 cmd->cdw10 = slba & 0xffffffff;
609 cmd->cdw11 = slba >> 32;
610 /* cdw12 represent number of lba to be read*/
612 cmd->addr = (unsigned long) s->iovecs[index].iov_base;
615 sqe->uring_cmd_flags = IORING_URING_CMD_FIXED;
616 sqe->buf_index = index;
622 static int prep_more_ios_uring(struct submitter *s, int max_ios)
624 struct io_sq_ring *ring = &s->sq_ring;
625 unsigned head, index, tail, next_tail, prepped = 0;
628 head = atomic_load_acquire(ring->head);
632 next_tail = tail = *ring->tail;
635 if (next_tail == head)
638 index = tail & sq_ring_mask;
640 init_io_pt(s, index);
645 } while (prepped < max_ios);
648 atomic_store_release(ring->tail, tail);
652 static int get_file_size(struct file *f)
656 if (fstat(f->real_fd, &st) < 0)
663 if (!S_ISCHR(st.st_mode)) {
664 fprintf(stderr, "passthrough works with only nvme-ns "
665 "generic devices (/dev/ngXnY)\n");
668 ret = nvme_get_info(f->real_fd, &f->nsid, &lbs, &nlba);
671 if ((bs % lbs) != 0) {
672 printf("error: bs:%d should be a multiple logical_block_size:%d\n",
676 f->max_blocks = nlba;
678 f->lba_shift = ilog2(lbs);
680 } else if (S_ISBLK(st.st_mode)) {
681 unsigned long long bytes;
683 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
686 f->max_blocks = bytes / bs;
689 } else if (S_ISREG(st.st_mode)) {
690 f->max_blocks = st.st_size / bs;
691 f->max_size = st.st_size;
698 static int reap_events_uring(struct submitter *s)
700 struct io_cq_ring *ring = &s->cq_ring;
701 struct io_uring_cqe *cqe;
702 unsigned head, reaped = 0;
703 int last_idx = -1, stat_nr = 0;
709 if (head == atomic_load_acquire(ring->tail))
711 cqe = &ring->cqes[head & cq_ring_mask];
713 int fileno = cqe->user_data & 0xffffffff;
715 f = &s->files[fileno];
717 if (cqe->res != bs) {
718 if (cqe->res == -ENODATA || cqe->res == -EIO) {
721 printf("io: unexpected ret=%d\n", cqe->res);
722 if (polled && cqe->res == -EOPNOTSUPP)
723 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
729 int clock_index = cqe->user_data >> 32;
731 if (last_idx != clock_index) {
732 if (last_idx != -1) {
733 add_stat(s, last_idx, stat_nr);
736 last_idx = clock_index;
745 add_stat(s, last_idx, stat_nr);
748 s->inflight -= reaped;
749 atomic_store_release(ring->head, head);
754 static int reap_events_uring_pt(struct submitter *s)
756 struct io_cq_ring *ring = &s->cq_ring;
757 struct io_uring_cqe *cqe;
758 unsigned head, reaped = 0;
759 int last_idx = -1, stat_nr = 0;
767 if (head == atomic_load_acquire(ring->tail))
769 index = head & cq_ring_mask;
770 cqe = &ring->cqes[index << 1];
771 fileno = cqe->user_data & 0xffffffff;
772 f = &s->files[fileno];
776 printf("io: unexpected ret=%d\n", cqe->res);
777 if (polled && cqe->res == -EINVAL)
778 printf("passthrough doesn't support polled IO\n");
782 int clock_index = cqe->user_data >> 32;
784 if (last_idx != clock_index) {
785 if (last_idx != -1) {
786 add_stat(s, last_idx, stat_nr);
789 last_idx = clock_index;
798 add_stat(s, last_idx, stat_nr);
801 s->inflight -= reaped;
802 atomic_store_release(ring->head, head);
807 static void set_affinity(struct submitter *s)
809 #ifdef CONFIG_LIBNUMA
810 struct bitmask *mask;
812 if (s->numa_node == -1)
815 numa_set_preferred(s->numa_node);
817 mask = numa_allocate_cpumask();
818 numa_node_to_cpus(s->numa_node, mask);
819 numa_sched_setaffinity(s->tid, mask);
823 static int detect_node(struct submitter *s, const char *name)
825 #ifdef CONFIG_LIBNUMA
826 const char *base = basename(name);
831 sprintf(str, "/sys/class/nvme-generic/%s/device/numa_node", base);
833 sprintf(str, "/sys/block/%s/device/numa_node", base);
834 fd = open(str, O_RDONLY);
838 ret = read(fd, str, sizeof(str));
852 static int setup_aio(struct submitter *s)
856 fprintf(stderr, "aio does not support polled IO\n");
859 if (sq_thread_poll) {
860 fprintf(stderr, "aio does not support SQPOLL IO\n");
864 fprintf(stderr, "aio does not support polled IO\n");
867 if (fixedbufs || register_files) {
868 fprintf(stderr, "aio does not support registered files or buffers\n");
869 fixedbufs = register_files = 0;
872 s->per_file_depth = (depth + s->nr_files - 1) / s->nr_files;
873 return io_queue_init(roundup_pow2(depth), &s->aio_ctx);
875 fprintf(stderr, "Legacy AIO not available on this system/build\n");
881 static int setup_ring(struct submitter *s)
883 struct io_sq_ring *sring = &s->sq_ring;
884 struct io_cq_ring *cring = &s->cq_ring;
885 struct io_uring_params p;
890 memset(&p, 0, sizeof(p));
892 if (polled && !do_nop)
893 p.flags |= IORING_SETUP_IOPOLL;
894 if (sq_thread_poll) {
895 p.flags |= IORING_SETUP_SQPOLL;
896 if (sq_thread_cpu != -1) {
897 p.flags |= IORING_SETUP_SQ_AFF;
898 p.sq_thread_cpu = sq_thread_cpu;
902 p.flags |= IORING_SETUP_SQE128;
903 p.flags |= IORING_SETUP_CQE32;
906 fd = io_uring_setup(depth, &p);
908 perror("io_uring_setup");
911 s->ring_fd = s->enter_ring_fd = fd;
918 rlim.rlim_cur = RLIM_INFINITY;
919 rlim.rlim_max = RLIM_INFINITY;
920 /* ignore potential error, not needed on newer kernels */
921 setrlimit(RLIMIT_MEMLOCK, &rlim);
923 ret = io_uring_register_buffers(s);
925 perror("io_uring_register_buffers");
930 if (register_files) {
931 ret = io_uring_register_files(s);
933 perror("io_uring_register_files");
938 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
939 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
941 sring->head = ptr + p.sq_off.head;
942 sring->tail = ptr + p.sq_off.tail;
943 sring->ring_mask = ptr + p.sq_off.ring_mask;
944 sring->ring_entries = ptr + p.sq_off.ring_entries;
945 sring->flags = ptr + p.sq_off.flags;
946 sring->array = ptr + p.sq_off.array;
947 sq_ring_mask = *sring->ring_mask;
949 if (p.flags & IORING_SETUP_SQE128)
950 len = 2 * p.sq_entries * sizeof(struct io_uring_sqe);
952 len = p.sq_entries * sizeof(struct io_uring_sqe);
953 s->sqes = mmap(0, len,
954 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
957 if (p.flags & IORING_SETUP_CQE32) {
958 len = p.cq_off.cqes +
959 2 * p.cq_entries * sizeof(struct io_uring_cqe);
961 len = p.cq_off.cqes +
962 p.cq_entries * sizeof(struct io_uring_cqe);
965 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
967 cring->head = ptr + p.cq_off.head;
968 cring->tail = ptr + p.cq_off.tail;
969 cring->ring_mask = ptr + p.cq_off.ring_mask;
970 cring->ring_entries = ptr + p.cq_off.ring_entries;
971 cring->cqes = ptr + p.cq_off.cqes;
972 cq_ring_mask = *cring->ring_mask;
974 for (i = 0; i < p.sq_entries; i++)
977 s->per_file_depth = (depth + s->nr_files - 1) / s->nr_files;
981 static void *allocate_mem(struct submitter *s, int size)
985 #ifdef CONFIG_LIBNUMA
986 if (s->numa_node != -1)
987 return numa_alloc_onnode(size, s->numa_node);
990 if (posix_memalign(&buf, t_io_uring_page_size, bs)) {
991 printf("failed alloc\n");
998 static int submitter_init(struct submitter *s)
1000 int i, nr_batch, err;
1001 static int init_printed;
1004 printf("submitter=%d, tid=%d, file=%s, nfiles=%d, node=%d\n", s->index, s->tid,
1005 s->filename, s->nr_files, s->numa_node);
1009 __init_rand64(&s->rand_state, s->tid);
1012 for (i = 0; i < MAX_FDS; i++)
1013 s->files[i].fileno = i;
1015 for (i = 0; i < roundup_pow2(depth); i++) {
1018 buf = allocate_mem(s, bs);
1021 s->iovecs[i].iov_base = buf;
1022 s->iovecs[i].iov_len = bs;
1026 sprintf(buf, "Engine=preadv2\n");
1029 err = setup_ring(s);
1031 sprintf(buf, "Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1033 sprintf(buf, "Engine=aio\n");
1037 printf("queue setup failed: %s, %d\n", strerror(errno), err);
1041 if (!init_printed) {
1042 printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, register_files, buffered, depth);
1048 nr_batch = roundup_pow2(depth / batch_submit);
1051 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
1054 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
1056 s->clock_batch = NULL;
1060 /* perform the expensive command initialization part for passthrough here
1061 * rather than in the fast path
1064 for (i = 0; i < roundup_pow2(depth); i++) {
1065 struct io_uring_sqe *sqe = &s->sqes[i << 1];
1067 memset(&sqe->cmd, 0, sizeof(struct nvme_uring_cmd));
1073 #ifdef CONFIG_LIBAIO
1074 static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
1081 while (index < max_ios) {
1082 struct iocb *iocb = &iocbs[index];
1084 f = get_next_file(s);
1086 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
1087 s->iovecs[index].iov_len, get_offset(s, f));
1090 if (stats && stats_running)
1091 data |= (((uint64_t) s->clock_index) << 32);
1092 iocb->data = (void *) (uintptr_t) data;
1098 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
1100 int last_idx = -1, stat_nr = 0;
1104 uint64_t data = (uintptr_t) events[reaped].data;
1105 struct file *f = &s->files[data & 0xffffffff];
1108 if (events[reaped].res != bs) {
1109 if (events[reaped].res == -ENODATA ||
1110 events[reaped].res == -EIO) {
1113 printf("io: unexpected ret=%ld\n", events[reaped].res);
1117 int clock_index = data >> 32;
1119 if (last_idx != clock_index) {
1120 if (last_idx != -1) {
1121 add_stat(s, last_idx, stat_nr);
1124 last_idx = clock_index;
1133 add_stat(s, last_idx, stat_nr);
1135 s->inflight -= reaped;
1140 static void *submitter_aio_fn(void *data)
1142 struct submitter *s = data;
1143 int i, ret, prepped;
1144 struct iocb **iocbsptr;
1146 struct io_event *events;
1147 #ifdef ARCH_HAVE_CPU_CLOCK
1151 ret = submitter_init(s);
1155 #ifdef ARCH_HAVE_CPU_CLOCK
1159 iocbsptr = calloc(depth, sizeof(struct iocb *));
1160 iocbs = calloc(depth, sizeof(struct iocb));
1161 events = calloc(depth, sizeof(struct io_event));
1163 for (i = 0; i < depth; i++)
1164 iocbsptr[i] = &iocbs[i];
1168 int to_wait, to_submit, to_prep;
1170 if (!prepped && s->inflight < depth) {
1171 to_prep = min(depth - s->inflight, batch_submit);
1172 prepped = prep_more_ios_aio(s, to_prep, iocbs);
1173 #ifdef ARCH_HAVE_CPU_CLOCK
1174 if (prepped && stats) {
1175 s->clock_batch[s->clock_index] = get_cpu_clock();
1176 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1180 s->inflight += prepped;
1181 to_submit = prepped;
1183 if (to_submit && (s->inflight + to_submit <= depth))
1186 to_wait = min(s->inflight + to_submit, batch_complete);
1188 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
1191 perror("io_submit");
1193 } else if (ret != to_submit) {
1194 printf("submitted %d, wanted %d\n", ret, to_submit);
1203 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
1205 perror("io_getevents");
1207 } else if (r != to_wait) {
1208 printf("r=%d, wait=%d\n", r, to_wait);
1211 r = reap_events_aio(s, events, r);
1215 } while (!s->finish);
1226 static void io_uring_unregister_ring(struct submitter *s)
1228 struct io_uring_rsrc_update up = {
1229 .offset = s->enter_ring_fd,
1232 syscall(__NR_io_uring_register, s->ring_fd, IORING_UNREGISTER_RING_FDS,
1236 static int io_uring_register_ring(struct submitter *s)
1238 struct io_uring_rsrc_update up = {
1244 ret = syscall(__NR_io_uring_register, s->ring_fd,
1245 IORING_REGISTER_RING_FDS, &up, 1);
1247 s->enter_ring_fd = up.offset;
1254 static void *submitter_uring_fn(void *data)
1256 struct submitter *s = data;
1257 struct io_sq_ring *ring = &s->sq_ring;
1259 #ifdef ARCH_HAVE_CPU_CLOCK
1263 ret = submitter_init(s);
1267 #ifdef ARCH_HAVE_CPU_CLOCK
1272 io_uring_register_ring(s);
1276 int to_wait, to_submit, this_reap, to_prep;
1277 unsigned ring_flags = 0;
1279 if (!prepped && s->inflight < depth) {
1280 to_prep = min(depth - s->inflight, batch_submit);
1281 prepped = prep_more_ios_uring(s, to_prep);
1282 #ifdef ARCH_HAVE_CPU_CLOCK
1283 if (prepped && stats) {
1284 s->clock_batch[s->clock_index] = get_cpu_clock();
1285 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1289 s->inflight += prepped;
1291 to_submit = prepped;
1293 if (to_submit && (s->inflight + to_submit <= depth))
1296 to_wait = min(s->inflight + to_submit, batch_complete);
1299 * Only need to call io_uring_enter if we're not using SQ thread
1300 * poll, or if IORING_SQ_NEED_WAKEUP is set.
1303 ring_flags = atomic_load_acquire(ring->flags);
1304 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
1308 flags = IORING_ENTER_GETEVENTS;
1309 if (ring_flags & IORING_SQ_NEED_WAKEUP)
1310 flags |= IORING_ENTER_SQ_WAKEUP;
1311 ret = io_uring_enter(s, to_submit, to_wait, flags);
1314 /* for SQPOLL, we submitted it all effectively */
1319 * For non SQ thread poll, we already got the events we needed
1320 * through the io_uring_enter() above. For SQ thread poll, we
1321 * need to loop here until we find enough events.
1328 r = reap_events_uring_pt(s);
1330 r = reap_events_uring(s);
1336 } while (sq_thread_poll && this_reap < to_wait);
1337 s->reaps += this_reap;
1345 } else if (ret < to_submit) {
1346 int diff = to_submit - ret;
1355 } else if (ret < 0) {
1356 if (errno == EAGAIN) {
1364 printf("io_submit: %s\n", strerror(errno));
1367 } while (!s->finish);
1370 io_uring_unregister_ring(s);
1377 #ifdef CONFIG_PWRITEV2
1378 static void *submitter_sync_fn(void *data)
1380 struct submitter *s = data;
1383 if (submitter_init(s) < 0)
1390 f = get_next_file(s);
1392 #ifdef ARCH_HAVE_CPU_CLOCK
1394 s->clock_batch[s->clock_index] = get_cpu_clock();
1400 offset = get_offset(s, f);
1402 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, RWF_HIPRI);
1404 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, 0);
1409 } else if (ret != bs) {
1417 add_stat(s, s->clock_index, 1);
1418 } while (!s->finish);
1425 static void *submitter_sync_fn(void *data)
1432 static struct submitter *get_submitter(int offset)
1438 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
1442 static void do_finish(const char *reason)
1446 printf("Exiting on %s\n", reason);
1447 for (j = 0; j < nthreads; j++) {
1448 struct submitter *s = get_submitter(j);
1451 if (max_iops > 1000000) {
1452 double miops = (double) max_iops / 1000000.0;
1453 printf("Maximum IOPS=%.2fM\n", miops);
1454 } else if (max_iops > 100000) {
1455 double kiops = (double) max_iops / 1000.0;
1456 printf("Maximum IOPS=%.2fK\n", kiops);
1458 printf("Maximum IOPS=%lu\n", max_iops);
1463 static void sig_int(int sig)
1465 do_finish("signal");
1468 static void arm_sig_int(void)
1470 struct sigaction act;
1472 memset(&act, 0, sizeof(act));
1473 act.sa_handler = sig_int;
1474 act.sa_flags = SA_RESTART;
1475 sigaction(SIGINT, &act, NULL);
1477 /* Windows uses SIGBREAK as a quit signal from other applications */
1479 sigaction(SIGBREAK, &act, NULL);
1483 static void usage(char *argv, int status)
1485 char runtime_str[16];
1486 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
1487 printf("%s [options] -- [filenames]\n"
1488 " -d <int> : IO Depth, default %d\n"
1489 " -s <int> : Batch submit, default %d\n"
1490 " -c <int> : Batch complete, default %d\n"
1491 " -b <int> : Block size, default %d\n"
1492 " -p <bool> : Polled IO, default %d\n"
1493 " -B <bool> : Fixed buffers, default %d\n"
1494 " -F <bool> : Register files, default %d\n"
1495 " -n <int> : Number of threads, default %d\n"
1496 " -O <bool> : Use O_DIRECT, default %d\n"
1497 " -N <bool> : Perform just no-op requests, default %d\n"
1498 " -t <bool> : Track IO latencies, default %d\n"
1499 " -T <int> : TSC rate in HZ\n"
1500 " -r <int> : Runtime in seconds, default %s\n"
1501 " -R <bool> : Use random IO, default %d\n"
1502 " -a <bool> : Use legacy aio, default %d\n"
1503 " -S <bool> : Use sync IO (preadv2), default %d\n"
1504 " -X <bool> : Use registered ring %d\n"
1505 " -P <bool> : Automatically place on device home node %d\n"
1506 " -u <bool> : Use nvme-passthrough I/O, default %d\n",
1507 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1508 fixedbufs, register_files, nthreads, !buffered, do_nop,
1509 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio,
1510 use_sync, register_ring, numa_placement, pt);
1514 static void read_tsc_rate(void)
1522 fd = open(TSC_RATE_FILE, O_RDONLY);
1526 ret = read(fd, buffer, sizeof(buffer));
1532 tsc_rate = strtoul(buffer, NULL, 10);
1533 printf("Using TSC rate %luHz\n", tsc_rate);
1537 static void write_tsc_rate(void)
1543 if (!stat(TSC_RATE_FILE, &sb))
1546 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1550 memset(buffer, 0, sizeof(buffer));
1551 sprintf(buffer, "%lu", tsc_rate);
1552 ret = write(fd, buffer, strlen(buffer));
1558 int main(int argc, char *argv[])
1560 struct submitter *s;
1561 unsigned long done, calls, reap, io_errors;
1562 int i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1566 if (!do_nop && argc < 2)
1569 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) {
1572 aio = !!atoi(optarg);
1575 depth = atoi(optarg);
1578 batch_submit = atoi(optarg);
1583 batch_complete = atoi(optarg);
1584 if (!batch_complete)
1591 polled = !!atoi(optarg);
1594 fixedbufs = !!atoi(optarg);
1597 register_files = !!atoi(optarg);
1600 nthreads = atoi(optarg);
1602 printf("Threads must be non-zero\n");
1607 do_nop = !!atoi(optarg);
1610 buffered = !atoi(optarg);
1613 #ifndef ARCH_HAVE_CPU_CLOCK
1614 fprintf(stderr, "Stats not supported on this CPU\n");
1617 stats = !!atoi(optarg);
1620 #ifndef ARCH_HAVE_CPU_CLOCK
1621 fprintf(stderr, "Stats not supported on this CPU\n");
1624 tsc_rate = strtoul(optarg, NULL, 10);
1628 runtime = atoi(optarg);
1631 random_io = !!atoi(optarg);
1634 register_ring = !!atoi(optarg);
1637 #ifdef CONFIG_PWRITEV2
1638 use_sync = !!atoi(optarg);
1640 fprintf(stderr, "preadv2 not supported\n");
1645 numa_placement = !!atoi(optarg);
1648 pt = !!atoi(optarg);
1661 if (batch_complete > depth)
1662 batch_complete = depth;
1663 if (batch_submit > depth)
1664 batch_submit = depth;
1666 submitter = calloc(nthreads, sizeof(*submitter) +
1667 roundup_pow2(depth) * sizeof(struct iovec));
1668 for (j = 0; j < nthreads; j++) {
1669 s = get_submitter(j);
1672 s->done = s->calls = s->reaps = s->io_errors = 0;
1675 flags = O_RDONLY | O_NOATIME;
1684 printf("No files specified\n");
1687 threads_per_f = nthreads / nfiles;
1688 /* make sure each thread gets assigned files */
1689 if (threads_per_f == 0) {
1692 threads_rem = nthreads - threads_per_f * nfiles;
1695 while (!do_nop && i < argc) {
1698 memset(&f, 0, sizeof(f));
1700 fd = open(argv[i], flags);
1706 if (get_file_size(&f)) {
1707 printf("failed getting size of device/file\n");
1710 if (f.max_blocks <= 1) {
1711 printf("Zero file/device size?\n");
1716 limit = threads_per_f;
1717 limit += threads_rem > 0 ? 1 : 0;
1718 for (k = 0; k < limit; k++) {
1719 s = get_submitter((j + k) % nthreads);
1721 if (s->nr_files == MAX_FDS) {
1722 printf("Max number of files (%d) reached\n", MAX_FDS);
1726 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1729 detect_node(s, argv[i]);
1731 s->filename = argv[i];
1741 t_io_uring_page_size = sysconf(_SC_PAGESIZE);
1742 if (t_io_uring_page_size < 0)
1743 t_io_uring_page_size = 4096;
1745 for (j = 0; j < nthreads; j++) {
1746 s = get_submitter(j);
1748 pthread_create(&s->thread, NULL, submitter_sync_fn, s);
1750 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1751 #ifdef CONFIG_LIBAIO
1753 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1757 reap = calls = done = io_errors = 0;
1759 unsigned long this_done = 0;
1760 unsigned long this_reap = 0;
1761 unsigned long this_call = 0;
1762 unsigned long this_io_errors = 0;
1763 unsigned long rpc = 0, ipc = 0;
1764 unsigned long iops, bw;
1767 if (runtime && !--runtime)
1768 do_finish("timeout");
1770 /* don't print partial run, if interrupted by signal */
1774 /* one second in to the run, enable stats */
1778 for (j = 0; j < nthreads; j++) {
1779 s = get_submitter(j);
1780 this_done += s->done;
1781 this_call += s->calls;
1782 this_reap += s->reaps;
1783 this_io_errors += s->io_errors;
1785 if (this_call - calls) {
1786 rpc = (this_done - done) / (this_call - calls);
1787 ipc = (this_reap - reap) / (this_call - calls);
1790 iops = this_done - done;
1791 iops -= this_io_errors - io_errors;
1793 bw = iops * (bs / 1048576);
1795 bw = iops / (1048576 / bs);
1796 if (iops > 1000000) {
1797 double miops = (double) iops / 1000000.0;
1798 printf("IOPS=%.2fM, ", miops);
1799 } else if (iops > 100000) {
1800 double kiops = (double) iops / 1000.0;
1801 printf("IOPS=%.2fK, ", kiops);
1803 printf("IOPS=%lu, ", iops);
1805 max_iops = max(max_iops, iops);
1808 double bw_g = (double) bw / 1000.0;
1810 printf("BW=%.2fGiB/s, ", bw_g);
1812 printf("BW=%luMiB/s, ", bw);
1815 printf("IOS/call=%ld/%ld\n", rpc, ipc);
1819 io_errors = this_io_errors;
1822 for (j = 0; j < nthreads; j++) {
1823 s = get_submitter(j);
1824 pthread_join(s->thread, &ret);
1828 printf("%d: %lu IO errors\n", s->tid, s->io_errors);
1833 printf("%d: Latency percentiles:\n", s->tid);
1834 for (i = 0, nr = 0; i < PLAT_NR; i++)
1836 show_clat_percentiles(s->plat, nr, 4);
1837 free(s->clock_batch);