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 22
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 #ifdef ARCH_HAVE_CPU_CLOCK
291 static unsigned int plat_val_to_idx(unsigned long val)
293 unsigned int msb, error_bits, base, offset, idx;
295 /* Find MSB starting from bit 0 */
299 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
302 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
303 * all bits of the sample as index
305 if (msb <= PLAT_BITS)
308 /* Compute the number of error bits to discard*/
309 error_bits = msb - PLAT_BITS;
311 /* Compute the number of buckets before the group */
312 base = (error_bits + 1) << PLAT_BITS;
315 * Discard the error bits and apply the mask to find the
316 * index for the buckets in the group
318 offset = (PLAT_VAL - 1) & (val >> error_bits);
320 /* Make sure the index does not exceed (array size - 1) */
321 idx = (base + offset) < (PLAT_NR - 1) ?
322 (base + offset) : (PLAT_NR - 1);
328 static void add_stat(struct submitter *s, int clock_index, int nr)
330 #ifdef ARCH_HAVE_CPU_CLOCK
331 unsigned long cycles;
334 if (!s->finish && clock_index) {
335 cycles = get_cpu_clock();
336 cycles -= s->clock_batch[clock_index];
337 pidx = plat_val_to_idx(cycles);
343 static int io_uring_map_buffers(struct submitter *s)
345 struct io_uring_map_buffers map = {
346 .fd = s->files[0].real_fd,
352 if (s->nr_files > 1) {
353 fprintf(stderr, "Can't map buffers with multiple files\n");
357 return syscall(__NR_io_uring_register, s->ring_fd,
358 IORING_REGISTER_MAP_BUFFERS, &map, 1);
361 static int io_uring_register_buffers(struct submitter *s)
366 return syscall(__NR_io_uring_register, s->ring_fd,
367 IORING_REGISTER_BUFFERS, s->iovecs, roundup_pow2(depth));
370 static int io_uring_register_files(struct submitter *s)
377 s->fds = calloc(s->nr_files, sizeof(__s32));
378 for (i = 0; i < s->nr_files; i++) {
379 s->fds[i] = s->files[i].real_fd;
380 s->files[i].fixed_fd = i;
383 return syscall(__NR_io_uring_register, s->ring_fd,
384 IORING_REGISTER_FILES, s->fds, s->nr_files);
387 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
390 * Clamp CQ ring size at our SQ ring size, we don't need more entries
393 p->flags |= IORING_SETUP_CQSIZE;
394 p->cq_entries = entries;
396 return syscall(__NR_io_uring_setup, entries, p);
399 static void io_uring_probe(int fd)
401 struct io_uring_probe *p;
404 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
408 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
409 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
413 if (IORING_OP_READ > p->ops_len)
416 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
422 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
423 unsigned int min_complete, unsigned int flags)
425 #ifdef FIO_ARCH_HAS_SYSCALL
426 return __do_syscall6(__NR_io_uring_enter, s->ring_fd, to_submit,
427 min_complete, flags, NULL, 0);
429 return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete,
434 #ifndef CONFIG_HAVE_GETTID
435 static int gettid(void)
437 return syscall(__NR_gettid);
441 static unsigned file_depth(struct submitter *s)
443 return (depth + s->nr_files - 1) / s->nr_files;
446 static void init_io(struct submitter *s, unsigned index)
448 struct io_uring_sqe *sqe = &s->sqes[index];
449 unsigned long offset;
454 sqe->opcode = IORING_OP_NOP;
458 if (s->nr_files == 1) {
461 f = &s->files[s->cur_file];
462 if (f->pending_ios >= file_depth(s)) {
464 if (s->cur_file == s->nr_files)
466 f = &s->files[s->cur_file];
472 r = __rand64(&s->rand_state);
473 offset = (r % (f->max_blocks - 1)) * bs;
477 if (f->cur_off + bs > f->max_size)
481 if (register_files) {
482 sqe->flags = IOSQE_FIXED_FILE;
483 sqe->fd = f->fixed_fd;
486 sqe->fd = f->real_fd;
489 sqe->opcode = IORING_OP_READ_FIXED;
490 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
492 sqe->buf_index = index;
493 } else if (!vectored) {
494 sqe->opcode = IORING_OP_READ;
495 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
499 sqe->opcode = IORING_OP_READV;
500 sqe->addr = (unsigned long) &s->iovecs[index];
506 sqe->user_data = (unsigned long) f->fileno;
507 if (stats && stats_running)
508 sqe->user_data |= ((uint64_t)s->clock_index << 32);
511 static int prep_more_ios_uring(struct submitter *s, int max_ios)
513 struct io_sq_ring *ring = &s->sq_ring;
514 unsigned index, tail, next_tail, prepped = 0;
516 next_tail = tail = *ring->tail;
519 if (next_tail == atomic_load_acquire(ring->head))
522 index = tail & sq_ring_mask;
524 ring->array[index] = index;
527 } while (prepped < max_ios);
530 atomic_store_release(ring->tail, tail);
534 static int get_file_size(struct file *f)
538 if (fstat(f->real_fd, &st) < 0)
540 if (S_ISBLK(st.st_mode)) {
541 unsigned long long bytes;
543 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
546 f->max_blocks = bytes / bs;
549 } else if (S_ISREG(st.st_mode)) {
550 f->max_blocks = st.st_size / bs;
551 f->max_size = st.st_size;
558 static int reap_events_uring(struct submitter *s)
560 struct io_cq_ring *ring = &s->cq_ring;
561 struct io_uring_cqe *cqe;
562 unsigned head, reaped = 0;
563 int last_idx = -1, stat_nr = 0;
570 if (head == atomic_load_acquire(ring->tail))
572 cqe = &ring->cqes[head & cq_ring_mask];
574 int fileno = cqe->user_data & 0xffffffff;
576 f = &s->files[fileno];
578 if (cqe->res != bs) {
579 printf("io: unexpected ret=%d\n", cqe->res);
580 if (polled && cqe->res == -EOPNOTSUPP)
581 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
586 int clock_index = cqe->user_data >> 32;
588 if (last_idx != clock_index) {
589 if (last_idx != -1) {
590 add_stat(s, last_idx, stat_nr);
593 last_idx = clock_index;
602 add_stat(s, last_idx, stat_nr);
605 s->inflight -= reaped;
606 atomic_store_release(ring->head, head);
611 static int submitter_init(struct submitter *s)
616 printf("submitter=%d, tid=%d\n", s->index, s->tid);
618 __init_rand64(&s->rand_state, pthread_self());
619 srand48(pthread_self());
621 for (i = 0; i < MAX_FDS; i++)
622 s->files[i].fileno = i;
625 nr_batch = roundup_pow2(depth / batch_submit);
628 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
631 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
633 s->clock_batch = NULL;
642 static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
651 while (index < max_ios) {
652 struct iocb *iocb = &iocbs[index];
654 if (s->nr_files == 1) {
657 f = &s->files[s->cur_file];
658 if (f->pending_ios >= file_depth(s)) {
660 if (s->cur_file == s->nr_files)
662 f = &s->files[s->cur_file];
668 offset = (r % (f->max_blocks - 1)) * bs;
669 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
670 s->iovecs[index].iov_len, offset);
673 if (stats && stats_running)
674 data |= (((uint64_t) s->clock_index) << 32);
675 iocb->data = (void *) (uintptr_t) data;
681 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
683 int last_idx = -1, stat_nr = 0;
687 uint64_t data = (uintptr_t) events[reaped].data;
688 struct file *f = &s->files[data & 0xffffffff];
691 if (events[reaped].res != bs) {
692 printf("io: unexpected ret=%ld\n", events[reaped].res);
696 int clock_index = data >> 32;
698 if (last_idx != clock_index) {
699 if (last_idx != -1) {
700 add_stat(s, last_idx, stat_nr);
703 last_idx = clock_index;
712 add_stat(s, last_idx, stat_nr);
714 s->inflight -= reaped;
719 static void *submitter_aio_fn(void *data)
721 struct submitter *s = data;
723 struct iocb **iocbsptr;
725 struct io_event *events;
726 #ifdef ARCH_HAVE_CPU_CLOCK
727 int nr_batch = submitter_init(s);
732 iocbsptr = calloc(depth, sizeof(struct iocb *));
733 iocbs = calloc(depth, sizeof(struct iocb));
734 events = calloc(depth, sizeof(struct io_event));
736 for (i = 0; i < depth; i++)
737 iocbsptr[i] = &iocbs[i];
741 int to_wait, to_submit, to_prep;
743 if (!prepped && s->inflight < depth) {
744 to_prep = min(depth - s->inflight, batch_submit);
745 prepped = prep_more_ios_aio(s, to_prep, iocbs);
746 #ifdef ARCH_HAVE_CPU_CLOCK
747 if (prepped && stats) {
748 s->clock_batch[s->clock_index] = get_cpu_clock();
749 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
753 s->inflight += prepped;
756 if (to_submit && (s->inflight + to_submit <= depth))
759 to_wait = min(s->inflight + to_submit, batch_complete);
761 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
766 } else if (ret != to_submit) {
767 printf("submitted %d, wanted %d\n", ret, to_submit);
776 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
778 perror("io_getevents");
780 } else if (r != to_wait) {
781 printf("r=%d, wait=%d\n", r, to_wait);
784 r = reap_events_aio(s, events, r);
788 } while (!s->finish);
798 static void *submitter_uring_fn(void *data)
800 struct submitter *s = data;
801 struct io_sq_ring *ring = &s->sq_ring;
803 #ifdef ARCH_HAVE_CPU_CLOCK
804 int nr_batch = submitter_init(s);
811 int to_wait, to_submit, this_reap, to_prep;
812 unsigned ring_flags = 0;
814 if (!prepped && s->inflight < depth) {
815 to_prep = min(depth - s->inflight, batch_submit);
816 prepped = prep_more_ios_uring(s, to_prep);
817 #ifdef ARCH_HAVE_CPU_CLOCK
818 if (prepped && stats) {
819 s->clock_batch[s->clock_index] = get_cpu_clock();
820 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
824 s->inflight += prepped;
828 if (to_submit && (s->inflight + to_submit <= depth))
831 to_wait = min(s->inflight + to_submit, batch_complete);
834 * Only need to call io_uring_enter if we're not using SQ thread
835 * poll, or if IORING_SQ_NEED_WAKEUP is set.
838 ring_flags = atomic_load_acquire(ring->flags);
839 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
843 flags = IORING_ENTER_GETEVENTS;
844 if (ring_flags & IORING_SQ_NEED_WAKEUP)
845 flags |= IORING_ENTER_SQ_WAKEUP;
846 ret = io_uring_enter(s, to_submit, to_wait, flags);
849 /* for SQPOLL, we submitted it all effectively */
854 * For non SQ thread poll, we already got the events we needed
855 * through the io_uring_enter() above. For SQ thread poll, we
856 * need to loop here until we find enough events.
862 r = reap_events_uring(s);
868 } while (sq_thread_poll && this_reap < to_wait);
869 s->reaps += this_reap;
877 } else if (ret < to_submit) {
878 int diff = to_submit - ret;
887 } else if (ret < 0) {
888 if (errno == EAGAIN) {
896 printf("io_submit: %s\n", strerror(errno));
899 } while (!s->finish);
905 static struct submitter *get_submitter(int offset)
911 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
915 static void do_finish(const char *reason)
918 printf("Exiting on %s\n", reason);
919 for (j = 0; j < nthreads; j++) {
920 struct submitter *s = get_submitter(j);
923 if (max_iops > 100000)
924 printf("Maximum IOPS=%luK\n", max_iops / 1000);
926 printf("Maximum IOPS=%lu\n", max_iops);
930 static void sig_int(int sig)
935 static void arm_sig_int(void)
937 struct sigaction act;
939 memset(&act, 0, sizeof(act));
940 act.sa_handler = sig_int;
941 act.sa_flags = SA_RESTART;
942 sigaction(SIGINT, &act, NULL);
944 /* Windows uses SIGBREAK as a quit signal from other applications */
946 sigaction(SIGBREAK, &act, NULL);
950 static int setup_aio(struct submitter *s)
954 fprintf(stderr, "aio does not support polled IO\n");
957 if (sq_thread_poll) {
958 fprintf(stderr, "aio does not support SQPOLL IO\n");
962 fprintf(stderr, "aio does not support polled IO\n");
965 if (fixedbufs || register_files) {
966 fprintf(stderr, "aio does not support registered files or buffers\n");
967 fixedbufs = register_files = 0;
970 return io_queue_init(roundup_pow2(depth), &s->aio_ctx);
972 fprintf(stderr, "Legacy AIO not available on this system/build\n");
978 static int setup_ring(struct submitter *s)
980 struct io_sq_ring *sring = &s->sq_ring;
981 struct io_cq_ring *cring = &s->cq_ring;
982 struct io_uring_params p;
986 memset(&p, 0, sizeof(p));
988 if (polled && !do_nop)
989 p.flags |= IORING_SETUP_IOPOLL;
990 if (sq_thread_poll) {
991 p.flags |= IORING_SETUP_SQPOLL;
992 if (sq_thread_cpu != -1) {
993 p.flags |= IORING_SETUP_SQ_AFF;
994 p.sq_thread_cpu = sq_thread_cpu;
998 fd = io_uring_setup(depth, &p);
1000 perror("io_uring_setup");
1010 rlim.rlim_cur = RLIM_INFINITY;
1011 rlim.rlim_max = RLIM_INFINITY;
1012 /* ignore potential error, not needed on newer kernels */
1013 setrlimit(RLIMIT_MEMLOCK, &rlim);
1015 ret = io_uring_register_buffers(s);
1017 perror("io_uring_register_buffers");
1022 ret = io_uring_map_buffers(s);
1024 perror("io_uring_map_buffers");
1030 if (register_files) {
1031 ret = io_uring_register_files(s);
1033 perror("io_uring_register_files");
1038 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
1039 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1040 IORING_OFF_SQ_RING);
1041 sring->head = ptr + p.sq_off.head;
1042 sring->tail = ptr + p.sq_off.tail;
1043 sring->ring_mask = ptr + p.sq_off.ring_mask;
1044 sring->ring_entries = ptr + p.sq_off.ring_entries;
1045 sring->flags = ptr + p.sq_off.flags;
1046 sring->array = ptr + p.sq_off.array;
1047 sq_ring_mask = *sring->ring_mask;
1049 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
1050 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1053 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
1054 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1055 IORING_OFF_CQ_RING);
1056 cring->head = ptr + p.cq_off.head;
1057 cring->tail = ptr + p.cq_off.tail;
1058 cring->ring_mask = ptr + p.cq_off.ring_mask;
1059 cring->ring_entries = ptr + p.cq_off.ring_entries;
1060 cring->cqes = ptr + p.cq_off.cqes;
1061 cq_ring_mask = *cring->ring_mask;
1065 static void file_depths(char *buf)
1073 for (j = 0; j < nthreads; j++) {
1074 struct submitter *s = get_submitter(j);
1076 for (i = 0; i < s->nr_files; i++) {
1077 struct file *f = &s->files[i];
1080 p += sprintf(p, " %d", f->pending_ios);
1082 p += sprintf(p, "%d", f->pending_ios);
1088 static void usage(char *argv, int status)
1090 char runtime_str[16];
1091 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
1092 printf("%s [options] -- [filenames]\n"
1093 " -d <int> : IO Depth, default %d\n"
1094 " -s <int> : Batch submit, default %d\n"
1095 " -c <int> : Batch complete, default %d\n"
1096 " -b <int> : Block size, default %d\n"
1097 " -p <bool> : Polled IO, default %d\n"
1098 " -B <bool> : Fixed buffers, default %d\n"
1099 " -D <bool> : DMA map fixed buffers, default %d\n"
1100 " -F <bool> : Register files, default %d\n"
1101 " -n <int> : Number of threads, default %d\n"
1102 " -O <bool> : Use O_DIRECT, default %d\n"
1103 " -N <bool> : Perform just no-op requests, default %d\n"
1104 " -t <bool> : Track IO latencies, default %d\n"
1105 " -T <int> : TSC rate in HZ\n"
1106 " -r <int> : Runtime in seconds, default %s\n"
1107 " -R <bool> : Use random IO, default %d\n"
1108 " -a <bool> : Use legacy aio, default %d\n",
1109 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1110 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
1111 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio);
1115 static void read_tsc_rate(void)
1123 fd = open(TSC_RATE_FILE, O_RDONLY);
1127 ret = read(fd, buffer, sizeof(buffer));
1133 tsc_rate = strtoul(buffer, NULL, 10);
1134 printf("Using TSC rate %luHz\n", tsc_rate);
1138 static void write_tsc_rate(void)
1144 if (!stat(TSC_RATE_FILE, &sb))
1147 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1151 memset(buffer, 0, sizeof(buffer));
1152 sprintf(buffer, "%lu", tsc_rate);
1153 ret = write(fd, buffer, strlen(buffer));
1159 int main(int argc, char *argv[])
1161 struct submitter *s;
1162 unsigned long done, calls, reap;
1163 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1169 if (!do_nop && argc < 2)
1172 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:r:D:R:h?")) != -1) {
1175 aio = !!atoi(optarg);
1178 depth = atoi(optarg);
1181 batch_submit = atoi(optarg);
1186 batch_complete = atoi(optarg);
1187 if (!batch_complete)
1194 polled = !!atoi(optarg);
1197 fixedbufs = !!atoi(optarg);
1200 register_files = !!atoi(optarg);
1203 nthreads = atoi(optarg);
1205 printf("Threads must be non-zero\n");
1210 do_nop = !!atoi(optarg);
1213 buffered = !atoi(optarg);
1216 #ifndef ARCH_HAVE_CPU_CLOCK
1217 fprintf(stderr, "Stats not supported on this CPU\n");
1220 stats = !!atoi(optarg);
1223 #ifndef ARCH_HAVE_CPU_CLOCK
1224 fprintf(stderr, "Stats not supported on this CPU\n");
1227 tsc_rate = strtoul(optarg, NULL, 10);
1231 runtime = atoi(optarg);
1234 dma_map = !!atoi(optarg);
1237 random_io = !!atoi(optarg);
1250 if (batch_complete > depth)
1251 batch_complete = depth;
1252 if (batch_submit > depth)
1253 batch_submit = depth;
1254 if (!fixedbufs && dma_map)
1257 submitter = calloc(nthreads, sizeof(*submitter) +
1258 roundup_pow2(depth) * sizeof(struct iovec));
1259 for (j = 0; j < nthreads; j++) {
1260 s = get_submitter(j);
1262 s->done = s->calls = s->reaps = 0;
1265 flags = O_RDONLY | O_NOATIME;
1274 printf("No files specified\n");
1277 threads_per_f = nthreads / nfiles;
1278 /* make sure each thread gets assigned files */
1279 if (threads_per_f == 0) {
1282 threads_rem = nthreads - threads_per_f * nfiles;
1285 while (!do_nop && i < argc) {
1288 memset(&f, 0, sizeof(f));
1290 fd = open(argv[i], flags);
1296 if (get_file_size(&f)) {
1297 printf("failed getting size of device/file\n");
1300 if (f.max_blocks <= 1) {
1301 printf("Zero file/device size?\n");
1306 limit = threads_per_f;
1307 limit += threads_rem > 0 ? 1 : 0;
1308 for (k = 0; k < limit; k++) {
1309 s = get_submitter((j + k) % nthreads);
1311 if (s->nr_files == MAX_FDS) {
1312 printf("Max number of files (%d) reached\n", MAX_FDS);
1316 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1318 printf("Added file %s (submitter %d)\n", argv[i], s->index);
1328 page_size = sysconf(_SC_PAGESIZE);
1332 for (j = 0; j < nthreads; j++) {
1333 s = get_submitter(j);
1334 for (i = 0; i < roundup_pow2(depth); i++) {
1337 if (posix_memalign(&buf, page_size, bs)) {
1338 printf("failed alloc\n");
1341 s->iovecs[i].iov_base = buf;
1342 s->iovecs[i].iov_len = bs;
1346 for (j = 0; j < nthreads; j++) {
1347 s = get_submitter(j);
1350 err = setup_ring(s);
1354 printf("ring setup failed: %s, %d\n", strerror(errno), err);
1358 s = get_submitter(0);
1359 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
1361 printf("Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1363 printf("Engine=aio\n");
1365 for (j = 0; j < nthreads; j++) {
1366 s = get_submitter(j);
1368 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1369 #ifdef CONFIG_LIBAIO
1371 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1375 fdepths = malloc(8 * s->nr_files * nthreads);
1376 reap = calls = done = 0;
1378 unsigned long this_done = 0;
1379 unsigned long this_reap = 0;
1380 unsigned long this_call = 0;
1381 unsigned long rpc = 0, ipc = 0;
1382 unsigned long iops, bw;
1385 if (runtime && !--runtime)
1386 do_finish("timeout");
1388 /* don't print partial run, if interrupted by signal */
1392 /* one second in to the run, enable stats */
1396 for (j = 0; j < nthreads; j++) {
1397 s = get_submitter(j);
1398 this_done += s->done;
1399 this_call += s->calls;
1400 this_reap += s->reaps;
1402 if (this_call - calls) {
1403 rpc = (this_done - done) / (this_call - calls);
1404 ipc = (this_reap - reap) / (this_call - calls);
1407 file_depths(fdepths);
1408 iops = this_done - done;
1410 bw = iops * (bs / 1048576);
1412 bw = iops / (1048576 / bs);
1414 printf("IOPS=%luK, ", iops / 1000);
1416 printf("IOPS=%lu, ", iops);
1417 max_iops = max(max_iops, iops);
1419 printf("BW=%luMiB/s, ", bw);
1420 printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths);
1426 for (j = 0; j < nthreads; j++) {
1427 s = get_submitter(j);
1428 pthread_join(s->thread, &ret);
1434 printf("%d: Latency percentiles:\n", s->tid);
1435 for (i = 0, nr = 0; i < PLAT_NR; i++)
1437 show_clat_percentiles(s->plat, nr, 4);
1438 free(s->clock_batch);