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;
114 const char *filename;
116 struct file files[MAX_FDS];
119 struct iovec iovecs[];
122 static struct submitter *submitter;
123 static volatile int finish;
124 static int stats_running;
125 static unsigned long max_iops;
126 static long t_io_uring_page_size;
128 static int depth = DEPTH;
129 static int batch_submit = BATCH_SUBMIT;
130 static int batch_complete = BATCH_COMPLETE;
132 static int polled = 1; /* use IO polling */
133 static int fixedbufs = 1; /* use fixed user buffers */
134 static int register_files = 1; /* use fixed files */
135 static int buffered = 0; /* use buffered IO, not O_DIRECT */
136 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
137 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
138 static int do_nop = 0; /* no-op SQ ring commands */
139 static int nthreads = 1;
140 static int stats = 0; /* generate IO stats */
141 static int aio = 0; /* use libaio */
142 static int runtime = 0; /* runtime */
143 static int random_io = 1; /* random or sequential IO */
144 static int register_ring = 1; /* register ring */
145 static int use_sync = 0; /* use preadv2 */
146 static int numa_placement = 0; /* set to node of device */
147 static int pt = 0; /* passthrough I/O or not */
149 static unsigned long tsc_rate;
151 #define TSC_RATE_FILE "tsc-rate"
153 static int vectored = 1;
155 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
156 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
157 static int plist_len = 17;
159 static int nvme_identify(int fd, __u32 nsid, enum nvme_identify_cns cns,
160 enum nvme_csi csi, void *data)
162 struct nvme_passthru_cmd cmd = {
163 .opcode = nvme_admin_identify,
165 .addr = (__u64)(uintptr_t)data,
166 .data_len = NVME_IDENTIFY_DATA_SIZE,
168 .cdw11 = csi << NVME_IDENTIFY_CSI_SHIFT,
169 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
172 return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
175 static int nvme_get_info(int fd, __u32 *nsid, __u32 *lba_sz, __u64 *nlba)
177 struct nvme_id_ns ns;
181 namespace_id = ioctl(fd, NVME_IOCTL_ID);
182 if (namespace_id < 0) {
183 fprintf(stderr, "error failed to fetch namespace-id\n");
189 * Identify namespace to get namespace-id, namespace size in LBA's
192 err = nvme_identify(fd, namespace_id, NVME_IDENTIFY_CNS_NS,
195 fprintf(stderr, "error failed to fetch identify namespace\n");
200 *nsid = namespace_id;
201 *lba_sz = 1 << ns.lbaf[(ns.flbas & 0x0f)].ds;
207 static unsigned long cycles_to_nsec(unsigned long cycles)
214 val = cycles * 1000000000ULL;
215 return val / tsc_rate;
218 static unsigned long plat_idx_to_val(unsigned int idx)
220 unsigned int error_bits;
221 unsigned long k, base;
223 assert(idx < PLAT_NR);
225 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
226 * all bits of the sample as index */
227 if (idx < (PLAT_VAL << 1))
228 return cycles_to_nsec(idx);
230 /* Find the group and compute the minimum value of that group */
231 error_bits = (idx >> PLAT_BITS) - 1;
232 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
234 /* Find its bucket number of the group */
237 /* Return the mean of the range of the bucket */
238 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
241 unsigned int calculate_clat_percentiles(unsigned long *io_u_plat,
242 unsigned long nr, unsigned long **output,
243 unsigned long *maxv, unsigned long *minv)
245 unsigned long sum = 0;
246 unsigned int len = plist_len, i, j = 0;
247 unsigned long *ovals = NULL;
253 ovals = malloc(len * sizeof(*ovals));
258 * Calculate bucket values, note down max and min values
261 for (i = 0; i < PLAT_NR && !is_last; i++) {
263 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
264 assert(plist[j] <= 100.0);
266 ovals[j] = plat_idx_to_val(i);
267 if (ovals[j] < *minv)
269 if (ovals[j] > *maxv)
272 is_last = (j == len - 1) != 0;
281 fprintf(stderr, "error calculating latency percentiles\n");
287 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
288 unsigned int precision)
290 unsigned int divisor, len, i, j = 0;
291 unsigned long minv, maxv;
292 unsigned long *ovals;
293 int per_line, scale_down, time_width;
297 len = calculate_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
304 printf(" percentiles (tsc ticks):\n |");
305 } else if (minv > 2000 && maxv > 99999) {
308 printf(" percentiles (usec):\n |");
312 printf(" percentiles (nsec):\n |");
315 time_width = max(5, (int) (log10(maxv / divisor) + 1));
316 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
317 precision, time_width);
318 /* fmt will be something like " %5.2fth=[%4llu]%c" */
319 per_line = (80 - 7) / (precision + 10 + time_width);
321 for (j = 0; j < len; j++) {
323 if (j != 0 && (j % per_line) == 0)
326 /* end of the list */
327 is_last = (j == len - 1) != 0;
329 for (i = 0; i < scale_down; i++)
330 ovals[j] = (ovals[j] + 999) / 1000;
332 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
337 if ((j % per_line) == per_line - 1) /* for formatting */
345 #ifdef ARCH_HAVE_CPU_CLOCK
346 static unsigned int plat_val_to_idx(unsigned long val)
348 unsigned int msb, error_bits, base, offset, idx;
350 /* Find MSB starting from bit 0 */
354 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
357 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
358 * all bits of the sample as index
360 if (msb <= PLAT_BITS)
363 /* Compute the number of error bits to discard*/
364 error_bits = msb - PLAT_BITS;
366 /* Compute the number of buckets before the group */
367 base = (error_bits + 1) << PLAT_BITS;
370 * Discard the error bits and apply the mask to find the
371 * index for the buckets in the group
373 offset = (PLAT_VAL - 1) & (val >> error_bits);
375 /* Make sure the index does not exceed (array size - 1) */
376 idx = (base + offset) < (PLAT_NR - 1) ?
377 (base + offset) : (PLAT_NR - 1);
383 static void add_stat(struct submitter *s, int clock_index, int nr)
385 #ifdef ARCH_HAVE_CPU_CLOCK
386 unsigned long cycles;
389 if (!s->finish && clock_index) {
390 cycles = get_cpu_clock();
391 cycles -= s->clock_batch[clock_index];
392 pidx = plat_val_to_idx(cycles);
398 static int io_uring_register_buffers(struct submitter *s)
403 return syscall(__NR_io_uring_register, s->ring_fd,
404 IORING_REGISTER_BUFFERS, s->iovecs, roundup_pow2(depth));
407 static int io_uring_register_files(struct submitter *s)
414 s->fds = calloc(s->nr_files, sizeof(__s32));
415 for (i = 0; i < s->nr_files; i++) {
416 s->fds[i] = s->files[i].real_fd;
417 s->files[i].fixed_fd = i;
420 return syscall(__NR_io_uring_register, s->ring_fd,
421 IORING_REGISTER_FILES, s->fds, s->nr_files);
424 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
429 * Clamp CQ ring size at our SQ ring size, we don't need more entries
432 p->flags |= IORING_SETUP_CQSIZE;
433 p->cq_entries = entries;
435 p->flags |= IORING_SETUP_COOP_TASKRUN;
436 p->flags |= IORING_SETUP_SINGLE_ISSUER;
437 p->flags |= IORING_SETUP_DEFER_TASKRUN;
439 ret = syscall(__NR_io_uring_setup, entries, p);
443 if (errno == EINVAL && p->flags & IORING_SETUP_COOP_TASKRUN) {
444 p->flags &= ~IORING_SETUP_COOP_TASKRUN;
447 if (errno == EINVAL && p->flags & IORING_SETUP_SINGLE_ISSUER) {
448 p->flags &= ~IORING_SETUP_SINGLE_ISSUER;
451 if (errno == EINVAL && p->flags & IORING_SETUP_DEFER_TASKRUN) {
452 p->flags &= ~IORING_SETUP_DEFER_TASKRUN;
459 static void io_uring_probe(int fd)
461 struct io_uring_probe *p;
464 p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
468 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
472 if (IORING_OP_READ > p->ops_len)
475 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
481 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
482 unsigned int min_complete, unsigned int flags)
485 flags |= IORING_ENTER_REGISTERED_RING;
486 #ifdef FIO_ARCH_HAS_SYSCALL
487 return __do_syscall6(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
488 min_complete, flags, NULL, 0);
490 return syscall(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
491 min_complete, flags, NULL, 0);
495 static unsigned long long get_offset(struct submitter *s, struct file *f)
497 unsigned long long offset;
501 unsigned long long block;
503 r = __rand64(&s->rand_state);
504 block = r % f->max_blocks;
505 offset = block * (unsigned long long) bs;
509 if (f->cur_off + bs > f->max_size)
516 static struct file *get_next_file(struct submitter *s)
520 if (s->nr_files == 1) {
523 f = &s->files[s->cur_file];
524 if (f->pending_ios >= s->per_file_depth) {
526 if (s->cur_file == s->nr_files)
528 f = &s->files[s->cur_file];
536 static void init_io(struct submitter *s, unsigned index)
538 struct io_uring_sqe *sqe = &s->sqes[index];
542 sqe->opcode = IORING_OP_NOP;
546 f = get_next_file(s);
548 if (register_files) {
549 sqe->flags = IOSQE_FIXED_FILE;
550 sqe->fd = f->fixed_fd;
553 sqe->fd = f->real_fd;
556 sqe->opcode = IORING_OP_READ_FIXED;
557 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
559 sqe->buf_index = index;
560 } else if (!vectored) {
561 sqe->opcode = IORING_OP_READ;
562 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
566 sqe->opcode = IORING_OP_READV;
567 sqe->addr = (unsigned long) &s->iovecs[index];
572 sqe->off = get_offset(s, f);
573 sqe->user_data = (unsigned long) f->fileno;
574 if (stats && stats_running)
575 sqe->user_data |= ((uint64_t)s->clock_index << 32);
578 static void init_io_pt(struct submitter *s, unsigned index)
580 struct io_uring_sqe *sqe = &s->sqes[index << 1];
581 unsigned long offset;
583 struct nvme_uring_cmd *cmd;
584 unsigned long long slba;
585 unsigned long long nlb;
587 f = get_next_file(s);
589 offset = get_offset(s, f);
591 if (register_files) {
592 sqe->fd = f->fixed_fd;
593 sqe->flags = IOSQE_FIXED_FILE;
595 sqe->fd = f->real_fd;
598 sqe->opcode = IORING_OP_URING_CMD;
599 sqe->user_data = (unsigned long) f->fileno;
601 sqe->user_data |= ((__u64) s->clock_index << 32ULL);
602 sqe->cmd_op = NVME_URING_CMD_IO;
603 slba = offset >> f->lba_shift;
604 nlb = (bs >> f->lba_shift) - 1;
605 cmd = (struct nvme_uring_cmd *)&sqe->cmd;
606 /* cdw10 and cdw11 represent starting slba*/
607 cmd->cdw10 = slba & 0xffffffff;
608 cmd->cdw11 = slba >> 32;
609 /* cdw12 represent number of lba to be read*/
611 cmd->addr = (unsigned long) s->iovecs[index].iov_base;
614 sqe->uring_cmd_flags = IORING_URING_CMD_FIXED;
615 sqe->buf_index = index;
621 static int prep_more_ios_uring(struct submitter *s, int max_ios)
623 struct io_sq_ring *ring = &s->sq_ring;
624 unsigned head, index, tail, next_tail, prepped = 0;
627 head = atomic_load_acquire(ring->head);
631 next_tail = tail = *ring->tail;
634 if (next_tail == head)
637 index = tail & sq_ring_mask;
639 init_io_pt(s, index);
644 } while (prepped < max_ios);
647 atomic_store_release(ring->tail, tail);
651 static int get_file_size(struct file *f)
655 if (fstat(f->real_fd, &st) < 0)
662 if (!S_ISCHR(st.st_mode)) {
663 fprintf(stderr, "passthrough works with only nvme-ns "
664 "generic devices (/dev/ngXnY)\n");
667 ret = nvme_get_info(f->real_fd, &f->nsid, &lbs, &nlba);
670 if ((bs % lbs) != 0) {
671 printf("error: bs:%d should be a multiple logical_block_size:%d\n",
675 f->max_blocks = nlba;
677 f->lba_shift = ilog2(lbs);
679 } else if (S_ISBLK(st.st_mode)) {
680 unsigned long long bytes;
682 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
685 f->max_blocks = bytes / bs;
688 } else if (S_ISREG(st.st_mode)) {
689 f->max_blocks = st.st_size / bs;
690 f->max_size = st.st_size;
697 static int reap_events_uring(struct submitter *s)
699 struct io_cq_ring *ring = &s->cq_ring;
700 struct io_uring_cqe *cqe;
701 unsigned head, reaped = 0;
702 int last_idx = -1, stat_nr = 0;
708 if (head == atomic_load_acquire(ring->tail))
710 cqe = &ring->cqes[head & cq_ring_mask];
712 int fileno = cqe->user_data & 0xffffffff;
714 f = &s->files[fileno];
716 if (cqe->res != bs) {
717 if (cqe->res == -ENODATA || cqe->res == -EIO) {
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");
728 int clock_index = cqe->user_data >> 32;
730 if (last_idx != clock_index) {
731 if (last_idx != -1) {
732 add_stat(s, last_idx, stat_nr);
735 last_idx = clock_index;
744 add_stat(s, last_idx, stat_nr);
747 s->inflight -= reaped;
748 atomic_store_release(ring->head, head);
753 static int reap_events_uring_pt(struct submitter *s)
755 struct io_cq_ring *ring = &s->cq_ring;
756 struct io_uring_cqe *cqe;
757 unsigned head, reaped = 0;
758 int last_idx = -1, stat_nr = 0;
766 if (head == atomic_load_acquire(ring->tail))
768 index = head & cq_ring_mask;
769 cqe = &ring->cqes[index << 1];
770 fileno = cqe->user_data & 0xffffffff;
771 f = &s->files[fileno];
775 printf("io: unexpected ret=%d\n", cqe->res);
776 if (polled && cqe->res == -EINVAL)
777 printf("passthrough doesn't support polled IO\n");
781 int clock_index = cqe->user_data >> 32;
783 if (last_idx != clock_index) {
784 if (last_idx != -1) {
785 add_stat(s, last_idx, stat_nr);
788 last_idx = clock_index;
797 add_stat(s, last_idx, stat_nr);
800 s->inflight -= reaped;
801 atomic_store_release(ring->head, head);
806 static void set_affinity(struct submitter *s)
808 #ifdef CONFIG_LIBNUMA
809 struct bitmask *mask;
811 if (s->numa_node == -1)
814 numa_set_preferred(s->numa_node);
816 mask = numa_allocate_cpumask();
817 numa_node_to_cpus(s->numa_node, mask);
818 numa_sched_setaffinity(s->tid, mask);
822 static int detect_node(struct submitter *s, const char *name)
824 #ifdef CONFIG_LIBNUMA
825 const char *base = basename(name);
830 sprintf(str, "/sys/class/nvme-generic/%s/device/numa_node", base);
832 sprintf(str, "/sys/block/%s/device/numa_node", base);
833 fd = open(str, O_RDONLY);
837 ret = read(fd, str, sizeof(str));
851 static int setup_aio(struct submitter *s)
855 fprintf(stderr, "aio does not support polled IO\n");
858 if (sq_thread_poll) {
859 fprintf(stderr, "aio does not support SQPOLL IO\n");
863 fprintf(stderr, "aio does not support polled IO\n");
866 if (fixedbufs || register_files) {
867 fprintf(stderr, "aio does not support registered files or buffers\n");
868 fixedbufs = register_files = 0;
871 s->per_file_depth = (depth + s->nr_files - 1) / s->nr_files;
872 return io_queue_init(roundup_pow2(depth), &s->aio_ctx);
874 fprintf(stderr, "Legacy AIO not available on this system/build\n");
880 static int setup_ring(struct submitter *s)
882 struct io_sq_ring *sring = &s->sq_ring;
883 struct io_cq_ring *cring = &s->cq_ring;
884 struct io_uring_params p;
889 memset(&p, 0, sizeof(p));
891 if (polled && !do_nop)
892 p.flags |= IORING_SETUP_IOPOLL;
893 if (sq_thread_poll) {
894 p.flags |= IORING_SETUP_SQPOLL;
895 if (sq_thread_cpu != -1) {
896 p.flags |= IORING_SETUP_SQ_AFF;
897 p.sq_thread_cpu = sq_thread_cpu;
901 p.flags |= IORING_SETUP_SQE128;
902 p.flags |= IORING_SETUP_CQE32;
905 fd = io_uring_setup(depth, &p);
907 perror("io_uring_setup");
910 s->ring_fd = s->enter_ring_fd = fd;
917 rlim.rlim_cur = RLIM_INFINITY;
918 rlim.rlim_max = RLIM_INFINITY;
919 /* ignore potential error, not needed on newer kernels */
920 setrlimit(RLIMIT_MEMLOCK, &rlim);
922 ret = io_uring_register_buffers(s);
924 perror("io_uring_register_buffers");
929 if (register_files) {
930 ret = io_uring_register_files(s);
932 perror("io_uring_register_files");
937 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
938 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
940 sring->head = ptr + p.sq_off.head;
941 sring->tail = ptr + p.sq_off.tail;
942 sring->ring_mask = ptr + p.sq_off.ring_mask;
943 sring->ring_entries = ptr + p.sq_off.ring_entries;
944 sring->flags = ptr + p.sq_off.flags;
945 sring->array = ptr + p.sq_off.array;
946 sq_ring_mask = *sring->ring_mask;
948 if (p.flags & IORING_SETUP_SQE128)
949 len = 2 * p.sq_entries * sizeof(struct io_uring_sqe);
951 len = p.sq_entries * sizeof(struct io_uring_sqe);
952 s->sqes = mmap(0, len,
953 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
956 if (p.flags & IORING_SETUP_CQE32) {
957 len = p.cq_off.cqes +
958 2 * p.cq_entries * sizeof(struct io_uring_cqe);
960 len = p.cq_off.cqes +
961 p.cq_entries * sizeof(struct io_uring_cqe);
964 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
966 cring->head = ptr + p.cq_off.head;
967 cring->tail = ptr + p.cq_off.tail;
968 cring->ring_mask = ptr + p.cq_off.ring_mask;
969 cring->ring_entries = ptr + p.cq_off.ring_entries;
970 cring->cqes = ptr + p.cq_off.cqes;
971 cq_ring_mask = *cring->ring_mask;
973 for (i = 0; i < p.sq_entries; i++)
976 s->per_file_depth = (depth + s->nr_files - 1) / s->nr_files;
980 static void *allocate_mem(struct submitter *s, int size)
984 #ifdef CONFIG_LIBNUMA
985 if (s->numa_node != -1)
986 return numa_alloc_onnode(size, s->numa_node);
989 if (posix_memalign(&buf, t_io_uring_page_size, bs)) {
990 printf("failed alloc\n");
997 static int submitter_init(struct submitter *s)
999 int i, nr_batch, err;
1000 static int init_printed;
1003 printf("submitter=%d, tid=%d, file=%s, nfiles=%d, node=%d\n", s->index, s->tid,
1004 s->filename, s->nr_files, s->numa_node);
1008 __init_rand64(&s->rand_state, s->tid);
1011 for (i = 0; i < MAX_FDS; i++)
1012 s->files[i].fileno = i;
1014 for (i = 0; i < roundup_pow2(depth); i++) {
1017 buf = allocate_mem(s, bs);
1020 s->iovecs[i].iov_base = buf;
1021 s->iovecs[i].iov_len = bs;
1025 sprintf(buf, "Engine=preadv2\n");
1028 err = setup_ring(s);
1030 sprintf(buf, "Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1032 sprintf(buf, "Engine=aio\n");
1036 printf("queue setup failed: %s, %d\n", strerror(errno), err);
1040 if (!init_printed) {
1041 printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, register_files, buffered, depth);
1047 nr_batch = roundup_pow2(depth / batch_submit);
1050 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
1053 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
1055 s->clock_batch = NULL;
1059 /* perform the expensive command initialization part for passthrough here
1060 * rather than in the fast path
1063 for (i = 0; i < roundup_pow2(depth); i++) {
1064 struct io_uring_sqe *sqe = &s->sqes[i << 1];
1066 memset(&sqe->cmd, 0, sizeof(struct nvme_uring_cmd));
1072 #ifdef CONFIG_LIBAIO
1073 static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
1080 while (index < max_ios) {
1081 struct iocb *iocb = &iocbs[index];
1083 f = get_next_file(s);
1085 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
1086 s->iovecs[index].iov_len, get_offset(s, f));
1089 if (stats && stats_running)
1090 data |= (((uint64_t) s->clock_index) << 32);
1091 iocb->data = (void *) (uintptr_t) data;
1097 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
1099 int last_idx = -1, stat_nr = 0;
1103 uint64_t data = (uintptr_t) events[reaped].data;
1104 struct file *f = &s->files[data & 0xffffffff];
1107 if (events[reaped].res != bs) {
1108 if (events[reaped].res == -ENODATA ||
1109 events[reaped].res == -EIO) {
1112 printf("io: unexpected ret=%ld\n", events[reaped].res);
1116 int clock_index = data >> 32;
1118 if (last_idx != clock_index) {
1119 if (last_idx != -1) {
1120 add_stat(s, last_idx, stat_nr);
1123 last_idx = clock_index;
1132 add_stat(s, last_idx, stat_nr);
1134 s->inflight -= reaped;
1139 static void *submitter_aio_fn(void *data)
1141 struct submitter *s = data;
1142 int i, ret, prepped;
1143 struct iocb **iocbsptr;
1145 struct io_event *events;
1146 #ifdef ARCH_HAVE_CPU_CLOCK
1150 ret = submitter_init(s);
1154 #ifdef ARCH_HAVE_CPU_CLOCK
1158 iocbsptr = calloc(depth, sizeof(struct iocb *));
1159 iocbs = calloc(depth, sizeof(struct iocb));
1160 events = calloc(depth, sizeof(struct io_event));
1162 for (i = 0; i < depth; i++)
1163 iocbsptr[i] = &iocbs[i];
1167 int to_wait, to_submit, to_prep;
1169 if (!prepped && s->inflight < depth) {
1170 to_prep = min(depth - s->inflight, batch_submit);
1171 prepped = prep_more_ios_aio(s, to_prep, iocbs);
1172 #ifdef ARCH_HAVE_CPU_CLOCK
1173 if (prepped && stats) {
1174 s->clock_batch[s->clock_index] = get_cpu_clock();
1175 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1179 s->inflight += prepped;
1180 to_submit = prepped;
1182 if (to_submit && (s->inflight + to_submit <= depth))
1185 to_wait = min(s->inflight + to_submit, batch_complete);
1187 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
1190 perror("io_submit");
1192 } else if (ret != to_submit) {
1193 printf("submitted %d, wanted %d\n", ret, to_submit);
1202 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
1204 perror("io_getevents");
1206 } else if (r != to_wait) {
1207 printf("r=%d, wait=%d\n", r, to_wait);
1210 r = reap_events_aio(s, events, r);
1214 } while (!s->finish);
1225 static void io_uring_unregister_ring(struct submitter *s)
1227 struct io_uring_rsrc_update up = {
1228 .offset = s->enter_ring_fd,
1231 syscall(__NR_io_uring_register, s->ring_fd, IORING_UNREGISTER_RING_FDS,
1235 static int io_uring_register_ring(struct submitter *s)
1237 struct io_uring_rsrc_update up = {
1243 ret = syscall(__NR_io_uring_register, s->ring_fd,
1244 IORING_REGISTER_RING_FDS, &up, 1);
1246 s->enter_ring_fd = up.offset;
1253 static void *submitter_uring_fn(void *data)
1255 struct submitter *s = data;
1256 struct io_sq_ring *ring = &s->sq_ring;
1258 #ifdef ARCH_HAVE_CPU_CLOCK
1262 ret = submitter_init(s);
1266 #ifdef ARCH_HAVE_CPU_CLOCK
1271 io_uring_register_ring(s);
1275 int to_wait, to_submit, this_reap, to_prep;
1276 unsigned ring_flags = 0;
1278 if (!prepped && s->inflight < depth) {
1279 to_prep = min(depth - s->inflight, batch_submit);
1280 prepped = prep_more_ios_uring(s, to_prep);
1281 #ifdef ARCH_HAVE_CPU_CLOCK
1282 if (prepped && stats) {
1283 s->clock_batch[s->clock_index] = get_cpu_clock();
1284 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1288 s->inflight += prepped;
1290 to_submit = prepped;
1292 if (to_submit && (s->inflight + to_submit <= depth))
1295 to_wait = min(s->inflight + to_submit, batch_complete);
1298 * Only need to call io_uring_enter if we're not using SQ thread
1299 * poll, or if IORING_SQ_NEED_WAKEUP is set.
1302 ring_flags = atomic_load_acquire(ring->flags);
1303 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
1307 flags = IORING_ENTER_GETEVENTS;
1308 if (ring_flags & IORING_SQ_NEED_WAKEUP)
1309 flags |= IORING_ENTER_SQ_WAKEUP;
1310 ret = io_uring_enter(s, to_submit, to_wait, flags);
1313 /* for SQPOLL, we submitted it all effectively */
1318 * For non SQ thread poll, we already got the events we needed
1319 * through the io_uring_enter() above. For SQ thread poll, we
1320 * need to loop here until we find enough events.
1327 r = reap_events_uring_pt(s);
1329 r = reap_events_uring(s);
1335 } while (sq_thread_poll && this_reap < to_wait);
1336 s->reaps += this_reap;
1344 } else if (ret < to_submit) {
1345 int diff = to_submit - ret;
1354 } else if (ret < 0) {
1355 if (errno == EAGAIN) {
1363 printf("io_submit: %s\n", strerror(errno));
1366 } while (!s->finish);
1369 io_uring_unregister_ring(s);
1376 #ifdef CONFIG_PWRITEV2
1377 static void *submitter_sync_fn(void *data)
1379 struct submitter *s = data;
1382 if (submitter_init(s) < 0)
1389 f = get_next_file(s);
1391 #ifdef ARCH_HAVE_CPU_CLOCK
1393 s->clock_batch[s->clock_index] = get_cpu_clock();
1399 offset = get_offset(s, f);
1401 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, RWF_HIPRI);
1403 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, 0);
1408 } else if (ret != bs) {
1416 add_stat(s, s->clock_index, 1);
1417 } while (!s->finish);
1424 static void *submitter_sync_fn(void *data)
1431 static struct submitter *get_submitter(int offset)
1437 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
1441 static void do_finish(const char *reason)
1445 printf("Exiting on %s\n", reason);
1446 for (j = 0; j < nthreads; j++) {
1447 struct submitter *s = get_submitter(j);
1450 if (max_iops > 1000000) {
1451 double miops = (double) max_iops / 1000000.0;
1452 printf("Maximum IOPS=%.2fM\n", miops);
1453 } else if (max_iops > 100000) {
1454 double kiops = (double) max_iops / 1000.0;
1455 printf("Maximum IOPS=%.2fK\n", kiops);
1457 printf("Maximum IOPS=%lu\n", max_iops);
1462 static void sig_int(int sig)
1464 do_finish("signal");
1467 static void arm_sig_int(void)
1469 struct sigaction act;
1471 memset(&act, 0, sizeof(act));
1472 act.sa_handler = sig_int;
1473 act.sa_flags = SA_RESTART;
1474 sigaction(SIGINT, &act, NULL);
1476 /* Windows uses SIGBREAK as a quit signal from other applications */
1478 sigaction(SIGBREAK, &act, NULL);
1482 static void usage(char *argv, int status)
1484 char runtime_str[16];
1485 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
1486 printf("%s [options] -- [filenames]\n"
1487 " -d <int> : IO Depth, default %d\n"
1488 " -s <int> : Batch submit, default %d\n"
1489 " -c <int> : Batch complete, default %d\n"
1490 " -b <int> : Block size, default %d\n"
1491 " -p <bool> : Polled IO, default %d\n"
1492 " -B <bool> : Fixed buffers, default %d\n"
1493 " -F <bool> : Register files, default %d\n"
1494 " -n <int> : Number of threads, default %d\n"
1495 " -O <bool> : Use O_DIRECT, default %d\n"
1496 " -N <bool> : Perform just no-op requests, default %d\n"
1497 " -t <bool> : Track IO latencies, default %d\n"
1498 " -T <int> : TSC rate in HZ\n"
1499 " -r <int> : Runtime in seconds, default %s\n"
1500 " -R <bool> : Use random IO, default %d\n"
1501 " -a <bool> : Use legacy aio, default %d\n"
1502 " -S <bool> : Use sync IO (preadv2), default %d\n"
1503 " -X <bool> : Use registered ring %d\n"
1504 " -P <bool> : Automatically place on device home node %d\n"
1505 " -u <bool> : Use nvme-passthrough I/O, default %d\n",
1506 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1507 fixedbufs, register_files, nthreads, !buffered, do_nop,
1508 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio,
1509 use_sync, register_ring, numa_placement, pt);
1513 static void read_tsc_rate(void)
1521 fd = open(TSC_RATE_FILE, O_RDONLY);
1525 ret = read(fd, buffer, sizeof(buffer));
1531 tsc_rate = strtoul(buffer, NULL, 10);
1532 printf("Using TSC rate %luHz\n", tsc_rate);
1536 static void write_tsc_rate(void)
1542 if (!stat(TSC_RATE_FILE, &sb))
1545 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1549 memset(buffer, 0, sizeof(buffer));
1550 sprintf(buffer, "%lu", tsc_rate);
1551 ret = write(fd, buffer, strlen(buffer));
1557 int main(int argc, char *argv[])
1559 struct submitter *s;
1560 unsigned long done, calls, reap, io_errors;
1561 int i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1565 if (!do_nop && argc < 2)
1568 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) {
1571 aio = !!atoi(optarg);
1574 depth = atoi(optarg);
1577 batch_submit = atoi(optarg);
1582 batch_complete = atoi(optarg);
1583 if (!batch_complete)
1590 polled = !!atoi(optarg);
1593 fixedbufs = !!atoi(optarg);
1596 register_files = !!atoi(optarg);
1599 nthreads = atoi(optarg);
1601 printf("Threads must be non-zero\n");
1606 do_nop = !!atoi(optarg);
1609 buffered = !atoi(optarg);
1612 #ifndef ARCH_HAVE_CPU_CLOCK
1613 fprintf(stderr, "Stats not supported on this CPU\n");
1616 stats = !!atoi(optarg);
1619 #ifndef ARCH_HAVE_CPU_CLOCK
1620 fprintf(stderr, "Stats not supported on this CPU\n");
1623 tsc_rate = strtoul(optarg, NULL, 10);
1627 runtime = atoi(optarg);
1630 random_io = !!atoi(optarg);
1633 register_ring = !!atoi(optarg);
1636 #ifdef CONFIG_PWRITEV2
1637 use_sync = !!atoi(optarg);
1639 fprintf(stderr, "preadv2 not supported\n");
1644 numa_placement = !!atoi(optarg);
1647 pt = !!atoi(optarg);
1660 if (batch_complete > depth)
1661 batch_complete = depth;
1662 if (batch_submit > depth)
1663 batch_submit = depth;
1665 submitter = calloc(nthreads, sizeof(*submitter) +
1666 roundup_pow2(depth) * sizeof(struct iovec));
1667 for (j = 0; j < nthreads; j++) {
1668 s = get_submitter(j);
1671 s->done = s->calls = s->reaps = s->io_errors = 0;
1674 flags = O_RDONLY | O_NOATIME;
1683 printf("No files specified\n");
1686 threads_per_f = nthreads / nfiles;
1687 /* make sure each thread gets assigned files */
1688 if (threads_per_f == 0) {
1691 threads_rem = nthreads - threads_per_f * nfiles;
1694 while (!do_nop && i < argc) {
1697 memset(&f, 0, sizeof(f));
1699 fd = open(argv[i], flags);
1705 if (get_file_size(&f)) {
1706 printf("failed getting size of device/file\n");
1709 if (f.max_blocks <= 1) {
1710 printf("Zero file/device size?\n");
1715 limit = threads_per_f;
1716 limit += threads_rem > 0 ? 1 : 0;
1717 for (k = 0; k < limit; k++) {
1718 s = get_submitter((j + k) % nthreads);
1720 if (s->nr_files == MAX_FDS) {
1721 printf("Max number of files (%d) reached\n", MAX_FDS);
1725 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1728 detect_node(s, argv[i]);
1730 s->filename = argv[i];
1740 t_io_uring_page_size = sysconf(_SC_PAGESIZE);
1741 if (t_io_uring_page_size < 0)
1742 t_io_uring_page_size = 4096;
1744 for (j = 0; j < nthreads; j++) {
1745 s = get_submitter(j);
1747 pthread_create(&s->thread, NULL, submitter_sync_fn, s);
1749 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1750 #ifdef CONFIG_LIBAIO
1752 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1756 reap = calls = done = io_errors = 0;
1758 unsigned long this_done = 0;
1759 unsigned long this_reap = 0;
1760 unsigned long this_call = 0;
1761 unsigned long this_io_errors = 0;
1762 unsigned long rpc = 0, ipc = 0;
1763 unsigned long iops, bw;
1766 if (runtime && !--runtime)
1767 do_finish("timeout");
1769 /* don't print partial run, if interrupted by signal */
1773 /* one second in to the run, enable stats */
1777 for (j = 0; j < nthreads; j++) {
1778 s = get_submitter(j);
1779 this_done += s->done;
1780 this_call += s->calls;
1781 this_reap += s->reaps;
1782 this_io_errors += s->io_errors;
1784 if (this_call - calls) {
1785 rpc = (this_done - done) / (this_call - calls);
1786 ipc = (this_reap - reap) / (this_call - calls);
1789 iops = this_done - done;
1790 iops -= this_io_errors - io_errors;
1792 bw = iops * (bs / 1048576);
1794 bw = iops / (1048576 / bs);
1795 if (iops > 1000000) {
1796 double miops = (double) iops / 1000000.0;
1797 printf("IOPS=%.2fM, ", miops);
1798 } else if (iops > 100000) {
1799 double kiops = (double) iops / 1000.0;
1800 printf("IOPS=%.2fK, ", kiops);
1802 printf("IOPS=%lu, ", iops);
1804 max_iops = max(max_iops, iops);
1807 double bw_g = (double) bw / 1000.0;
1809 printf("BW=%.2fGiB/s, ", bw_g);
1811 printf("BW=%luMiB/s, ", bw);
1814 printf("IOS/call=%ld/%ld\n", rpc, ipc);
1818 io_errors = this_io_errors;
1821 for (j = 0; j < nthreads; j++) {
1822 s = get_submitter(j);
1823 pthread_join(s->thread, &ret);
1827 printf("%d: %lu IO errors\n", s->tid, s->io_errors);
1832 printf("%d: Latency percentiles:\n", s->tid);
1833 for (i = 0, nr = 0; i < PLAT_NR; i++)
1835 show_clat_percentiles(s->plat, nr, 4);
1836 free(s->clock_batch);