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 "../lib/rand.h"
32 #include "../minmax.h"
33 #include "../os/linux/io_uring.h"
39 unsigned *ring_entries;
48 unsigned *ring_entries;
49 struct io_uring_cqe *cqes;
53 #define BATCH_SUBMIT 32
54 #define BATCH_COMPLETE 32
59 static unsigned sq_ring_mask, cq_ring_mask;
62 unsigned long max_blocks;
63 unsigned long max_size;
64 unsigned long cur_off;
72 #define PLAT_VAL (1 << PLAT_BITS)
73 #define PLAT_GROUP_NR 29
74 #define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
80 struct io_sq_ring sq_ring;
81 struct io_uring_sqe *sqes;
82 struct io_cq_ring cq_ring;
92 struct taus258_state rand_state;
94 unsigned long *clock_batch;
102 struct file files[MAX_FDS];
105 struct iovec iovecs[];
108 static struct submitter *submitter;
109 static volatile int finish;
110 static int stats_running;
111 static unsigned long max_iops;
113 static int depth = DEPTH;
114 static int batch_submit = BATCH_SUBMIT;
115 static int batch_complete = BATCH_COMPLETE;
117 static int polled = 1; /* use IO polling */
118 static int fixedbufs = 1; /* use fixed user buffers */
119 static int dma_map; /* pre-map DMA buffers */
120 static int register_files = 1; /* use fixed files */
121 static int buffered = 0; /* use buffered IO, not O_DIRECT */
122 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
123 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
124 static int do_nop = 0; /* no-op SQ ring commands */
125 static int nthreads = 1;
126 static int stats = 0; /* generate IO stats */
127 static int aio = 0; /* use libaio */
128 static int runtime = 0; /* runtime */
129 static int random_io = 1; /* random or sequential IO */
131 static unsigned long tsc_rate;
133 #define TSC_RATE_FILE "tsc-rate"
135 static int vectored = 1;
137 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
138 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
139 static int plist_len = 17;
141 #ifndef IORING_REGISTER_MAP_BUFFERS
142 #define IORING_REGISTER_MAP_BUFFERS 20
143 struct io_uring_map_buffers {
152 static unsigned long cycles_to_nsec(unsigned long cycles)
159 val = cycles * 1000000000ULL;
160 return val / tsc_rate;
163 static unsigned long plat_idx_to_val(unsigned int idx)
165 unsigned int error_bits;
166 unsigned long k, base;
168 assert(idx < PLAT_NR);
170 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
171 * all bits of the sample as index */
172 if (idx < (PLAT_VAL << 1))
173 return cycles_to_nsec(idx);
175 /* Find the group and compute the minimum value of that group */
176 error_bits = (idx >> PLAT_BITS) - 1;
177 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
179 /* Find its bucket number of the group */
182 /* Return the mean of the range of the bucket */
183 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
186 unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
187 unsigned long **output,
188 unsigned long *maxv, unsigned long *minv)
190 unsigned long sum = 0;
191 unsigned int len = plist_len, i, j = 0;
192 unsigned long *ovals = NULL;
198 ovals = malloc(len * sizeof(*ovals));
203 * Calculate bucket values, note down max and min values
206 for (i = 0; i < PLAT_NR && !is_last; i++) {
208 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
209 assert(plist[j] <= 100.0);
211 ovals[j] = plat_idx_to_val(i);
212 if (ovals[j] < *minv)
214 if (ovals[j] > *maxv)
217 is_last = (j == len - 1) != 0;
226 fprintf(stderr, "error calculating latency percentiles\n");
232 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
233 unsigned int precision)
235 unsigned int divisor, len, i, j = 0;
236 unsigned long minv, maxv;
237 unsigned long *ovals;
238 int per_line, scale_down, time_width;
242 len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
249 printf(" percentiles (tsc ticks):\n |");
250 } else if (minv > 2000 && maxv > 99999) {
253 printf(" percentiles (usec):\n |");
257 printf(" percentiles (nsec):\n |");
260 time_width = max(5, (int) (log10(maxv / divisor) + 1));
261 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
262 precision, time_width);
263 /* fmt will be something like " %5.2fth=[%4llu]%c" */
264 per_line = (80 - 7) / (precision + 10 + time_width);
266 for (j = 0; j < len; j++) {
268 if (j != 0 && (j % per_line) == 0)
271 /* end of the list */
272 is_last = (j == len - 1) != 0;
274 for (i = 0; i < scale_down; i++)
275 ovals[j] = (ovals[j] + 999) / 1000;
277 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
282 if ((j % per_line) == per_line - 1) /* for formatting */
290 static unsigned int plat_val_to_idx(unsigned long val)
292 unsigned int msb, error_bits, base, offset, idx;
294 /* Find MSB starting from bit 0 */
298 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
301 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
302 * all bits of the sample as index
304 if (msb <= PLAT_BITS)
307 /* Compute the number of error bits to discard*/
308 error_bits = msb - PLAT_BITS;
310 /* Compute the number of buckets before the group */
311 base = (error_bits + 1) << PLAT_BITS;
314 * Discard the error bits and apply the mask to find the
315 * index for the buckets in the group
317 offset = (PLAT_VAL - 1) & (val >> error_bits);
319 /* Make sure the index does not exceed (array size - 1) */
320 idx = (base + offset) < (PLAT_NR - 1) ?
321 (base + offset) : (PLAT_NR - 1);
326 static void add_stat(struct submitter *s, int clock_index, int nr)
328 #ifdef ARCH_HAVE_CPU_CLOCK
329 unsigned long cycles;
332 if (!s->finish && clock_index) {
333 cycles = get_cpu_clock();
334 cycles -= s->clock_batch[clock_index];
335 pidx = plat_val_to_idx(cycles);
341 static int io_uring_map_buffers(struct submitter *s)
343 struct io_uring_map_buffers map = {
344 .fd = s->files[0].real_fd,
350 if (s->nr_files > 1) {
351 fprintf(stderr, "Can't map buffers with multiple files\n");
355 return syscall(__NR_io_uring_register, s->ring_fd,
356 IORING_REGISTER_MAP_BUFFERS, &map, 1);
359 static int io_uring_register_buffers(struct submitter *s)
364 return syscall(__NR_io_uring_register, s->ring_fd,
365 IORING_REGISTER_BUFFERS, s->iovecs, depth);
368 static int io_uring_register_files(struct submitter *s)
375 s->fds = calloc(s->nr_files, sizeof(__s32));
376 for (i = 0; i < s->nr_files; i++) {
377 s->fds[i] = s->files[i].real_fd;
378 s->files[i].fixed_fd = i;
381 return syscall(__NR_io_uring_register, s->ring_fd,
382 IORING_REGISTER_FILES, s->fds, s->nr_files);
385 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
388 * Clamp CQ ring size at our SQ ring size, we don't need more entries
391 p->flags |= IORING_SETUP_CQSIZE;
392 p->cq_entries = entries;
394 return syscall(__NR_io_uring_setup, entries, p);
397 static void io_uring_probe(int fd)
399 struct io_uring_probe *p;
402 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
406 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
407 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
411 if (IORING_OP_READ > p->ops_len)
414 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
420 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
421 unsigned int min_complete, unsigned int flags)
423 return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete,
427 #ifndef CONFIG_HAVE_GETTID
428 static int gettid(void)
430 return syscall(__NR_gettid);
434 static unsigned file_depth(struct submitter *s)
436 return (depth + s->nr_files - 1) / s->nr_files;
439 static void init_io(struct submitter *s, unsigned index)
441 struct io_uring_sqe *sqe = &s->sqes[index];
442 unsigned long offset;
447 sqe->opcode = IORING_OP_NOP;
451 if (s->nr_files == 1) {
454 f = &s->files[s->cur_file];
455 if (f->pending_ios >= file_depth(s)) {
457 if (s->cur_file == s->nr_files)
459 f = &s->files[s->cur_file];
465 r = __rand64(&s->rand_state);
466 offset = (r % (f->max_blocks - 1)) * bs;
470 if (f->cur_off + bs > f->max_size)
474 if (register_files) {
475 sqe->flags = IOSQE_FIXED_FILE;
476 sqe->fd = f->fixed_fd;
479 sqe->fd = f->real_fd;
482 sqe->opcode = IORING_OP_READ_FIXED;
483 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
485 sqe->buf_index = index;
486 } else if (!vectored) {
487 sqe->opcode = IORING_OP_READ;
488 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
492 sqe->opcode = IORING_OP_READV;
493 sqe->addr = (unsigned long) &s->iovecs[index];
499 sqe->user_data = (unsigned long) f->fileno;
500 if (stats && stats_running)
501 sqe->user_data |= ((uint64_t)s->clock_index << 32);
504 static int prep_more_ios_uring(struct submitter *s, int max_ios)
506 struct io_sq_ring *ring = &s->sq_ring;
507 unsigned index, tail, next_tail, prepped = 0;
509 next_tail = tail = *ring->tail;
512 if (next_tail == atomic_load_acquire(ring->head))
515 index = tail & sq_ring_mask;
517 ring->array[index] = index;
520 } while (prepped < max_ios);
523 atomic_store_release(ring->tail, tail);
527 static int get_file_size(struct file *f)
531 if (fstat(f->real_fd, &st) < 0)
533 if (S_ISBLK(st.st_mode)) {
534 unsigned long long bytes;
536 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
539 f->max_blocks = bytes / bs;
542 } else if (S_ISREG(st.st_mode)) {
543 f->max_blocks = st.st_size / bs;
544 f->max_size = st.st_size;
551 static int reap_events_uring(struct submitter *s)
553 struct io_cq_ring *ring = &s->cq_ring;
554 struct io_uring_cqe *cqe;
555 unsigned head, reaped = 0;
556 int last_idx = -1, stat_nr = 0;
563 if (head == atomic_load_acquire(ring->tail))
565 cqe = &ring->cqes[head & cq_ring_mask];
567 int fileno = cqe->user_data & 0xffffffff;
569 f = &s->files[fileno];
571 if (cqe->res != bs) {
572 printf("io: unexpected ret=%d\n", cqe->res);
573 if (polled && cqe->res == -EOPNOTSUPP)
574 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
579 int clock_index = cqe->user_data >> 32;
581 if (last_idx != clock_index) {
582 if (last_idx != -1) {
583 add_stat(s, last_idx, stat_nr);
586 last_idx = clock_index;
595 add_stat(s, last_idx, stat_nr);
598 s->inflight -= reaped;
599 atomic_store_release(ring->head, head);
604 static int submitter_init(struct submitter *s)
609 printf("submitter=%d, tid=%d\n", s->index, s->tid);
611 __init_rand64(&s->rand_state, pthread_self());
612 srand48(pthread_self());
614 for (i = 0; i < MAX_FDS; i++)
615 s->files[i].fileno = i;
618 nr_batch = roundup_pow2(depth / batch_submit);
621 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
624 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
626 s->clock_batch = NULL;
635 static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
644 while (index < max_ios) {
645 struct iocb *iocb = &iocbs[index];
647 if (s->nr_files == 1) {
650 f = &s->files[s->cur_file];
651 if (f->pending_ios >= file_depth(s)) {
653 if (s->cur_file == s->nr_files)
655 f = &s->files[s->cur_file];
661 offset = (r % (f->max_blocks - 1)) * bs;
662 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
663 s->iovecs[index].iov_len, offset);
666 if (stats && stats_running)
667 data |= (((uint64_t) s->clock_index) << 32);
668 iocb->data = (void *) (uintptr_t) data;
674 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
676 int last_idx = -1, stat_nr = 0;
680 uint64_t data = (uintptr_t) events[reaped].data;
681 struct file *f = &s->files[data & 0xffffffff];
684 if (events[reaped].res != bs) {
685 printf("io: unexpected ret=%ld\n", events[reaped].res);
689 int clock_index = data >> 32;
691 if (last_idx != clock_index) {
692 if (last_idx != -1) {
693 add_stat(s, last_idx, stat_nr);
696 last_idx = clock_index;
705 add_stat(s, last_idx, stat_nr);
707 s->inflight -= reaped;
712 static void *submitter_aio_fn(void *data)
714 struct submitter *s = data;
715 int i, ret, prepped, nr_batch;
716 struct iocb **iocbsptr;
718 struct io_event *events;
720 nr_batch = submitter_init(s);
722 iocbsptr = calloc(depth, sizeof(struct iocb *));
723 iocbs = calloc(depth, sizeof(struct iocb));
724 events = calloc(depth, sizeof(struct io_event));
726 for (i = 0; i < depth; i++)
727 iocbsptr[i] = &iocbs[i];
731 int to_wait, to_submit, to_prep;
733 if (!prepped && s->inflight < depth) {
734 to_prep = min(depth - s->inflight, batch_submit);
735 prepped = prep_more_ios_aio(s, to_prep, iocbs);
736 #ifdef ARCH_HAVE_CPU_CLOCK
737 if (prepped && stats) {
738 s->clock_batch[s->clock_index] = get_cpu_clock();
739 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
743 s->inflight += prepped;
746 if (to_submit && (s->inflight + to_submit <= depth))
749 to_wait = min(s->inflight + to_submit, batch_complete);
751 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
756 } else if (ret != to_submit) {
757 printf("submitted %d, wanted %d\n", ret, to_submit);
766 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
768 perror("io_getevents");
770 } else if (r != to_wait) {
771 printf("r=%d, wait=%d\n", r, to_wait);
774 r = reap_events_aio(s, events, r);
778 } while (!s->finish);
788 static void *submitter_uring_fn(void *data)
790 struct submitter *s = data;
791 struct io_sq_ring *ring = &s->sq_ring;
792 int ret, prepped, nr_batch;
794 nr_batch = submitter_init(s);
798 int to_wait, to_submit, this_reap, to_prep;
799 unsigned ring_flags = 0;
801 if (!prepped && s->inflight < depth) {
802 to_prep = min(depth - s->inflight, batch_submit);
803 prepped = prep_more_ios_uring(s, to_prep);
804 #ifdef ARCH_HAVE_CPU_CLOCK
805 if (prepped && stats) {
806 s->clock_batch[s->clock_index] = get_cpu_clock();
807 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
811 s->inflight += prepped;
815 if (to_submit && (s->inflight + to_submit <= depth))
818 to_wait = min(s->inflight + to_submit, batch_complete);
821 * Only need to call io_uring_enter if we're not using SQ thread
822 * poll, or if IORING_SQ_NEED_WAKEUP is set.
825 ring_flags = atomic_load_acquire(ring->flags);
826 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
830 flags = IORING_ENTER_GETEVENTS;
831 if (ring_flags & IORING_SQ_NEED_WAKEUP)
832 flags |= IORING_ENTER_SQ_WAKEUP;
833 ret = io_uring_enter(s, to_submit, to_wait, flags);
836 /* for SQPOLL, we submitted it all effectively */
841 * For non SQ thread poll, we already got the events we needed
842 * through the io_uring_enter() above. For SQ thread poll, we
843 * need to loop here until we find enough events.
849 r = reap_events_uring(s);
855 } while (sq_thread_poll && this_reap < to_wait);
856 s->reaps += this_reap;
864 } else if (ret < to_submit) {
865 int diff = to_submit - ret;
874 } else if (ret < 0) {
875 if (errno == EAGAIN) {
883 printf("io_submit: %s\n", strerror(errno));
886 } while (!s->finish);
892 static struct submitter *get_submitter(int offset)
898 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
902 static void do_finish(const char *reason)
905 printf("Exiting on %s\n", reason);
906 for (j = 0; j < nthreads; j++) {
907 struct submitter *s = get_submitter(j);
910 if (max_iops > 100000)
911 printf("Maximum IOPS=%luK\n", max_iops / 1000);
913 printf("Maximum IOPS=%lu\n", max_iops);
917 static void sig_int(int sig)
922 static void arm_sig_int(void)
924 struct sigaction act;
926 memset(&act, 0, sizeof(act));
927 act.sa_handler = sig_int;
928 act.sa_flags = SA_RESTART;
929 sigaction(SIGINT, &act, NULL);
931 /* Windows uses SIGBREAK as a quit signal from other applications */
933 sigaction(SIGBREAK, &act, NULL);
937 static int setup_aio(struct submitter *s)
941 fprintf(stderr, "aio does not support polled IO\n");
944 if (sq_thread_poll) {
945 fprintf(stderr, "aio does not support SQPOLL IO\n");
949 fprintf(stderr, "aio does not support polled IO\n");
952 if (fixedbufs || register_files) {
953 fprintf(stderr, "aio does not support registered files or buffers\n");
954 fixedbufs = register_files = 0;
957 return io_queue_init(depth, &s->aio_ctx);
959 fprintf(stderr, "Legacy AIO not available on this system/build\n");
965 static int setup_ring(struct submitter *s)
967 struct io_sq_ring *sring = &s->sq_ring;
968 struct io_cq_ring *cring = &s->cq_ring;
969 struct io_uring_params p;
973 memset(&p, 0, sizeof(p));
975 if (polled && !do_nop)
976 p.flags |= IORING_SETUP_IOPOLL;
977 if (sq_thread_poll) {
978 p.flags |= IORING_SETUP_SQPOLL;
979 if (sq_thread_cpu != -1) {
980 p.flags |= IORING_SETUP_SQ_AFF;
981 p.sq_thread_cpu = sq_thread_cpu;
985 fd = io_uring_setup(depth, &p);
987 perror("io_uring_setup");
997 rlim.rlim_cur = RLIM_INFINITY;
998 rlim.rlim_max = RLIM_INFINITY;
999 /* ignore potential error, not needed on newer kernels */
1000 setrlimit(RLIMIT_MEMLOCK, &rlim);
1002 ret = io_uring_register_buffers(s);
1004 perror("io_uring_register_buffers");
1009 ret = io_uring_map_buffers(s);
1011 perror("io_uring_map_buffers");
1017 if (register_files) {
1018 ret = io_uring_register_files(s);
1020 perror("io_uring_register_files");
1025 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
1026 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1027 IORING_OFF_SQ_RING);
1028 sring->head = ptr + p.sq_off.head;
1029 sring->tail = ptr + p.sq_off.tail;
1030 sring->ring_mask = ptr + p.sq_off.ring_mask;
1031 sring->ring_entries = ptr + p.sq_off.ring_entries;
1032 sring->flags = ptr + p.sq_off.flags;
1033 sring->array = ptr + p.sq_off.array;
1034 sq_ring_mask = *sring->ring_mask;
1036 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
1037 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1040 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
1041 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1042 IORING_OFF_CQ_RING);
1043 cring->head = ptr + p.cq_off.head;
1044 cring->tail = ptr + p.cq_off.tail;
1045 cring->ring_mask = ptr + p.cq_off.ring_mask;
1046 cring->ring_entries = ptr + p.cq_off.ring_entries;
1047 cring->cqes = ptr + p.cq_off.cqes;
1048 cq_ring_mask = *cring->ring_mask;
1052 static void file_depths(char *buf)
1060 for (j = 0; j < nthreads; j++) {
1061 struct submitter *s = get_submitter(j);
1063 for (i = 0; i < s->nr_files; i++) {
1064 struct file *f = &s->files[i];
1067 p += sprintf(p, " %d", f->pending_ios);
1069 p += sprintf(p, "%d", f->pending_ios);
1075 static void usage(char *argv, int status)
1077 char runtime_str[16];
1078 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
1079 printf("%s [options] -- [filenames]\n"
1080 " -d <int> : IO Depth, default %d\n"
1081 " -s <int> : Batch submit, default %d\n"
1082 " -c <int> : Batch complete, default %d\n"
1083 " -b <int> : Block size, default %d\n"
1084 " -p <bool> : Polled IO, default %d\n"
1085 " -B <bool> : Fixed buffers, default %d\n"
1086 " -D <bool> : DMA map fixed buffers, default %d\n"
1087 " -F <bool> : Register files, default %d\n"
1088 " -n <int> : Number of threads, default %d\n"
1089 " -O <bool> : Use O_DIRECT, default %d\n"
1090 " -N <bool> : Perform just no-op requests, default %d\n"
1091 " -t <bool> : Track IO latencies, default %d\n"
1092 " -T <int> : TSC rate in HZ\n"
1093 " -r <int> : Runtime in seconds, default %s\n"
1094 " -R <bool> : Use random IO, default %d\n"
1095 " -a <bool> : Use legacy aio, default %d\n",
1096 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1097 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
1098 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio);
1102 static void read_tsc_rate(void)
1110 fd = open(TSC_RATE_FILE, O_RDONLY);
1114 ret = read(fd, buffer, sizeof(buffer));
1120 tsc_rate = strtoul(buffer, NULL, 10);
1121 printf("Using TSC rate %luHz\n", tsc_rate);
1125 static void write_tsc_rate(void)
1131 if (!stat(TSC_RATE_FILE, &sb))
1134 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1138 memset(buffer, 0, sizeof(buffer));
1139 sprintf(buffer, "%lu", tsc_rate);
1140 ret = write(fd, buffer, strlen(buffer));
1146 int main(int argc, char *argv[])
1148 struct submitter *s;
1149 unsigned long done, calls, reap;
1150 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1155 if (!do_nop && argc < 2)
1158 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:r:D:R:h?")) != -1) {
1161 aio = !!atoi(optarg);
1164 depth = atoi(optarg);
1167 batch_submit = atoi(optarg);
1172 batch_complete = atoi(optarg);
1173 if (!batch_complete)
1180 polled = !!atoi(optarg);
1183 fixedbufs = !!atoi(optarg);
1186 register_files = !!atoi(optarg);
1189 nthreads = atoi(optarg);
1191 printf("Threads must be non-zero\n");
1196 do_nop = !!atoi(optarg);
1199 buffered = !atoi(optarg);
1202 #ifndef ARCH_HAVE_CPU_CLOCK
1203 fprintf(stderr, "Stats not supported on this CPU\n");
1206 stats = !!atoi(optarg);
1209 #ifndef ARCH_HAVE_CPU_CLOCK
1210 fprintf(stderr, "Stats not supported on this CPU\n");
1213 tsc_rate = strtoul(optarg, NULL, 10);
1217 runtime = atoi(optarg);
1220 dma_map = !!atoi(optarg);
1223 random_io = !!atoi(optarg);
1236 if (batch_complete > depth)
1237 batch_complete = depth;
1238 if (batch_submit > depth)
1239 batch_submit = depth;
1240 if (!fixedbufs && dma_map)
1243 submitter = calloc(nthreads, sizeof(*submitter) +
1244 depth * sizeof(struct iovec));
1245 for (j = 0; j < nthreads; j++) {
1246 s = get_submitter(j);
1248 s->done = s->calls = s->reaps = 0;
1251 flags = O_RDONLY | O_NOATIME;
1260 printf("No files specified\n");
1263 threads_per_f = nthreads / nfiles;
1264 /* make sure each thread gets assigned files */
1265 if (threads_per_f == 0) {
1268 threads_rem = nthreads - threads_per_f * nfiles;
1271 while (!do_nop && i < argc) {
1274 memset(&f, 0, sizeof(f));
1276 fd = open(argv[i], flags);
1282 if (get_file_size(&f)) {
1283 printf("failed getting size of device/file\n");
1286 if (f.max_blocks <= 1) {
1287 printf("Zero file/device size?\n");
1292 limit = threads_per_f;
1293 limit += threads_rem > 0 ? 1 : 0;
1294 for (k = 0; k < limit; k++) {
1295 s = get_submitter((j + k) % nthreads);
1297 if (s->nr_files == MAX_FDS) {
1298 printf("Max number of files (%d) reached\n", MAX_FDS);
1302 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1304 printf("Added file %s (submitter %d)\n", argv[i], s->index);
1314 for (j = 0; j < nthreads; j++) {
1315 s = get_submitter(j);
1316 for (i = 0; i < depth; i++) {
1319 if (posix_memalign(&buf, bs, bs)) {
1320 printf("failed alloc\n");
1323 s->iovecs[i].iov_base = buf;
1324 s->iovecs[i].iov_len = bs;
1328 for (j = 0; j < nthreads; j++) {
1329 s = get_submitter(j);
1332 err = setup_ring(s);
1336 printf("ring setup failed: %s, %d\n", strerror(errno), err);
1340 s = get_submitter(0);
1341 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
1343 printf("Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1345 printf("Engine=aio\n");
1347 for (j = 0; j < nthreads; j++) {
1348 s = get_submitter(j);
1350 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1351 #ifdef CONFIG_LIBAIO
1353 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1357 fdepths = malloc(8 * s->nr_files * nthreads);
1358 reap = calls = done = 0;
1360 unsigned long this_done = 0;
1361 unsigned long this_reap = 0;
1362 unsigned long this_call = 0;
1363 unsigned long rpc = 0, ipc = 0;
1364 unsigned long iops, bw;
1367 if (runtime && !--runtime)
1368 do_finish("timeout");
1370 /* don't print partial run, if interrupted by signal */
1374 /* one second in to the run, enable stats */
1378 for (j = 0; j < nthreads; j++) {
1379 s = get_submitter(j);
1380 this_done += s->done;
1381 this_call += s->calls;
1382 this_reap += s->reaps;
1384 if (this_call - calls) {
1385 rpc = (this_done - done) / (this_call - calls);
1386 ipc = (this_reap - reap) / (this_call - calls);
1389 file_depths(fdepths);
1390 iops = this_done - done;
1392 bw = iops * (bs / 1048576);
1394 bw = iops / (1048576 / bs);
1396 printf("IOPS=%luK, ", iops / 1000);
1398 printf("IOPS=%lu, ", iops);
1399 max_iops = max(max_iops, iops);
1401 printf("BW=%luMiB/s, ", bw);
1402 printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths);
1408 for (j = 0; j < nthreads; j++) {
1409 s = get_submitter(j);
1410 pthread_join(s->thread, &ret);
1416 printf("%d: Latency percentiles:\n", s->tid);
1417 for (i = 0, nr = 0; i < PLAT_NR; i++)
1419 show_clat_percentiles(s->plat, nr, 4);
1420 free(s->clock_batch);