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"
33 #include "../lib/types.h"
34 #include "../lib/roundup.h"
35 #include "../lib/rand.h"
36 #include "../minmax.h"
37 #include "../os/linux/io_uring.h"
38 #include "../engines/nvme.h"
44 unsigned *ring_entries;
53 unsigned *ring_entries;
54 struct io_uring_cqe *cqes;
58 #define BATCH_SUBMIT 32
59 #define BATCH_COMPLETE 32
64 static unsigned sq_ring_mask, cq_ring_mask;
67 unsigned long max_blocks;
68 unsigned long max_size;
69 unsigned long cur_off;
71 unsigned int nsid; /* nsid field required for nvme-passthrough */
72 unsigned int lba_shift; /* lba_shift field required for nvme-passthrough */
79 #define PLAT_VAL (1 << PLAT_BITS)
80 #define PLAT_GROUP_NR 29
81 #define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
88 struct io_sq_ring sq_ring;
89 struct io_uring_sqe *sqes;
90 struct io_cq_ring cq_ring;
100 struct taus258_state rand_state;
102 unsigned long *clock_batch;
107 io_context_t aio_ctx;
111 const char *filename;
113 struct file files[MAX_FDS];
116 struct iovec iovecs[];
119 static struct submitter *submitter;
120 static volatile int finish;
121 static int stats_running;
122 static unsigned long max_iops;
123 static long t_io_uring_page_size;
125 static int depth = DEPTH;
126 static int batch_submit = BATCH_SUBMIT;
127 static int batch_complete = BATCH_COMPLETE;
129 static int polled = 1; /* use IO polling */
130 static int fixedbufs = 1; /* use fixed user buffers */
131 static int dma_map; /* pre-map DMA buffers */
132 static int register_files = 1; /* use fixed files */
133 static int buffered = 0; /* use buffered IO, not O_DIRECT */
134 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
135 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
136 static int do_nop = 0; /* no-op SQ ring commands */
137 static int nthreads = 1;
138 static int stats = 0; /* generate IO stats */
139 static int aio = 0; /* use libaio */
140 static int runtime = 0; /* runtime */
141 static int random_io = 1; /* random or sequential IO */
142 static int register_ring = 1; /* register ring */
143 static int use_sync = 0; /* use preadv2 */
144 static int numa_placement = 0; /* set to node of device */
145 static int pt = 0; /* passthrough I/O or not */
147 static unsigned long tsc_rate;
149 #define TSC_RATE_FILE "tsc-rate"
151 static int vectored = 1;
153 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
154 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
155 static int plist_len = 17;
157 #ifndef IORING_REGISTER_MAP_BUFFERS
158 #define IORING_REGISTER_MAP_BUFFERS 22
159 struct io_uring_map_buffers {
168 static int nvme_identify(int fd, __u32 nsid, enum nvme_identify_cns cns,
169 enum nvme_csi csi, void *data)
171 struct nvme_passthru_cmd cmd = {
172 .opcode = nvme_admin_identify,
174 .addr = (__u64)(uintptr_t)data,
175 .data_len = NVME_IDENTIFY_DATA_SIZE,
177 .cdw11 = csi << NVME_IDENTIFY_CSI_SHIFT,
178 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
181 return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
184 static int nvme_get_info(int fd, __u32 *nsid, __u32 *lba_sz, __u64 *nlba)
186 struct nvme_id_ns ns;
190 namespace_id = ioctl(fd, NVME_IOCTL_ID);
191 if (namespace_id < 0) {
192 fprintf(stderr, "error failed to fetch namespace-id\n");
198 * Identify namespace to get namespace-id, namespace size in LBA's
201 err = nvme_identify(fd, namespace_id, NVME_IDENTIFY_CNS_NS,
204 fprintf(stderr, "error failed to fetch identify namespace\n");
209 *nsid = namespace_id;
210 *lba_sz = 1 << ns.lbaf[(ns.flbas & 0x0f)].ds;
216 static unsigned long cycles_to_nsec(unsigned long cycles)
223 val = cycles * 1000000000ULL;
224 return val / tsc_rate;
227 static unsigned long plat_idx_to_val(unsigned int idx)
229 unsigned int error_bits;
230 unsigned long k, base;
232 assert(idx < PLAT_NR);
234 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
235 * all bits of the sample as index */
236 if (idx < (PLAT_VAL << 1))
237 return cycles_to_nsec(idx);
239 /* Find the group and compute the minimum value of that group */
240 error_bits = (idx >> PLAT_BITS) - 1;
241 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
243 /* Find its bucket number of the group */
246 /* Return the mean of the range of the bucket */
247 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
250 unsigned int calculate_clat_percentiles(unsigned long *io_u_plat,
251 unsigned long nr, unsigned long **output,
252 unsigned long *maxv, unsigned long *minv)
254 unsigned long sum = 0;
255 unsigned int len = plist_len, i, j = 0;
256 unsigned long *ovals = NULL;
262 ovals = malloc(len * sizeof(*ovals));
267 * Calculate bucket values, note down max and min values
270 for (i = 0; i < PLAT_NR && !is_last; i++) {
272 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
273 assert(plist[j] <= 100.0);
275 ovals[j] = plat_idx_to_val(i);
276 if (ovals[j] < *minv)
278 if (ovals[j] > *maxv)
281 is_last = (j == len - 1) != 0;
290 fprintf(stderr, "error calculating latency percentiles\n");
296 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
297 unsigned int precision)
299 unsigned int divisor, len, i, j = 0;
300 unsigned long minv, maxv;
301 unsigned long *ovals;
302 int per_line, scale_down, time_width;
306 len = calculate_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
313 printf(" percentiles (tsc ticks):\n |");
314 } else if (minv > 2000 && maxv > 99999) {
317 printf(" percentiles (usec):\n |");
321 printf(" percentiles (nsec):\n |");
324 time_width = max(5, (int) (log10(maxv / divisor) + 1));
325 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
326 precision, time_width);
327 /* fmt will be something like " %5.2fth=[%4llu]%c" */
328 per_line = (80 - 7) / (precision + 10 + time_width);
330 for (j = 0; j < len; j++) {
332 if (j != 0 && (j % per_line) == 0)
335 /* end of the list */
336 is_last = (j == len - 1) != 0;
338 for (i = 0; i < scale_down; i++)
339 ovals[j] = (ovals[j] + 999) / 1000;
341 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
346 if ((j % per_line) == per_line - 1) /* for formatting */
354 #ifdef ARCH_HAVE_CPU_CLOCK
355 static unsigned int plat_val_to_idx(unsigned long val)
357 unsigned int msb, error_bits, base, offset, idx;
359 /* Find MSB starting from bit 0 */
363 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
366 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
367 * all bits of the sample as index
369 if (msb <= PLAT_BITS)
372 /* Compute the number of error bits to discard*/
373 error_bits = msb - PLAT_BITS;
375 /* Compute the number of buckets before the group */
376 base = (error_bits + 1) << PLAT_BITS;
379 * Discard the error bits and apply the mask to find the
380 * index for the buckets in the group
382 offset = (PLAT_VAL - 1) & (val >> error_bits);
384 /* Make sure the index does not exceed (array size - 1) */
385 idx = (base + offset) < (PLAT_NR - 1) ?
386 (base + offset) : (PLAT_NR - 1);
392 static void add_stat(struct submitter *s, int clock_index, int nr)
394 #ifdef ARCH_HAVE_CPU_CLOCK
395 unsigned long cycles;
398 if (!s->finish && clock_index) {
399 cycles = get_cpu_clock();
400 cycles -= s->clock_batch[clock_index];
401 pidx = plat_val_to_idx(cycles);
407 static int io_uring_map_buffers(struct submitter *s)
409 struct io_uring_map_buffers map = {
410 .fd = s->files[0].real_fd,
417 fprintf(stdout, "Mapping buffers may not work with multiple files\n");
419 return syscall(__NR_io_uring_register, s->ring_fd,
420 IORING_REGISTER_MAP_BUFFERS, &map, 1);
423 static int io_uring_register_buffers(struct submitter *s)
428 return syscall(__NR_io_uring_register, s->ring_fd,
429 IORING_REGISTER_BUFFERS, s->iovecs, roundup_pow2(depth));
432 static int io_uring_register_files(struct submitter *s)
439 s->fds = calloc(s->nr_files, sizeof(__s32));
440 for (i = 0; i < s->nr_files; i++) {
441 s->fds[i] = s->files[i].real_fd;
442 s->files[i].fixed_fd = i;
445 return syscall(__NR_io_uring_register, s->ring_fd,
446 IORING_REGISTER_FILES, s->fds, s->nr_files);
449 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
452 * Clamp CQ ring size at our SQ ring size, we don't need more entries
455 p->flags |= IORING_SETUP_CQSIZE;
456 p->cq_entries = entries;
458 return syscall(__NR_io_uring_setup, entries, p);
461 static void io_uring_probe(int fd)
463 struct io_uring_probe *p;
466 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
470 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
471 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
475 if (IORING_OP_READ > p->ops_len)
478 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
484 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
485 unsigned int min_complete, unsigned int flags)
488 flags |= IORING_ENTER_REGISTERED_RING;
489 #ifdef FIO_ARCH_HAS_SYSCALL
490 return __do_syscall6(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
491 min_complete, flags, NULL, 0);
493 return syscall(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
494 min_complete, flags, NULL, 0);
498 #ifndef CONFIG_HAVE_GETTID
499 static int gettid(void)
501 return syscall(__NR_gettid);
505 static unsigned file_depth(struct submitter *s)
507 return (depth + s->nr_files - 1) / s->nr_files;
510 static void init_io(struct submitter *s, unsigned index)
512 struct io_uring_sqe *sqe = &s->sqes[index];
513 unsigned long offset;
518 sqe->opcode = IORING_OP_NOP;
522 if (s->nr_files == 1) {
525 f = &s->files[s->cur_file];
526 if (f->pending_ios >= file_depth(s)) {
528 if (s->cur_file == s->nr_files)
530 f = &s->files[s->cur_file];
536 r = __rand64(&s->rand_state);
537 offset = (r % (f->max_blocks - 1)) * bs;
541 if (f->cur_off + bs > f->max_size)
545 if (register_files) {
546 sqe->flags = IOSQE_FIXED_FILE;
547 sqe->fd = f->fixed_fd;
550 sqe->fd = f->real_fd;
553 sqe->opcode = IORING_OP_READ_FIXED;
554 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
556 sqe->buf_index = index;
557 } else if (!vectored) {
558 sqe->opcode = IORING_OP_READ;
559 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
563 sqe->opcode = IORING_OP_READV;
564 sqe->addr = (unsigned long) &s->iovecs[index];
570 sqe->user_data = (unsigned long) f->fileno;
571 if (stats && stats_running)
572 sqe->user_data |= ((uint64_t)s->clock_index << 32);
575 static void init_io_pt(struct submitter *s, unsigned index)
577 struct io_uring_sqe *sqe = &s->sqes[index << 1];
578 unsigned long offset;
580 struct nvme_uring_cmd *cmd;
581 unsigned long long slba;
582 unsigned long long nlb;
585 if (s->nr_files == 1) {
588 f = &s->files[s->cur_file];
589 if (f->pending_ios >= file_depth(s)) {
591 if (s->cur_file == s->nr_files)
593 f = &s->files[s->cur_file];
599 r = __rand64(&s->rand_state);
600 offset = (r % (f->max_blocks - 1)) * bs;
604 if (f->cur_off + bs > f->max_size)
608 if (register_files) {
609 sqe->fd = f->fixed_fd;
610 sqe->flags = IOSQE_FIXED_FILE;
612 sqe->fd = f->real_fd;
615 sqe->opcode = IORING_OP_URING_CMD;
616 sqe->user_data = (unsigned long) f->fileno;
618 sqe->user_data |= ((__u64) s->clock_index << 32ULL);
619 sqe->cmd_op = NVME_URING_CMD_IO;
620 slba = offset >> f->lba_shift;
621 nlb = (bs >> f->lba_shift) - 1;
622 cmd = (struct nvme_uring_cmd *)&sqe->cmd;
623 /* cdw10 and cdw11 represent starting slba*/
624 cmd->cdw10 = slba & 0xffffffff;
625 cmd->cdw11 = slba >> 32;
626 /* cdw12 represent number of lba to be read*/
628 cmd->addr = (unsigned long) s->iovecs[index].iov_base;
634 static int prep_more_ios_uring(struct submitter *s, int max_ios)
636 struct io_sq_ring *ring = &s->sq_ring;
637 unsigned index, tail, next_tail, prepped = 0;
639 next_tail = tail = *ring->tail;
642 if (next_tail == atomic_load_acquire(ring->head))
645 index = tail & sq_ring_mask;
647 init_io_pt(s, index);
650 ring->array[index] = index;
653 } while (prepped < max_ios);
656 atomic_store_release(ring->tail, tail);
660 static int get_file_size(struct file *f)
664 if (fstat(f->real_fd, &st) < 0)
671 if (!S_ISCHR(st.st_mode)) {
672 fprintf(stderr, "passthrough works with only nvme-ns "
673 "generic devices (/dev/ngXnY)\n");
676 ret = nvme_get_info(f->real_fd, &f->nsid, &lbs, &nlba);
679 if ((bs % lbs) != 0) {
680 printf("error: bs:%d should be a multiple logical_block_size:%d\n",
684 f->max_blocks = nlba / bs;
686 f->lba_shift = ilog2(lbs);
688 } else if (S_ISBLK(st.st_mode)) {
689 unsigned long long bytes;
691 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
694 f->max_blocks = bytes / bs;
697 } else if (S_ISREG(st.st_mode)) {
698 f->max_blocks = st.st_size / bs;
699 f->max_size = st.st_size;
706 static int reap_events_uring(struct submitter *s)
708 struct io_cq_ring *ring = &s->cq_ring;
709 struct io_uring_cqe *cqe;
710 unsigned head, reaped = 0;
711 int last_idx = -1, stat_nr = 0;
718 if (head == atomic_load_acquire(ring->tail))
720 cqe = &ring->cqes[head & cq_ring_mask];
722 int fileno = cqe->user_data & 0xffffffff;
724 f = &s->files[fileno];
726 if (cqe->res != bs) {
727 printf("io: unexpected ret=%d\n", cqe->res);
728 if (polled && cqe->res == -EOPNOTSUPP)
729 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
734 int clock_index = cqe->user_data >> 32;
736 if (last_idx != clock_index) {
737 if (last_idx != -1) {
738 add_stat(s, last_idx, stat_nr);
741 last_idx = clock_index;
750 add_stat(s, last_idx, stat_nr);
753 s->inflight -= reaped;
754 atomic_store_release(ring->head, head);
759 static int reap_events_uring_pt(struct submitter *s)
761 struct io_cq_ring *ring = &s->cq_ring;
762 struct io_uring_cqe *cqe;
763 unsigned head, reaped = 0;
764 int last_idx = -1, stat_nr = 0;
773 if (head == atomic_load_acquire(ring->tail))
775 index = head & cq_ring_mask;
776 cqe = &ring->cqes[index << 1];
777 fileno = cqe->user_data & 0xffffffff;
778 f = &s->files[fileno];
782 printf("io: unexpected ret=%d\n", cqe->res);
783 if (polled && cqe->res == -EINVAL)
784 printf("passthrough doesn't support polled IO\n");
788 int clock_index = cqe->user_data >> 32;
790 if (last_idx != clock_index) {
791 if (last_idx != -1) {
792 add_stat(s, last_idx, stat_nr);
795 last_idx = clock_index;
804 add_stat(s, last_idx, stat_nr);
807 s->inflight -= reaped;
808 atomic_store_release(ring->head, head);
813 static void set_affinity(struct submitter *s)
815 #ifdef CONFIG_LIBNUMA
816 struct bitmask *mask;
818 if (s->numa_node == -1)
821 numa_set_preferred(s->numa_node);
823 mask = numa_allocate_cpumask();
824 numa_node_to_cpus(s->numa_node, mask);
825 numa_sched_setaffinity(s->tid, mask);
829 static int detect_node(struct submitter *s, const char *name)
831 #ifdef CONFIG_LIBNUMA
832 const char *base = basename(name);
836 sprintf(str, "/sys/block/%s/device/numa_node", base);
837 fd = open(str, O_RDONLY);
841 ret = read(fd, str, sizeof(str));
855 static int setup_aio(struct submitter *s)
859 fprintf(stderr, "aio does not support polled IO\n");
862 if (sq_thread_poll) {
863 fprintf(stderr, "aio does not support SQPOLL IO\n");
867 fprintf(stderr, "aio does not support polled IO\n");
870 if (fixedbufs || register_files) {
871 fprintf(stderr, "aio does not support registered files or buffers\n");
872 fixedbufs = register_files = 0;
875 return io_queue_init(roundup_pow2(depth), &s->aio_ctx);
877 fprintf(stderr, "Legacy AIO not available on this system/build\n");
883 static int setup_ring(struct submitter *s)
885 struct io_sq_ring *sring = &s->sq_ring;
886 struct io_cq_ring *cring = &s->cq_ring;
887 struct io_uring_params p;
892 memset(&p, 0, sizeof(p));
894 if (polled && !do_nop)
895 p.flags |= IORING_SETUP_IOPOLL;
896 if (sq_thread_poll) {
897 p.flags |= IORING_SETUP_SQPOLL;
898 if (sq_thread_cpu != -1) {
899 p.flags |= IORING_SETUP_SQ_AFF;
900 p.sq_thread_cpu = sq_thread_cpu;
904 p.flags |= IORING_SETUP_SQE128;
905 p.flags |= IORING_SETUP_CQE32;
908 fd = io_uring_setup(depth, &p);
910 perror("io_uring_setup");
913 s->ring_fd = s->enter_ring_fd = fd;
920 rlim.rlim_cur = RLIM_INFINITY;
921 rlim.rlim_max = RLIM_INFINITY;
922 /* ignore potential error, not needed on newer kernels */
923 setrlimit(RLIMIT_MEMLOCK, &rlim);
925 ret = io_uring_register_buffers(s);
927 perror("io_uring_register_buffers");
932 ret = io_uring_map_buffers(s);
934 perror("io_uring_map_buffers");
940 if (register_files) {
941 ret = io_uring_register_files(s);
943 perror("io_uring_register_files");
948 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
949 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
951 sring->head = ptr + p.sq_off.head;
952 sring->tail = ptr + p.sq_off.tail;
953 sring->ring_mask = ptr + p.sq_off.ring_mask;
954 sring->ring_entries = ptr + p.sq_off.ring_entries;
955 sring->flags = ptr + p.sq_off.flags;
956 sring->array = ptr + p.sq_off.array;
957 sq_ring_mask = *sring->ring_mask;
959 if (p.flags & IORING_SETUP_SQE128)
960 len = 2 * p.sq_entries * sizeof(struct io_uring_sqe);
962 len = p.sq_entries * sizeof(struct io_uring_sqe);
963 s->sqes = mmap(0, len,
964 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
967 if (p.flags & IORING_SETUP_CQE32) {
968 len = p.cq_off.cqes +
969 2 * p.cq_entries * sizeof(struct io_uring_cqe);
971 len = p.cq_off.cqes +
972 p.cq_entries * sizeof(struct io_uring_cqe);
975 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
977 cring->head = ptr + p.cq_off.head;
978 cring->tail = ptr + p.cq_off.tail;
979 cring->ring_mask = ptr + p.cq_off.ring_mask;
980 cring->ring_entries = ptr + p.cq_off.ring_entries;
981 cring->cqes = ptr + p.cq_off.cqes;
982 cq_ring_mask = *cring->ring_mask;
986 static void *allocate_mem(struct submitter *s, int size)
990 #ifdef CONFIG_LIBNUMA
991 if (s->numa_node != -1)
992 return numa_alloc_onnode(size, s->numa_node);
995 if (posix_memalign(&buf, t_io_uring_page_size, bs)) {
996 printf("failed alloc\n");
1003 static int submitter_init(struct submitter *s)
1005 int i, nr_batch, err;
1006 static int init_printed;
1009 printf("submitter=%d, tid=%d, file=%s, node=%d\n", s->index, s->tid,
1010 s->filename, s->numa_node);
1014 __init_rand64(&s->rand_state, s->tid);
1017 for (i = 0; i < MAX_FDS; i++)
1018 s->files[i].fileno = i;
1020 for (i = 0; i < roundup_pow2(depth); i++) {
1023 buf = allocate_mem(s, bs);
1026 s->iovecs[i].iov_base = buf;
1027 s->iovecs[i].iov_len = bs;
1031 sprintf(buf, "Engine=preadv2\n");
1034 err = setup_ring(s);
1035 sprintf(buf, "Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1037 sprintf(buf, "Engine=aio\n");
1041 printf("queue setup failed: %s, %d\n", strerror(errno), err);
1045 if (!init_printed) {
1046 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
1052 nr_batch = roundup_pow2(depth / batch_submit);
1055 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
1058 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
1060 s->clock_batch = NULL;
1064 /* perform the expensive command initialization part for passthrough here
1065 * rather than in the fast path
1068 for (i = 0; i < roundup_pow2(depth); i++) {
1069 struct io_uring_sqe *sqe = &s->sqes[i << 1];
1071 memset(&sqe->cmd, 0, sizeof(struct nvme_uring_cmd));
1077 #ifdef CONFIG_LIBAIO
1078 static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
1087 while (index < max_ios) {
1088 struct iocb *iocb = &iocbs[index];
1090 if (s->nr_files == 1) {
1093 f = &s->files[s->cur_file];
1094 if (f->pending_ios >= file_depth(s)) {
1096 if (s->cur_file == s->nr_files)
1098 f = &s->files[s->cur_file];
1104 offset = (r % (f->max_blocks - 1)) * bs;
1105 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
1106 s->iovecs[index].iov_len, offset);
1109 if (stats && stats_running)
1110 data |= (((uint64_t) s->clock_index) << 32);
1111 iocb->data = (void *) (uintptr_t) data;
1117 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
1119 int last_idx = -1, stat_nr = 0;
1123 uint64_t data = (uintptr_t) events[reaped].data;
1124 struct file *f = &s->files[data & 0xffffffff];
1127 if (events[reaped].res != bs) {
1128 printf("io: unexpected ret=%ld\n", events[reaped].res);
1132 int clock_index = data >> 32;
1134 if (last_idx != clock_index) {
1135 if (last_idx != -1) {
1136 add_stat(s, last_idx, stat_nr);
1139 last_idx = clock_index;
1148 add_stat(s, last_idx, stat_nr);
1150 s->inflight -= reaped;
1155 static void *submitter_aio_fn(void *data)
1157 struct submitter *s = data;
1158 int i, ret, prepped;
1159 struct iocb **iocbsptr;
1161 struct io_event *events;
1162 #ifdef ARCH_HAVE_CPU_CLOCK
1163 int nr_batch = submitter_init(s);
1168 iocbsptr = calloc(depth, sizeof(struct iocb *));
1169 iocbs = calloc(depth, sizeof(struct iocb));
1170 events = calloc(depth, sizeof(struct io_event));
1172 for (i = 0; i < depth; i++)
1173 iocbsptr[i] = &iocbs[i];
1177 int to_wait, to_submit, to_prep;
1179 if (!prepped && s->inflight < depth) {
1180 to_prep = min(depth - s->inflight, batch_submit);
1181 prepped = prep_more_ios_aio(s, to_prep, iocbs);
1182 #ifdef ARCH_HAVE_CPU_CLOCK
1183 if (prepped && stats) {
1184 s->clock_batch[s->clock_index] = get_cpu_clock();
1185 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1189 s->inflight += prepped;
1190 to_submit = prepped;
1192 if (to_submit && (s->inflight + to_submit <= depth))
1195 to_wait = min(s->inflight + to_submit, batch_complete);
1197 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
1200 perror("io_submit");
1202 } else if (ret != to_submit) {
1203 printf("submitted %d, wanted %d\n", ret, to_submit);
1212 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
1214 perror("io_getevents");
1216 } else if (r != to_wait) {
1217 printf("r=%d, wait=%d\n", r, to_wait);
1220 r = reap_events_aio(s, events, r);
1224 } while (!s->finish);
1234 static void io_uring_unregister_ring(struct submitter *s)
1236 struct io_uring_rsrc_update up = {
1237 .offset = s->enter_ring_fd,
1240 syscall(__NR_io_uring_register, s->ring_fd, IORING_UNREGISTER_RING_FDS,
1244 static int io_uring_register_ring(struct submitter *s)
1246 struct io_uring_rsrc_update up = {
1252 ret = syscall(__NR_io_uring_register, s->ring_fd,
1253 IORING_REGISTER_RING_FDS, &up, 1);
1255 s->enter_ring_fd = up.offset;
1262 static void *submitter_uring_fn(void *data)
1264 struct submitter *s = data;
1265 struct io_sq_ring *ring = &s->sq_ring;
1267 #ifdef ARCH_HAVE_CPU_CLOCK
1268 int nr_batch = submitter_init(s);
1274 io_uring_register_ring(s);
1278 int to_wait, to_submit, this_reap, to_prep;
1279 unsigned ring_flags = 0;
1281 if (!prepped && s->inflight < depth) {
1282 to_prep = min(depth - s->inflight, batch_submit);
1283 prepped = prep_more_ios_uring(s, to_prep);
1284 #ifdef ARCH_HAVE_CPU_CLOCK
1285 if (prepped && stats) {
1286 s->clock_batch[s->clock_index] = get_cpu_clock();
1287 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1291 s->inflight += prepped;
1293 to_submit = prepped;
1295 if (to_submit && (s->inflight + to_submit <= depth))
1298 to_wait = min(s->inflight + to_submit, batch_complete);
1301 * Only need to call io_uring_enter if we're not using SQ thread
1302 * poll, or if IORING_SQ_NEED_WAKEUP is set.
1305 ring_flags = atomic_load_acquire(ring->flags);
1306 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
1310 flags = IORING_ENTER_GETEVENTS;
1311 if (ring_flags & IORING_SQ_NEED_WAKEUP)
1312 flags |= IORING_ENTER_SQ_WAKEUP;
1313 ret = io_uring_enter(s, to_submit, to_wait, flags);
1316 /* for SQPOLL, we submitted it all effectively */
1321 * For non SQ thread poll, we already got the events we needed
1322 * through the io_uring_enter() above. For SQ thread poll, we
1323 * need to loop here until we find enough events.
1330 r = reap_events_uring_pt(s);
1332 r = reap_events_uring(s);
1338 } while (sq_thread_poll && this_reap < to_wait);
1339 s->reaps += this_reap;
1347 } else if (ret < to_submit) {
1348 int diff = to_submit - ret;
1357 } else if (ret < 0) {
1358 if (errno == EAGAIN) {
1366 printf("io_submit: %s\n", strerror(errno));
1369 } while (!s->finish);
1372 io_uring_unregister_ring(s);
1378 #ifdef CONFIG_PWRITEV2
1379 static void *submitter_sync_fn(void *data)
1381 struct submitter *s = data;
1391 if (s->nr_files == 1) {
1394 f = &s->files[s->cur_file];
1395 if (f->pending_ios >= file_depth(s)) {
1397 if (s->cur_file == s->nr_files)
1399 f = &s->files[s->cur_file];
1405 r = __rand64(&s->rand_state);
1406 offset = (r % (f->max_blocks - 1)) * bs;
1408 offset = f->cur_off;
1410 if (f->cur_off + bs > f->max_size)
1414 #ifdef ARCH_HAVE_CPU_CLOCK
1416 s->clock_batch[s->clock_index] = get_cpu_clock();
1423 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, RWF_HIPRI);
1425 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, 0);
1430 } else if (ret != bs) {
1438 add_stat(s, s->clock_index, 1);
1439 } while (!s->finish);
1445 static void *submitter_sync_fn(void *data)
1452 static struct submitter *get_submitter(int offset)
1458 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
1462 static void do_finish(const char *reason)
1466 printf("Exiting on %s\n", reason);
1467 for (j = 0; j < nthreads; j++) {
1468 struct submitter *s = get_submitter(j);
1471 if (max_iops > 1000000) {
1472 double miops = (double) max_iops / 1000000.0;
1473 printf("Maximum IOPS=%.2fM\n", miops);
1474 } else if (max_iops > 100000) {
1475 double kiops = (double) max_iops / 1000.0;
1476 printf("Maximum IOPS=%.2fK\n", kiops);
1478 printf("Maximum IOPS=%lu\n", max_iops);
1483 static void sig_int(int sig)
1485 do_finish("signal");
1488 static void arm_sig_int(void)
1490 struct sigaction act;
1492 memset(&act, 0, sizeof(act));
1493 act.sa_handler = sig_int;
1494 act.sa_flags = SA_RESTART;
1495 sigaction(SIGINT, &act, NULL);
1497 /* Windows uses SIGBREAK as a quit signal from other applications */
1499 sigaction(SIGBREAK, &act, NULL);
1503 static void usage(char *argv, int status)
1505 char runtime_str[16];
1506 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
1507 printf("%s [options] -- [filenames]\n"
1508 " -d <int> : IO Depth, default %d\n"
1509 " -s <int> : Batch submit, default %d\n"
1510 " -c <int> : Batch complete, default %d\n"
1511 " -b <int> : Block size, default %d\n"
1512 " -p <bool> : Polled IO, default %d\n"
1513 " -B <bool> : Fixed buffers, default %d\n"
1514 " -D <bool> : DMA map fixed buffers, default %d\n"
1515 " -F <bool> : Register files, default %d\n"
1516 " -n <int> : Number of threads, default %d\n"
1517 " -O <bool> : Use O_DIRECT, default %d\n"
1518 " -N <bool> : Perform just no-op requests, default %d\n"
1519 " -t <bool> : Track IO latencies, default %d\n"
1520 " -T <int> : TSC rate in HZ\n"
1521 " -r <int> : Runtime in seconds, default %s\n"
1522 " -R <bool> : Use random IO, default %d\n"
1523 " -a <bool> : Use legacy aio, default %d\n"
1524 " -S <bool> : Use sync IO (preadv2), default %d\n"
1525 " -X <bool> : Use registered ring %d\n"
1526 " -P <bool> : Automatically place on device home node %d\n"
1527 " -u <bool> : Use nvme-passthrough I/O, default %d\n",
1528 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1529 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
1530 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio,
1531 use_sync, register_ring, numa_placement, pt);
1535 static void read_tsc_rate(void)
1543 fd = open(TSC_RATE_FILE, O_RDONLY);
1547 ret = read(fd, buffer, sizeof(buffer));
1553 tsc_rate = strtoul(buffer, NULL, 10);
1554 printf("Using TSC rate %luHz\n", tsc_rate);
1558 static void write_tsc_rate(void)
1564 if (!stat(TSC_RATE_FILE, &sb))
1567 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1571 memset(buffer, 0, sizeof(buffer));
1572 sprintf(buffer, "%lu", tsc_rate);
1573 ret = write(fd, buffer, strlen(buffer));
1579 int main(int argc, char *argv[])
1581 struct submitter *s;
1582 unsigned long done, calls, reap;
1583 int i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1587 if (!do_nop && argc < 2)
1590 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) {
1593 aio = !!atoi(optarg);
1596 depth = atoi(optarg);
1599 batch_submit = atoi(optarg);
1604 batch_complete = atoi(optarg);
1605 if (!batch_complete)
1612 polled = !!atoi(optarg);
1615 fixedbufs = !!atoi(optarg);
1618 register_files = !!atoi(optarg);
1621 nthreads = atoi(optarg);
1623 printf("Threads must be non-zero\n");
1628 do_nop = !!atoi(optarg);
1631 buffered = !atoi(optarg);
1634 #ifndef ARCH_HAVE_CPU_CLOCK
1635 fprintf(stderr, "Stats not supported on this CPU\n");
1638 stats = !!atoi(optarg);
1641 #ifndef ARCH_HAVE_CPU_CLOCK
1642 fprintf(stderr, "Stats not supported on this CPU\n");
1645 tsc_rate = strtoul(optarg, NULL, 10);
1649 runtime = atoi(optarg);
1652 dma_map = !!atoi(optarg);
1655 random_io = !!atoi(optarg);
1658 register_ring = !!atoi(optarg);
1661 #ifdef CONFIG_PWRITEV2
1662 use_sync = !!atoi(optarg);
1664 fprintf(stderr, "preadv2 not supported\n");
1669 numa_placement = !!atoi(optarg);
1672 pt = !!atoi(optarg);
1685 if (batch_complete > depth)
1686 batch_complete = depth;
1687 if (batch_submit > depth)
1688 batch_submit = depth;
1689 if (!fixedbufs && dma_map)
1692 submitter = calloc(nthreads, sizeof(*submitter) +
1693 roundup_pow2(depth) * sizeof(struct iovec));
1694 for (j = 0; j < nthreads; j++) {
1695 s = get_submitter(j);
1698 s->done = s->calls = s->reaps = 0;
1701 flags = O_RDONLY | O_NOATIME;
1710 printf("No files specified\n");
1713 threads_per_f = nthreads / nfiles;
1714 /* make sure each thread gets assigned files */
1715 if (threads_per_f == 0) {
1718 threads_rem = nthreads - threads_per_f * nfiles;
1721 while (!do_nop && i < argc) {
1724 memset(&f, 0, sizeof(f));
1726 fd = open(argv[i], flags);
1732 if (get_file_size(&f)) {
1733 printf("failed getting size of device/file\n");
1736 if (f.max_blocks <= 1) {
1737 printf("Zero file/device size?\n");
1742 limit = threads_per_f;
1743 limit += threads_rem > 0 ? 1 : 0;
1744 for (k = 0; k < limit; k++) {
1745 s = get_submitter((j + k) % nthreads);
1747 if (s->nr_files == MAX_FDS) {
1748 printf("Max number of files (%d) reached\n", MAX_FDS);
1752 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1755 detect_node(s, argv[i]);
1757 s->filename = argv[i];
1767 t_io_uring_page_size = sysconf(_SC_PAGESIZE);
1768 if (t_io_uring_page_size < 0)
1769 t_io_uring_page_size = 4096;
1771 for (j = 0; j < nthreads; j++) {
1772 s = get_submitter(j);
1774 pthread_create(&s->thread, NULL, submitter_sync_fn, s);
1776 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1777 #ifdef CONFIG_LIBAIO
1779 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1783 reap = calls = done = 0;
1785 unsigned long this_done = 0;
1786 unsigned long this_reap = 0;
1787 unsigned long this_call = 0;
1788 unsigned long rpc = 0, ipc = 0;
1789 unsigned long iops, bw;
1792 if (runtime && !--runtime)
1793 do_finish("timeout");
1795 /* don't print partial run, if interrupted by signal */
1799 /* one second in to the run, enable stats */
1803 for (j = 0; j < nthreads; j++) {
1804 s = get_submitter(j);
1805 this_done += s->done;
1806 this_call += s->calls;
1807 this_reap += s->reaps;
1809 if (this_call - calls) {
1810 rpc = (this_done - done) / (this_call - calls);
1811 ipc = (this_reap - reap) / (this_call - calls);
1814 iops = this_done - done;
1816 bw = iops * (bs / 1048576);
1818 bw = iops / (1048576 / bs);
1819 if (iops > 1000000) {
1820 double miops = (double) iops / 1000000.0;
1821 printf("IOPS=%.2fM, ", miops);
1822 } else if (iops > 100000) {
1823 double kiops = (double) iops / 1000.0;
1824 printf("IOPS=%.2fK, ", kiops);
1826 printf("IOPS=%lu, ", iops);
1828 max_iops = max(max_iops, iops);
1831 double bw_g = (double) bw / 1000.0;
1833 printf("BW=%.2fGiB/s, ", bw_g);
1835 printf("BW=%luMiB/s, ", bw);
1838 printf("IOS/call=%ld/%ld\n", rpc, ipc);
1844 for (j = 0; j < nthreads; j++) {
1845 s = get_submitter(j);
1846 pthread_join(s->thread, &ret);
1852 printf("%d: Latency percentiles:\n", s->tid);
1853 for (i = 0, nr = 0; i < PLAT_NR; i++)
1855 show_clat_percentiles(s->plat, nr, 4);
1856 free(s->clock_batch);