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 register_files = 1; /* use fixed files */
114 static int buffered = 0; /* use buffered IO, not O_DIRECT */
115 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
116 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
117 static int do_nop = 0; /* no-op SQ ring commands */
118 static int nthreads = 1;
119 static int stats = 0; /* generate IO stats */
120 static int aio = 0; /* use libaio */
122 static unsigned long tsc_rate;
124 #define TSC_RATE_FILE "tsc-rate"
126 static int vectored = 1;
128 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
129 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
130 static int plist_len = 17;
132 static unsigned long cycles_to_nsec(unsigned long cycles)
139 val = cycles * 1000000000ULL;
140 return val / tsc_rate;
143 static unsigned long plat_idx_to_val(unsigned int idx)
145 unsigned int error_bits;
146 unsigned long k, base;
148 assert(idx < PLAT_NR);
150 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
151 * all bits of the sample as index */
152 if (idx < (PLAT_VAL << 1))
153 return cycles_to_nsec(idx);
155 /* Find the group and compute the minimum value of that group */
156 error_bits = (idx >> PLAT_BITS) - 1;
157 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
159 /* Find its bucket number of the group */
162 /* Return the mean of the range of the bucket */
163 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
166 unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
167 unsigned long **output,
168 unsigned long *maxv, unsigned long *minv)
170 unsigned long sum = 0;
171 unsigned int len = plist_len, i, j = 0;
172 unsigned long *ovals = NULL;
178 ovals = malloc(len * sizeof(*ovals));
183 * Calculate bucket values, note down max and min values
186 for (i = 0; i < PLAT_NR && !is_last; i++) {
188 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
189 assert(plist[j] <= 100.0);
191 ovals[j] = plat_idx_to_val(i);
192 if (ovals[j] < *minv)
194 if (ovals[j] > *maxv)
197 is_last = (j == len - 1) != 0;
206 fprintf(stderr, "error calculating latency percentiles\n");
212 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
213 unsigned int precision)
215 unsigned int divisor, len, i, j = 0;
216 unsigned long minv, maxv;
217 unsigned long *ovals;
218 int per_line, scale_down, time_width;
222 len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
229 printf(" percentiles (tsc ticks):\n |");
230 } else if (minv > 2000 && maxv > 99999) {
233 printf(" percentiles (usec):\n |");
237 printf(" percentiles (nsec):\n |");
240 time_width = max(5, (int) (log10(maxv / divisor) + 1));
241 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
242 precision, time_width);
243 /* fmt will be something like " %5.2fth=[%4llu]%c" */
244 per_line = (80 - 7) / (precision + 10 + time_width);
246 for (j = 0; j < len; j++) {
248 if (j != 0 && (j % per_line) == 0)
251 /* end of the list */
252 is_last = (j == len - 1) != 0;
254 for (i = 0; i < scale_down; i++)
255 ovals[j] = (ovals[j] + 999) / 1000;
257 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
262 if ((j % per_line) == per_line - 1) /* for formatting */
270 static unsigned int plat_val_to_idx(unsigned long val)
272 unsigned int msb, error_bits, base, offset, idx;
274 /* Find MSB starting from bit 0 */
278 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
281 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
282 * all bits of the sample as index
284 if (msb <= PLAT_BITS)
287 /* Compute the number of error bits to discard*/
288 error_bits = msb - PLAT_BITS;
290 /* Compute the number of buckets before the group */
291 base = (error_bits + 1) << PLAT_BITS;
294 * Discard the error bits and apply the mask to find the
295 * index for the buckets in the group
297 offset = (PLAT_VAL - 1) & (val >> error_bits);
299 /* Make sure the index does not exceed (array size - 1) */
300 idx = (base + offset) < (PLAT_NR - 1) ?
301 (base + offset) : (PLAT_NR - 1);
306 static void add_stat(struct submitter *s, int clock_index, int nr)
308 #ifdef ARCH_HAVE_CPU_CLOCK
309 unsigned long cycles;
312 if (!s->finish && clock_index) {
313 cycles = get_cpu_clock();
314 cycles -= s->clock_batch[clock_index];
315 pidx = plat_val_to_idx(cycles);
321 static int io_uring_register_buffers(struct submitter *s)
326 return syscall(__NR_io_uring_register, s->ring_fd,
327 IORING_REGISTER_BUFFERS, s->iovecs, depth);
330 static int io_uring_register_files(struct submitter *s)
337 s->fds = calloc(s->nr_files, sizeof(__s32));
338 for (i = 0; i < s->nr_files; i++) {
339 s->fds[i] = s->files[i].real_fd;
340 s->files[i].fixed_fd = i;
343 return syscall(__NR_io_uring_register, s->ring_fd,
344 IORING_REGISTER_FILES, s->fds, s->nr_files);
347 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
349 return syscall(__NR_io_uring_setup, entries, p);
352 static void io_uring_probe(int fd)
354 struct io_uring_probe *p;
357 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
361 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
362 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
366 if (IORING_OP_READ > p->ops_len)
369 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
375 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
376 unsigned int min_complete, unsigned int flags)
378 return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete,
382 #ifndef CONFIG_HAVE_GETTID
383 static int gettid(void)
385 return syscall(__NR_gettid);
389 static unsigned file_depth(struct submitter *s)
391 return (depth + s->nr_files - 1) / s->nr_files;
394 static void init_io(struct submitter *s, unsigned index)
396 struct io_uring_sqe *sqe = &s->sqes[index];
397 unsigned long offset;
402 sqe->opcode = IORING_OP_NOP;
406 if (s->nr_files == 1) {
409 f = &s->files[s->cur_file];
410 if (f->pending_ios >= file_depth(s)) {
412 if (s->cur_file == s->nr_files)
414 f = &s->files[s->cur_file];
420 offset = (r % (f->max_blocks - 1)) * bs;
422 if (register_files) {
423 sqe->flags = IOSQE_FIXED_FILE;
424 sqe->fd = f->fixed_fd;
427 sqe->fd = f->real_fd;
430 sqe->opcode = IORING_OP_READ_FIXED;
431 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
433 sqe->buf_index = index;
434 } else if (!vectored) {
435 sqe->opcode = IORING_OP_READ;
436 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
440 sqe->opcode = IORING_OP_READV;
441 sqe->addr = (unsigned long) &s->iovecs[index];
447 sqe->user_data = (unsigned long) f->fileno;
448 if (stats && stats_running)
449 sqe->user_data |= ((unsigned long)s->clock_index << 32);
452 static int prep_more_ios_uring(struct submitter *s, int max_ios)
454 struct io_sq_ring *ring = &s->sq_ring;
455 unsigned index, tail, next_tail, prepped = 0;
457 next_tail = tail = *ring->tail;
460 if (next_tail == atomic_load_acquire(ring->head))
463 index = tail & sq_ring_mask;
465 ring->array[index] = index;
468 } while (prepped < max_ios);
471 atomic_store_release(ring->tail, tail);
475 static int get_file_size(struct file *f)
479 if (fstat(f->real_fd, &st) < 0)
481 if (S_ISBLK(st.st_mode)) {
482 unsigned long long bytes;
484 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
487 f->max_blocks = bytes / bs;
489 } else if (S_ISREG(st.st_mode)) {
490 f->max_blocks = st.st_size / bs;
497 static int reap_events_uring(struct submitter *s)
499 struct io_cq_ring *ring = &s->cq_ring;
500 struct io_uring_cqe *cqe;
501 unsigned head, reaped = 0;
502 int last_idx = -1, stat_nr = 0;
509 if (head == atomic_load_acquire(ring->tail))
511 cqe = &ring->cqes[head & cq_ring_mask];
513 int fileno = cqe->user_data & 0xffffffff;
515 f = &s->files[fileno];
517 if (cqe->res != bs) {
518 printf("io: unexpected ret=%d\n", cqe->res);
519 if (polled && cqe->res == -EOPNOTSUPP)
520 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
525 int clock_index = cqe->user_data >> 32;
527 if (last_idx != clock_index) {
528 if (last_idx != -1) {
529 add_stat(s, last_idx, stat_nr);
532 last_idx = clock_index;
533 } else if (clock_index)
541 add_stat(s, last_idx, stat_nr);
544 s->inflight -= reaped;
545 atomic_store_release(ring->head, head);
550 static int submitter_init(struct submitter *s)
555 printf("submitter=%d\n", s->tid);
557 srand48(pthread_self());
559 for (i = 0; i < MAX_FDS; i++)
560 s->files[i].fileno = i;
563 nr_batch = roundup_pow2(depth / batch_submit);
564 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
567 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
569 s->clock_batch = NULL;
578 static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
580 unsigned long offset, data;
586 while (index < max_ios) {
587 struct iocb *iocb = &iocbs[index];
589 if (s->nr_files == 1) {
592 f = &s->files[s->cur_file];
593 if (f->pending_ios >= file_depth(s)) {
595 if (s->cur_file == s->nr_files)
597 f = &s->files[s->cur_file];
603 offset = (r % (f->max_blocks - 1)) * bs;
604 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
605 s->iovecs[index].iov_len, offset);
608 if (stats && stats_running)
609 data |= ((unsigned long) s->clock_index << 32);
610 iocb->data = (void *) (uintptr_t) data;
616 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
618 int last_idx = -1, stat_nr = 0;
622 unsigned long data = (uintptr_t) events[reaped].data;
623 struct file *f = &s->files[data & 0xffffffff];
626 if (events[reaped].res != bs) {
627 printf("io: unexpected ret=%ld\n", events[reaped].res);
631 int clock_index = data >> 32;
633 if (last_idx != clock_index) {
634 if (last_idx != -1) {
635 add_stat(s, last_idx, stat_nr);
638 last_idx = clock_index;
639 } else if (clock_index)
647 add_stat(s, last_idx, stat_nr);
649 s->inflight -= reaped;
654 static void *submitter_aio_fn(void *data)
656 struct submitter *s = data;
657 int i, ret, prepped, nr_batch;
658 struct iocb **iocbsptr;
660 struct io_event *events;
662 nr_batch = submitter_init(s);
664 iocbsptr = calloc(depth, sizeof(struct iocb *));
665 iocbs = calloc(depth, sizeof(struct iocb));
666 events = calloc(depth, sizeof(struct io_event));
668 for (i = 0; i < depth; i++)
669 iocbsptr[i] = &iocbs[i];
673 int to_wait, to_submit, to_prep;
675 if (!prepped && s->inflight < depth) {
676 to_prep = min(depth - s->inflight, batch_submit);
677 prepped = prep_more_ios_aio(s, to_prep, iocbs);
678 #ifdef ARCH_HAVE_CPU_CLOCK
679 if (prepped && stats) {
680 s->clock_batch[s->clock_index] = get_cpu_clock();
681 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
685 s->inflight += prepped;
688 if (to_submit && (s->inflight + to_submit <= depth))
691 to_wait = min(s->inflight + to_submit, batch_complete);
693 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
698 } else if (ret != to_submit) {
699 printf("submitted %d, wanted %d\n", ret, to_submit);
709 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
711 perror("io_getevents");
713 } else if (r != to_wait) {
714 printf("r=%d, wait=%d\n", r, to_wait);
717 r = reap_events_aio(s, events, r);
722 } while (!s->finish);
732 static void *submitter_uring_fn(void *data)
734 struct submitter *s = data;
735 struct io_sq_ring *ring = &s->sq_ring;
736 int ret, prepped, nr_batch;
738 nr_batch = submitter_init(s);
742 int to_wait, to_submit, this_reap, to_prep;
743 unsigned ring_flags = 0;
745 if (!prepped && s->inflight < depth) {
746 to_prep = min(depth - s->inflight, batch_submit);
747 prepped = prep_more_ios_uring(s, to_prep);
748 #ifdef ARCH_HAVE_CPU_CLOCK
749 if (prepped && stats) {
750 s->clock_batch[s->clock_index] = get_cpu_clock();
751 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
755 s->inflight += prepped;
759 if (to_submit && (s->inflight + to_submit <= depth))
762 to_wait = min(s->inflight + to_submit, batch_complete);
765 * Only need to call io_uring_enter if we're not using SQ thread
766 * poll, or if IORING_SQ_NEED_WAKEUP is set.
769 ring_flags = atomic_load_acquire(ring->flags);
770 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
774 flags = IORING_ENTER_GETEVENTS;
775 if (ring_flags & IORING_SQ_NEED_WAKEUP)
776 flags |= IORING_ENTER_SQ_WAKEUP;
777 ret = io_uring_enter(s, to_submit, to_wait, flags);
780 /* for SQPOLL, we submitted it all effectively */
785 * For non SQ thread poll, we already got the events we needed
786 * through the io_uring_enter() above. For SQ thread poll, we
787 * need to loop here until we find enough events.
793 r = reap_events_uring(s);
799 } while (sq_thread_poll && this_reap < to_wait);
800 s->reaps += this_reap;
808 } else if (ret < to_submit) {
809 int diff = to_submit - ret;
818 } else if (ret < 0) {
819 if (errno == EAGAIN) {
827 printf("io_submit: %s\n", strerror(errno));
830 } while (!s->finish);
836 static struct submitter *get_submitter(int offset)
842 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
846 static void sig_int(int sig)
850 printf("Exiting on signal %d\n", sig);
851 for (j = 0; j < nthreads; j++) {
852 struct submitter *s = get_submitter(j);
858 static void arm_sig_int(void)
860 struct sigaction act;
862 memset(&act, 0, sizeof(act));
863 act.sa_handler = sig_int;
864 act.sa_flags = SA_RESTART;
865 sigaction(SIGINT, &act, NULL);
867 /* Windows uses SIGBREAK as a quit signal from other applications */
869 sigaction(SIGBREAK, &act, NULL);
873 static int setup_aio(struct submitter *s)
877 fprintf(stderr, "aio does not support polled IO\n");
880 if (sq_thread_poll) {
881 fprintf(stderr, "aio does not support SQPOLL IO\n");
885 fprintf(stderr, "aio does not support polled IO\n");
888 if (fixedbufs || register_files) {
889 fprintf(stderr, "aio does not support registered files or buffers\n");
890 fixedbufs = register_files = 0;
893 return io_queue_init(depth, &s->aio_ctx);
895 fprintf(stderr, "Legacy AIO not available on this system/build\n");
901 static int setup_ring(struct submitter *s)
903 struct io_sq_ring *sring = &s->sq_ring;
904 struct io_cq_ring *cring = &s->cq_ring;
905 struct io_uring_params p;
909 memset(&p, 0, sizeof(p));
911 if (polled && !do_nop)
912 p.flags |= IORING_SETUP_IOPOLL;
913 if (sq_thread_poll) {
914 p.flags |= IORING_SETUP_SQPOLL;
915 if (sq_thread_cpu != -1) {
916 p.flags |= IORING_SETUP_SQ_AFF;
917 p.sq_thread_cpu = sq_thread_cpu;
921 fd = io_uring_setup(depth, &p);
923 perror("io_uring_setup");
933 rlim.rlim_cur = RLIM_INFINITY;
934 rlim.rlim_max = RLIM_INFINITY;
935 /* ignore potential error, not needed on newer kernels */
936 setrlimit(RLIMIT_MEMLOCK, &rlim);
938 ret = io_uring_register_buffers(s);
940 perror("io_uring_register_buffers");
945 if (register_files) {
946 ret = io_uring_register_files(s);
948 perror("io_uring_register_files");
953 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
954 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
956 printf("sq_ring ptr = 0x%p\n", ptr);
957 sring->head = ptr + p.sq_off.head;
958 sring->tail = ptr + p.sq_off.tail;
959 sring->ring_mask = ptr + p.sq_off.ring_mask;
960 sring->ring_entries = ptr + p.sq_off.ring_entries;
961 sring->flags = ptr + p.sq_off.flags;
962 sring->array = ptr + p.sq_off.array;
963 sq_ring_mask = *sring->ring_mask;
965 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
966 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
968 printf("sqes ptr = 0x%p\n", s->sqes);
970 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
971 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
973 printf("cq_ring ptr = 0x%p\n", ptr);
974 cring->head = ptr + p.cq_off.head;
975 cring->tail = ptr + p.cq_off.tail;
976 cring->ring_mask = ptr + p.cq_off.ring_mask;
977 cring->ring_entries = ptr + p.cq_off.ring_entries;
978 cring->cqes = ptr + p.cq_off.cqes;
979 cq_ring_mask = *cring->ring_mask;
983 static void file_depths(char *buf)
991 for (j = 0; j < nthreads; j++) {
992 struct submitter *s = get_submitter(j);
994 for (i = 0; i < s->nr_files; i++) {
995 struct file *f = &s->files[i];
998 p += sprintf(p, " %d", f->pending_ios);
1000 p += sprintf(p, "%d", f->pending_ios);
1006 static void usage(char *argv, int status)
1008 printf("%s [options] -- [filenames]\n"
1009 " -d <int> : IO Depth, default %d\n"
1010 " -s <int> : Batch submit, default %d\n"
1011 " -c <int> : Batch complete, default %d\n"
1012 " -b <int> : Block size, default %d\n"
1013 " -p <bool> : Polled IO, default %d\n"
1014 " -B <bool> : Fixed buffers, default %d\n"
1015 " -F <bool> : Register files, default %d\n"
1016 " -n <int> : Number of threads, default %d\n"
1017 " -O <bool> : Use O_DIRECT, default %d\n"
1018 " -N <bool> : Perform just no-op requests, default %d\n"
1019 " -t <bool> : Track IO latencies, default %d\n"
1020 " -T <int> : TSC rate in HZ\n"
1021 " -a <bool> : Use legacy aio, default %d\n",
1022 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1023 fixedbufs, register_files, nthreads, !buffered, do_nop, stats, aio);
1027 static void read_tsc_rate(void)
1035 fd = open(TSC_RATE_FILE, O_RDONLY);
1039 ret = read(fd, buffer, sizeof(buffer));
1045 tsc_rate = strtoul(buffer, NULL, 10);
1046 printf("Using TSC rate %luHz\n", tsc_rate);
1050 static void write_tsc_rate(void)
1056 if (!stat(TSC_RATE_FILE, &sb))
1059 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1063 memset(buffer, 0, sizeof(buffer));
1064 sprintf(buffer, "%lu", tsc_rate);
1065 ret = write(fd, buffer, strlen(buffer));
1071 int main(int argc, char *argv[])
1073 struct submitter *s;
1074 unsigned long done, calls, reap;
1075 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1080 if (!do_nop && argc < 2)
1083 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:h?")) != -1) {
1086 aio = !!atoi(optarg);
1089 depth = atoi(optarg);
1092 batch_submit = atoi(optarg);
1097 batch_complete = atoi(optarg);
1098 if (!batch_complete)
1105 polled = !!atoi(optarg);
1108 fixedbufs = !!atoi(optarg);
1111 register_files = !!atoi(optarg);
1114 nthreads = atoi(optarg);
1116 printf("Threads must be non-zero\n");
1121 do_nop = !!atoi(optarg);
1124 buffered = !atoi(optarg);
1127 #ifndef ARCH_HAVE_CPU_CLOCK
1128 fprintf(stderr, "Stats not supported on this CPU\n");
1131 stats = !!atoi(optarg);
1134 #ifndef ARCH_HAVE_CPU_CLOCK
1135 fprintf(stderr, "Stats not supported on this CPU\n");
1138 tsc_rate = strtoul(optarg, NULL, 10);
1152 if (batch_complete > depth)
1153 batch_complete = depth;
1154 if (batch_submit > depth)
1155 batch_submit = depth;
1157 submitter = calloc(nthreads, sizeof(*submitter) +
1158 depth * sizeof(struct iovec));
1159 for (j = 0; j < nthreads; j++) {
1160 s = get_submitter(j);
1162 s->done = s->calls = s->reaps = 0;
1165 flags = O_RDONLY | O_NOATIME;
1174 printf("No files specified\n");
1177 threads_per_f = nthreads / nfiles;
1178 /* make sure each thread gets assigned files */
1179 if (threads_per_f == 0) {
1182 threads_rem = nthreads - threads_per_f * nfiles;
1185 while (!do_nop && i < argc) {
1188 memset(&f, 0, sizeof(f));
1190 fd = open(argv[i], flags);
1196 if (get_file_size(&f)) {
1197 printf("failed getting size of device/file\n");
1200 if (f.max_blocks <= 1) {
1201 printf("Zero file/device size?\n");
1206 limit = threads_per_f;
1207 limit += threads_rem > 0 ? 1 : 0;
1208 for (k = 0; k < limit; k++) {
1209 s = get_submitter((j + k) % nthreads);
1211 if (s->nr_files == MAX_FDS) {
1212 printf("Max number of files (%d) reached\n", MAX_FDS);
1216 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1218 printf("Added file %s (submitter %d)\n", argv[i], s->index);
1228 for (j = 0; j < nthreads; j++) {
1229 s = get_submitter(j);
1230 for (i = 0; i < depth; i++) {
1233 if (posix_memalign(&buf, bs, bs)) {
1234 printf("failed alloc\n");
1237 s->iovecs[i].iov_base = buf;
1238 s->iovecs[i].iov_len = bs;
1242 for (j = 0; j < nthreads; j++) {
1243 s = get_submitter(j);
1246 err = setup_ring(s);
1250 printf("ring setup failed: %s, %d\n", strerror(errno), err);
1254 s = get_submitter(0);
1255 printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, register_files, buffered, depth);
1257 printf("Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1258 #ifdef CONFIG_LIBAIO
1260 printf("Engine=aio, ctx=%p\n", &s->aio_ctx);
1263 for (j = 0; j < nthreads; j++) {
1264 s = get_submitter(j);
1266 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1267 #ifdef CONFIG_LIBAIO
1269 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1273 fdepths = malloc(8 * s->nr_files * nthreads);
1274 reap = calls = done = 0;
1276 unsigned long this_done = 0;
1277 unsigned long this_reap = 0;
1278 unsigned long this_call = 0;
1279 unsigned long rpc = 0, ipc = 0;
1280 unsigned long iops, bw;
1284 /* don't print partial run, if interrupted by signal */
1288 /* one second in to the run, enable stats */
1292 for (j = 0; j < nthreads; j++) {
1293 this_done += s->done;
1294 this_call += s->calls;
1295 this_reap += s->reaps;
1297 if (this_call - calls) {
1298 rpc = (this_done - done) / (this_call - calls);
1299 ipc = (this_reap - reap) / (this_call - calls);
1302 file_depths(fdepths);
1303 iops = this_done - done;
1305 bw = iops * (bs / 1048576);
1307 bw = iops / (1048576 / bs);
1308 printf("IOPS=%lu, ", iops);
1310 printf("BW=%luMiB/s, ", bw);
1311 printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths);
1317 for (j = 0; j < nthreads; j++) {
1318 s = get_submitter(j);
1319 pthread_join(s->thread, &ret);
1325 printf("%d: Latency percentiles:\n", s->tid);
1326 for (i = 0, nr = 0; i < PLAT_NR; i++)
1328 show_clat_percentiles(s->plat, nr, 4);
1329 free(s->clock_batch);