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;
106 static int depth = DEPTH;
107 static int batch_submit = BATCH_SUBMIT;
108 static int batch_complete = BATCH_COMPLETE;
110 static int polled = 1; /* use IO polling */
111 static int fixedbufs = 1; /* use fixed user buffers */
112 static int register_files = 1; /* use fixed files */
113 static int buffered = 0; /* use buffered IO, not O_DIRECT */
114 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
115 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
116 static int do_nop = 0; /* no-op SQ ring commands */
117 static int nthreads = 1;
118 static int stats = 0; /* generate IO stats */
119 static int aio = 0; /* use libaio */
121 static unsigned long tsc_rate;
123 #define TSC_RATE_FILE "tsc-rate"
125 static int vectored = 1;
127 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
128 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
129 static int plist_len = 17;
131 static unsigned long cycles_to_nsec(unsigned long cycles)
138 val = cycles * 1000000000ULL;
139 return val / tsc_rate;
142 static unsigned long plat_idx_to_val(unsigned int idx)
144 unsigned int error_bits;
145 unsigned long k, base;
147 assert(idx < PLAT_NR);
149 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
150 * all bits of the sample as index */
151 if (idx < (PLAT_VAL << 1))
152 return cycles_to_nsec(idx);
154 /* Find the group and compute the minimum value of that group */
155 error_bits = (idx >> PLAT_BITS) - 1;
156 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
158 /* Find its bucket number of the group */
161 /* Return the mean of the range of the bucket */
162 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
165 unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
166 unsigned long **output,
167 unsigned long *maxv, unsigned long *minv)
169 unsigned long sum = 0;
170 unsigned int len = plist_len, i, j = 0;
171 unsigned long *ovals = NULL;
177 ovals = malloc(len * sizeof(*ovals));
182 * Calculate bucket values, note down max and min values
185 for (i = 0; i < PLAT_NR && !is_last; i++) {
187 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
188 assert(plist[j] <= 100.0);
190 ovals[j] = plat_idx_to_val(i);
191 if (ovals[j] < *minv)
193 if (ovals[j] > *maxv)
196 is_last = (j == len - 1) != 0;
205 fprintf(stderr, "error calculating latency percentiles\n");
211 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
212 unsigned int precision)
214 unsigned int divisor, len, i, j = 0;
215 unsigned long minv, maxv;
216 unsigned long *ovals;
217 int per_line, scale_down, time_width;
221 len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
228 printf(" percentiles (tsc ticks):\n |");
229 } else if (minv > 2000 && maxv > 99999) {
232 printf(" percentiles (usec):\n |");
236 printf(" percentiles (nsec):\n |");
239 time_width = max(5, (int) (log10(maxv / divisor) + 1));
240 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
241 precision, time_width);
242 /* fmt will be something like " %5.2fth=[%4llu]%c" */
243 per_line = (80 - 7) / (precision + 10 + time_width);
245 for (j = 0; j < len; j++) {
247 if (j != 0 && (j % per_line) == 0)
250 /* end of the list */
251 is_last = (j == len - 1) != 0;
253 for (i = 0; i < scale_down; i++)
254 ovals[j] = (ovals[j] + 999) / 1000;
256 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
261 if ((j % per_line) == per_line - 1) /* for formatting */
269 static unsigned int plat_val_to_idx(unsigned long val)
271 unsigned int msb, error_bits, base, offset, idx;
273 /* Find MSB starting from bit 0 */
277 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
280 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
281 * all bits of the sample as index
283 if (msb <= PLAT_BITS)
286 /* Compute the number of error bits to discard*/
287 error_bits = msb - PLAT_BITS;
289 /* Compute the number of buckets before the group */
290 base = (error_bits + 1) << PLAT_BITS;
293 * Discard the error bits and apply the mask to find the
294 * index for the buckets in the group
296 offset = (PLAT_VAL - 1) & (val >> error_bits);
298 /* Make sure the index does not exceed (array size - 1) */
299 idx = (base + offset) < (PLAT_NR - 1) ?
300 (base + offset) : (PLAT_NR - 1);
305 static void add_stat(struct submitter *s, int clock_index, int nr)
307 #ifdef ARCH_HAVE_CPU_CLOCK
308 unsigned long cycles;
311 cycles = get_cpu_clock();
312 cycles -= s->clock_batch[clock_index];
313 pidx = plat_val_to_idx(cycles);
318 static int io_uring_register_buffers(struct submitter *s)
323 return syscall(__NR_io_uring_register, s->ring_fd,
324 IORING_REGISTER_BUFFERS, s->iovecs, depth);
327 static int io_uring_register_files(struct submitter *s)
334 s->fds = calloc(s->nr_files, sizeof(__s32));
335 for (i = 0; i < s->nr_files; i++) {
336 s->fds[i] = s->files[i].real_fd;
337 s->files[i].fixed_fd = i;
340 return syscall(__NR_io_uring_register, s->ring_fd,
341 IORING_REGISTER_FILES, s->fds, s->nr_files);
344 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
346 return syscall(__NR_io_uring_setup, entries, p);
349 static void io_uring_probe(int fd)
351 struct io_uring_probe *p;
354 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
358 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
359 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
363 if (IORING_OP_READ > p->ops_len)
366 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
372 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
373 unsigned int min_complete, unsigned int flags)
375 return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete,
379 #ifndef CONFIG_HAVE_GETTID
380 static int gettid(void)
382 return syscall(__NR_gettid);
386 static unsigned file_depth(struct submitter *s)
388 return (depth + s->nr_files - 1) / s->nr_files;
391 static void init_io(struct submitter *s, unsigned index)
393 struct io_uring_sqe *sqe = &s->sqes[index];
394 unsigned long offset;
399 sqe->opcode = IORING_OP_NOP;
403 if (s->nr_files == 1) {
406 f = &s->files[s->cur_file];
407 if (f->pending_ios >= file_depth(s)) {
409 if (s->cur_file == s->nr_files)
411 f = &s->files[s->cur_file];
417 offset = (r % (f->max_blocks - 1)) * bs;
419 if (register_files) {
420 sqe->flags = IOSQE_FIXED_FILE;
421 sqe->fd = f->fixed_fd;
424 sqe->fd = f->real_fd;
427 sqe->opcode = IORING_OP_READ_FIXED;
428 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
430 sqe->buf_index = index;
431 } else if (!vectored) {
432 sqe->opcode = IORING_OP_READ;
433 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
437 sqe->opcode = IORING_OP_READV;
438 sqe->addr = (unsigned long) &s->iovecs[index];
444 sqe->user_data = (unsigned long) f->fileno;
446 sqe->user_data |= ((unsigned long)s->clock_index << 32);
449 static int prep_more_ios_uring(struct submitter *s, int max_ios)
451 struct io_sq_ring *ring = &s->sq_ring;
452 unsigned index, tail, next_tail, prepped = 0;
454 next_tail = tail = *ring->tail;
457 if (next_tail == atomic_load_acquire(ring->head))
460 index = tail & sq_ring_mask;
462 ring->array[index] = index;
465 } while (prepped < max_ios);
468 atomic_store_release(ring->tail, tail);
472 static int get_file_size(struct file *f)
476 if (fstat(f->real_fd, &st) < 0)
478 if (S_ISBLK(st.st_mode)) {
479 unsigned long long bytes;
481 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
484 f->max_blocks = bytes / bs;
486 } else if (S_ISREG(st.st_mode)) {
487 f->max_blocks = st.st_size / bs;
494 static int reap_events_uring(struct submitter *s)
496 struct io_cq_ring *ring = &s->cq_ring;
497 struct io_uring_cqe *cqe;
498 unsigned head, reaped = 0;
499 int last_idx = -1, stat_nr = 0;
506 if (head == atomic_load_acquire(ring->tail))
508 cqe = &ring->cqes[head & cq_ring_mask];
510 int fileno = cqe->user_data & 0xffffffff;
512 f = &s->files[fileno];
514 if (cqe->res != bs) {
515 printf("io: unexpected ret=%d\n", cqe->res);
516 if (polled && cqe->res == -EOPNOTSUPP)
517 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
522 int clock_index = cqe->user_data >> 32;
524 if (last_idx != clock_index) {
525 if (last_idx != -1) {
526 add_stat(s, last_idx, stat_nr);
529 last_idx = clock_index;
538 add_stat(s, last_idx, stat_nr);
541 s->inflight -= reaped;
542 atomic_store_release(ring->head, head);
547 static int submitter_init(struct submitter *s)
552 printf("submitter=%d\n", s->tid);
554 srand48(pthread_self());
556 for (i = 0; i < MAX_FDS; i++)
557 s->files[i].fileno = i;
560 nr_batch = roundup_pow2(depth / batch_submit);
561 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
564 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
566 s->clock_batch = NULL;
575 static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
577 unsigned long offset, data;
583 while (index < max_ios) {
584 struct iocb *iocb = &iocbs[index];
586 if (s->nr_files == 1) {
589 f = &s->files[s->cur_file];
590 if (f->pending_ios >= file_depth(s)) {
592 if (s->cur_file == s->nr_files)
594 f = &s->files[s->cur_file];
600 offset = (r % (f->max_blocks - 1)) * bs;
601 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
602 s->iovecs[index].iov_len, offset);
606 data |= ((unsigned long) s->clock_index << 32);
607 iocb->data = (void *) (uintptr_t) data;
613 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
615 int last_idx = -1, stat_nr = 0;
619 unsigned long data = (uintptr_t) events[reaped].data;
620 struct file *f = &s->files[data & 0xffffffff];
623 if (events[reaped].res != bs) {
624 printf("io: unexpected ret=%ld\n", events[reaped].res);
628 int clock_index = data >> 32;
630 if (last_idx != clock_index) {
631 if (last_idx != -1) {
632 add_stat(s, last_idx, stat_nr);
635 last_idx = clock_index;
644 add_stat(s, last_idx, stat_nr);
646 s->inflight -= reaped;
651 static void *submitter_aio_fn(void *data)
653 struct submitter *s = data;
654 int i, ret, prepped, nr_batch;
655 struct iocb **iocbsptr;
657 struct io_event *events;
659 nr_batch = submitter_init(s);
661 iocbsptr = calloc(depth, sizeof(struct iocb *));
662 iocbs = calloc(depth, sizeof(struct iocb));
663 events = calloc(depth, sizeof(struct io_event));
665 for (i = 0; i < depth; i++)
666 iocbsptr[i] = &iocbs[i];
670 int to_wait, to_submit, to_prep;
672 if (!prepped && s->inflight < depth) {
673 to_prep = min(depth - s->inflight, batch_submit);
674 prepped = prep_more_ios_aio(s, to_prep, iocbs);
675 #ifdef ARCH_HAVE_CPU_CLOCK
676 if (prepped && stats) {
677 s->clock_batch[s->clock_index] = get_cpu_clock();
678 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
682 s->inflight += prepped;
685 if (to_submit && (s->inflight + to_submit <= depth))
688 to_wait = min(s->inflight + to_submit, batch_complete);
690 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
695 } else if (ret != to_submit) {
696 printf("submitted %d, wanted %d\n", ret, to_submit);
706 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
708 perror("io_getevents");
710 } else if (r != to_wait) {
711 printf("r=%d, wait=%d\n", r, to_wait);
714 r = reap_events_aio(s, events, r);
719 } while (!s->finish);
729 static void *submitter_uring_fn(void *data)
731 struct submitter *s = data;
732 struct io_sq_ring *ring = &s->sq_ring;
733 int ret, prepped, nr_batch;
735 nr_batch = submitter_init(s);
739 int to_wait, to_submit, this_reap, to_prep;
740 unsigned ring_flags = 0;
742 if (!prepped && s->inflight < depth) {
743 to_prep = min(depth - s->inflight, batch_submit);
744 prepped = prep_more_ios_uring(s, to_prep);
745 #ifdef ARCH_HAVE_CPU_CLOCK
746 if (prepped && stats) {
747 s->clock_batch[s->clock_index] = get_cpu_clock();
748 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
752 s->inflight += prepped;
756 if (to_submit && (s->inflight + to_submit <= depth))
759 to_wait = min(s->inflight + to_submit, batch_complete);
762 * Only need to call io_uring_enter if we're not using SQ thread
763 * poll, or if IORING_SQ_NEED_WAKEUP is set.
766 ring_flags = atomic_load_acquire(ring->flags);
767 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
771 flags = IORING_ENTER_GETEVENTS;
772 if (ring_flags & IORING_SQ_NEED_WAKEUP)
773 flags |= IORING_ENTER_SQ_WAKEUP;
774 ret = io_uring_enter(s, to_submit, to_wait, flags);
777 /* for SQPOLL, we submitted it all effectively */
782 * For non SQ thread poll, we already got the events we needed
783 * through the io_uring_enter() above. For SQ thread poll, we
784 * need to loop here until we find enough events.
790 r = reap_events_uring(s);
796 } while (sq_thread_poll && this_reap < to_wait);
797 s->reaps += this_reap;
805 } else if (ret < to_submit) {
806 int diff = to_submit - ret;
815 } else if (ret < 0) {
816 if (errno == EAGAIN) {
824 printf("io_submit: %s\n", strerror(errno));
827 } while (!s->finish);
833 static struct submitter *get_submitter(int offset)
839 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
843 static void sig_int(int sig)
847 printf("Exiting on signal %d\n", sig);
848 for (j = 0; j < nthreads; j++) {
849 struct submitter *s = get_submitter(j);
855 static void arm_sig_int(void)
857 struct sigaction act;
859 memset(&act, 0, sizeof(act));
860 act.sa_handler = sig_int;
861 act.sa_flags = SA_RESTART;
862 sigaction(SIGINT, &act, NULL);
864 /* Windows uses SIGBREAK as a quit signal from other applications */
866 sigaction(SIGBREAK, &act, NULL);
870 static int setup_aio(struct submitter *s)
874 fprintf(stderr, "aio does not support polled IO\n");
877 if (sq_thread_poll) {
878 fprintf(stderr, "aio does not support SQPOLL IO\n");
882 fprintf(stderr, "aio does not support polled IO\n");
885 if (fixedbufs || register_files) {
886 fprintf(stderr, "aio does not support registered files or buffers\n");
887 fixedbufs = register_files = 0;
890 return io_queue_init(depth, &s->aio_ctx);
892 fprintf(stderr, "Legacy AIO not available on this system/build\n");
898 static int setup_ring(struct submitter *s)
900 struct io_sq_ring *sring = &s->sq_ring;
901 struct io_cq_ring *cring = &s->cq_ring;
902 struct io_uring_params p;
906 memset(&p, 0, sizeof(p));
908 if (polled && !do_nop)
909 p.flags |= IORING_SETUP_IOPOLL;
910 if (sq_thread_poll) {
911 p.flags |= IORING_SETUP_SQPOLL;
912 if (sq_thread_cpu != -1) {
913 p.flags |= IORING_SETUP_SQ_AFF;
914 p.sq_thread_cpu = sq_thread_cpu;
918 fd = io_uring_setup(depth, &p);
920 perror("io_uring_setup");
930 rlim.rlim_cur = RLIM_INFINITY;
931 rlim.rlim_max = RLIM_INFINITY;
932 /* ignore potential error, not needed on newer kernels */
933 setrlimit(RLIMIT_MEMLOCK, &rlim);
935 ret = io_uring_register_buffers(s);
937 perror("io_uring_register_buffers");
942 if (register_files) {
943 ret = io_uring_register_files(s);
945 perror("io_uring_register_files");
950 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
951 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
953 printf("sq_ring ptr = 0x%p\n", ptr);
954 sring->head = ptr + p.sq_off.head;
955 sring->tail = ptr + p.sq_off.tail;
956 sring->ring_mask = ptr + p.sq_off.ring_mask;
957 sring->ring_entries = ptr + p.sq_off.ring_entries;
958 sring->flags = ptr + p.sq_off.flags;
959 sring->array = ptr + p.sq_off.array;
960 sq_ring_mask = *sring->ring_mask;
962 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
963 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
965 printf("sqes ptr = 0x%p\n", s->sqes);
967 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
968 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
970 printf("cq_ring ptr = 0x%p\n", ptr);
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 file_depths(char *buf)
988 for (j = 0; j < nthreads; j++) {
989 struct submitter *s = get_submitter(j);
991 for (i = 0; i < s->nr_files; i++) {
992 struct file *f = &s->files[i];
995 p += sprintf(p, " %d", f->pending_ios);
997 p += sprintf(p, "%d", f->pending_ios);
1003 static void usage(char *argv, int status)
1005 printf("%s [options] -- [filenames]\n"
1006 " -d <int> : IO Depth, default %d\n"
1007 " -s <int> : Batch submit, default %d\n"
1008 " -c <int> : Batch complete, default %d\n"
1009 " -b <int> : Block size, default %d\n"
1010 " -p <bool> : Polled IO, default %d\n"
1011 " -B <bool> : Fixed buffers, default %d\n"
1012 " -F <bool> : Register files, default %d\n"
1013 " -n <int> : Number of threads, default %d\n"
1014 " -O <bool> : Use O_DIRECT, default %d\n"
1015 " -N <bool> : Perform just no-op requests, default %d\n"
1016 " -t <bool> : Track IO latencies, default %d\n"
1017 " -T <int> : TSC rate in HZ\n"
1018 " -a <bool> : Use legacy aio, default %d\n",
1019 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1020 fixedbufs, register_files, nthreads, !buffered, do_nop, stats, aio);
1024 static void read_tsc_rate(void)
1032 fd = open(TSC_RATE_FILE, O_RDONLY);
1036 ret = read(fd, buffer, sizeof(buffer));
1042 tsc_rate = strtoul(buffer, NULL, 10);
1043 printf("Using TSC rate %luHz\n", tsc_rate);
1047 static void write_tsc_rate(void)
1053 if (!stat(TSC_RATE_FILE, &sb))
1056 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1060 memset(buffer, 0, sizeof(buffer));
1061 sprintf(buffer, "%lu", tsc_rate);
1062 ret = write(fd, buffer, strlen(buffer));
1068 int main(int argc, char *argv[])
1070 struct submitter *s;
1071 unsigned long done, calls, reap;
1072 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1077 if (!do_nop && argc < 2)
1080 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:h?")) != -1) {
1083 aio = !!atoi(optarg);
1086 depth = atoi(optarg);
1089 batch_submit = atoi(optarg);
1094 batch_complete = atoi(optarg);
1095 if (!batch_complete)
1102 polled = !!atoi(optarg);
1105 fixedbufs = !!atoi(optarg);
1108 register_files = !!atoi(optarg);
1111 nthreads = atoi(optarg);
1113 printf("Threads must be non-zero\n");
1118 do_nop = !!atoi(optarg);
1121 buffered = !atoi(optarg);
1124 #ifndef ARCH_HAVE_CPU_CLOCK
1125 fprintf(stderr, "Stats not supported on this CPU\n");
1128 stats = !!atoi(optarg);
1131 #ifndef ARCH_HAVE_CPU_CLOCK
1132 fprintf(stderr, "Stats not supported on this CPU\n");
1135 tsc_rate = strtoul(optarg, NULL, 10);
1149 if (batch_complete > depth)
1150 batch_complete = depth;
1151 if (batch_submit > depth)
1152 batch_submit = depth;
1154 submitter = calloc(nthreads, sizeof(*submitter) +
1155 depth * sizeof(struct iovec));
1156 for (j = 0; j < nthreads; j++) {
1157 s = get_submitter(j);
1159 s->done = s->calls = s->reaps = 0;
1162 flags = O_RDONLY | O_NOATIME;
1171 printf("No files specified\n");
1174 threads_per_f = nthreads / nfiles;
1175 /* make sure each thread gets assigned files */
1176 if (threads_per_f == 0) {
1179 threads_rem = nthreads - threads_per_f * nfiles;
1182 while (!do_nop && i < argc) {
1185 memset(&f, 0, sizeof(f));
1187 fd = open(argv[i], flags);
1193 if (get_file_size(&f)) {
1194 printf("failed getting size of device/file\n");
1197 if (f.max_blocks <= 1) {
1198 printf("Zero file/device size?\n");
1203 limit = threads_per_f;
1204 limit += threads_rem > 0 ? 1 : 0;
1205 for (k = 0; k < limit; k++) {
1206 s = get_submitter((j + k) % nthreads);
1208 if (s->nr_files == MAX_FDS) {
1209 printf("Max number of files (%d) reached\n", MAX_FDS);
1213 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1215 printf("Added file %s (submitter %d)\n", argv[i], s->index);
1225 for (j = 0; j < nthreads; j++) {
1226 s = get_submitter(j);
1227 for (i = 0; i < depth; i++) {
1230 if (posix_memalign(&buf, bs, bs)) {
1231 printf("failed alloc\n");
1234 s->iovecs[i].iov_base = buf;
1235 s->iovecs[i].iov_len = bs;
1239 for (j = 0; j < nthreads; j++) {
1240 s = get_submitter(j);
1243 err = setup_ring(s);
1247 printf("ring setup failed: %s, %d\n", strerror(errno), err);
1251 s = get_submitter(0);
1252 printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, register_files, buffered, depth);
1254 printf("Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1255 #ifdef CONFIG_LIBAIO
1257 printf("Engine=aio, ctx=%p\n", &s->aio_ctx);
1260 for (j = 0; j < nthreads; j++) {
1261 s = get_submitter(j);
1263 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1264 #ifdef CONFIG_LIBAIO
1266 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1270 fdepths = malloc(8 * s->nr_files * nthreads);
1271 reap = calls = done = 0;
1273 unsigned long this_done = 0;
1274 unsigned long this_reap = 0;
1275 unsigned long this_call = 0;
1276 unsigned long rpc = 0, ipc = 0;
1277 unsigned long iops, bw;
1281 /* don't print partial run, if interrupted by signal */
1284 for (j = 0; j < nthreads; j++) {
1285 this_done += s->done;
1286 this_call += s->calls;
1287 this_reap += s->reaps;
1289 if (this_call - calls) {
1290 rpc = (this_done - done) / (this_call - calls);
1291 ipc = (this_reap - reap) / (this_call - calls);
1294 file_depths(fdepths);
1295 iops = this_done - done;
1297 bw = iops * (bs / 1048576);
1299 bw = iops / (1048576 / bs);
1300 printf("IOPS=%lu, ", iops);
1302 printf("BW=%luMiB/s, ", bw);
1303 printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths);
1309 for (j = 0; j < nthreads; j++) {
1310 s = get_submitter(j);
1311 pthread_join(s->thread, &ret);
1317 printf("%d: Latency percentiles:\n", s->tid);
1318 for (i = 0, nr = 0; i < PLAT_NR; i++)
1320 show_clat_percentiles(s->plat, nr, 4);
1321 free(s->clock_batch);