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;
107 static int depth = DEPTH;
108 static int batch_submit = BATCH_SUBMIT;
109 static int batch_complete = BATCH_COMPLETE;
111 static int polled = 1; /* use IO polling */
112 static int fixedbufs = 1; /* use fixed user buffers */
113 static int dma_map; /* pre-map DMA buffers */
114 static int register_files = 1; /* use fixed files */
115 static int buffered = 0; /* use buffered IO, not O_DIRECT */
116 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
117 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
118 static int do_nop = 0; /* no-op SQ ring commands */
119 static int nthreads = 1;
120 static int stats = 0; /* generate IO stats */
121 static int aio = 0; /* use libaio */
122 static int runtime = 0; /* runtime */
124 static unsigned long tsc_rate;
126 #define TSC_RATE_FILE "tsc-rate"
128 static int vectored = 1;
130 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
131 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
132 static int plist_len = 17;
134 #ifndef IORING_REGISTER_MAP_BUFFERS
135 #define IORING_REGISTER_MAP_BUFFERS 20
136 struct io_uring_map_buffers {
144 static unsigned long cycles_to_nsec(unsigned long cycles)
151 val = cycles * 1000000000ULL;
152 return val / tsc_rate;
155 static unsigned long plat_idx_to_val(unsigned int idx)
157 unsigned int error_bits;
158 unsigned long k, base;
160 assert(idx < PLAT_NR);
162 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
163 * all bits of the sample as index */
164 if (idx < (PLAT_VAL << 1))
165 return cycles_to_nsec(idx);
167 /* Find the group and compute the minimum value of that group */
168 error_bits = (idx >> PLAT_BITS) - 1;
169 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
171 /* Find its bucket number of the group */
174 /* Return the mean of the range of the bucket */
175 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
178 unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
179 unsigned long **output,
180 unsigned long *maxv, unsigned long *minv)
182 unsigned long sum = 0;
183 unsigned int len = plist_len, i, j = 0;
184 unsigned long *ovals = NULL;
190 ovals = malloc(len * sizeof(*ovals));
195 * Calculate bucket values, note down max and min values
198 for (i = 0; i < PLAT_NR && !is_last; i++) {
200 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
201 assert(plist[j] <= 100.0);
203 ovals[j] = plat_idx_to_val(i);
204 if (ovals[j] < *minv)
206 if (ovals[j] > *maxv)
209 is_last = (j == len - 1) != 0;
218 fprintf(stderr, "error calculating latency percentiles\n");
224 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
225 unsigned int precision)
227 unsigned int divisor, len, i, j = 0;
228 unsigned long minv, maxv;
229 unsigned long *ovals;
230 int per_line, scale_down, time_width;
234 len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
241 printf(" percentiles (tsc ticks):\n |");
242 } else if (minv > 2000 && maxv > 99999) {
245 printf(" percentiles (usec):\n |");
249 printf(" percentiles (nsec):\n |");
252 time_width = max(5, (int) (log10(maxv / divisor) + 1));
253 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
254 precision, time_width);
255 /* fmt will be something like " %5.2fth=[%4llu]%c" */
256 per_line = (80 - 7) / (precision + 10 + time_width);
258 for (j = 0; j < len; j++) {
260 if (j != 0 && (j % per_line) == 0)
263 /* end of the list */
264 is_last = (j == len - 1) != 0;
266 for (i = 0; i < scale_down; i++)
267 ovals[j] = (ovals[j] + 999) / 1000;
269 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
274 if ((j % per_line) == per_line - 1) /* for formatting */
282 static unsigned int plat_val_to_idx(unsigned long val)
284 unsigned int msb, error_bits, base, offset, idx;
286 /* Find MSB starting from bit 0 */
290 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
293 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
294 * all bits of the sample as index
296 if (msb <= PLAT_BITS)
299 /* Compute the number of error bits to discard*/
300 error_bits = msb - PLAT_BITS;
302 /* Compute the number of buckets before the group */
303 base = (error_bits + 1) << PLAT_BITS;
306 * Discard the error bits and apply the mask to find the
307 * index for the buckets in the group
309 offset = (PLAT_VAL - 1) & (val >> error_bits);
311 /* Make sure the index does not exceed (array size - 1) */
312 idx = (base + offset) < (PLAT_NR - 1) ?
313 (base + offset) : (PLAT_NR - 1);
318 static void add_stat(struct submitter *s, int clock_index, int nr)
320 #ifdef ARCH_HAVE_CPU_CLOCK
321 unsigned long cycles;
324 if (!s->finish && clock_index) {
325 cycles = get_cpu_clock();
326 cycles -= s->clock_batch[clock_index];
327 pidx = plat_val_to_idx(cycles);
333 static int io_uring_map_buffers(struct submitter *s)
335 struct io_uring_map_buffers map = {
336 .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);
889 static void sig_int(int sig)
894 static void arm_sig_int(void)
896 struct sigaction act;
898 memset(&act, 0, sizeof(act));
899 act.sa_handler = sig_int;
900 act.sa_flags = SA_RESTART;
901 sigaction(SIGINT, &act, NULL);
903 /* Windows uses SIGBREAK as a quit signal from other applications */
905 sigaction(SIGBREAK, &act, NULL);
909 static int setup_aio(struct submitter *s)
913 fprintf(stderr, "aio does not support polled IO\n");
916 if (sq_thread_poll) {
917 fprintf(stderr, "aio does not support SQPOLL IO\n");
921 fprintf(stderr, "aio does not support polled IO\n");
924 if (fixedbufs || register_files) {
925 fprintf(stderr, "aio does not support registered files or buffers\n");
926 fixedbufs = register_files = 0;
929 return io_queue_init(depth, &s->aio_ctx);
931 fprintf(stderr, "Legacy AIO not available on this system/build\n");
937 static int setup_ring(struct submitter *s)
939 struct io_sq_ring *sring = &s->sq_ring;
940 struct io_cq_ring *cring = &s->cq_ring;
941 struct io_uring_params p;
945 memset(&p, 0, sizeof(p));
947 if (polled && !do_nop)
948 p.flags |= IORING_SETUP_IOPOLL;
949 if (sq_thread_poll) {
950 p.flags |= IORING_SETUP_SQPOLL;
951 if (sq_thread_cpu != -1) {
952 p.flags |= IORING_SETUP_SQ_AFF;
953 p.sq_thread_cpu = sq_thread_cpu;
957 fd = io_uring_setup(depth, &p);
959 perror("io_uring_setup");
969 rlim.rlim_cur = RLIM_INFINITY;
970 rlim.rlim_max = RLIM_INFINITY;
971 /* ignore potential error, not needed on newer kernels */
972 setrlimit(RLIMIT_MEMLOCK, &rlim);
974 ret = io_uring_register_buffers(s);
976 perror("io_uring_register_buffers");
981 ret = io_uring_map_buffers(s);
983 perror("io_uring_map_buffers");
989 if (register_files) {
990 ret = io_uring_register_files(s);
992 perror("io_uring_register_files");
997 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
998 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1000 sring->head = ptr + p.sq_off.head;
1001 sring->tail = ptr + p.sq_off.tail;
1002 sring->ring_mask = ptr + p.sq_off.ring_mask;
1003 sring->ring_entries = ptr + p.sq_off.ring_entries;
1004 sring->flags = ptr + p.sq_off.flags;
1005 sring->array = ptr + p.sq_off.array;
1006 sq_ring_mask = *sring->ring_mask;
1008 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
1009 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1012 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
1013 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1014 IORING_OFF_CQ_RING);
1015 cring->head = ptr + p.cq_off.head;
1016 cring->tail = ptr + p.cq_off.tail;
1017 cring->ring_mask = ptr + p.cq_off.ring_mask;
1018 cring->ring_entries = ptr + p.cq_off.ring_entries;
1019 cring->cqes = ptr + p.cq_off.cqes;
1020 cq_ring_mask = *cring->ring_mask;
1024 static void file_depths(char *buf)
1032 for (j = 0; j < nthreads; j++) {
1033 struct submitter *s = get_submitter(j);
1035 for (i = 0; i < s->nr_files; i++) {
1036 struct file *f = &s->files[i];
1039 p += sprintf(p, " %d", f->pending_ios);
1041 p += sprintf(p, "%d", f->pending_ios);
1047 static void usage(char *argv, int status)
1049 char runtime_str[16];
1050 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
1051 printf("%s [options] -- [filenames]\n"
1052 " -d <int> : IO Depth, default %d\n"
1053 " -s <int> : Batch submit, default %d\n"
1054 " -c <int> : Batch complete, default %d\n"
1055 " -b <int> : Block size, default %d\n"
1056 " -p <bool> : Polled IO, default %d\n"
1057 " -B <bool> : Fixed buffers, default %d\n"
1058 " -R <bool> : DMA map fixed buffers, default %d\n"
1059 " -F <bool> : Register files, default %d\n"
1060 " -n <int> : Number of threads, default %d\n"
1061 " -O <bool> : Use O_DIRECT, default %d\n"
1062 " -N <bool> : Perform just no-op requests, default %d\n"
1063 " -t <bool> : Track IO latencies, default %d\n"
1064 " -T <int> : TSC rate in HZ\n"
1065 " -a <bool> : Use legacy aio, default %d\n"
1066 " -r <int> : Runtime in seconds, default %s\n",
1067 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1068 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
1069 stats, aio, runtime == 0 ? "unlimited" : runtime_str);
1073 static void read_tsc_rate(void)
1081 fd = open(TSC_RATE_FILE, O_RDONLY);
1085 ret = read(fd, buffer, sizeof(buffer));
1091 tsc_rate = strtoul(buffer, NULL, 10);
1092 printf("Using TSC rate %luHz\n", tsc_rate);
1096 static void write_tsc_rate(void)
1102 if (!stat(TSC_RATE_FILE, &sb))
1105 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1109 memset(buffer, 0, sizeof(buffer));
1110 sprintf(buffer, "%lu", tsc_rate);
1111 ret = write(fd, buffer, strlen(buffer));
1117 int main(int argc, char *argv[])
1119 struct submitter *s;
1120 unsigned long done, calls, reap;
1121 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1126 if (!do_nop && argc < 2)
1129 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:r:D:h?")) != -1) {
1132 aio = !!atoi(optarg);
1135 depth = atoi(optarg);
1138 batch_submit = atoi(optarg);
1143 batch_complete = atoi(optarg);
1144 if (!batch_complete)
1151 polled = !!atoi(optarg);
1154 fixedbufs = !!atoi(optarg);
1157 register_files = !!atoi(optarg);
1160 nthreads = atoi(optarg);
1162 printf("Threads must be non-zero\n");
1167 do_nop = !!atoi(optarg);
1170 buffered = !atoi(optarg);
1173 #ifndef ARCH_HAVE_CPU_CLOCK
1174 fprintf(stderr, "Stats not supported on this CPU\n");
1177 stats = !!atoi(optarg);
1180 #ifndef ARCH_HAVE_CPU_CLOCK
1181 fprintf(stderr, "Stats not supported on this CPU\n");
1184 tsc_rate = strtoul(optarg, NULL, 10);
1188 runtime = atoi(optarg);
1191 dma_map = !!atoi(optarg);
1204 if (batch_complete > depth)
1205 batch_complete = depth;
1206 if (batch_submit > depth)
1207 batch_submit = depth;
1208 if (!fixedbufs && dma_map)
1211 submitter = calloc(nthreads, sizeof(*submitter) +
1212 depth * sizeof(struct iovec));
1213 for (j = 0; j < nthreads; j++) {
1214 s = get_submitter(j);
1216 s->done = s->calls = s->reaps = 0;
1219 flags = O_RDONLY | O_NOATIME;
1228 printf("No files specified\n");
1231 threads_per_f = nthreads / nfiles;
1232 /* make sure each thread gets assigned files */
1233 if (threads_per_f == 0) {
1236 threads_rem = nthreads - threads_per_f * nfiles;
1239 while (!do_nop && i < argc) {
1242 memset(&f, 0, sizeof(f));
1244 fd = open(argv[i], flags);
1250 if (get_file_size(&f)) {
1251 printf("failed getting size of device/file\n");
1254 if (f.max_blocks <= 1) {
1255 printf("Zero file/device size?\n");
1260 limit = threads_per_f;
1261 limit += threads_rem > 0 ? 1 : 0;
1262 for (k = 0; k < limit; k++) {
1263 s = get_submitter((j + k) % nthreads);
1265 if (s->nr_files == MAX_FDS) {
1266 printf("Max number of files (%d) reached\n", MAX_FDS);
1270 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1272 printf("Added file %s (submitter %d)\n", argv[i], s->index);
1282 for (j = 0; j < nthreads; j++) {
1283 s = get_submitter(j);
1284 for (i = 0; i < depth; i++) {
1287 if (posix_memalign(&buf, bs, bs)) {
1288 printf("failed alloc\n");
1291 s->iovecs[i].iov_base = buf;
1292 s->iovecs[i].iov_len = bs;
1296 for (j = 0; j < nthreads; j++) {
1297 s = get_submitter(j);
1300 err = setup_ring(s);
1304 printf("ring setup failed: %s, %d\n", strerror(errno), err);
1308 s = get_submitter(0);
1309 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
1311 printf("Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1313 printf("Engine=aio\n");
1315 for (j = 0; j < nthreads; j++) {
1316 s = get_submitter(j);
1318 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1319 #ifdef CONFIG_LIBAIO
1321 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1325 fdepths = malloc(8 * s->nr_files * nthreads);
1326 reap = calls = done = 0;
1328 unsigned long this_done = 0;
1329 unsigned long this_reap = 0;
1330 unsigned long this_call = 0;
1331 unsigned long rpc = 0, ipc = 0;
1332 unsigned long iops, bw;
1335 if (runtime && !--runtime)
1336 do_finish("timeout");
1338 /* don't print partial run, if interrupted by signal */
1342 /* one second in to the run, enable stats */
1346 for (j = 0; j < nthreads; j++) {
1347 this_done += s->done;
1348 this_call += s->calls;
1349 this_reap += s->reaps;
1351 if (this_call - calls) {
1352 rpc = (this_done - done) / (this_call - calls);
1353 ipc = (this_reap - reap) / (this_call - calls);
1356 file_depths(fdepths);
1357 iops = this_done - done;
1359 bw = iops * (bs / 1048576);
1361 bw = iops / (1048576 / bs);
1363 printf("IOPS=%luK, ", iops / 1000);
1365 printf("IOPS=%luK, ", iops);
1367 printf("BW=%luMiB/s, ", bw);
1368 printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths);
1374 for (j = 0; j < nthreads; j++) {
1375 s = get_submitter(j);
1376 pthread_join(s->thread, &ret);
1382 printf("%d: Latency percentiles:\n", s->tid);
1383 for (i = 0, nr = 0; i < PLAT_NR; i++)
1385 show_clat_percentiles(s->plat, nr, 4);
1386 free(s->clock_batch);