18 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/syscall.h>
22 #include <sys/resource.h>
32 #include "../arch/arch.h"
33 #include "../lib/types.h"
34 #include "../lib/roundup.h"
35 #include "../lib/rand.h"
36 #include "../minmax.h"
37 #include "../os/linux/io_uring.h"
43 unsigned *ring_entries;
52 unsigned *ring_entries;
53 struct io_uring_cqe *cqes;
57 #define BATCH_SUBMIT 32
58 #define BATCH_COMPLETE 32
63 static unsigned sq_ring_mask, cq_ring_mask;
66 unsigned long max_blocks;
67 unsigned long max_size;
68 unsigned long cur_off;
76 #define PLAT_VAL (1 << PLAT_BITS)
77 #define PLAT_GROUP_NR 29
78 #define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
85 struct io_sq_ring sq_ring;
86 struct io_uring_sqe *sqes;
87 struct io_cq_ring cq_ring;
97 struct taus258_state rand_state;
99 unsigned long *clock_batch;
104 io_context_t aio_ctx;
108 const char *filename;
110 struct file files[MAX_FDS];
113 struct iovec iovecs[];
116 static struct submitter *submitter;
117 static volatile int finish;
118 static int stats_running;
119 static unsigned long max_iops;
120 static long page_size;
122 static int depth = DEPTH;
123 static int batch_submit = BATCH_SUBMIT;
124 static int batch_complete = BATCH_COMPLETE;
126 static int polled = 1; /* use IO polling */
127 static int fixedbufs = 1; /* use fixed user buffers */
128 static int dma_map; /* pre-map DMA buffers */
129 static int register_files = 1; /* use fixed files */
130 static int buffered = 0; /* use buffered IO, not O_DIRECT */
131 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
132 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
133 static int do_nop = 0; /* no-op SQ ring commands */
134 static int nthreads = 1;
135 static int stats = 0; /* generate IO stats */
136 static int aio = 0; /* use libaio */
137 static int runtime = 0; /* runtime */
138 static int random_io = 1; /* random or sequential IO */
139 static int register_ring = 1; /* register ring */
140 static int use_sync = 0; /* use preadv2 */
141 static int numa_placement = 0; /* set to node of device */
143 static unsigned long tsc_rate;
145 #define TSC_RATE_FILE "tsc-rate"
147 static int vectored = 1;
149 static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
150 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
151 static int plist_len = 17;
153 #ifndef IORING_REGISTER_MAP_BUFFERS
154 #define IORING_REGISTER_MAP_BUFFERS 22
155 struct io_uring_map_buffers {
164 static unsigned long cycles_to_nsec(unsigned long cycles)
171 val = cycles * 1000000000ULL;
172 return val / tsc_rate;
175 static unsigned long plat_idx_to_val(unsigned int idx)
177 unsigned int error_bits;
178 unsigned long k, base;
180 assert(idx < PLAT_NR);
182 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
183 * all bits of the sample as index */
184 if (idx < (PLAT_VAL << 1))
185 return cycles_to_nsec(idx);
187 /* Find the group and compute the minimum value of that group */
188 error_bits = (idx >> PLAT_BITS) - 1;
189 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
191 /* Find its bucket number of the group */
194 /* Return the mean of the range of the bucket */
195 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
198 unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
199 unsigned long **output,
200 unsigned long *maxv, unsigned long *minv)
202 unsigned long sum = 0;
203 unsigned int len = plist_len, i, j = 0;
204 unsigned long *ovals = NULL;
210 ovals = malloc(len * sizeof(*ovals));
215 * Calculate bucket values, note down max and min values
218 for (i = 0; i < PLAT_NR && !is_last; i++) {
220 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
221 assert(plist[j] <= 100.0);
223 ovals[j] = plat_idx_to_val(i);
224 if (ovals[j] < *minv)
226 if (ovals[j] > *maxv)
229 is_last = (j == len - 1) != 0;
238 fprintf(stderr, "error calculating latency percentiles\n");
244 static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
245 unsigned int precision)
247 unsigned int divisor, len, i, j = 0;
248 unsigned long minv, maxv;
249 unsigned long *ovals;
250 int per_line, scale_down, time_width;
254 len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
261 printf(" percentiles (tsc ticks):\n |");
262 } else if (minv > 2000 && maxv > 99999) {
265 printf(" percentiles (usec):\n |");
269 printf(" percentiles (nsec):\n |");
272 time_width = max(5, (int) (log10(maxv / divisor) + 1));
273 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
274 precision, time_width);
275 /* fmt will be something like " %5.2fth=[%4llu]%c" */
276 per_line = (80 - 7) / (precision + 10 + time_width);
278 for (j = 0; j < len; j++) {
280 if (j != 0 && (j % per_line) == 0)
283 /* end of the list */
284 is_last = (j == len - 1) != 0;
286 for (i = 0; i < scale_down; i++)
287 ovals[j] = (ovals[j] + 999) / 1000;
289 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
294 if ((j % per_line) == per_line - 1) /* for formatting */
302 #ifdef ARCH_HAVE_CPU_CLOCK
303 static unsigned int plat_val_to_idx(unsigned long val)
305 unsigned int msb, error_bits, base, offset, idx;
307 /* Find MSB starting from bit 0 */
311 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
314 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
315 * all bits of the sample as index
317 if (msb <= PLAT_BITS)
320 /* Compute the number of error bits to discard*/
321 error_bits = msb - PLAT_BITS;
323 /* Compute the number of buckets before the group */
324 base = (error_bits + 1) << PLAT_BITS;
327 * Discard the error bits and apply the mask to find the
328 * index for the buckets in the group
330 offset = (PLAT_VAL - 1) & (val >> error_bits);
332 /* Make sure the index does not exceed (array size - 1) */
333 idx = (base + offset) < (PLAT_NR - 1) ?
334 (base + offset) : (PLAT_NR - 1);
340 static void add_stat(struct submitter *s, int clock_index, int nr)
342 #ifdef ARCH_HAVE_CPU_CLOCK
343 unsigned long cycles;
346 if (!s->finish && clock_index) {
347 cycles = get_cpu_clock();
348 cycles -= s->clock_batch[clock_index];
349 pidx = plat_val_to_idx(cycles);
355 static int io_uring_map_buffers(struct submitter *s)
357 struct io_uring_map_buffers map = {
358 .fd = s->files[0].real_fd,
365 fprintf(stdout, "Mapping buffers may not work with multiple files\n");
367 return syscall(__NR_io_uring_register, s->ring_fd,
368 IORING_REGISTER_MAP_BUFFERS, &map, 1);
371 static int io_uring_register_buffers(struct submitter *s)
376 return syscall(__NR_io_uring_register, s->ring_fd,
377 IORING_REGISTER_BUFFERS, s->iovecs, roundup_pow2(depth));
380 static int io_uring_register_files(struct submitter *s)
387 s->fds = calloc(s->nr_files, sizeof(__s32));
388 for (i = 0; i < s->nr_files; i++) {
389 s->fds[i] = s->files[i].real_fd;
390 s->files[i].fixed_fd = i;
393 return syscall(__NR_io_uring_register, s->ring_fd,
394 IORING_REGISTER_FILES, s->fds, s->nr_files);
397 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
400 * Clamp CQ ring size at our SQ ring size, we don't need more entries
403 p->flags |= IORING_SETUP_CQSIZE;
404 p->cq_entries = entries;
406 return syscall(__NR_io_uring_setup, entries, p);
409 static void io_uring_probe(int fd)
411 struct io_uring_probe *p;
414 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
418 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
419 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
423 if (IORING_OP_READ > p->ops_len)
426 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
432 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
433 unsigned int min_complete, unsigned int flags)
436 flags |= IORING_ENTER_REGISTERED_RING;
437 #ifdef FIO_ARCH_HAS_SYSCALL
438 return __do_syscall6(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
439 min_complete, flags, NULL, 0);
441 return syscall(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
442 min_complete, flags, NULL, 0);
446 #ifndef CONFIG_HAVE_GETTID
447 static int gettid(void)
449 return syscall(__NR_gettid);
453 static unsigned file_depth(struct submitter *s)
455 return (depth + s->nr_files - 1) / s->nr_files;
458 static void init_io(struct submitter *s, unsigned index)
460 struct io_uring_sqe *sqe = &s->sqes[index];
461 unsigned long offset;
466 sqe->opcode = IORING_OP_NOP;
470 if (s->nr_files == 1) {
473 f = &s->files[s->cur_file];
474 if (f->pending_ios >= file_depth(s)) {
476 if (s->cur_file == s->nr_files)
478 f = &s->files[s->cur_file];
484 r = __rand64(&s->rand_state);
485 offset = (r % (f->max_blocks - 1)) * bs;
489 if (f->cur_off + bs > f->max_size)
493 if (register_files) {
494 sqe->flags = IOSQE_FIXED_FILE;
495 sqe->fd = f->fixed_fd;
498 sqe->fd = f->real_fd;
501 sqe->opcode = IORING_OP_READ_FIXED;
502 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
504 sqe->buf_index = index;
505 } else if (!vectored) {
506 sqe->opcode = IORING_OP_READ;
507 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
511 sqe->opcode = IORING_OP_READV;
512 sqe->addr = (unsigned long) &s->iovecs[index];
518 sqe->user_data = (unsigned long) f->fileno;
519 if (stats && stats_running)
520 sqe->user_data |= ((uint64_t)s->clock_index << 32);
523 static int prep_more_ios_uring(struct submitter *s, int max_ios)
525 struct io_sq_ring *ring = &s->sq_ring;
526 unsigned index, tail, next_tail, prepped = 0;
528 next_tail = tail = *ring->tail;
531 if (next_tail == atomic_load_acquire(ring->head))
534 index = tail & sq_ring_mask;
536 ring->array[index] = index;
539 } while (prepped < max_ios);
542 atomic_store_release(ring->tail, tail);
546 static int get_file_size(struct file *f)
550 if (fstat(f->real_fd, &st) < 0)
552 if (S_ISBLK(st.st_mode)) {
553 unsigned long long bytes;
555 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
558 f->max_blocks = bytes / bs;
561 } else if (S_ISREG(st.st_mode)) {
562 f->max_blocks = st.st_size / bs;
563 f->max_size = st.st_size;
570 static int reap_events_uring(struct submitter *s)
572 struct io_cq_ring *ring = &s->cq_ring;
573 struct io_uring_cqe *cqe;
574 unsigned head, reaped = 0;
575 int last_idx = -1, stat_nr = 0;
582 if (head == atomic_load_acquire(ring->tail))
584 cqe = &ring->cqes[head & cq_ring_mask];
586 int fileno = cqe->user_data & 0xffffffff;
588 f = &s->files[fileno];
590 if (cqe->res != bs) {
591 printf("io: unexpected ret=%d\n", cqe->res);
592 if (polled && cqe->res == -EOPNOTSUPP)
593 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
598 int clock_index = cqe->user_data >> 32;
600 if (last_idx != clock_index) {
601 if (last_idx != -1) {
602 add_stat(s, last_idx, stat_nr);
605 last_idx = clock_index;
614 add_stat(s, last_idx, stat_nr);
617 s->inflight -= reaped;
618 atomic_store_release(ring->head, head);
623 static void set_affinity(struct submitter *s)
625 #ifdef CONFIG_LIBNUMA
626 struct bitmask *mask;
628 if (s->numa_node == -1)
631 numa_set_preferred(s->numa_node);
633 mask = numa_allocate_cpumask();
634 numa_node_to_cpus(s->numa_node, mask);
635 numa_sched_setaffinity(s->tid, mask);
639 static int detect_node(struct submitter *s, const char *name)
641 #ifdef CONFIG_LIBNUMA
642 const char *base = basename(name);
646 sprintf(str, "/sys/block/%s/device/numa_node", base);
647 fd = open(str, O_RDONLY);
651 ret = read(fd, str, sizeof(str));
665 static int setup_aio(struct submitter *s)
669 fprintf(stderr, "aio does not support polled IO\n");
672 if (sq_thread_poll) {
673 fprintf(stderr, "aio does not support SQPOLL IO\n");
677 fprintf(stderr, "aio does not support polled IO\n");
680 if (fixedbufs || register_files) {
681 fprintf(stderr, "aio does not support registered files or buffers\n");
682 fixedbufs = register_files = 0;
685 return io_queue_init(roundup_pow2(depth), &s->aio_ctx);
687 fprintf(stderr, "Legacy AIO not available on this system/build\n");
693 static int setup_ring(struct submitter *s)
695 struct io_sq_ring *sring = &s->sq_ring;
696 struct io_cq_ring *cring = &s->cq_ring;
697 struct io_uring_params p;
701 memset(&p, 0, sizeof(p));
703 if (polled && !do_nop)
704 p.flags |= IORING_SETUP_IOPOLL;
705 if (sq_thread_poll) {
706 p.flags |= IORING_SETUP_SQPOLL;
707 if (sq_thread_cpu != -1) {
708 p.flags |= IORING_SETUP_SQ_AFF;
709 p.sq_thread_cpu = sq_thread_cpu;
713 fd = io_uring_setup(depth, &p);
715 perror("io_uring_setup");
718 s->ring_fd = s->enter_ring_fd = fd;
725 rlim.rlim_cur = RLIM_INFINITY;
726 rlim.rlim_max = RLIM_INFINITY;
727 /* ignore potential error, not needed on newer kernels */
728 setrlimit(RLIMIT_MEMLOCK, &rlim);
730 ret = io_uring_register_buffers(s);
732 perror("io_uring_register_buffers");
737 ret = io_uring_map_buffers(s);
739 perror("io_uring_map_buffers");
745 if (register_files) {
746 ret = io_uring_register_files(s);
748 perror("io_uring_register_files");
753 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
754 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
756 sring->head = ptr + p.sq_off.head;
757 sring->tail = ptr + p.sq_off.tail;
758 sring->ring_mask = ptr + p.sq_off.ring_mask;
759 sring->ring_entries = ptr + p.sq_off.ring_entries;
760 sring->flags = ptr + p.sq_off.flags;
761 sring->array = ptr + p.sq_off.array;
762 sq_ring_mask = *sring->ring_mask;
764 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
765 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
768 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
769 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
771 cring->head = ptr + p.cq_off.head;
772 cring->tail = ptr + p.cq_off.tail;
773 cring->ring_mask = ptr + p.cq_off.ring_mask;
774 cring->ring_entries = ptr + p.cq_off.ring_entries;
775 cring->cqes = ptr + p.cq_off.cqes;
776 cq_ring_mask = *cring->ring_mask;
780 static void *allocate_mem(struct submitter *s, int size)
784 #ifdef CONFIG_LIBNUMA
785 if (s->numa_node != -1)
786 return numa_alloc_onnode(size, s->numa_node);
789 if (posix_memalign(&buf, page_size, bs)) {
790 printf("failed alloc\n");
797 static int submitter_init(struct submitter *s)
799 int i, nr_batch, err;
800 static int init_printed;
804 printf("submitter=%d, tid=%d, file=%s, node=%d\n", s->index, s->tid,
805 s->filename, s->numa_node);
809 __init_rand64(&s->rand_state, pthread_self());
810 srand48(pthread_self());
812 for (i = 0; i < MAX_FDS; i++)
813 s->files[i].fileno = i;
815 for (i = 0; i < roundup_pow2(depth); i++) {
818 buf = allocate_mem(s, bs);
821 s->iovecs[i].iov_base = buf;
822 s->iovecs[i].iov_len = bs;
826 sprintf(buf, "Engine=preadv2\n");
830 sprintf(buf, "Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
832 sprintf(buf, "Engine=aio\n");
836 printf("queue setup failed: %s, %d\n", strerror(errno), err);
841 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
847 nr_batch = roundup_pow2(depth / batch_submit);
850 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
853 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
855 s->clock_batch = NULL;
864 static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
873 while (index < max_ios) {
874 struct iocb *iocb = &iocbs[index];
876 if (s->nr_files == 1) {
879 f = &s->files[s->cur_file];
880 if (f->pending_ios >= file_depth(s)) {
882 if (s->cur_file == s->nr_files)
884 f = &s->files[s->cur_file];
890 offset = (r % (f->max_blocks - 1)) * bs;
891 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
892 s->iovecs[index].iov_len, offset);
895 if (stats && stats_running)
896 data |= (((uint64_t) s->clock_index) << 32);
897 iocb->data = (void *) (uintptr_t) data;
903 static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
905 int last_idx = -1, stat_nr = 0;
909 uint64_t data = (uintptr_t) events[reaped].data;
910 struct file *f = &s->files[data & 0xffffffff];
913 if (events[reaped].res != bs) {
914 printf("io: unexpected ret=%ld\n", events[reaped].res);
918 int clock_index = data >> 32;
920 if (last_idx != clock_index) {
921 if (last_idx != -1) {
922 add_stat(s, last_idx, stat_nr);
925 last_idx = clock_index;
934 add_stat(s, last_idx, stat_nr);
936 s->inflight -= reaped;
941 static void *submitter_aio_fn(void *data)
943 struct submitter *s = data;
945 struct iocb **iocbsptr;
947 struct io_event *events;
948 #ifdef ARCH_HAVE_CPU_CLOCK
949 int nr_batch = submitter_init(s);
954 iocbsptr = calloc(depth, sizeof(struct iocb *));
955 iocbs = calloc(depth, sizeof(struct iocb));
956 events = calloc(depth, sizeof(struct io_event));
958 for (i = 0; i < depth; i++)
959 iocbsptr[i] = &iocbs[i];
963 int to_wait, to_submit, to_prep;
965 if (!prepped && s->inflight < depth) {
966 to_prep = min(depth - s->inflight, batch_submit);
967 prepped = prep_more_ios_aio(s, to_prep, iocbs);
968 #ifdef ARCH_HAVE_CPU_CLOCK
969 if (prepped && stats) {
970 s->clock_batch[s->clock_index] = get_cpu_clock();
971 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
975 s->inflight += prepped;
978 if (to_submit && (s->inflight + to_submit <= depth))
981 to_wait = min(s->inflight + to_submit, batch_complete);
983 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
988 } else if (ret != to_submit) {
989 printf("submitted %d, wanted %d\n", ret, to_submit);
998 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
1000 perror("io_getevents");
1002 } else if (r != to_wait) {
1003 printf("r=%d, wait=%d\n", r, to_wait);
1006 r = reap_events_aio(s, events, r);
1010 } while (!s->finish);
1020 static void io_uring_unregister_ring(struct submitter *s)
1022 struct io_uring_rsrc_update up = {
1023 .offset = s->enter_ring_fd,
1026 syscall(__NR_io_uring_register, s->ring_fd, IORING_UNREGISTER_RING_FDS,
1030 static int io_uring_register_ring(struct submitter *s)
1032 struct io_uring_rsrc_update up = {
1038 ret = syscall(__NR_io_uring_register, s->ring_fd,
1039 IORING_REGISTER_RING_FDS, &up, 1);
1041 s->enter_ring_fd = up.offset;
1048 static void *submitter_uring_fn(void *data)
1050 struct submitter *s = data;
1051 struct io_sq_ring *ring = &s->sq_ring;
1053 #ifdef ARCH_HAVE_CPU_CLOCK
1054 int nr_batch = submitter_init(s);
1060 io_uring_register_ring(s);
1064 int to_wait, to_submit, this_reap, to_prep;
1065 unsigned ring_flags = 0;
1067 if (!prepped && s->inflight < depth) {
1068 to_prep = min(depth - s->inflight, batch_submit);
1069 prepped = prep_more_ios_uring(s, to_prep);
1070 #ifdef ARCH_HAVE_CPU_CLOCK
1071 if (prepped && stats) {
1072 s->clock_batch[s->clock_index] = get_cpu_clock();
1073 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1077 s->inflight += prepped;
1079 to_submit = prepped;
1081 if (to_submit && (s->inflight + to_submit <= depth))
1084 to_wait = min(s->inflight + to_submit, batch_complete);
1087 * Only need to call io_uring_enter if we're not using SQ thread
1088 * poll, or if IORING_SQ_NEED_WAKEUP is set.
1091 ring_flags = atomic_load_acquire(ring->flags);
1092 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
1096 flags = IORING_ENTER_GETEVENTS;
1097 if (ring_flags & IORING_SQ_NEED_WAKEUP)
1098 flags |= IORING_ENTER_SQ_WAKEUP;
1099 ret = io_uring_enter(s, to_submit, to_wait, flags);
1102 /* for SQPOLL, we submitted it all effectively */
1107 * For non SQ thread poll, we already got the events we needed
1108 * through the io_uring_enter() above. For SQ thread poll, we
1109 * need to loop here until we find enough events.
1115 r = reap_events_uring(s);
1121 } while (sq_thread_poll && this_reap < to_wait);
1122 s->reaps += this_reap;
1130 } else if (ret < to_submit) {
1131 int diff = to_submit - ret;
1140 } else if (ret < 0) {
1141 if (errno == EAGAIN) {
1149 printf("io_submit: %s\n", strerror(errno));
1152 } while (!s->finish);
1155 io_uring_unregister_ring(s);
1161 #ifdef CONFIG_PWRITEV2
1162 static void *submitter_sync_fn(void *data)
1164 struct submitter *s = data;
1174 if (s->nr_files == 1) {
1177 f = &s->files[s->cur_file];
1178 if (f->pending_ios >= file_depth(s)) {
1180 if (s->cur_file == s->nr_files)
1182 f = &s->files[s->cur_file];
1188 r = __rand64(&s->rand_state);
1189 offset = (r % (f->max_blocks - 1)) * bs;
1191 offset = f->cur_off;
1193 if (f->cur_off + bs > f->max_size)
1197 #ifdef ARCH_HAVE_CPU_CLOCK
1199 s->clock_batch[s->clock_index] = get_cpu_clock();
1206 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, RWF_HIPRI);
1208 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, 0);
1213 } else if (ret != bs) {
1221 add_stat(s, s->clock_index, 1);
1222 } while (!s->finish);
1228 static void *submitter_sync_fn(void *data)
1235 static struct submitter *get_submitter(int offset)
1241 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
1245 static void do_finish(const char *reason)
1249 printf("Exiting on %s\n", reason);
1250 for (j = 0; j < nthreads; j++) {
1251 struct submitter *s = get_submitter(j);
1254 if (max_iops > 1000000) {
1255 double miops = (double) max_iops / 1000000.0;
1256 printf("Maximum IOPS=%.2fM\n", miops);
1257 } else if (max_iops > 100000) {
1258 double kiops = (double) max_iops / 1000.0;
1259 printf("Maximum IOPS=%.2fK\n", kiops);
1261 printf("Maximum IOPS=%lu\n", max_iops);
1266 static void sig_int(int sig)
1268 do_finish("signal");
1271 static void arm_sig_int(void)
1273 struct sigaction act;
1275 memset(&act, 0, sizeof(act));
1276 act.sa_handler = sig_int;
1277 act.sa_flags = SA_RESTART;
1278 sigaction(SIGINT, &act, NULL);
1280 /* Windows uses SIGBREAK as a quit signal from other applications */
1282 sigaction(SIGBREAK, &act, NULL);
1286 static void usage(char *argv, int status)
1288 char runtime_str[16];
1289 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
1290 printf("%s [options] -- [filenames]\n"
1291 " -d <int> : IO Depth, default %d\n"
1292 " -s <int> : Batch submit, default %d\n"
1293 " -c <int> : Batch complete, default %d\n"
1294 " -b <int> : Block size, default %d\n"
1295 " -p <bool> : Polled IO, default %d\n"
1296 " -B <bool> : Fixed buffers, default %d\n"
1297 " -D <bool> : DMA map fixed buffers, default %d\n"
1298 " -F <bool> : Register files, default %d\n"
1299 " -n <int> : Number of threads, default %d\n"
1300 " -O <bool> : Use O_DIRECT, default %d\n"
1301 " -N <bool> : Perform just no-op requests, default %d\n"
1302 " -t <bool> : Track IO latencies, default %d\n"
1303 " -T <int> : TSC rate in HZ\n"
1304 " -r <int> : Runtime in seconds, default %s\n"
1305 " -R <bool> : Use random IO, default %d\n"
1306 " -a <bool> : Use legacy aio, default %d\n"
1307 " -S <bool> : Use sync IO (preadv2), default %d\n"
1308 " -X <bool> : Use registered ring %d\n"
1309 " -P <bool> : Automatically place on device home node %d\n",
1310 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
1311 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
1312 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio,
1313 use_sync, register_ring, numa_placement);
1317 static void read_tsc_rate(void)
1325 fd = open(TSC_RATE_FILE, O_RDONLY);
1329 ret = read(fd, buffer, sizeof(buffer));
1335 tsc_rate = strtoul(buffer, NULL, 10);
1336 printf("Using TSC rate %luHz\n", tsc_rate);
1340 static void write_tsc_rate(void)
1346 if (!stat(TSC_RATE_FILE, &sb))
1349 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1353 memset(buffer, 0, sizeof(buffer));
1354 sprintf(buffer, "%lu", tsc_rate);
1355 ret = write(fd, buffer, strlen(buffer));
1361 int main(int argc, char *argv[])
1363 struct submitter *s;
1364 unsigned long done, calls, reap;
1365 int i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1369 if (!do_nop && argc < 2)
1372 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:r:D:R:X:S:P:h?")) != -1) {
1375 aio = !!atoi(optarg);
1378 depth = atoi(optarg);
1381 batch_submit = atoi(optarg);
1386 batch_complete = atoi(optarg);
1387 if (!batch_complete)
1394 polled = !!atoi(optarg);
1397 fixedbufs = !!atoi(optarg);
1400 register_files = !!atoi(optarg);
1403 nthreads = atoi(optarg);
1405 printf("Threads must be non-zero\n");
1410 do_nop = !!atoi(optarg);
1413 buffered = !atoi(optarg);
1416 #ifndef ARCH_HAVE_CPU_CLOCK
1417 fprintf(stderr, "Stats not supported on this CPU\n");
1420 stats = !!atoi(optarg);
1423 #ifndef ARCH_HAVE_CPU_CLOCK
1424 fprintf(stderr, "Stats not supported on this CPU\n");
1427 tsc_rate = strtoul(optarg, NULL, 10);
1431 runtime = atoi(optarg);
1434 dma_map = !!atoi(optarg);
1437 random_io = !!atoi(optarg);
1440 register_ring = !!atoi(optarg);
1443 #ifdef CONFIG_PWRITEV2
1444 use_sync = !!atoi(optarg);
1446 fprintf(stderr, "preadv2 not supported\n");
1451 numa_placement = !!atoi(optarg);
1464 if (batch_complete > depth)
1465 batch_complete = depth;
1466 if (batch_submit > depth)
1467 batch_submit = depth;
1468 if (!fixedbufs && dma_map)
1471 submitter = calloc(nthreads, sizeof(*submitter) +
1472 roundup_pow2(depth) * sizeof(struct iovec));
1473 for (j = 0; j < nthreads; j++) {
1474 s = get_submitter(j);
1477 s->done = s->calls = s->reaps = 0;
1480 flags = O_RDONLY | O_NOATIME;
1489 printf("No files specified\n");
1492 threads_per_f = nthreads / nfiles;
1493 /* make sure each thread gets assigned files */
1494 if (threads_per_f == 0) {
1497 threads_rem = nthreads - threads_per_f * nfiles;
1500 while (!do_nop && i < argc) {
1503 memset(&f, 0, sizeof(f));
1505 fd = open(argv[i], flags);
1511 if (get_file_size(&f)) {
1512 printf("failed getting size of device/file\n");
1515 if (f.max_blocks <= 1) {
1516 printf("Zero file/device size?\n");
1521 limit = threads_per_f;
1522 limit += threads_rem > 0 ? 1 : 0;
1523 for (k = 0; k < limit; k++) {
1524 s = get_submitter((j + k) % nthreads);
1526 if (s->nr_files == MAX_FDS) {
1527 printf("Max number of files (%d) reached\n", MAX_FDS);
1531 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1534 detect_node(s, argv[i]);
1536 s->filename = argv[i];
1546 page_size = sysconf(_SC_PAGESIZE);
1550 for (j = 0; j < nthreads; j++) {
1551 s = get_submitter(j);
1553 pthread_create(&s->thread, NULL, submitter_sync_fn, s);
1555 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1556 #ifdef CONFIG_LIBAIO
1558 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1562 reap = calls = done = 0;
1564 unsigned long this_done = 0;
1565 unsigned long this_reap = 0;
1566 unsigned long this_call = 0;
1567 unsigned long rpc = 0, ipc = 0;
1568 unsigned long iops, bw;
1571 if (runtime && !--runtime)
1572 do_finish("timeout");
1574 /* don't print partial run, if interrupted by signal */
1578 /* one second in to the run, enable stats */
1582 for (j = 0; j < nthreads; j++) {
1583 s = get_submitter(j);
1584 this_done += s->done;
1585 this_call += s->calls;
1586 this_reap += s->reaps;
1588 if (this_call - calls) {
1589 rpc = (this_done - done) / (this_call - calls);
1590 ipc = (this_reap - reap) / (this_call - calls);
1593 iops = this_done - done;
1595 bw = iops * (bs / 1048576);
1597 bw = iops / (1048576 / bs);
1598 if (iops > 1000000) {
1599 double miops = (double) iops / 1000000.0;
1600 printf("IOPS=%.2fM, ", miops);
1601 } else if (iops > 100000) {
1602 double kiops = (double) iops / 1000.0;
1603 printf("IOPS=%.2fK, ", kiops);
1605 printf("IOPS=%lu, ", iops);
1607 max_iops = max(max_iops, iops);
1610 double bw_g = (double) bw / 1000.0;
1612 printf("BW=%.2fGiB/s, ", bw_g);
1614 printf("BW=%luMiB/s, ", bw);
1617 printf("IOS/call=%ld/%ld\n", rpc, ipc);
1623 for (j = 0; j < nthreads; j++) {
1624 s = get_submitter(j);
1625 pthread_join(s->thread, &ret);
1631 printf("%d: Latency percentiles:\n", s->tid);
1632 for (i = 0, nr = 0; i < PLAT_NR; i++)
1634 show_clat_percentiles(s->plat, nr, 4);
1635 free(s->clock_batch);