10 #include <sys/types.h>
12 #include <sys/ioctl.h>
13 #include <sys/syscall.h>
14 #include <sys/resource.h>
24 #include "../arch/arch.h"
25 #include "../lib/types.h"
26 #include "../lib/roundup.h"
27 #include "../minmax.h"
28 #include "../os/linux/io_uring.h"
34 unsigned *ring_entries;
43 unsigned *ring_entries;
44 struct io_uring_cqe *cqes;
48 #define BATCH_SUBMIT 32
49 #define BATCH_COMPLETE 32
54 static unsigned sq_ring_mask, cq_ring_mask;
57 unsigned long max_blocks;
65 #define PLAT_VAL (1 << PLAT_BITS)
66 #define PLAT_GROUP_NR 29
67 #define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
73 struct io_sq_ring sq_ring;
74 struct io_uring_sqe *sqes;
75 struct io_cq_ring cq_ring;
85 unsigned long *clock_batch;
89 struct file files[MAX_FDS];
92 struct iovec iovecs[];
95 static struct submitter *submitter;
96 static volatile int finish;
98 static int depth = DEPTH;
99 static int batch_submit = BATCH_SUBMIT;
100 static int batch_complete = BATCH_COMPLETE;
102 static int polled = 1; /* use IO polling */
103 static int fixedbufs = 1; /* use fixed user buffers */
104 static int register_files = 1; /* use fixed files */
105 static int buffered = 0; /* use buffered IO, not O_DIRECT */
106 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
107 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
108 static int do_nop = 0; /* no-op SQ ring commands */
109 static int nthreads = 1;
110 static int stats = 0; /* generate IO stats */
111 static unsigned long tsc_rate;
113 static int vectored = 1;
115 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
116 80.0, 90.0, 95.0, 99.9, 99.5, 99.9, 99.95, 99.99 };
117 static int plist_len = 17;
119 static unsigned long cycles_to_nsec(unsigned long cycles)
126 val = cycles * 1000000000ULL;
127 return val / tsc_rate;
130 static unsigned long plat_idx_to_val(unsigned int idx)
132 unsigned int error_bits;
133 unsigned long k, base;
135 assert(idx < PLAT_NR);
137 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
138 * all bits of the sample as index */
139 if (idx < (PLAT_VAL << 1))
140 return cycles_to_nsec(idx);
142 /* Find the group and compute the minimum value of that group */
143 error_bits = (idx >> PLAT_BITS) - 1;
144 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
146 /* Find its bucket number of the group */
149 /* Return the mean of the range of the bucket */
150 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
153 unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
154 unsigned long **output,
155 unsigned long *maxv, unsigned long *minv)
157 unsigned long sum = 0;
158 unsigned int len = plist_len, i, j = 0;
159 unsigned long *ovals = NULL;
165 ovals = malloc(len * sizeof(*ovals));
170 * Calculate bucket values, note down max and min values
173 for (i = 0; i < PLAT_NR && !is_last; i++) {
175 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
176 assert(plist[j] <= 100.0);
178 ovals[j] = plat_idx_to_val(i);
179 if (ovals[j] < *minv)
181 if (ovals[j] > *maxv)
184 is_last = (j == len - 1) != 0;
193 fprintf(stderr, "error calculating latency percentiles\n");
199 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
200 unsigned int precision)
202 unsigned int divisor, len, i, j = 0;
203 unsigned long minv, maxv;
204 unsigned long *ovals;
205 int per_line, scale_down, time_width;
209 len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
216 printf(" percentiles (tsc ticks):\n |");
217 } else if (minv > 2000 && maxv > 99999) {
220 printf(" percentiles (usec):\n |");
224 printf(" percentiles (nsec):\n |");
227 time_width = max(5, (int) (log10(maxv / divisor) + 1));
228 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
229 precision, time_width);
230 /* fmt will be something like " %5.2fth=[%4llu]%c" */
231 per_line = (80 - 7) / (precision + 10 + time_width);
233 for (j = 0; j < len; j++) {
235 if (j != 0 && (j % per_line) == 0)
238 /* end of the list */
239 is_last = (j == len - 1) != 0;
241 for (i = 0; i < scale_down; i++)
242 ovals[j] = (ovals[j] + 999) / 1000;
244 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
249 if ((j % per_line) == per_line - 1) /* for formatting */
257 static unsigned int plat_val_to_idx(unsigned long val)
259 unsigned int msb, error_bits, base, offset, idx;
261 /* Find MSB starting from bit 0 */
265 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
268 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
269 * all bits of the sample as index
271 if (msb <= PLAT_BITS)
274 /* Compute the number of error bits to discard*/
275 error_bits = msb - PLAT_BITS;
277 /* Compute the number of buckets before the group */
278 base = (error_bits + 1) << PLAT_BITS;
281 * Discard the error bits and apply the mask to find the
282 * index for the buckets in the group
284 offset = (PLAT_VAL - 1) & (val >> error_bits);
286 /* Make sure the index does not exceed (array size - 1) */
287 idx = (base + offset) < (PLAT_NR - 1) ?
288 (base + offset) : (PLAT_NR - 1);
293 static void add_stat(struct submitter *s, int clock_index, int nr)
295 #ifdef ARCH_HAVE_CPU_CLOCK
296 unsigned long cycles;
299 cycles = get_cpu_clock();
300 cycles -= s->clock_batch[clock_index];
301 pidx = plat_val_to_idx(cycles);
306 static int io_uring_register_buffers(struct submitter *s)
311 return syscall(__NR_io_uring_register, s->ring_fd,
312 IORING_REGISTER_BUFFERS, s->iovecs, depth);
315 static int io_uring_register_files(struct submitter *s)
322 s->fds = calloc(s->nr_files, sizeof(__s32));
323 for (i = 0; i < s->nr_files; i++) {
324 s->fds[i] = s->files[i].real_fd;
325 s->files[i].fixed_fd = i;
328 return syscall(__NR_io_uring_register, s->ring_fd,
329 IORING_REGISTER_FILES, s->fds, s->nr_files);
332 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
334 return syscall(__NR_io_uring_setup, entries, p);
337 static void io_uring_probe(int fd)
339 struct io_uring_probe *p;
342 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
346 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
347 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
351 if (IORING_OP_READ > p->ops_len)
354 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
360 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
361 unsigned int min_complete, unsigned int flags)
363 return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete,
367 #ifndef CONFIG_HAVE_GETTID
368 static int gettid(void)
370 return syscall(__NR_gettid);
374 static unsigned file_depth(struct submitter *s)
376 return (depth + s->nr_files - 1) / s->nr_files;
379 static void init_io(struct submitter *s, unsigned index)
381 struct io_uring_sqe *sqe = &s->sqes[index];
382 unsigned long offset;
387 sqe->opcode = IORING_OP_NOP;
391 if (s->nr_files == 1) {
394 f = &s->files[s->cur_file];
395 if (f->pending_ios >= file_depth(s)) {
397 if (s->cur_file == s->nr_files)
399 f = &s->files[s->cur_file];
405 offset = (r % (f->max_blocks - 1)) * bs;
407 if (register_files) {
408 sqe->flags = IOSQE_FIXED_FILE;
409 sqe->fd = f->fixed_fd;
412 sqe->fd = f->real_fd;
415 sqe->opcode = IORING_OP_READ_FIXED;
416 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
418 sqe->buf_index = index;
419 } else if (!vectored) {
420 sqe->opcode = IORING_OP_READ;
421 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
425 sqe->opcode = IORING_OP_READV;
426 sqe->addr = (unsigned long) &s->iovecs[index];
432 sqe->user_data = (unsigned long) f->fileno;
434 sqe->user_data |= ((unsigned long)s->clock_index << 32);
437 static int prep_more_ios(struct submitter *s, int max_ios)
439 struct io_sq_ring *ring = &s->sq_ring;
440 unsigned index, tail, next_tail, prepped = 0;
442 next_tail = tail = *ring->tail;
445 if (next_tail == atomic_load_acquire(ring->head))
448 index = tail & sq_ring_mask;
450 ring->array[index] = index;
453 } while (prepped < max_ios);
456 atomic_store_release(ring->tail, tail);
460 static int get_file_size(struct file *f)
464 if (fstat(f->real_fd, &st) < 0)
466 if (S_ISBLK(st.st_mode)) {
467 unsigned long long bytes;
469 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
472 f->max_blocks = bytes / bs;
474 } else if (S_ISREG(st.st_mode)) {
475 f->max_blocks = st.st_size / bs;
482 static int reap_events(struct submitter *s)
484 struct io_cq_ring *ring = &s->cq_ring;
485 struct io_uring_cqe *cqe;
486 unsigned head, reaped = 0;
487 int last_idx = -1, stat_nr = 0;
494 if (head == atomic_load_acquire(ring->tail))
496 cqe = &ring->cqes[head & cq_ring_mask];
498 int fileno = cqe->user_data & 0xffffffff;
500 f = &s->files[fileno];
502 if (cqe->res != bs) {
503 printf("io: unexpected ret=%d\n", cqe->res);
504 if (polled && cqe->res == -EOPNOTSUPP)
505 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
510 int clock_index = cqe->user_data >> 32;
512 if (last_idx != clock_index) {
513 if (last_idx != -1) {
514 add_stat(s, last_idx, stat_nr);
517 last_idx = clock_index;
520 add_stat(s, clock_index, 1);
527 add_stat(s, last_idx, stat_nr);
530 s->inflight -= reaped;
531 atomic_store_release(ring->head, head);
536 static void *submitter_fn(void *data)
538 struct submitter *s = data;
539 struct io_sq_ring *ring = &s->sq_ring;
540 int i, ret, prepped, nr_batch;
543 printf("submitter=%d\n", s->tid);
545 srand48(pthread_self());
547 for (i = 0; i < MAX_FDS; i++)
548 s->files[i].fileno = i;
551 nr_batch = roundup_pow2(depth / batch_submit);
552 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
555 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
557 s->clock_batch = NULL;
564 int to_wait, to_submit, this_reap, to_prep;
565 unsigned ring_flags = 0;
567 if (!prepped && s->inflight < depth) {
568 to_prep = min(depth - s->inflight, batch_submit);
569 prepped = prep_more_ios(s, to_prep);
570 #ifdef ARCH_HAVE_CPU_CLOCK
571 if (prepped && stats) {
572 s->clock_batch[s->clock_index] = get_cpu_clock();
573 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
577 s->inflight += prepped;
581 if (to_submit && (s->inflight + to_submit <= depth))
584 to_wait = min(s->inflight + to_submit, batch_complete);
587 * Only need to call io_uring_enter if we're not using SQ thread
588 * poll, or if IORING_SQ_NEED_WAKEUP is set.
591 ring_flags = atomic_load_acquire(ring->flags);
592 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
596 flags = IORING_ENTER_GETEVENTS;
597 if (ring_flags & IORING_SQ_NEED_WAKEUP)
598 flags |= IORING_ENTER_SQ_WAKEUP;
599 ret = io_uring_enter(s, to_submit, to_wait, flags);
602 /* for SQPOLL, we submitted it all effectively */
607 * For non SQ thread poll, we already got the events we needed
608 * through the io_uring_enter() above. For SQ thread poll, we
609 * need to loop here until we find enough events.
620 } while (sq_thread_poll && this_reap < to_wait);
621 s->reaps += this_reap;
629 } else if (ret < to_submit) {
630 int diff = to_submit - ret;
639 } else if (ret < 0) {
640 if (errno == EAGAIN) {
648 printf("io_submit: %s\n", strerror(errno));
651 } while (!s->finish);
657 static struct submitter *get_submitter(int offset)
663 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
667 static void sig_int(int sig)
671 printf("Exiting on signal %d\n", sig);
672 for (j = 0; j < nthreads; j++) {
673 struct submitter *s = get_submitter(j);
679 static void arm_sig_int(void)
681 struct sigaction act;
683 memset(&act, 0, sizeof(act));
684 act.sa_handler = sig_int;
685 act.sa_flags = SA_RESTART;
686 sigaction(SIGINT, &act, NULL);
688 /* Windows uses SIGBREAK as a quit signal from other applications */
690 sigaction(SIGBREAK, &act, NULL);
694 static int setup_ring(struct submitter *s)
696 struct io_sq_ring *sring = &s->sq_ring;
697 struct io_cq_ring *cring = &s->cq_ring;
698 struct io_uring_params p;
702 memset(&p, 0, sizeof(p));
704 if (polled && !do_nop)
705 p.flags |= IORING_SETUP_IOPOLL;
706 if (sq_thread_poll) {
707 p.flags |= IORING_SETUP_SQPOLL;
708 if (sq_thread_cpu != -1) {
709 p.flags |= IORING_SETUP_SQ_AFF;
710 p.sq_thread_cpu = sq_thread_cpu;
714 fd = io_uring_setup(depth, &p);
716 perror("io_uring_setup");
726 rlim.rlim_cur = RLIM_INFINITY;
727 rlim.rlim_max = RLIM_INFINITY;
728 /* ignore potential error, not needed on newer kernels */
729 setrlimit(RLIMIT_MEMLOCK, &rlim);
731 ret = io_uring_register_buffers(s);
733 perror("io_uring_register_buffers");
738 if (register_files) {
739 ret = io_uring_register_files(s);
741 perror("io_uring_register_files");
746 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
747 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
749 printf("sq_ring ptr = 0x%p\n", ptr);
750 sring->head = ptr + p.sq_off.head;
751 sring->tail = ptr + p.sq_off.tail;
752 sring->ring_mask = ptr + p.sq_off.ring_mask;
753 sring->ring_entries = ptr + p.sq_off.ring_entries;
754 sring->flags = ptr + p.sq_off.flags;
755 sring->array = ptr + p.sq_off.array;
756 sq_ring_mask = *sring->ring_mask;
758 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
759 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
761 printf("sqes ptr = 0x%p\n", s->sqes);
763 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
764 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
766 printf("cq_ring ptr = 0x%p\n", ptr);
767 cring->head = ptr + p.cq_off.head;
768 cring->tail = ptr + p.cq_off.tail;
769 cring->ring_mask = ptr + p.cq_off.ring_mask;
770 cring->ring_entries = ptr + p.cq_off.ring_entries;
771 cring->cqes = ptr + p.cq_off.cqes;
772 cq_ring_mask = *cring->ring_mask;
776 static void file_depths(char *buf)
784 for (j = 0; j < nthreads; j++) {
785 struct submitter *s = get_submitter(j);
787 for (i = 0; i < s->nr_files; i++) {
788 struct file *f = &s->files[i];
791 p += sprintf(p, " %d", f->pending_ios);
793 p += sprintf(p, "%d", f->pending_ios);
799 static void usage(char *argv, int status)
801 printf("%s [options] -- [filenames]\n"
802 " -d <int> : IO Depth, default %d\n"
803 " -s <int> : Batch submit, default %d\n"
804 " -c <int> : Batch complete, default %d\n"
805 " -b <int> : Block size, default %d\n"
806 " -p <bool> : Polled IO, default %d\n"
807 " -B <bool> : Fixed buffers, default %d\n"
808 " -F <bool> : Register files, default %d\n"
809 " -n <int> : Number of threads, default %d\n"
810 " -O <bool> : Use O_DIRECT, default %d\n"
811 " -N <bool> : Perform just no-op requests, default %d\n"
812 " -t <bool> : Track IO latencies, default %d\n"
813 " -T <int> : TSC rate in HZ\n",
814 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
815 fixedbufs, register_files, nthreads, !buffered, do_nop, stats);
819 int main(int argc, char *argv[])
822 unsigned long done, calls, reap;
823 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
828 if (!do_nop && argc < 2)
831 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:h?")) != -1) {
834 depth = atoi(optarg);
837 batch_submit = atoi(optarg);
842 batch_complete = atoi(optarg);
850 polled = !!atoi(optarg);
853 fixedbufs = !!atoi(optarg);
856 register_files = !!atoi(optarg);
859 nthreads = atoi(optarg);
861 printf("Threads must be non-zero\n");
866 do_nop = !!atoi(optarg);
869 buffered = !atoi(optarg);
872 #ifndef ARCH_HAVE_CPU_CLOCK
873 fprintf(stderr, "Stats not supported on this CPU\n");
876 stats = !!atoi(optarg);
879 #ifndef ARCH_HAVE_CPU_CLOCK
880 fprintf(stderr, "Stats not supported on this CPU\n");
883 tsc_rate = strtoul(optarg, NULL, 10);
893 if (batch_complete > depth)
894 batch_complete = depth;
895 if (batch_submit > depth)
896 batch_submit = depth;
898 submitter = calloc(nthreads, sizeof(*submitter) +
899 depth * sizeof(struct iovec));
900 for (j = 0; j < nthreads; j++) {
901 s = get_submitter(j);
903 s->done = s->calls = s->reaps = 0;
906 flags = O_RDONLY | O_NOATIME;
915 printf("No files specified\n");
918 threads_per_f = nthreads / nfiles;
919 /* make sure each thread gets assigned files */
920 if (threads_per_f == 0) {
923 threads_rem = nthreads - threads_per_f * nfiles;
926 while (!do_nop && i < argc) {
929 memset(&f, 0, sizeof(f));
931 fd = open(argv[i], flags);
937 if (get_file_size(&f)) {
938 printf("failed getting size of device/file\n");
941 if (f.max_blocks <= 1) {
942 printf("Zero file/device size?\n");
947 limit = threads_per_f;
948 limit += threads_rem > 0 ? 1 : 0;
949 for (k = 0; k < limit; k++) {
950 s = get_submitter((j + k) % nthreads);
952 if (s->nr_files == MAX_FDS) {
953 printf("Max number of files (%d) reached\n", MAX_FDS);
957 memcpy(&s->files[s->nr_files], &f, sizeof(f));
959 printf("Added file %s (submitter %d)\n", argv[i], s->index);
969 for (j = 0; j < nthreads; j++) {
970 s = get_submitter(j);
971 for (i = 0; i < depth; i++) {
974 if (posix_memalign(&buf, bs, bs)) {
975 printf("failed alloc\n");
978 s->iovecs[i].iov_base = buf;
979 s->iovecs[i].iov_len = bs;
983 for (j = 0; j < nthreads; j++) {
984 s = get_submitter(j);
988 printf("ring setup failed: %s, %d\n", strerror(errno), err);
992 s = get_submitter(0);
993 printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d", polled, fixedbufs, register_files, buffered);
994 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
996 for (j = 0; j < nthreads; j++) {
997 s = get_submitter(j);
998 pthread_create(&s->thread, NULL, submitter_fn, s);
1001 fdepths = malloc(8 * s->nr_files * nthreads);
1002 reap = calls = done = 0;
1004 unsigned long this_done = 0;
1005 unsigned long this_reap = 0;
1006 unsigned long this_call = 0;
1007 unsigned long rpc = 0, ipc = 0;
1008 unsigned long iops, bw;
1011 for (j = 0; j < nthreads; j++) {
1012 this_done += s->done;
1013 this_call += s->calls;
1014 this_reap += s->reaps;
1016 if (this_call - calls) {
1017 rpc = (this_done - done) / (this_call - calls);
1018 ipc = (this_reap - reap) / (this_call - calls);
1021 file_depths(fdepths);
1022 iops = this_done - done;
1024 bw = iops * (bs / 1048576);
1026 bw = iops / (1048576 / bs);
1027 printf("IOPS=%lu, ", iops);
1029 printf("BW=%luMiB/s, ", bw);
1030 printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths);
1036 for (j = 0; j < nthreads; j++) {
1037 s = get_submitter(j);
1038 pthread_join(s->thread, &ret);
1044 printf("%d: Latency percentiles:\n", s->tid);
1045 for (i = 0, nr = 0; i < PLAT_NR; i++)
1047 show_clat_percentiles(s->plat, nr, 4);
1048 free(s->clock_batch);