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)
81 struct io_sq_ring sq_ring;
82 struct io_uring_sqe *sqes;
83 struct io_cq_ring cq_ring;
93 struct taus258_state rand_state;
95 unsigned long *clock_batch;
100 io_context_t aio_ctx;
103 struct file files[MAX_FDS];
106 struct iovec iovecs[];
109 static struct submitter *submitter;
110 static volatile int finish;
111 static int stats_running;
112 static unsigned long max_iops;
114 static int depth = DEPTH;
115 static int batch_submit = BATCH_SUBMIT;
116 static int batch_complete = BATCH_COMPLETE;
118 static int polled = 1; /* use IO polling */
119 static int fixedbufs = 1; /* use fixed user buffers */
120 static int dma_map; /* pre-map DMA buffers */
121 static int register_files = 1; /* use fixed files */
122 static int buffered = 0; /* use buffered IO, not O_DIRECT */
123 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
124 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
125 static int do_nop = 0; /* no-op SQ ring commands */
126 static int nthreads = 1;
127 static int stats = 0; /* generate IO stats */
128 static int aio = 0; /* use libaio */
129 static int runtime = 0; /* runtime */
130 static int random_io = 1; /* random or sequential IO */
131 static int register_ring = 1; /* register ring */
133 static unsigned long tsc_rate;
135 #define TSC_RATE_FILE "tsc-rate"
137 static int vectored = 1;
139 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
140 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
141 static int plist_len = 17;
143 #ifndef IORING_REGISTER_MAP_BUFFERS
144 #define IORING_REGISTER_MAP_BUFFERS 22
145 struct io_uring_map_buffers {
154 static unsigned long cycles_to_nsec(unsigned long cycles)
161 val = cycles * 1000000000ULL;
162 return val / tsc_rate;
165 static unsigned long plat_idx_to_val(unsigned int idx)
167 unsigned int error_bits;
168 unsigned long k, base;
170 assert(idx < PLAT_NR);
172 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
173 * all bits of the sample as index */
174 if (idx < (PLAT_VAL << 1))
175 return cycles_to_nsec(idx);
177 /* Find the group and compute the minimum value of that group */
178 error_bits = (idx >> PLAT_BITS) - 1;
179 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
181 /* Find its bucket number of the group */
184 /* Return the mean of the range of the bucket */
185 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
188 unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
189 unsigned long **output,
190 unsigned long *maxv, unsigned long *minv)
192 unsigned long sum = 0;
193 unsigned int len = plist_len, i, j = 0;
194 unsigned long *ovals = NULL;
200 ovals = malloc(len * sizeof(*ovals));
205 * Calculate bucket values, note down max and min values
208 for (i = 0; i < PLAT_NR && !is_last; i++) {
210 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
211 assert(plist[j] <= 100.0);
213 ovals[j] = plat_idx_to_val(i);
214 if (ovals[j] < *minv)
216 if (ovals[j] > *maxv)
219 is_last = (j == len - 1) != 0;
228 fprintf(stderr, "error calculating latency percentiles\n");
234 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
235 unsigned int precision)
237 unsigned int divisor, len, i, j = 0;
238 unsigned long minv, maxv;
239 unsigned long *ovals;
240 int per_line, scale_down, time_width;
244 len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
251 printf(" percentiles (tsc ticks):\n |");
252 } else if (minv > 2000 && maxv > 99999) {
255 printf(" percentiles (usec):\n |");
259 printf(" percentiles (nsec):\n |");
262 time_width = max(5, (int) (log10(maxv / divisor) + 1));
263 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
264 precision, time_width);
265 /* fmt will be something like " %5.2fth=[%4llu]%c" */
266 per_line = (80 - 7) / (precision + 10 + time_width);
268 for (j = 0; j < len; j++) {
270 if (j != 0 && (j % per_line) == 0)
273 /* end of the list */
274 is_last = (j == len - 1) != 0;
276 for (i = 0; i < scale_down; i++)
277 ovals[j] = (ovals[j] + 999) / 1000;
279 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
284 if ((j % per_line) == per_line - 1) /* for formatting */
292 #ifdef ARCH_HAVE_CPU_CLOCK
293 static unsigned int plat_val_to_idx(unsigned long val)
295 unsigned int msb, error_bits, base, offset, idx;
297 /* Find MSB starting from bit 0 */
301 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
304 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
305 * all bits of the sample as index
307 if (msb <= PLAT_BITS)
310 /* Compute the number of error bits to discard*/
311 error_bits = msb - PLAT_BITS;
313 /* Compute the number of buckets before the group */
314 base = (error_bits + 1) << PLAT_BITS;
317 * Discard the error bits and apply the mask to find the
318 * index for the buckets in the group
320 offset = (PLAT_VAL - 1) & (val >> error_bits);
322 /* Make sure the index does not exceed (array size - 1) */
323 idx = (base + offset) < (PLAT_NR - 1) ?
324 (base + offset) : (PLAT_NR - 1);
330 static void add_stat(struct submitter *s, int clock_index, int nr)
332 #ifdef ARCH_HAVE_CPU_CLOCK
333 unsigned long cycles;
336 if (!s->finish && clock_index) {
337 cycles = get_cpu_clock();
338 cycles -= s->clock_batch[clock_index];
339 pidx = plat_val_to_idx(cycles);
345 static int io_uring_map_buffers(struct submitter *s)
347 struct io_uring_map_buffers map = {
348 .fd = s->files[0].real_fd,
355 fprintf(stdout, "Mapping buffers may not work 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)
426 flags |= IORING_ENTER_REGISTERED_RING;
427 #ifdef FIO_ARCH_HAS_SYSCALL
428 return __do_syscall6(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
429 min_complete, flags, NULL, 0);
431 return syscall(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
432 min_complete, flags, NULL, 0);
436 #ifndef CONFIG_HAVE_GETTID
437 static int gettid(void)
439 return syscall(__NR_gettid);
443 static unsigned file_depth(struct submitter *s)
445 return (depth + s->nr_files - 1) / s->nr_files;
448 static void init_io(struct submitter *s, unsigned index)
450 struct io_uring_sqe *sqe = &s->sqes[index];
451 unsigned long offset;
456 sqe->opcode = IORING_OP_NOP;
460 if (s->nr_files == 1) {
463 f = &s->files[s->cur_file];
464 if (f->pending_ios >= file_depth(s)) {
466 if (s->cur_file == s->nr_files)
468 f = &s->files[s->cur_file];
474 r = __rand64(&s->rand_state);
475 offset = (r % (f->max_blocks - 1)) * bs;
479 if (f->cur_off + bs > f->max_size)
483 if (register_files) {
484 sqe->flags = IOSQE_FIXED_FILE;
485 sqe->fd = f->fixed_fd;
488 sqe->fd = f->real_fd;
491 sqe->opcode = IORING_OP_READ_FIXED;
492 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
494 sqe->buf_index = index;
495 } else if (!vectored) {
496 sqe->opcode = IORING_OP_READ;
497 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
501 sqe->opcode = IORING_OP_READV;
502 sqe->addr = (unsigned long) &s->iovecs[index];
508 sqe->user_data = (unsigned long) f->fileno;
509 if (stats && stats_running)
510 sqe->user_data |= ((uint64_t)s->clock_index << 32);
513 static int prep_more_ios_uring(struct submitter *s, int max_ios)
515 struct io_sq_ring *ring = &s->sq_ring;
516 unsigned index, tail, next_tail, prepped = 0;
518 next_tail = tail = *ring->tail;
521 if (next_tail == atomic_load_acquire(ring->head))
524 index = tail & sq_ring_mask;
526 ring->array[index] = index;
529 } while (prepped < max_ios);
532 atomic_store_release(ring->tail, tail);
536 static int get_file_size(struct file *f)
540 if (fstat(f->real_fd, &st) < 0)
542 if (S_ISBLK(st.st_mode)) {
543 unsigned long long bytes;
545 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
548 f->max_blocks = bytes / bs;
551 } else if (S_ISREG(st.st_mode)) {
552 f->max_blocks = st.st_size / bs;
553 f->max_size = st.st_size;
560 static int reap_events_uring(struct submitter *s)
562 struct io_cq_ring *ring = &s->cq_ring;
563 struct io_uring_cqe *cqe;
564 unsigned head, reaped = 0;
565 int last_idx = -1, stat_nr = 0;
572 if (head == atomic_load_acquire(ring->tail))
574 cqe = &ring->cqes[head & cq_ring_mask];
576 int fileno = cqe->user_data & 0xffffffff;
578 f = &s->files[fileno];
580 if (cqe->res != bs) {
581 printf("io: unexpected ret=%d\n", cqe->res);
582 if (polled && cqe->res == -EOPNOTSUPP)
583 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
588 int clock_index = cqe->user_data >> 32;
590 if (last_idx != clock_index) {
591 if (last_idx != -1) {
592 add_stat(s, last_idx, stat_nr);
595 last_idx = clock_index;
604 add_stat(s, last_idx, stat_nr);
607 s->inflight -= reaped;
608 atomic_store_release(ring->head, head);
613 static int submitter_init(struct submitter *s)
618 printf("submitter=%d, tid=%d\n", s->index, s->tid);
620 __init_rand64(&s->rand_state, pthread_self());
621 srand48(pthread_self());
623 for (i = 0; i < MAX_FDS; i++)
624 s->files[i].fileno = i;
627 nr_batch = roundup_pow2(depth / batch_submit);
630 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
633 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
635 s->clock_batch = NULL;
644 static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
653 while (index < max_ios) {
654 struct iocb *iocb = &iocbs[index];
656 if (s->nr_files == 1) {
659 f = &s->files[s->cur_file];
660 if (f->pending_ios >= file_depth(s)) {
662 if (s->cur_file == s->nr_files)
664 f = &s->files[s->cur_file];
670 offset = (r % (f->max_blocks - 1)) * bs;
671 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
672 s->iovecs[index].iov_len, offset);
675 if (stats && stats_running)
676 data |= (((uint64_t) s->clock_index) << 32);
677 iocb->data = (void *) (uintptr_t) data;
683 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
685 int last_idx = -1, stat_nr = 0;
689 uint64_t data = (uintptr_t) events[reaped].data;
690 struct file *f = &s->files[data & 0xffffffff];
693 if (events[reaped].res != bs) {
694 printf("io: unexpected ret=%ld\n", events[reaped].res);
698 int clock_index = data >> 32;
700 if (last_idx != clock_index) {
701 if (last_idx != -1) {
702 add_stat(s, last_idx, stat_nr);
705 last_idx = clock_index;
714 add_stat(s, last_idx, stat_nr);
716 s->inflight -= reaped;
721 static void *submitter_aio_fn(void *data)
723 struct submitter *s = data;
725 struct iocb **iocbsptr;
727 struct io_event *events;
728 #ifdef ARCH_HAVE_CPU_CLOCK
729 int nr_batch = submitter_init(s);
734 iocbsptr = calloc(depth, sizeof(struct iocb *));
735 iocbs = calloc(depth, sizeof(struct iocb));
736 events = calloc(depth, sizeof(struct io_event));
738 for (i = 0; i < depth; i++)
739 iocbsptr[i] = &iocbs[i];
743 int to_wait, to_submit, to_prep;
745 if (!prepped && s->inflight < depth) {
746 to_prep = min(depth - s->inflight, batch_submit);
747 prepped = prep_more_ios_aio(s, to_prep, iocbs);
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;
758 if (to_submit && (s->inflight + to_submit <= depth))
761 to_wait = min(s->inflight + to_submit, batch_complete);
763 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
768 } else if (ret != to_submit) {
769 printf("submitted %d, wanted %d\n", ret, to_submit);
778 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
780 perror("io_getevents");
782 } else if (r != to_wait) {
783 printf("r=%d, wait=%d\n", r, to_wait);
786 r = reap_events_aio(s, events, r);
790 } while (!s->finish);
800 static void io_uring_unregister_ring(struct submitter *s)
802 struct io_uring_rsrc_update up = {
803 .offset = s->enter_ring_fd,
806 syscall(__NR_io_uring_register, s->ring_fd, IORING_UNREGISTER_RING_FDS,
810 static int io_uring_register_ring(struct submitter *s)
812 struct io_uring_rsrc_update up = {
818 ret = syscall(__NR_io_uring_register, s->ring_fd,
819 IORING_REGISTER_RING_FDS, &up, 1);
821 s->enter_ring_fd = up.offset;
828 static void *submitter_uring_fn(void *data)
830 struct submitter *s = data;
831 struct io_sq_ring *ring = &s->sq_ring;
833 #ifdef ARCH_HAVE_CPU_CLOCK
834 int nr_batch = submitter_init(s);
840 io_uring_register_ring(s);
844 int to_wait, to_submit, this_reap, to_prep;
845 unsigned ring_flags = 0;
847 if (!prepped && s->inflight < depth) {
848 to_prep = min(depth - s->inflight, batch_submit);
849 prepped = prep_more_ios_uring(s, to_prep);
850 #ifdef ARCH_HAVE_CPU_CLOCK
851 if (prepped && stats) {
852 s->clock_batch[s->clock_index] = get_cpu_clock();
853 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
857 s->inflight += prepped;
861 if (to_submit && (s->inflight + to_submit <= depth))
864 to_wait = min(s->inflight + to_submit, batch_complete);
867 * Only need to call io_uring_enter if we're not using SQ thread
868 * poll, or if IORING_SQ_NEED_WAKEUP is set.
871 ring_flags = atomic_load_acquire(ring->flags);
872 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
876 flags = IORING_ENTER_GETEVENTS;
877 if (ring_flags & IORING_SQ_NEED_WAKEUP)
878 flags |= IORING_ENTER_SQ_WAKEUP;
879 ret = io_uring_enter(s, to_submit, to_wait, flags);
882 /* for SQPOLL, we submitted it all effectively */
887 * For non SQ thread poll, we already got the events we needed
888 * through the io_uring_enter() above. For SQ thread poll, we
889 * need to loop here until we find enough events.
895 r = reap_events_uring(s);
901 } while (sq_thread_poll && this_reap < to_wait);
902 s->reaps += this_reap;
910 } else if (ret < to_submit) {
911 int diff = to_submit - ret;
920 } else if (ret < 0) {
921 if (errno == EAGAIN) {
929 printf("io_submit: %s\n", strerror(errno));
932 } while (!s->finish);
935 io_uring_unregister_ring(s);
941 static struct submitter *get_submitter(int offset)
947 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
951 static void do_finish(const char *reason)
954 printf("Exiting on %s\n", reason);
955 for (j = 0; j < nthreads; j++) {
956 struct submitter *s = get_submitter(j);
959 if (max_iops > 100000)
960 printf("Maximum IOPS=%luK\n", max_iops / 1000);
962 printf("Maximum IOPS=%lu\n", max_iops);
966 static void sig_int(int sig)
971 static void arm_sig_int(void)
973 struct sigaction act;
975 memset(&act, 0, sizeof(act));
976 act.sa_handler = sig_int;
977 act.sa_flags = SA_RESTART;
978 sigaction(SIGINT, &act, NULL);
980 /* Windows uses SIGBREAK as a quit signal from other applications */
982 sigaction(SIGBREAK, &act, NULL);
986 static int setup_aio(struct submitter *s)
990 fprintf(stderr, "aio does not support polled IO\n");
993 if (sq_thread_poll) {
994 fprintf(stderr, "aio does not support SQPOLL IO\n");
998 fprintf(stderr, "aio does not support polled IO\n");
1001 if (fixedbufs || register_files) {
1002 fprintf(stderr, "aio does not support registered files or buffers\n");
1003 fixedbufs = register_files = 0;
1006 return io_queue_init(roundup_pow2(depth), &s->aio_ctx);
1008 fprintf(stderr, "Legacy AIO not available on this system/build\n");
1014 static int setup_ring(struct submitter *s)
1016 struct io_sq_ring *sring = &s->sq_ring;
1017 struct io_cq_ring *cring = &s->cq_ring;
1018 struct io_uring_params p;
1022 memset(&p, 0, sizeof(p));
1024 if (polled && !do_nop)
1025 p.flags |= IORING_SETUP_IOPOLL;
1026 if (sq_thread_poll) {
1027 p.flags |= IORING_SETUP_SQPOLL;
1028 if (sq_thread_cpu != -1) {
1029 p.flags |= IORING_SETUP_SQ_AFF;
1030 p.sq_thread_cpu = sq_thread_cpu;
1034 fd = io_uring_setup(depth, &p);
1036 perror("io_uring_setup");
1039 s->ring_fd = s->enter_ring_fd = fd;
1046 rlim.rlim_cur = RLIM_INFINITY;
1047 rlim.rlim_max = RLIM_INFINITY;
1048 /* ignore potential error, not needed on newer kernels */
1049 setrlimit(RLIMIT_MEMLOCK, &rlim);
1051 ret = io_uring_register_buffers(s);
1053 perror("io_uring_register_buffers");
1058 ret = io_uring_map_buffers(s);
1060 perror("io_uring_map_buffers");
1066 if (register_files) {
1067 ret = io_uring_register_files(s);
1069 perror("io_uring_register_files");
1074 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
1075 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1076 IORING_OFF_SQ_RING);
1077 sring->head = ptr + p.sq_off.head;
1078 sring->tail = ptr + p.sq_off.tail;
1079 sring->ring_mask = ptr + p.sq_off.ring_mask;
1080 sring->ring_entries = ptr + p.sq_off.ring_entries;
1081 sring->flags = ptr + p.sq_off.flags;
1082 sring->array = ptr + p.sq_off.array;
1083 sq_ring_mask = *sring->ring_mask;
1085 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
1086 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1089 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
1090 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1091 IORING_OFF_CQ_RING);
1092 cring->head = ptr + p.cq_off.head;
1093 cring->tail = ptr + p.cq_off.tail;
1094 cring->ring_mask = ptr + p.cq_off.ring_mask;
1095 cring->ring_entries = ptr + p.cq_off.ring_entries;
1096 cring->cqes = ptr + p.cq_off.cqes;
1097 cq_ring_mask = *cring->ring_mask;
1101 static void file_depths(char *buf)
1109 for (j = 0; j < nthreads; j++) {
1110 struct submitter *s = get_submitter(j);
1112 for (i = 0; i < s->nr_files; i++) {
1113 struct file *f = &s->files[i];
1116 p += sprintf(p, " %d", f->pending_ios);
1118 p += sprintf(p, "%d", f->pending_ios);
1124 static void usage(char *argv, int status)
1126 char runtime_str[16];
1127 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
1128 printf("%s [options] -- [filenames]\n"
1129 " -d <int> : IO Depth, default %d\n"
1130 " -s <int> : Batch submit, default %d\n"
1131 " -c <int> : Batch complete, default %d\n"
1132 " -b <int> : Block size, default %d\n"
1133 " -p <bool> : Polled IO, default %d\n"
1134 " -B <bool> : Fixed buffers, default %d\n"
1135 " -D <bool> : DMA map fixed buffers, default %d\n"
1136 " -F <bool> : Register files, default %d\n"
1137 " -n <int> : Number of threads, default %d\n"
1138 " -O <bool> : Use O_DIRECT, default %d\n"
1139 " -N <bool> : Perform just no-op requests, default %d\n"
1140 " -t <bool> : Track IO latencies, default %d\n"
1141 " -T <int> : TSC rate in HZ\n"
1142 " -r <int> : Runtime in seconds, default %s\n"
1143 " -R <bool> : Use random IO, default %d\n"
1144 " -a <bool> : Use legacy aio, default %d\n"
1145 " -X <bool> : Use registered ring %d\n",
1146 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1147 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
1148 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio,
1153 static void read_tsc_rate(void)
1161 fd = open(TSC_RATE_FILE, O_RDONLY);
1165 ret = read(fd, buffer, sizeof(buffer));
1171 tsc_rate = strtoul(buffer, NULL, 10);
1172 printf("Using TSC rate %luHz\n", tsc_rate);
1176 static void write_tsc_rate(void)
1182 if (!stat(TSC_RATE_FILE, &sb))
1185 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1189 memset(buffer, 0, sizeof(buffer));
1190 sprintf(buffer, "%lu", tsc_rate);
1191 ret = write(fd, buffer, strlen(buffer));
1197 int main(int argc, char *argv[])
1199 struct submitter *s;
1200 unsigned long done, calls, reap;
1201 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1207 if (!do_nop && argc < 2)
1210 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:r:D:R:X:h?")) != -1) {
1213 aio = !!atoi(optarg);
1216 depth = atoi(optarg);
1219 batch_submit = atoi(optarg);
1224 batch_complete = atoi(optarg);
1225 if (!batch_complete)
1232 polled = !!atoi(optarg);
1235 fixedbufs = !!atoi(optarg);
1238 register_files = !!atoi(optarg);
1241 nthreads = atoi(optarg);
1243 printf("Threads must be non-zero\n");
1248 do_nop = !!atoi(optarg);
1251 buffered = !atoi(optarg);
1254 #ifndef ARCH_HAVE_CPU_CLOCK
1255 fprintf(stderr, "Stats not supported on this CPU\n");
1258 stats = !!atoi(optarg);
1261 #ifndef ARCH_HAVE_CPU_CLOCK
1262 fprintf(stderr, "Stats not supported on this CPU\n");
1265 tsc_rate = strtoul(optarg, NULL, 10);
1269 runtime = atoi(optarg);
1272 dma_map = !!atoi(optarg);
1275 random_io = !!atoi(optarg);
1278 register_ring = !!atoi(optarg);
1291 if (batch_complete > depth)
1292 batch_complete = depth;
1293 if (batch_submit > depth)
1294 batch_submit = depth;
1295 if (!fixedbufs && dma_map)
1298 submitter = calloc(nthreads, sizeof(*submitter) +
1299 roundup_pow2(depth) * sizeof(struct iovec));
1300 for (j = 0; j < nthreads; j++) {
1301 s = get_submitter(j);
1303 s->done = s->calls = s->reaps = 0;
1306 flags = O_RDONLY | O_NOATIME;
1315 printf("No files specified\n");
1318 threads_per_f = nthreads / nfiles;
1319 /* make sure each thread gets assigned files */
1320 if (threads_per_f == 0) {
1323 threads_rem = nthreads - threads_per_f * nfiles;
1326 while (!do_nop && i < argc) {
1329 memset(&f, 0, sizeof(f));
1331 fd = open(argv[i], flags);
1337 if (get_file_size(&f)) {
1338 printf("failed getting size of device/file\n");
1341 if (f.max_blocks <= 1) {
1342 printf("Zero file/device size?\n");
1347 limit = threads_per_f;
1348 limit += threads_rem > 0 ? 1 : 0;
1349 for (k = 0; k < limit; k++) {
1350 s = get_submitter((j + k) % nthreads);
1352 if (s->nr_files == MAX_FDS) {
1353 printf("Max number of files (%d) reached\n", MAX_FDS);
1357 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1359 printf("Added file %s (submitter %d)\n", argv[i], s->index);
1369 page_size = sysconf(_SC_PAGESIZE);
1373 for (j = 0; j < nthreads; j++) {
1374 s = get_submitter(j);
1375 for (i = 0; i < roundup_pow2(depth); i++) {
1378 if (posix_memalign(&buf, page_size, bs)) {
1379 printf("failed alloc\n");
1382 s->iovecs[i].iov_base = buf;
1383 s->iovecs[i].iov_len = bs;
1387 for (j = 0; j < nthreads; j++) {
1388 s = get_submitter(j);
1391 err = setup_ring(s);
1395 printf("ring setup failed: %s, %d\n", strerror(errno), err);
1399 s = get_submitter(0);
1400 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
1402 printf("Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
1404 printf("Engine=aio\n");
1406 for (j = 0; j < nthreads; j++) {
1407 s = get_submitter(j);
1409 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1410 #ifdef CONFIG_LIBAIO
1412 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1416 fdepths = malloc(8 * s->nr_files * nthreads);
1417 reap = calls = done = 0;
1419 unsigned long this_done = 0;
1420 unsigned long this_reap = 0;
1421 unsigned long this_call = 0;
1422 unsigned long rpc = 0, ipc = 0;
1423 unsigned long iops, bw;
1426 if (runtime && !--runtime)
1427 do_finish("timeout");
1429 /* don't print partial run, if interrupted by signal */
1433 /* one second in to the run, enable stats */
1437 for (j = 0; j < nthreads; j++) {
1438 s = get_submitter(j);
1439 this_done += s->done;
1440 this_call += s->calls;
1441 this_reap += s->reaps;
1443 if (this_call - calls) {
1444 rpc = (this_done - done) / (this_call - calls);
1445 ipc = (this_reap - reap) / (this_call - calls);
1448 file_depths(fdepths);
1449 iops = this_done - done;
1451 bw = iops * (bs / 1048576);
1453 bw = iops / (1048576 / bs);
1455 printf("IOPS=%luK, ", iops / 1000);
1457 printf("IOPS=%lu, ", iops);
1458 max_iops = max(max_iops, iops);
1460 printf("BW=%luMiB/s, ", bw);
1461 printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths);
1467 for (j = 0; j < nthreads; j++) {
1468 s = get_submitter(j);
1469 pthread_join(s->thread, &ret);
1475 printf("%d: Latency percentiles:\n", s->tid);
1476 for (i = 0, nr = 0; i < PLAT_NR; i++)
1478 show_clat_percentiles(s->plat, nr, 4);
1479 free(s->clock_batch);