18 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/syscall.h>
22 #include <sys/resource.h>
32 #include "../arch/arch.h"
34 #include "../lib/types.h"
35 #include "../lib/roundup.h"
36 #include "../lib/rand.h"
37 #include "../minmax.h"
38 #include "../os/linux/io_uring.h"
39 #include "../engines/nvme.h"
45 unsigned *ring_entries;
54 unsigned *ring_entries;
55 struct io_uring_cqe *cqes;
59 #define BATCH_SUBMIT 32
60 #define BATCH_COMPLETE 32
65 static unsigned sq_ring_mask, cq_ring_mask;
68 unsigned long max_blocks;
69 unsigned long max_size;
70 unsigned long cur_off;
72 unsigned int nsid; /* nsid field required for nvme-passthrough */
73 unsigned int lba_shift; /* lba_shift field required for nvme-passthrough */
80 #define PLAT_VAL (1 << PLAT_BITS)
81 #define PLAT_GROUP_NR 29
82 #define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
89 struct io_sq_ring sq_ring;
90 struct io_uring_sqe *sqes;
91 struct io_cq_ring cq_ring;
101 struct taus258_state rand_state;
103 unsigned long *clock_batch;
108 io_context_t aio_ctx;
112 const char *filename;
114 struct file files[MAX_FDS];
117 struct iovec iovecs[];
120 static struct submitter *submitter;
121 static volatile int finish;
122 static int stats_running;
123 static unsigned long max_iops;
124 static long t_io_uring_page_size;
126 static int depth = DEPTH;
127 static int batch_submit = BATCH_SUBMIT;
128 static int batch_complete = BATCH_COMPLETE;
130 static int polled = 1; /* use IO polling */
131 static int fixedbufs = 1; /* use fixed user buffers */
132 static int dma_map; /* pre-map DMA 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 #ifndef IORING_REGISTER_MAP_BUFFERS
159 #define IORING_REGISTER_MAP_BUFFERS 22
160 struct io_uring_map_buffers {
169 static int nvme_identify(int fd, __u32 nsid, enum nvme_identify_cns cns,
170 enum nvme_csi csi, void *data)
172 struct nvme_passthru_cmd cmd = {
173 .opcode = nvme_admin_identify,
175 .addr = (__u64)(uintptr_t)data,
176 .data_len = NVME_IDENTIFY_DATA_SIZE,
178 .cdw11 = csi << NVME_IDENTIFY_CSI_SHIFT,
179 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
182 return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
185 static int nvme_get_info(int fd, __u32 *nsid, __u32 *lba_sz, __u64 *nlba)
187 struct nvme_id_ns ns;
191 namespace_id = ioctl(fd, NVME_IOCTL_ID);
192 if (namespace_id < 0) {
193 fprintf(stderr, "error failed to fetch namespace-id\n");
199 * Identify namespace to get namespace-id, namespace size in LBA's
202 err = nvme_identify(fd, namespace_id, NVME_IDENTIFY_CNS_NS,
205 fprintf(stderr, "error failed to fetch identify namespace\n");
210 *nsid = namespace_id;
211 *lba_sz = 1 << ns.lbaf[(ns.flbas & 0x0f)].ds;
217 static unsigned long cycles_to_nsec(unsigned long cycles)
224 val = cycles * 1000000000ULL;
225 return val / tsc_rate;
228 static unsigned long plat_idx_to_val(unsigned int idx)
230 unsigned int error_bits;
231 unsigned long k, base;
233 assert(idx < PLAT_NR);
235 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
236 * all bits of the sample as index */
237 if (idx < (PLAT_VAL << 1))
238 return cycles_to_nsec(idx);
240 /* Find the group and compute the minimum value of that group */
241 error_bits = (idx >> PLAT_BITS) - 1;
242 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
244 /* Find its bucket number of the group */
247 /* Return the mean of the range of the bucket */
248 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
251 unsigned int calculate_clat_percentiles(unsigned long *io_u_plat,
252 unsigned long nr, unsigned long **output,
253 unsigned long *maxv, unsigned long *minv)
255 unsigned long sum = 0;
256 unsigned int len = plist_len, i, j = 0;
257 unsigned long *ovals = NULL;
263 ovals = malloc(len * sizeof(*ovals));
268 * Calculate bucket values, note down max and min values
271 for (i = 0; i < PLAT_NR && !is_last; i++) {
273 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
274 assert(plist[j] <= 100.0);
276 ovals[j] = plat_idx_to_val(i);
277 if (ovals[j] < *minv)
279 if (ovals[j] > *maxv)
282 is_last = (j == len - 1) != 0;
291 fprintf(stderr, "error calculating latency percentiles\n");
297 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
298 unsigned int precision)
300 unsigned int divisor, len, i, j = 0;
301 unsigned long minv, maxv;
302 unsigned long *ovals;
303 int per_line, scale_down, time_width;
307 len = calculate_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
314 printf(" percentiles (tsc ticks):\n |");
315 } else if (minv > 2000 && maxv > 99999) {
318 printf(" percentiles (usec):\n |");
322 printf(" percentiles (nsec):\n |");
325 time_width = max(5, (int) (log10(maxv / divisor) + 1));
326 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
327 precision, time_width);
328 /* fmt will be something like " %5.2fth=[%4llu]%c" */
329 per_line = (80 - 7) / (precision + 10 + time_width);
331 for (j = 0; j < len; j++) {
333 if (j != 0 && (j % per_line) == 0)
336 /* end of the list */
337 is_last = (j == len - 1) != 0;
339 for (i = 0; i < scale_down; i++)
340 ovals[j] = (ovals[j] + 999) / 1000;
342 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
347 if ((j % per_line) == per_line - 1) /* for formatting */
355 #ifdef ARCH_HAVE_CPU_CLOCK
356 static unsigned int plat_val_to_idx(unsigned long val)
358 unsigned int msb, error_bits, base, offset, idx;
360 /* Find MSB starting from bit 0 */
364 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
367 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
368 * all bits of the sample as index
370 if (msb <= PLAT_BITS)
373 /* Compute the number of error bits to discard*/
374 error_bits = msb - PLAT_BITS;
376 /* Compute the number of buckets before the group */
377 base = (error_bits + 1) << PLAT_BITS;
380 * Discard the error bits and apply the mask to find the
381 * index for the buckets in the group
383 offset = (PLAT_VAL - 1) & (val >> error_bits);
385 /* Make sure the index does not exceed (array size - 1) */
386 idx = (base + offset) < (PLAT_NR - 1) ?
387 (base + offset) : (PLAT_NR - 1);
393 static void add_stat(struct submitter *s, int clock_index, int nr)
395 #ifdef ARCH_HAVE_CPU_CLOCK
396 unsigned long cycles;
399 if (!s->finish && clock_index) {
400 cycles = get_cpu_clock();
401 cycles -= s->clock_batch[clock_index];
402 pidx = plat_val_to_idx(cycles);
408 static int io_uring_map_buffers(struct submitter *s)
410 struct io_uring_map_buffers map = {
411 .fd = s->files[0].real_fd,
418 fprintf(stdout, "Mapping buffers may not work with multiple files\n");
420 return syscall(__NR_io_uring_register, s->ring_fd,
421 IORING_REGISTER_MAP_BUFFERS, &map, 1);
424 static int io_uring_register_buffers(struct submitter *s)
429 return syscall(__NR_io_uring_register, s->ring_fd,
430 IORING_REGISTER_BUFFERS, s->iovecs, roundup_pow2(depth));
433 static int io_uring_register_files(struct submitter *s)
440 s->fds = calloc(s->nr_files, sizeof(__s32));
441 for (i = 0; i < s->nr_files; i++) {
442 s->fds[i] = s->files[i].real_fd;
443 s->files[i].fixed_fd = i;
446 return syscall(__NR_io_uring_register, s->ring_fd,
447 IORING_REGISTER_FILES, s->fds, s->nr_files);
450 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
453 * Clamp CQ ring size at our SQ ring size, we don't need more entries
456 p->flags |= IORING_SETUP_CQSIZE;
457 p->cq_entries = entries;
459 return syscall(__NR_io_uring_setup, entries, p);
462 static void io_uring_probe(int fd)
464 struct io_uring_probe *p;
467 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
471 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
472 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
476 if (IORING_OP_READ > p->ops_len)
479 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
485 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
486 unsigned int min_complete, unsigned int flags)
489 flags |= IORING_ENTER_REGISTERED_RING;
490 #ifdef FIO_ARCH_HAS_SYSCALL
491 return __do_syscall6(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
492 min_complete, flags, NULL, 0);
494 return syscall(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
495 min_complete, flags, NULL, 0);
499 static unsigned file_depth(struct submitter *s)
501 return (depth + s->nr_files - 1) / s->nr_files;
504 static void init_io(struct submitter *s, unsigned index)
506 struct io_uring_sqe *sqe = &s->sqes[index];
507 unsigned long offset;
512 sqe->opcode = IORING_OP_NOP;
516 if (s->nr_files == 1) {
519 f = &s->files[s->cur_file];
520 if (f->pending_ios >= file_depth(s)) {
522 if (s->cur_file == s->nr_files)
524 f = &s->files[s->cur_file];
530 r = __rand64(&s->rand_state);
531 offset = (r % (f->max_blocks - 1)) * bs;
535 if (f->cur_off + bs > f->max_size)
539 if (register_files) {
540 sqe->flags = IOSQE_FIXED_FILE;
541 sqe->fd = f->fixed_fd;
544 sqe->fd = f->real_fd;
547 sqe->opcode = IORING_OP_READ_FIXED;
548 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
550 sqe->buf_index = index;
551 } else if (!vectored) {
552 sqe->opcode = IORING_OP_READ;
553 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
557 sqe->opcode = IORING_OP_READV;
558 sqe->addr = (unsigned long) &s->iovecs[index];
564 sqe->user_data = (unsigned long) f->fileno;
565 if (stats && stats_running)
566 sqe->user_data |= ((uint64_t)s->clock_index << 32);
569 static void init_io_pt(struct submitter *s, unsigned index)
571 struct io_uring_sqe *sqe = &s->sqes[index << 1];
572 unsigned long offset;
574 struct nvme_uring_cmd *cmd;
575 unsigned long long slba;
576 unsigned long long nlb;
579 if (s->nr_files == 1) {
582 f = &s->files[s->cur_file];
583 if (f->pending_ios >= file_depth(s)) {
585 if (s->cur_file == s->nr_files)
587 f = &s->files[s->cur_file];
593 r = __rand64(&s->rand_state);
594 offset = (r % (f->max_blocks - 1)) * bs;
598 if (f->cur_off + bs > f->max_size)
602 if (register_files) {
603 sqe->fd = f->fixed_fd;
604 sqe->flags = IOSQE_FIXED_FILE;
606 sqe->fd = f->real_fd;
609 sqe->opcode = IORING_OP_URING_CMD;
610 sqe->user_data = (unsigned long) f->fileno;
612 sqe->user_data |= ((__u64) s->clock_index << 32ULL);
613 sqe->cmd_op = NVME_URING_CMD_IO;
614 slba = offset >> f->lba_shift;
615 nlb = (bs >> f->lba_shift) - 1;
616 cmd = (struct nvme_uring_cmd *)&sqe->cmd;
617 /* cdw10 and cdw11 represent starting slba*/
618 cmd->cdw10 = slba & 0xffffffff;
619 cmd->cdw11 = slba >> 32;
620 /* cdw12 represent number of lba to be read*/
622 cmd->addr = (unsigned long) s->iovecs[index].iov_base;
628 static int prep_more_ios_uring(struct submitter *s, int max_ios)
630 struct io_sq_ring *ring = &s->sq_ring;
631 unsigned index, tail, next_tail, prepped = 0;
633 next_tail = tail = *ring->tail;
636 if (next_tail == atomic_load_acquire(ring->head))
639 index = tail & sq_ring_mask;
641 init_io_pt(s, index);
644 ring->array[index] = index;
647 } while (prepped < max_ios);
650 atomic_store_release(ring->tail, tail);
654 static int get_file_size(struct file *f)
658 if (fstat(f->real_fd, &st) < 0)
665 if (!S_ISCHR(st.st_mode)) {
666 fprintf(stderr, "passthrough works with only nvme-ns "
667 "generic devices (/dev/ngXnY)\n");
670 ret = nvme_get_info(f->real_fd, &f->nsid, &lbs, &nlba);
673 if ((bs % lbs) != 0) {
674 printf("error: bs:%d should be a multiple logical_block_size:%d\n",
678 f->max_blocks = nlba / bs;
680 f->lba_shift = ilog2(lbs);
682 } else if (S_ISBLK(st.st_mode)) {
683 unsigned long long bytes;
685 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
688 f->max_blocks = bytes / bs;
691 } else if (S_ISREG(st.st_mode)) {
692 f->max_blocks = st.st_size / bs;
693 f->max_size = st.st_size;
700 static int reap_events_uring(struct submitter *s)
702 struct io_cq_ring *ring = &s->cq_ring;
703 struct io_uring_cqe *cqe;
704 unsigned head, reaped = 0;
705 int last_idx = -1, stat_nr = 0;
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 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");
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;
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);
830 sprintf(str, "/sys/block/%s/device/numa_node", base);
831 fd = open(str, O_RDONLY);
835 ret = read(fd, str, sizeof(str));
849 static int setup_aio(struct submitter *s)
853 fprintf(stderr, "aio does not support polled IO\n");
856 if (sq_thread_poll) {
857 fprintf(stderr, "aio does not support SQPOLL IO\n");
861 fprintf(stderr, "aio does not support polled IO\n");
864 if (fixedbufs || register_files) {
865 fprintf(stderr, "aio does not support registered files or buffers\n");
866 fixedbufs = register_files = 0;
869 return io_queue_init(roundup_pow2(depth), &s->aio_ctx);
871 fprintf(stderr, "Legacy AIO not available on this system/build\n");
877 static int setup_ring(struct submitter *s)
879 struct io_sq_ring *sring = &s->sq_ring;
880 struct io_cq_ring *cring = &s->cq_ring;
881 struct io_uring_params p;
886 memset(&p, 0, sizeof(p));
888 if (polled && !do_nop)
889 p.flags |= IORING_SETUP_IOPOLL;
890 if (sq_thread_poll) {
891 p.flags |= IORING_SETUP_SQPOLL;
892 if (sq_thread_cpu != -1) {
893 p.flags |= IORING_SETUP_SQ_AFF;
894 p.sq_thread_cpu = sq_thread_cpu;
898 p.flags |= IORING_SETUP_SQE128;
899 p.flags |= IORING_SETUP_CQE32;
902 fd = io_uring_setup(depth, &p);
904 perror("io_uring_setup");
907 s->ring_fd = s->enter_ring_fd = fd;
914 rlim.rlim_cur = RLIM_INFINITY;
915 rlim.rlim_max = RLIM_INFINITY;
916 /* ignore potential error, not needed on newer kernels */
917 setrlimit(RLIMIT_MEMLOCK, &rlim);
919 ret = io_uring_register_buffers(s);
921 perror("io_uring_register_buffers");
926 ret = io_uring_map_buffers(s);
928 perror("io_uring_map_buffers");
934 if (register_files) {
935 ret = io_uring_register_files(s);
937 perror("io_uring_register_files");
942 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
943 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
945 sring->head = ptr + p.sq_off.head;
946 sring->tail = ptr + p.sq_off.tail;
947 sring->ring_mask = ptr + p.sq_off.ring_mask;
948 sring->ring_entries = ptr + p.sq_off.ring_entries;
949 sring->flags = ptr + p.sq_off.flags;
950 sring->array = ptr + p.sq_off.array;
951 sq_ring_mask = *sring->ring_mask;
953 if (p.flags & IORING_SETUP_SQE128)
954 len = 2 * p.sq_entries * sizeof(struct io_uring_sqe);
956 len = p.sq_entries * sizeof(struct io_uring_sqe);
957 s->sqes = mmap(0, len,
958 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
961 if (p.flags & IORING_SETUP_CQE32) {
962 len = p.cq_off.cqes +
963 2 * p.cq_entries * sizeof(struct io_uring_cqe);
965 len = p.cq_off.cqes +
966 p.cq_entries * sizeof(struct io_uring_cqe);
969 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
971 cring->head = ptr + p.cq_off.head;
972 cring->tail = ptr + p.cq_off.tail;
973 cring->ring_mask = ptr + p.cq_off.ring_mask;
974 cring->ring_entries = ptr + p.cq_off.ring_entries;
975 cring->cqes = ptr + p.cq_off.cqes;
976 cq_ring_mask = *cring->ring_mask;
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, node=%d\n", s->index, s->tid,
1004 s->filename, 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);
1029 sprintf(buf, "Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1031 sprintf(buf, "Engine=aio\n");
1035 printf("queue setup failed: %s, %d\n", strerror(errno), err);
1039 if (!init_printed) {
1040 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
1046 nr_batch = roundup_pow2(depth / batch_submit);
1049 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
1052 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
1054 s->clock_batch = NULL;
1058 /* perform the expensive command initialization part for passthrough here
1059 * rather than in the fast path
1062 for (i = 0; i < roundup_pow2(depth); i++) {
1063 struct io_uring_sqe *sqe = &s->sqes[i << 1];
1065 memset(&sqe->cmd, 0, sizeof(struct nvme_uring_cmd));
1071 #ifdef CONFIG_LIBAIO
1072 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 if (s->nr_files == 1) {
1087 f = &s->files[s->cur_file];
1088 if (f->pending_ios >= file_depth(s)) {
1090 if (s->cur_file == s->nr_files)
1092 f = &s->files[s->cur_file];
1098 offset = (r % (f->max_blocks - 1)) * bs;
1099 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
1100 s->iovecs[index].iov_len, offset);
1103 if (stats && stats_running)
1104 data |= (((uint64_t) s->clock_index) << 32);
1105 iocb->data = (void *) (uintptr_t) data;
1111 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
1113 int last_idx = -1, stat_nr = 0;
1117 uint64_t data = (uintptr_t) events[reaped].data;
1118 struct file *f = &s->files[data & 0xffffffff];
1121 if (events[reaped].res != bs) {
1122 printf("io: unexpected ret=%ld\n", events[reaped].res);
1126 int clock_index = data >> 32;
1128 if (last_idx != clock_index) {
1129 if (last_idx != -1) {
1130 add_stat(s, last_idx, stat_nr);
1133 last_idx = clock_index;
1142 add_stat(s, last_idx, stat_nr);
1144 s->inflight -= reaped;
1149 static void *submitter_aio_fn(void *data)
1151 struct submitter *s = data;
1152 int i, ret, prepped;
1153 struct iocb **iocbsptr;
1155 struct io_event *events;
1156 #ifdef ARCH_HAVE_CPU_CLOCK
1157 int nr_batch = submitter_init(s);
1162 iocbsptr = calloc(depth, sizeof(struct iocb *));
1163 iocbs = calloc(depth, sizeof(struct iocb));
1164 events = calloc(depth, sizeof(struct io_event));
1166 for (i = 0; i < depth; i++)
1167 iocbsptr[i] = &iocbs[i];
1171 int to_wait, to_submit, to_prep;
1173 if (!prepped && s->inflight < depth) {
1174 to_prep = min(depth - s->inflight, batch_submit);
1175 prepped = prep_more_ios_aio(s, to_prep, iocbs);
1176 #ifdef ARCH_HAVE_CPU_CLOCK
1177 if (prepped && stats) {
1178 s->clock_batch[s->clock_index] = get_cpu_clock();
1179 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1183 s->inflight += prepped;
1184 to_submit = prepped;
1186 if (to_submit && (s->inflight + to_submit <= depth))
1189 to_wait = min(s->inflight + to_submit, batch_complete);
1191 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
1194 perror("io_submit");
1196 } else if (ret != to_submit) {
1197 printf("submitted %d, wanted %d\n", ret, to_submit);
1206 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
1208 perror("io_getevents");
1210 } else if (r != to_wait) {
1211 printf("r=%d, wait=%d\n", r, to_wait);
1214 r = reap_events_aio(s, events, r);
1218 } while (!s->finish);
1228 static void io_uring_unregister_ring(struct submitter *s)
1230 struct io_uring_rsrc_update up = {
1231 .offset = s->enter_ring_fd,
1234 syscall(__NR_io_uring_register, s->ring_fd, IORING_UNREGISTER_RING_FDS,
1238 static int io_uring_register_ring(struct submitter *s)
1240 struct io_uring_rsrc_update up = {
1246 ret = syscall(__NR_io_uring_register, s->ring_fd,
1247 IORING_REGISTER_RING_FDS, &up, 1);
1249 s->enter_ring_fd = up.offset;
1256 static void *submitter_uring_fn(void *data)
1258 struct submitter *s = data;
1259 struct io_sq_ring *ring = &s->sq_ring;
1261 #ifdef ARCH_HAVE_CPU_CLOCK
1262 int nr_batch = submitter_init(s);
1268 io_uring_register_ring(s);
1272 int to_wait, to_submit, this_reap, to_prep;
1273 unsigned ring_flags = 0;
1275 if (!prepped && s->inflight < depth) {
1276 to_prep = min(depth - s->inflight, batch_submit);
1277 prepped = prep_more_ios_uring(s, to_prep);
1278 #ifdef ARCH_HAVE_CPU_CLOCK
1279 if (prepped && stats) {
1280 s->clock_batch[s->clock_index] = get_cpu_clock();
1281 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1285 s->inflight += prepped;
1287 to_submit = prepped;
1289 if (to_submit && (s->inflight + to_submit <= depth))
1292 to_wait = min(s->inflight + to_submit, batch_complete);
1295 * Only need to call io_uring_enter if we're not using SQ thread
1296 * poll, or if IORING_SQ_NEED_WAKEUP is set.
1299 ring_flags = atomic_load_acquire(ring->flags);
1300 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
1304 flags = IORING_ENTER_GETEVENTS;
1305 if (ring_flags & IORING_SQ_NEED_WAKEUP)
1306 flags |= IORING_ENTER_SQ_WAKEUP;
1307 ret = io_uring_enter(s, to_submit, to_wait, flags);
1310 /* for SQPOLL, we submitted it all effectively */
1315 * For non SQ thread poll, we already got the events we needed
1316 * through the io_uring_enter() above. For SQ thread poll, we
1317 * need to loop here until we find enough events.
1324 r = reap_events_uring_pt(s);
1326 r = reap_events_uring(s);
1332 } while (sq_thread_poll && this_reap < to_wait);
1333 s->reaps += this_reap;
1341 } else if (ret < to_submit) {
1342 int diff = to_submit - ret;
1351 } else if (ret < 0) {
1352 if (errno == EAGAIN) {
1360 printf("io_submit: %s\n", strerror(errno));
1363 } while (!s->finish);
1366 io_uring_unregister_ring(s);
1372 #ifdef CONFIG_PWRITEV2
1373 static void *submitter_sync_fn(void *data)
1375 struct submitter *s = data;
1385 if (s->nr_files == 1) {
1388 f = &s->files[s->cur_file];
1389 if (f->pending_ios >= file_depth(s)) {
1391 if (s->cur_file == s->nr_files)
1393 f = &s->files[s->cur_file];
1399 r = __rand64(&s->rand_state);
1400 offset = (r % (f->max_blocks - 1)) * bs;
1402 offset = f->cur_off;
1404 if (f->cur_off + bs > f->max_size)
1408 #ifdef ARCH_HAVE_CPU_CLOCK
1410 s->clock_batch[s->clock_index] = get_cpu_clock();
1417 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, RWF_HIPRI);
1419 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, 0);
1424 } else if (ret != bs) {
1432 add_stat(s, s->clock_index, 1);
1433 } while (!s->finish);
1439 static void *submitter_sync_fn(void *data)
1446 static struct submitter *get_submitter(int offset)
1452 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
1456 static void do_finish(const char *reason)
1460 printf("Exiting on %s\n", reason);
1461 for (j = 0; j < nthreads; j++) {
1462 struct submitter *s = get_submitter(j);
1465 if (max_iops > 1000000) {
1466 double miops = (double) max_iops / 1000000.0;
1467 printf("Maximum IOPS=%.2fM\n", miops);
1468 } else if (max_iops > 100000) {
1469 double kiops = (double) max_iops / 1000.0;
1470 printf("Maximum IOPS=%.2fK\n", kiops);
1472 printf("Maximum IOPS=%lu\n", max_iops);
1477 static void sig_int(int sig)
1479 do_finish("signal");
1482 static void arm_sig_int(void)
1484 struct sigaction act;
1486 memset(&act, 0, sizeof(act));
1487 act.sa_handler = sig_int;
1488 act.sa_flags = SA_RESTART;
1489 sigaction(SIGINT, &act, NULL);
1491 /* Windows uses SIGBREAK as a quit signal from other applications */
1493 sigaction(SIGBREAK, &act, NULL);
1497 static void usage(char *argv, int status)
1499 char runtime_str[16];
1500 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
1501 printf("%s [options] -- [filenames]\n"
1502 " -d <int> : IO Depth, default %d\n"
1503 " -s <int> : Batch submit, default %d\n"
1504 " -c <int> : Batch complete, default %d\n"
1505 " -b <int> : Block size, default %d\n"
1506 " -p <bool> : Polled IO, default %d\n"
1507 " -B <bool> : Fixed buffers, default %d\n"
1508 " -D <bool> : DMA map fixed buffers, default %d\n"
1509 " -F <bool> : Register files, default %d\n"
1510 " -n <int> : Number of threads, default %d\n"
1511 " -O <bool> : Use O_DIRECT, default %d\n"
1512 " -N <bool> : Perform just no-op requests, default %d\n"
1513 " -t <bool> : Track IO latencies, default %d\n"
1514 " -T <int> : TSC rate in HZ\n"
1515 " -r <int> : Runtime in seconds, default %s\n"
1516 " -R <bool> : Use random IO, default %d\n"
1517 " -a <bool> : Use legacy aio, default %d\n"
1518 " -S <bool> : Use sync IO (preadv2), default %d\n"
1519 " -X <bool> : Use registered ring %d\n"
1520 " -P <bool> : Automatically place on device home node %d\n"
1521 " -u <bool> : Use nvme-passthrough I/O, default %d\n",
1522 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1523 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
1524 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio,
1525 use_sync, register_ring, numa_placement, pt);
1529 static void read_tsc_rate(void)
1537 fd = open(TSC_RATE_FILE, O_RDONLY);
1541 ret = read(fd, buffer, sizeof(buffer));
1547 tsc_rate = strtoul(buffer, NULL, 10);
1548 printf("Using TSC rate %luHz\n", tsc_rate);
1552 static void write_tsc_rate(void)
1558 if (!stat(TSC_RATE_FILE, &sb))
1561 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1565 memset(buffer, 0, sizeof(buffer));
1566 sprintf(buffer, "%lu", tsc_rate);
1567 ret = write(fd, buffer, strlen(buffer));
1573 int main(int argc, char *argv[])
1575 struct submitter *s;
1576 unsigned long done, calls, reap;
1577 int i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1581 if (!do_nop && argc < 2)
1584 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) {
1587 aio = !!atoi(optarg);
1590 depth = atoi(optarg);
1593 batch_submit = atoi(optarg);
1598 batch_complete = atoi(optarg);
1599 if (!batch_complete)
1606 polled = !!atoi(optarg);
1609 fixedbufs = !!atoi(optarg);
1612 register_files = !!atoi(optarg);
1615 nthreads = atoi(optarg);
1617 printf("Threads must be non-zero\n");
1622 do_nop = !!atoi(optarg);
1625 buffered = !atoi(optarg);
1628 #ifndef ARCH_HAVE_CPU_CLOCK
1629 fprintf(stderr, "Stats not supported on this CPU\n");
1632 stats = !!atoi(optarg);
1635 #ifndef ARCH_HAVE_CPU_CLOCK
1636 fprintf(stderr, "Stats not supported on this CPU\n");
1639 tsc_rate = strtoul(optarg, NULL, 10);
1643 runtime = atoi(optarg);
1646 dma_map = !!atoi(optarg);
1649 random_io = !!atoi(optarg);
1652 register_ring = !!atoi(optarg);
1655 #ifdef CONFIG_PWRITEV2
1656 use_sync = !!atoi(optarg);
1658 fprintf(stderr, "preadv2 not supported\n");
1663 numa_placement = !!atoi(optarg);
1666 pt = !!atoi(optarg);
1679 if (batch_complete > depth)
1680 batch_complete = depth;
1681 if (batch_submit > depth)
1682 batch_submit = depth;
1683 if (!fixedbufs && dma_map)
1686 submitter = calloc(nthreads, sizeof(*submitter) +
1687 roundup_pow2(depth) * sizeof(struct iovec));
1688 for (j = 0; j < nthreads; j++) {
1689 s = get_submitter(j);
1692 s->done = s->calls = s->reaps = 0;
1695 flags = O_RDONLY | O_NOATIME;
1704 printf("No files specified\n");
1707 threads_per_f = nthreads / nfiles;
1708 /* make sure each thread gets assigned files */
1709 if (threads_per_f == 0) {
1712 threads_rem = nthreads - threads_per_f * nfiles;
1715 while (!do_nop && i < argc) {
1718 memset(&f, 0, sizeof(f));
1720 fd = open(argv[i], flags);
1726 if (get_file_size(&f)) {
1727 printf("failed getting size of device/file\n");
1730 if (f.max_blocks <= 1) {
1731 printf("Zero file/device size?\n");
1736 limit = threads_per_f;
1737 limit += threads_rem > 0 ? 1 : 0;
1738 for (k = 0; k < limit; k++) {
1739 s = get_submitter((j + k) % nthreads);
1741 if (s->nr_files == MAX_FDS) {
1742 printf("Max number of files (%d) reached\n", MAX_FDS);
1746 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1749 detect_node(s, argv[i]);
1751 s->filename = argv[i];
1761 t_io_uring_page_size = sysconf(_SC_PAGESIZE);
1762 if (t_io_uring_page_size < 0)
1763 t_io_uring_page_size = 4096;
1765 for (j = 0; j < nthreads; j++) {
1766 s = get_submitter(j);
1768 pthread_create(&s->thread, NULL, submitter_sync_fn, s);
1770 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1771 #ifdef CONFIG_LIBAIO
1773 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1777 reap = calls = done = 0;
1779 unsigned long this_done = 0;
1780 unsigned long this_reap = 0;
1781 unsigned long this_call = 0;
1782 unsigned long rpc = 0, ipc = 0;
1783 unsigned long iops, bw;
1786 if (runtime && !--runtime)
1787 do_finish("timeout");
1789 /* don't print partial run, if interrupted by signal */
1793 /* one second in to the run, enable stats */
1797 for (j = 0; j < nthreads; j++) {
1798 s = get_submitter(j);
1799 this_done += s->done;
1800 this_call += s->calls;
1801 this_reap += s->reaps;
1803 if (this_call - calls) {
1804 rpc = (this_done - done) / (this_call - calls);
1805 ipc = (this_reap - reap) / (this_call - calls);
1808 iops = this_done - done;
1810 bw = iops * (bs / 1048576);
1812 bw = iops / (1048576 / bs);
1813 if (iops > 1000000) {
1814 double miops = (double) iops / 1000000.0;
1815 printf("IOPS=%.2fM, ", miops);
1816 } else if (iops > 100000) {
1817 double kiops = (double) iops / 1000.0;
1818 printf("IOPS=%.2fK, ", kiops);
1820 printf("IOPS=%lu, ", iops);
1822 max_iops = max(max_iops, iops);
1825 double bw_g = (double) bw / 1000.0;
1827 printf("BW=%.2fGiB/s, ", bw_g);
1829 printf("BW=%luMiB/s, ", bw);
1832 printf("IOS/call=%ld/%ld\n", rpc, ipc);
1838 for (j = 0; j < nthreads; j++) {
1839 s = get_submitter(j);
1840 pthread_join(s->thread, &ret);
1846 printf("%d: Latency percentiles:\n", s->tid);
1847 for (i = 0, nr = 0; i < PLAT_NR; i++)
1849 show_clat_percentiles(s->plat, nr, 4);
1850 free(s->clock_batch);