14 #include <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <sys/syscall.h>
18 #include <sys/resource.h>
28 #include "../arch/arch.h"
29 #include "../lib/types.h"
30 #include "../lib/roundup.h"
31 #include "../minmax.h"
32 #include "../os/linux/io_uring.h"
38 unsigned *ring_entries;
47 unsigned *ring_entries;
48 struct io_uring_cqe *cqes;
52 #define BATCH_SUBMIT 32
53 #define BATCH_COMPLETE 32
58 static unsigned sq_ring_mask, cq_ring_mask;
61 unsigned long max_blocks;
69 #define PLAT_VAL (1 << PLAT_BITS)
70 #define PLAT_GROUP_NR 29
71 #define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
77 struct io_sq_ring sq_ring;
78 struct io_uring_sqe *sqes;
79 struct io_cq_ring cq_ring;
89 unsigned long *clock_batch;
97 struct file files[MAX_FDS];
100 struct iovec iovecs[];
103 static struct submitter *submitter;
104 static volatile int finish;
105 static int stats_running;
106 static unsigned long max_iops;
108 static int depth = DEPTH;
109 static int batch_submit = BATCH_SUBMIT;
110 static int batch_complete = BATCH_COMPLETE;
112 static int polled = 1; /* use IO polling */
113 static int fixedbufs = 1; /* use fixed user buffers */
114 static int dma_map; /* pre-map DMA buffers */
115 static int register_files = 1; /* use fixed files */
116 static int buffered = 0; /* use buffered IO, not O_DIRECT */
117 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
118 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
119 static int do_nop = 0; /* no-op SQ ring commands */
120 static int nthreads = 1;
121 static int stats = 0; /* generate IO stats */
122 static int aio = 0; /* use libaio */
123 static int runtime = 0; /* runtime */
125 static unsigned long tsc_rate;
127 #define TSC_RATE_FILE "tsc-rate"
129 static int vectored = 1;
131 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
132 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
133 static int plist_len = 17;
135 #ifndef IORING_REGISTER_MAP_BUFFERS
136 #define IORING_REGISTER_MAP_BUFFERS 20
137 struct io_uring_map_buffers {
146 static unsigned long cycles_to_nsec(unsigned long cycles)
153 val = cycles * 1000000000ULL;
154 return val / tsc_rate;
157 static unsigned long plat_idx_to_val(unsigned int idx)
159 unsigned int error_bits;
160 unsigned long k, base;
162 assert(idx < PLAT_NR);
164 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
165 * all bits of the sample as index */
166 if (idx < (PLAT_VAL << 1))
167 return cycles_to_nsec(idx);
169 /* Find the group and compute the minimum value of that group */
170 error_bits = (idx >> PLAT_BITS) - 1;
171 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
173 /* Find its bucket number of the group */
176 /* Return the mean of the range of the bucket */
177 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
180 unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
181 unsigned long **output,
182 unsigned long *maxv, unsigned long *minv)
184 unsigned long sum = 0;
185 unsigned int len = plist_len, i, j = 0;
186 unsigned long *ovals = NULL;
192 ovals = malloc(len * sizeof(*ovals));
197 * Calculate bucket values, note down max and min values
200 for (i = 0; i < PLAT_NR && !is_last; i++) {
202 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
203 assert(plist[j] <= 100.0);
205 ovals[j] = plat_idx_to_val(i);
206 if (ovals[j] < *minv)
208 if (ovals[j] > *maxv)
211 is_last = (j == len - 1) != 0;
220 fprintf(stderr, "error calculating latency percentiles\n");
226 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
227 unsigned int precision)
229 unsigned int divisor, len, i, j = 0;
230 unsigned long minv, maxv;
231 unsigned long *ovals;
232 int per_line, scale_down, time_width;
236 len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
243 printf(" percentiles (tsc ticks):\n |");
244 } else if (minv > 2000 && maxv > 99999) {
247 printf(" percentiles (usec):\n |");
251 printf(" percentiles (nsec):\n |");
254 time_width = max(5, (int) (log10(maxv / divisor) + 1));
255 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
256 precision, time_width);
257 /* fmt will be something like " %5.2fth=[%4llu]%c" */
258 per_line = (80 - 7) / (precision + 10 + time_width);
260 for (j = 0; j < len; j++) {
262 if (j != 0 && (j % per_line) == 0)
265 /* end of the list */
266 is_last = (j == len - 1) != 0;
268 for (i = 0; i < scale_down; i++)
269 ovals[j] = (ovals[j] + 999) / 1000;
271 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
276 if ((j % per_line) == per_line - 1) /* for formatting */
284 static unsigned int plat_val_to_idx(unsigned long val)
286 unsigned int msb, error_bits, base, offset, idx;
288 /* Find MSB starting from bit 0 */
292 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
295 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
296 * all bits of the sample as index
298 if (msb <= PLAT_BITS)
301 /* Compute the number of error bits to discard*/
302 error_bits = msb - PLAT_BITS;
304 /* Compute the number of buckets before the group */
305 base = (error_bits + 1) << PLAT_BITS;
308 * Discard the error bits and apply the mask to find the
309 * index for the buckets in the group
311 offset = (PLAT_VAL - 1) & (val >> error_bits);
313 /* Make sure the index does not exceed (array size - 1) */
314 idx = (base + offset) < (PLAT_NR - 1) ?
315 (base + offset) : (PLAT_NR - 1);
320 static void add_stat(struct submitter *s, int clock_index, int nr)
322 #ifdef ARCH_HAVE_CPU_CLOCK
323 unsigned long cycles;
326 if (!s->finish && clock_index) {
327 cycles = get_cpu_clock();
328 cycles -= s->clock_batch[clock_index];
329 pidx = plat_val_to_idx(cycles);
335 static int io_uring_map_buffers(struct submitter *s)
337 struct io_uring_map_buffers map = {
338 .fd = s->files[0].real_fd,
344 if (s->nr_files > 1) {
345 fprintf(stderr, "Can't map buffers with multiple files\n");
349 return syscall(__NR_io_uring_register, s->ring_fd,
350 IORING_REGISTER_MAP_BUFFERS, &map, 1);
353 static int io_uring_register_buffers(struct submitter *s)
358 return syscall(__NR_io_uring_register, s->ring_fd,
359 IORING_REGISTER_BUFFERS, s->iovecs, depth);
362 static int io_uring_register_files(struct submitter *s)
369 s->fds = calloc(s->nr_files, sizeof(__s32));
370 for (i = 0; i < s->nr_files; i++) {
371 s->fds[i] = s->files[i].real_fd;
372 s->files[i].fixed_fd = i;
375 return syscall(__NR_io_uring_register, s->ring_fd,
376 IORING_REGISTER_FILES, s->fds, s->nr_files);
379 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
381 return syscall(__NR_io_uring_setup, entries, p);
384 static void io_uring_probe(int fd)
386 struct io_uring_probe *p;
389 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
393 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
394 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
398 if (IORING_OP_READ > p->ops_len)
401 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
407 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
408 unsigned int min_complete, unsigned int flags)
410 return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete,
414 #ifndef CONFIG_HAVE_GETTID
415 static int gettid(void)
417 return syscall(__NR_gettid);
421 static unsigned file_depth(struct submitter *s)
423 return (depth + s->nr_files - 1) / s->nr_files;
426 static void init_io(struct submitter *s, unsigned index)
428 struct io_uring_sqe *sqe = &s->sqes[index];
429 unsigned long offset;
434 sqe->opcode = IORING_OP_NOP;
438 if (s->nr_files == 1) {
441 f = &s->files[s->cur_file];
442 if (f->pending_ios >= file_depth(s)) {
444 if (s->cur_file == s->nr_files)
446 f = &s->files[s->cur_file];
452 offset = (r % (f->max_blocks - 1)) * bs;
454 if (register_files) {
455 sqe->flags = IOSQE_FIXED_FILE;
456 sqe->fd = f->fixed_fd;
459 sqe->fd = f->real_fd;
462 sqe->opcode = IORING_OP_READ_FIXED;
463 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
465 sqe->buf_index = index;
466 } else if (!vectored) {
467 sqe->opcode = IORING_OP_READ;
468 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
472 sqe->opcode = IORING_OP_READV;
473 sqe->addr = (unsigned long) &s->iovecs[index];
479 sqe->user_data = (unsigned long) f->fileno;
480 if (stats && stats_running)
481 sqe->user_data |= ((unsigned long)s->clock_index << 32);
484 static int prep_more_ios_uring(struct submitter *s, int max_ios)
486 struct io_sq_ring *ring = &s->sq_ring;
487 unsigned index, tail, next_tail, prepped = 0;
489 next_tail = tail = *ring->tail;
492 if (next_tail == atomic_load_acquire(ring->head))
495 index = tail & sq_ring_mask;
497 ring->array[index] = index;
500 } while (prepped < max_ios);
503 atomic_store_release(ring->tail, tail);
507 static int get_file_size(struct file *f)
511 if (fstat(f->real_fd, &st) < 0)
513 if (S_ISBLK(st.st_mode)) {
514 unsigned long long bytes;
516 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
519 f->max_blocks = bytes / bs;
521 } else if (S_ISREG(st.st_mode)) {
522 f->max_blocks = st.st_size / bs;
529 static int reap_events_uring(struct submitter *s)
531 struct io_cq_ring *ring = &s->cq_ring;
532 struct io_uring_cqe *cqe;
533 unsigned head, reaped = 0;
534 int last_idx = -1, stat_nr = 0;
541 if (head == atomic_load_acquire(ring->tail))
543 cqe = &ring->cqes[head & cq_ring_mask];
545 int fileno = cqe->user_data & 0xffffffff;
547 f = &s->files[fileno];
549 if (cqe->res != bs) {
550 printf("io: unexpected ret=%d\n", cqe->res);
551 if (polled && cqe->res == -EOPNOTSUPP)
552 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
557 int clock_index = cqe->user_data >> 32;
559 if (last_idx != clock_index) {
560 if (last_idx != -1) {
561 add_stat(s, last_idx, stat_nr);
564 last_idx = clock_index;
573 add_stat(s, last_idx, stat_nr);
576 s->inflight -= reaped;
577 atomic_store_release(ring->head, head);
582 static int submitter_init(struct submitter *s)
587 printf("submitter=%d, tid=%d\n", s->index, s->tid);
589 srand48(pthread_self());
591 for (i = 0; i < MAX_FDS; i++)
592 s->files[i].fileno = i;
595 nr_batch = roundup_pow2(depth / batch_submit);
598 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
601 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
603 s->clock_batch = NULL;
612 static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
614 unsigned long offset, data;
620 while (index < max_ios) {
621 struct iocb *iocb = &iocbs[index];
623 if (s->nr_files == 1) {
626 f = &s->files[s->cur_file];
627 if (f->pending_ios >= file_depth(s)) {
629 if (s->cur_file == s->nr_files)
631 f = &s->files[s->cur_file];
637 offset = (r % (f->max_blocks - 1)) * bs;
638 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
639 s->iovecs[index].iov_len, offset);
642 if (stats && stats_running)
643 data |= ((unsigned long) s->clock_index << 32);
644 iocb->data = (void *) (uintptr_t) data;
650 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
652 int last_idx = -1, stat_nr = 0;
656 unsigned long data = (uintptr_t) events[reaped].data;
657 struct file *f = &s->files[data & 0xffffffff];
660 if (events[reaped].res != bs) {
661 printf("io: unexpected ret=%ld\n", events[reaped].res);
665 int clock_index = data >> 32;
667 if (last_idx != clock_index) {
668 if (last_idx != -1) {
669 add_stat(s, last_idx, stat_nr);
672 last_idx = clock_index;
681 add_stat(s, last_idx, stat_nr);
683 s->inflight -= reaped;
688 static void *submitter_aio_fn(void *data)
690 struct submitter *s = data;
691 int i, ret, prepped, nr_batch;
692 struct iocb **iocbsptr;
694 struct io_event *events;
696 nr_batch = submitter_init(s);
698 iocbsptr = calloc(depth, sizeof(struct iocb *));
699 iocbs = calloc(depth, sizeof(struct iocb));
700 events = calloc(depth, sizeof(struct io_event));
702 for (i = 0; i < depth; i++)
703 iocbsptr[i] = &iocbs[i];
707 int to_wait, to_submit, to_prep;
709 if (!prepped && s->inflight < depth) {
710 to_prep = min(depth - s->inflight, batch_submit);
711 prepped = prep_more_ios_aio(s, to_prep, iocbs);
712 #ifdef ARCH_HAVE_CPU_CLOCK
713 if (prepped && stats) {
714 s->clock_batch[s->clock_index] = get_cpu_clock();
715 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
719 s->inflight += prepped;
722 if (to_submit && (s->inflight + to_submit <= depth))
725 to_wait = min(s->inflight + to_submit, batch_complete);
727 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
732 } else if (ret != to_submit) {
733 printf("submitted %d, wanted %d\n", ret, to_submit);
742 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
744 perror("io_getevents");
746 } else if (r != to_wait) {
747 printf("r=%d, wait=%d\n", r, to_wait);
750 r = reap_events_aio(s, events, r);
754 } while (!s->finish);
764 static void *submitter_uring_fn(void *data)
766 struct submitter *s = data;
767 struct io_sq_ring *ring = &s->sq_ring;
768 int ret, prepped, nr_batch;
770 nr_batch = submitter_init(s);
774 int to_wait, to_submit, this_reap, to_prep;
775 unsigned ring_flags = 0;
777 if (!prepped && s->inflight < depth) {
778 to_prep = min(depth - s->inflight, batch_submit);
779 prepped = prep_more_ios_uring(s, to_prep);
780 #ifdef ARCH_HAVE_CPU_CLOCK
781 if (prepped && stats) {
782 s->clock_batch[s->clock_index] = get_cpu_clock();
783 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
787 s->inflight += prepped;
791 if (to_submit && (s->inflight + to_submit <= depth))
794 to_wait = min(s->inflight + to_submit, batch_complete);
797 * Only need to call io_uring_enter if we're not using SQ thread
798 * poll, or if IORING_SQ_NEED_WAKEUP is set.
801 ring_flags = atomic_load_acquire(ring->flags);
802 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
806 flags = IORING_ENTER_GETEVENTS;
807 if (ring_flags & IORING_SQ_NEED_WAKEUP)
808 flags |= IORING_ENTER_SQ_WAKEUP;
809 ret = io_uring_enter(s, to_submit, to_wait, flags);
812 /* for SQPOLL, we submitted it all effectively */
817 * For non SQ thread poll, we already got the events we needed
818 * through the io_uring_enter() above. For SQ thread poll, we
819 * need to loop here until we find enough events.
825 r = reap_events_uring(s);
831 } while (sq_thread_poll && this_reap < to_wait);
832 s->reaps += this_reap;
840 } else if (ret < to_submit) {
841 int diff = to_submit - ret;
850 } else if (ret < 0) {
851 if (errno == EAGAIN) {
859 printf("io_submit: %s\n", strerror(errno));
862 } while (!s->finish);
868 static struct submitter *get_submitter(int offset)
874 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
878 static void do_finish(const char *reason)
881 printf("Exiting on %s\n", reason);
882 for (j = 0; j < nthreads; j++) {
883 struct submitter *s = get_submitter(j);
886 if (max_iops > 100000)
887 printf("Maximum IOPS=%luK\n", max_iops / 1000);
889 printf("Maximum IOPS=%lu\n", max_iops);
893 static void sig_int(int sig)
898 static void arm_sig_int(void)
900 struct sigaction act;
902 memset(&act, 0, sizeof(act));
903 act.sa_handler = sig_int;
904 act.sa_flags = SA_RESTART;
905 sigaction(SIGINT, &act, NULL);
907 /* Windows uses SIGBREAK as a quit signal from other applications */
909 sigaction(SIGBREAK, &act, NULL);
913 static int setup_aio(struct submitter *s)
917 fprintf(stderr, "aio does not support polled IO\n");
920 if (sq_thread_poll) {
921 fprintf(stderr, "aio does not support SQPOLL IO\n");
925 fprintf(stderr, "aio does not support polled IO\n");
928 if (fixedbufs || register_files) {
929 fprintf(stderr, "aio does not support registered files or buffers\n");
930 fixedbufs = register_files = 0;
933 return io_queue_init(depth, &s->aio_ctx);
935 fprintf(stderr, "Legacy AIO not available on this system/build\n");
941 static int setup_ring(struct submitter *s)
943 struct io_sq_ring *sring = &s->sq_ring;
944 struct io_cq_ring *cring = &s->cq_ring;
945 struct io_uring_params p;
949 memset(&p, 0, sizeof(p));
951 if (polled && !do_nop)
952 p.flags |= IORING_SETUP_IOPOLL;
953 if (sq_thread_poll) {
954 p.flags |= IORING_SETUP_SQPOLL;
955 if (sq_thread_cpu != -1) {
956 p.flags |= IORING_SETUP_SQ_AFF;
957 p.sq_thread_cpu = sq_thread_cpu;
961 fd = io_uring_setup(depth, &p);
963 perror("io_uring_setup");
973 rlim.rlim_cur = RLIM_INFINITY;
974 rlim.rlim_max = RLIM_INFINITY;
975 /* ignore potential error, not needed on newer kernels */
976 setrlimit(RLIMIT_MEMLOCK, &rlim);
978 ret = io_uring_register_buffers(s);
980 perror("io_uring_register_buffers");
985 ret = io_uring_map_buffers(s);
987 perror("io_uring_map_buffers");
993 if (register_files) {
994 ret = io_uring_register_files(s);
996 perror("io_uring_register_files");
1001 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
1002 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1003 IORING_OFF_SQ_RING);
1004 sring->head = ptr + p.sq_off.head;
1005 sring->tail = ptr + p.sq_off.tail;
1006 sring->ring_mask = ptr + p.sq_off.ring_mask;
1007 sring->ring_entries = ptr + p.sq_off.ring_entries;
1008 sring->flags = ptr + p.sq_off.flags;
1009 sring->array = ptr + p.sq_off.array;
1010 sq_ring_mask = *sring->ring_mask;
1012 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
1013 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1016 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
1017 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1018 IORING_OFF_CQ_RING);
1019 cring->head = ptr + p.cq_off.head;
1020 cring->tail = ptr + p.cq_off.tail;
1021 cring->ring_mask = ptr + p.cq_off.ring_mask;
1022 cring->ring_entries = ptr + p.cq_off.ring_entries;
1023 cring->cqes = ptr + p.cq_off.cqes;
1024 cq_ring_mask = *cring->ring_mask;
1028 static void file_depths(char *buf)
1036 for (j = 0; j < nthreads; j++) {
1037 struct submitter *s = get_submitter(j);
1039 for (i = 0; i < s->nr_files; i++) {
1040 struct file *f = &s->files[i];
1043 p += sprintf(p, " %d", f->pending_ios);
1045 p += sprintf(p, "%d", f->pending_ios);
1051 static void usage(char *argv, int status)
1053 char runtime_str[16];
1054 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
1055 printf("%s [options] -- [filenames]\n"
1056 " -d <int> : IO Depth, default %d\n"
1057 " -s <int> : Batch submit, default %d\n"
1058 " -c <int> : Batch complete, default %d\n"
1059 " -b <int> : Block size, default %d\n"
1060 " -p <bool> : Polled IO, default %d\n"
1061 " -B <bool> : Fixed buffers, default %d\n"
1062 " -D <bool> : DMA map fixed buffers, default %d\n"
1063 " -F <bool> : Register files, default %d\n"
1064 " -n <int> : Number of threads, default %d\n"
1065 " -O <bool> : Use O_DIRECT, default %d\n"
1066 " -N <bool> : Perform just no-op requests, default %d\n"
1067 " -t <bool> : Track IO latencies, default %d\n"
1068 " -T <int> : TSC rate in HZ\n"
1069 " -a <bool> : Use legacy aio, default %d\n"
1070 " -r <int> : Runtime in seconds, default %s\n",
1071 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1072 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
1073 stats, aio, runtime == 0 ? "unlimited" : runtime_str);
1077 static void read_tsc_rate(void)
1085 fd = open(TSC_RATE_FILE, O_RDONLY);
1089 ret = read(fd, buffer, sizeof(buffer));
1095 tsc_rate = strtoul(buffer, NULL, 10);
1096 printf("Using TSC rate %luHz\n", tsc_rate);
1100 static void write_tsc_rate(void)
1106 if (!stat(TSC_RATE_FILE, &sb))
1109 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1113 memset(buffer, 0, sizeof(buffer));
1114 sprintf(buffer, "%lu", tsc_rate);
1115 ret = write(fd, buffer, strlen(buffer));
1121 int main(int argc, char *argv[])
1123 struct submitter *s;
1124 unsigned long done, calls, reap;
1125 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1130 if (!do_nop && argc < 2)
1133 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:r:D:h?")) != -1) {
1136 aio = !!atoi(optarg);
1139 depth = atoi(optarg);
1142 batch_submit = atoi(optarg);
1147 batch_complete = atoi(optarg);
1148 if (!batch_complete)
1155 polled = !!atoi(optarg);
1158 fixedbufs = !!atoi(optarg);
1161 register_files = !!atoi(optarg);
1164 nthreads = atoi(optarg);
1166 printf("Threads must be non-zero\n");
1171 do_nop = !!atoi(optarg);
1174 buffered = !atoi(optarg);
1177 #ifndef ARCH_HAVE_CPU_CLOCK
1178 fprintf(stderr, "Stats not supported on this CPU\n");
1181 stats = !!atoi(optarg);
1184 #ifndef ARCH_HAVE_CPU_CLOCK
1185 fprintf(stderr, "Stats not supported on this CPU\n");
1188 tsc_rate = strtoul(optarg, NULL, 10);
1192 runtime = atoi(optarg);
1195 dma_map = !!atoi(optarg);
1208 if (batch_complete > depth)
1209 batch_complete = depth;
1210 if (batch_submit > depth)
1211 batch_submit = depth;
1212 if (!fixedbufs && dma_map)
1215 submitter = calloc(nthreads, sizeof(*submitter) +
1216 depth * sizeof(struct iovec));
1217 for (j = 0; j < nthreads; j++) {
1218 s = get_submitter(j);
1220 s->done = s->calls = s->reaps = 0;
1223 flags = O_RDONLY | O_NOATIME;
1232 printf("No files specified\n");
1235 threads_per_f = nthreads / nfiles;
1236 /* make sure each thread gets assigned files */
1237 if (threads_per_f == 0) {
1240 threads_rem = nthreads - threads_per_f * nfiles;
1243 while (!do_nop && i < argc) {
1246 memset(&f, 0, sizeof(f));
1248 fd = open(argv[i], flags);
1254 if (get_file_size(&f)) {
1255 printf("failed getting size of device/file\n");
1258 if (f.max_blocks <= 1) {
1259 printf("Zero file/device size?\n");
1264 limit = threads_per_f;
1265 limit += threads_rem > 0 ? 1 : 0;
1266 for (k = 0; k < limit; k++) {
1267 s = get_submitter((j + k) % nthreads);
1269 if (s->nr_files == MAX_FDS) {
1270 printf("Max number of files (%d) reached\n", MAX_FDS);
1274 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1276 printf("Added file %s (submitter %d)\n", argv[i], s->index);
1286 for (j = 0; j < nthreads; j++) {
1287 s = get_submitter(j);
1288 for (i = 0; i < depth; i++) {
1291 if (posix_memalign(&buf, bs, bs)) {
1292 printf("failed alloc\n");
1295 s->iovecs[i].iov_base = buf;
1296 s->iovecs[i].iov_len = bs;
1300 for (j = 0; j < nthreads; j++) {
1301 s = get_submitter(j);
1304 err = setup_ring(s);
1308 printf("ring setup failed: %s, %d\n", strerror(errno), err);
1312 s = get_submitter(0);
1313 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
1315 printf("Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1317 printf("Engine=aio\n");
1319 for (j = 0; j < nthreads; j++) {
1320 s = get_submitter(j);
1322 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1323 #ifdef CONFIG_LIBAIO
1325 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1329 fdepths = malloc(8 * s->nr_files * nthreads);
1330 reap = calls = done = 0;
1332 unsigned long this_done = 0;
1333 unsigned long this_reap = 0;
1334 unsigned long this_call = 0;
1335 unsigned long rpc = 0, ipc = 0;
1336 unsigned long iops, bw;
1339 if (runtime && !--runtime)
1340 do_finish("timeout");
1342 /* don't print partial run, if interrupted by signal */
1346 /* one second in to the run, enable stats */
1350 for (j = 0; j < nthreads; j++) {
1351 s = get_submitter(j);
1352 this_done += s->done;
1353 this_call += s->calls;
1354 this_reap += s->reaps;
1356 if (this_call - calls) {
1357 rpc = (this_done - done) / (this_call - calls);
1358 ipc = (this_reap - reap) / (this_call - calls);
1361 file_depths(fdepths);
1362 iops = this_done - done;
1364 bw = iops * (bs / 1048576);
1366 bw = iops / (1048576 / bs);
1368 printf("IOPS=%luK, ", iops / 1000);
1370 printf("IOPS=%lu, ", iops);
1371 max_iops = max(max_iops, iops);
1373 printf("BW=%luMiB/s, ", bw);
1374 printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths);
1380 for (j = 0; j < nthreads; j++) {
1381 s = get_submitter(j);
1382 pthread_join(s->thread, &ret);
1388 printf("%d: Latency percentiles:\n", s->tid);
1389 for (i = 0, nr = 0; i < PLAT_NR; i++)
1391 show_clat_percentiles(s->plat, nr, 4);
1392 free(s->clock_batch);