t/io_uring: add -R option for random/sequential IO
[fio.git] / t / io_uring.c
CommitLineData
c9fb4c5b
JA
1#include <stdio.h>
2#include <errno.h>
3#include <assert.h>
4#include <stdlib.h>
5#include <stddef.h>
6#include <signal.h>
7#include <inttypes.h>
932131c9 8#include <math.h>
c9fb4c5b 9
256714ea
JA
10#ifdef CONFIG_LIBAIO
11#include <libaio.h>
12#endif
13
c9fb4c5b
JA
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <sys/ioctl.h>
17#include <sys/syscall.h>
18#include <sys/resource.h>
c3e2fc25 19#include <sys/mman.h>
e31b8288 20#include <sys/uio.h>
c9fb4c5b
JA
21#include <linux/fs.h>
22#include <fcntl.h>
23#include <unistd.h>
c9fb4c5b
JA
24#include <string.h>
25#include <pthread.h>
26#include <sched.h>
27
74efb029 28#include "../arch/arch.h"
57fa61f0 29#include "../lib/types.h"
932131c9 30#include "../lib/roundup.h"
9eff5320 31#include "../lib/rand.h"
932131c9 32#include "../minmax.h"
f3e769a4 33#include "../os/linux/io_uring.h"
ac122fea 34
e31b8288 35struct io_sq_ring {
e2239016
JA
36 unsigned *head;
37 unsigned *tail;
38 unsigned *ring_mask;
39 unsigned *ring_entries;
ce1705de 40 unsigned *flags;
e2239016 41 unsigned *array;
c9fb4c5b
JA
42};
43
e31b8288 44struct io_cq_ring {
e2239016
JA
45 unsigned *head;
46 unsigned *tail;
47 unsigned *ring_mask;
48 unsigned *ring_entries;
f0403f94 49 struct io_uring_cqe *cqes;
c9fb4c5b
JA
50};
51
701d1277 52#define DEPTH 128
2e7888ef
JA
53#define BATCH_SUBMIT 32
54#define BATCH_COMPLETE 32
c9fb4c5b
JA
55#define BS 4096
56
a7086591
JA
57#define MAX_FDS 16
58
c3e2fc25 59static unsigned sq_ring_mask, cq_ring_mask;
e39c34dc 60
a7086591
JA
61struct file {
62 unsigned long max_blocks;
beda9d8d
JA
63 unsigned long max_size;
64 unsigned long cur_off;
701d1277 65 unsigned pending_ios;
48e698fa
JA
66 int real_fd;
67 int fixed_fd;
932131c9 68 int fileno;
a7086591
JA
69};
70
932131c9
JA
71#define PLAT_BITS 6
72#define PLAT_VAL (1 << PLAT_BITS)
73#define PLAT_GROUP_NR 29
74#define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
75
c9fb4c5b
JA
76struct submitter {
77 pthread_t thread;
f310970e 78 int ring_fd;
54319661 79 int index;
e31b8288 80 struct io_sq_ring sq_ring;
f0403f94 81 struct io_uring_sqe *sqes;
e31b8288 82 struct io_cq_ring cq_ring;
c9fb4c5b 83 int inflight;
932131c9 84 int tid;
c9fb4c5b
JA
85 unsigned long reaps;
86 unsigned long done;
87 unsigned long calls;
88 volatile int finish;
701d1277 89
48e698fa
JA
90 __s32 *fds;
91
9eff5320
JA
92 struct taus258_state rand_state;
93
932131c9
JA
94 unsigned long *clock_batch;
95 int clock_index;
96 unsigned long *plat;
97
256714ea
JA
98#ifdef CONFIG_LIBAIO
99 io_context_t aio_ctx;
100#endif
101
a7086591
JA
102 struct file files[MAX_FDS];
103 unsigned nr_files;
104 unsigned cur_file;
e39863e3 105 struct iovec iovecs[];
c9fb4c5b
JA
106};
107
e39863e3 108static struct submitter *submitter;
c9fb4c5b 109static volatile int finish;
52479d8b 110static int stats_running;
16d25711 111static unsigned long max_iops;
c9fb4c5b 112
e39863e3
KB
113static int depth = DEPTH;
114static int batch_submit = BATCH_SUBMIT;
115static int batch_complete = BATCH_COMPLETE;
5bd526f2 116static int bs = BS;
f0403f94 117static int polled = 1; /* use IO polling */
701d1277 118static int fixedbufs = 1; /* use fixed user buffers */
a71ad043 119static int dma_map; /* pre-map DMA buffers */
8c5fa755 120static int register_files = 1; /* use fixed files */
f0403f94 121static int buffered = 0; /* use buffered IO, not O_DIRECT */
3d7d00a3
JA
122static int sq_thread_poll = 0; /* use kernel submission/poller thread */
123static int sq_thread_cpu = -1; /* pin above thread to this CPU */
8025517d 124static int do_nop = 0; /* no-op SQ ring commands */
54319661 125static int nthreads = 1;
932131c9 126static int stats = 0; /* generate IO stats */
256714ea 127static int aio = 0; /* use libaio */
beda9d8d
JA
128static int runtime = 0; /* runtime */
129static int random_io = 1; /* random or sequential IO */
256714ea 130
932131c9 131static unsigned long tsc_rate;
c9fb4c5b 132
203e4c26
JA
133#define TSC_RATE_FILE "tsc-rate"
134
84106576 135static int vectored = 1;
b3915995 136
932131c9 137static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
ad45a465 138 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
932131c9
JA
139static int plist_len = 17;
140
a71ad043
JA
141#ifndef IORING_REGISTER_MAP_BUFFERS
142#define IORING_REGISTER_MAP_BUFFERS 20
143struct io_uring_map_buffers {
144 __s32 fd;
145 __u32 buf_start;
146 __u32 buf_end;
702b0be2
JA
147 __u32 flags;
148 __u64 rsvd[2];
a71ad043
JA
149};
150#endif
151
932131c9
JA
152static unsigned long cycles_to_nsec(unsigned long cycles)
153{
154 uint64_t val;
155
156 if (!tsc_rate)
157 return cycles;
158
159 val = cycles * 1000000000ULL;
160 return val / tsc_rate;
161}
162
163static unsigned long plat_idx_to_val(unsigned int idx)
164{
165 unsigned int error_bits;
166 unsigned long k, base;
167
168 assert(idx < PLAT_NR);
169
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);
174
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);
178
179 /* Find its bucket number of the group */
180 k = idx % PLAT_VAL;
181
182 /* Return the mean of the range of the bucket */
183 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
184}
185
186unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
187 unsigned long **output,
188 unsigned long *maxv, unsigned long *minv)
189{
190 unsigned long sum = 0;
191 unsigned int len = plist_len, i, j = 0;
192 unsigned long *ovals = NULL;
193 bool is_last;
194
195 *minv = -1ULL;
196 *maxv = 0;
197
198 ovals = malloc(len * sizeof(*ovals));
199 if (!ovals)
200 return 0;
201
202 /*
203 * Calculate bucket values, note down max and min values
204 */
205 is_last = false;
206 for (i = 0; i < PLAT_NR && !is_last; i++) {
207 sum += io_u_plat[i];
208 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
209 assert(plist[j] <= 100.0);
210
211 ovals[j] = plat_idx_to_val(i);
212 if (ovals[j] < *minv)
213 *minv = ovals[j];
214 if (ovals[j] > *maxv)
215 *maxv = ovals[j];
216
217 is_last = (j == len - 1) != 0;
218 if (is_last)
219 break;
220
221 j++;
222 }
223 }
224
225 if (!is_last)
226 fprintf(stderr, "error calculating latency percentiles\n");
227
228 *output = ovals;
229 return len;
230}
231
232static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
233 unsigned int precision)
234{
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;
239 bool is_last;
240 char fmt[32];
241
242 len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
243 if (!len || !ovals)
244 goto out;
245
246 if (!tsc_rate) {
247 scale_down = 0;
248 divisor = 1;
249 printf(" percentiles (tsc ticks):\n |");
250 } else if (minv > 2000 && maxv > 99999) {
251 scale_down = 1;
252 divisor = 1000;
253 printf(" percentiles (usec):\n |");
254 } else {
255 scale_down = 0;
256 divisor = 1;
257 printf(" percentiles (nsec):\n |");
258 }
259
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);
265
266 for (j = 0; j < len; j++) {
267 /* for formatting */
268 if (j != 0 && (j % per_line) == 0)
269 printf(" |");
270
271 /* end of the list */
272 is_last = (j == len - 1) != 0;
273
274 for (i = 0; i < scale_down; i++)
275 ovals[j] = (ovals[j] + 999) / 1000;
276
277 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
278
279 if (is_last)
280 break;
281
282 if ((j % per_line) == per_line - 1) /* for formatting */
283 printf("\n");
284 }
285
286out:
287 free(ovals);
288}
289
290static unsigned int plat_val_to_idx(unsigned long val)
291{
292 unsigned int msb, error_bits, base, offset, idx;
293
294 /* Find MSB starting from bit 0 */
295 if (val == 0)
296 msb = 0;
297 else
298 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
299
300 /*
301 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
302 * all bits of the sample as index
303 */
304 if (msb <= PLAT_BITS)
305 return val;
306
307 /* Compute the number of error bits to discard*/
308 error_bits = msb - PLAT_BITS;
309
310 /* Compute the number of buckets before the group */
311 base = (error_bits + 1) << PLAT_BITS;
312
313 /*
314 * Discard the error bits and apply the mask to find the
315 * index for the buckets in the group
316 */
317 offset = (PLAT_VAL - 1) & (val >> error_bits);
318
319 /* Make sure the index does not exceed (array size - 1) */
320 idx = (base + offset) < (PLAT_NR - 1) ?
321 (base + offset) : (PLAT_NR - 1);
322
323 return idx;
324}
325
326static void add_stat(struct submitter *s, int clock_index, int nr)
327{
328#ifdef ARCH_HAVE_CPU_CLOCK
329 unsigned long cycles;
330 unsigned int pidx;
331
265697fc
JA
332 if (!s->finish && clock_index) {
333 cycles = get_cpu_clock();
334 cycles -= s->clock_batch[clock_index];
335 pidx = plat_val_to_idx(cycles);
336 s->plat[pidx] += nr;
337 }
932131c9
JA
338#endif
339}
340
a71ad043
JA
341static int io_uring_map_buffers(struct submitter *s)
342{
343 struct io_uring_map_buffers map = {
344 .fd = s->files[0].real_fd,
a71ad043 345 .buf_end = depth,
a71ad043
JA
346 };
347
348 if (do_nop)
349 return 0;
350 if (s->nr_files > 1) {
351 fprintf(stderr, "Can't map buffers with multiple files\n");
352 return -1;
353 }
354
355 return syscall(__NR_io_uring_register, s->ring_fd,
356 IORING_REGISTER_MAP_BUFFERS, &map, 1);
357}
358
2ea53ca3 359static int io_uring_register_buffers(struct submitter *s)
c9fb4c5b 360{
8025517d
JA
361 if (do_nop)
362 return 0;
363
bfed648c 364 return syscall(__NR_io_uring_register, s->ring_fd,
e39863e3 365 IORING_REGISTER_BUFFERS, s->iovecs, depth);
2ea53ca3
JA
366}
367
a7abc9fb
JA
368static int io_uring_register_files(struct submitter *s)
369{
48e698fa 370 int i;
a7abc9fb 371
8025517d
JA
372 if (do_nop)
373 return 0;
374
48e698fa
JA
375 s->fds = calloc(s->nr_files, sizeof(__s32));
376 for (i = 0; i < s->nr_files; i++) {
377 s->fds[i] = s->files[i].real_fd;
378 s->files[i].fixed_fd = i;
379 }
a7abc9fb 380
bfed648c 381 return syscall(__NR_io_uring_register, s->ring_fd,
919850d2 382 IORING_REGISTER_FILES, s->fds, s->nr_files);
a7abc9fb
JA
383}
384
2ea53ca3
JA
385static int io_uring_setup(unsigned entries, struct io_uring_params *p)
386{
bfed648c 387 return syscall(__NR_io_uring_setup, entries, p);
c9fb4c5b
JA
388}
389
b3915995
JA
390static void io_uring_probe(int fd)
391{
392 struct io_uring_probe *p;
393 int ret;
394
395 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
396 if (!p)
397 return;
398
399 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
400 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
401 if (ret < 0)
402 goto out;
403
404 if (IORING_OP_READ > p->ops_len)
405 goto out;
406
407 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
84106576 408 vectored = 0;
b3915995
JA
409out:
410 free(p);
411}
412
c3e2fc25
JA
413static int io_uring_enter(struct submitter *s, unsigned int to_submit,
414 unsigned int min_complete, unsigned int flags)
c9fb4c5b 415{
bfed648c
JA
416 return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete,
417 flags, NULL, 0);
c9fb4c5b
JA
418}
419
77d814a1 420#ifndef CONFIG_HAVE_GETTID
c9fb4c5b
JA
421static int gettid(void)
422{
423 return syscall(__NR_gettid);
424}
77d814a1 425#endif
c9fb4c5b 426
701d1277
JA
427static unsigned file_depth(struct submitter *s)
428{
e39863e3 429 return (depth + s->nr_files - 1) / s->nr_files;
701d1277
JA
430}
431
a7086591 432static void init_io(struct submitter *s, unsigned index)
c9fb4c5b 433{
f0403f94 434 struct io_uring_sqe *sqe = &s->sqes[index];
c9fb4c5b 435 unsigned long offset;
a7086591 436 struct file *f;
c9fb4c5b
JA
437 long r;
438
8025517d
JA
439 if (do_nop) {
440 sqe->opcode = IORING_OP_NOP;
441 return;
442 }
443
701d1277
JA
444 if (s->nr_files == 1) {
445 f = &s->files[0];
446 } else {
447 f = &s->files[s->cur_file];
448 if (f->pending_ios >= file_depth(s)) {
449 s->cur_file++;
450 if (s->cur_file == s->nr_files)
451 s->cur_file = 0;
93d1811c 452 f = &s->files[s->cur_file];
701d1277
JA
453 }
454 }
455 f->pending_ios++;
a7086591 456
beda9d8d
JA
457 if (random_io) {
458 r = __rand64(&s->rand_state);
459 offset = (r % (f->max_blocks - 1)) * bs;
460 } else {
461 offset = f->cur_off;
462 f->cur_off += bs;
463 if (f->cur_off + bs > f->max_size)
464 f->cur_off = 0;
465 }
c9fb4c5b 466
8c5fa755
JA
467 if (register_files) {
468 sqe->flags = IOSQE_FIXED_FILE;
469 sqe->fd = f->fixed_fd;
470 } else {
471 sqe->flags = 0;
472 sqe->fd = f->real_fd;
473 }
f0403f94 474 if (fixedbufs) {
48e698fa 475 sqe->opcode = IORING_OP_READ_FIXED;
919850d2 476 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
5bd526f2 477 sqe->len = bs;
2ea53ca3 478 sqe->buf_index = index;
84106576 479 } else if (!vectored) {
b3915995
JA
480 sqe->opcode = IORING_OP_READ;
481 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
482 sqe->len = bs;
483 sqe->buf_index = 0;
84106576
JA
484 } else {
485 sqe->opcode = IORING_OP_READV;
486 sqe->addr = (unsigned long) &s->iovecs[index];
487 sqe->len = 1;
488 sqe->buf_index = 0;
f0403f94 489 }
f0403f94 490 sqe->ioprio = 0;
f0403f94 491 sqe->off = offset;
932131c9 492 sqe->user_data = (unsigned long) f->fileno;
52479d8b 493 if (stats && stats_running)
932131c9 494 sqe->user_data |= ((unsigned long)s->clock_index << 32);
c9fb4c5b
JA
495}
496
256714ea 497static int prep_more_ios_uring(struct submitter *s, int max_ios)
c9fb4c5b 498{
e31b8288 499 struct io_sq_ring *ring = &s->sq_ring;
e2239016 500 unsigned index, tail, next_tail, prepped = 0;
c9fb4c5b 501
c3e2fc25 502 next_tail = tail = *ring->tail;
c9fb4c5b
JA
503 do {
504 next_tail++;
fc2dc21b 505 if (next_tail == atomic_load_acquire(ring->head))
c9fb4c5b
JA
506 break;
507
e39c34dc 508 index = tail & sq_ring_mask;
a7086591 509 init_io(s, index);
c3e2fc25 510 ring->array[index] = index;
c9fb4c5b
JA
511 prepped++;
512 tail = next_tail;
513 } while (prepped < max_ios);
514
fc2dc21b
JA
515 if (prepped)
516 atomic_store_release(ring->tail, tail);
c9fb4c5b
JA
517 return prepped;
518}
519
a7086591 520static int get_file_size(struct file *f)
c9fb4c5b
JA
521{
522 struct stat st;
523
48e698fa 524 if (fstat(f->real_fd, &st) < 0)
c9fb4c5b
JA
525 return -1;
526 if (S_ISBLK(st.st_mode)) {
527 unsigned long long bytes;
528
48e698fa 529 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
c9fb4c5b
JA
530 return -1;
531
5bd526f2 532 f->max_blocks = bytes / bs;
beda9d8d 533 f->max_size = bytes;
c9fb4c5b
JA
534 return 0;
535 } else if (S_ISREG(st.st_mode)) {
5bd526f2 536 f->max_blocks = st.st_size / bs;
beda9d8d 537 f->max_size = st.st_size;
c9fb4c5b
JA
538 return 0;
539 }
540
541 return -1;
542}
543
256714ea 544static int reap_events_uring(struct submitter *s)
c9fb4c5b 545{
e31b8288 546 struct io_cq_ring *ring = &s->cq_ring;
f0403f94 547 struct io_uring_cqe *cqe;
e2239016 548 unsigned head, reaped = 0;
ab85494f 549 int last_idx = -1, stat_nr = 0;
c9fb4c5b 550
c3e2fc25 551 head = *ring->head;
c9fb4c5b 552 do {
701d1277
JA
553 struct file *f;
554
679d8352 555 read_barrier();
fc2dc21b 556 if (head == atomic_load_acquire(ring->tail))
c9fb4c5b 557 break;
f0403f94 558 cqe = &ring->cqes[head & cq_ring_mask];
8025517d 559 if (!do_nop) {
932131c9
JA
560 int fileno = cqe->user_data & 0xffffffff;
561
562 f = &s->files[fileno];
8025517d 563 f->pending_ios--;
5bd526f2 564 if (cqe->res != bs) {
8025517d 565 printf("io: unexpected ret=%d\n", cqe->res);
154a9582 566 if (polled && cqe->res == -EOPNOTSUPP)
8066f6b6 567 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
8025517d
JA
568 return -1;
569 }
c9fb4c5b 570 }
932131c9
JA
571 if (stats) {
572 int clock_index = cqe->user_data >> 32;
573
ab85494f
JA
574 if (last_idx != clock_index) {
575 if (last_idx != -1) {
576 add_stat(s, last_idx, stat_nr);
577 stat_nr = 0;
578 }
579 last_idx = clock_index;
d4af2ece
JA
580 }
581 stat_nr++;
932131c9 582 }
c9fb4c5b
JA
583 reaped++;
584 head++;
c9fb4c5b
JA
585 } while (1);
586
ab85494f
JA
587 if (stat_nr)
588 add_stat(s, last_idx, stat_nr);
589
fc2dc21b
JA
590 if (reaped) {
591 s->inflight -= reaped;
592 atomic_store_release(ring->head, head);
593 }
c9fb4c5b
JA
594 return reaped;
595}
596
256714ea 597static int submitter_init(struct submitter *s)
c9fb4c5b 598{
256714ea 599 int i, nr_batch;
c9fb4c5b 600
932131c9 601 s->tid = gettid();
7e847fd0 602 printf("submitter=%d, tid=%d\n", s->index, s->tid);
c9fb4c5b 603
9eff5320 604 __init_rand64(&s->rand_state, pthread_self());
5e8865c0 605 srand48(pthread_self());
c9fb4c5b 606
932131c9
JA
607 for (i = 0; i < MAX_FDS; i++)
608 s->files[i].fileno = i;
609
610 if (stats) {
611 nr_batch = roundup_pow2(depth / batch_submit);
d4af2ece
JA
612 if (nr_batch < 2)
613 nr_batch = 2;
932131c9 614 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
52479d8b 615 s->clock_index = 1;
932131c9
JA
616
617 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
618 } else {
619 s->clock_batch = NULL;
620 s->plat = NULL;
621 nr_batch = 0;
622 }
623
256714ea
JA
624 return nr_batch;
625}
626
627#ifdef CONFIG_LIBAIO
628static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
629{
630 unsigned long offset, data;
631 struct file *f;
632 unsigned index;
633 long r;
634
635 index = 0;
636 while (index < max_ios) {
637 struct iocb *iocb = &iocbs[index];
638
639 if (s->nr_files == 1) {
640 f = &s->files[0];
641 } else {
642 f = &s->files[s->cur_file];
643 if (f->pending_ios >= file_depth(s)) {
644 s->cur_file++;
645 if (s->cur_file == s->nr_files)
646 s->cur_file = 0;
647 f = &s->files[s->cur_file];
648 }
649 }
650 f->pending_ios++;
651
652 r = lrand48();
653 offset = (r % (f->max_blocks - 1)) * bs;
654 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
655 s->iovecs[index].iov_len, offset);
656
657 data = f->fileno;
52479d8b 658 if (stats && stats_running)
256714ea
JA
659 data |= ((unsigned long) s->clock_index << 32);
660 iocb->data = (void *) (uintptr_t) data;
661 index++;
662 }
663 return index;
664}
665
666static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
667{
668 int last_idx = -1, stat_nr = 0;
669 int reaped = 0;
670
671 while (evs) {
672 unsigned long data = (uintptr_t) events[reaped].data;
673 struct file *f = &s->files[data & 0xffffffff];
674
675 f->pending_ios--;
676 if (events[reaped].res != bs) {
677 printf("io: unexpected ret=%ld\n", events[reaped].res);
678 return -1;
679 }
680 if (stats) {
681 int clock_index = data >> 32;
682
683 if (last_idx != clock_index) {
684 if (last_idx != -1) {
685 add_stat(s, last_idx, stat_nr);
686 stat_nr = 0;
687 }
688 last_idx = clock_index;
d4af2ece
JA
689 }
690 stat_nr++;
256714ea
JA
691 }
692 reaped++;
693 evs--;
694 }
695
696 if (stat_nr)
697 add_stat(s, last_idx, stat_nr);
698
699 s->inflight -= reaped;
700 s->done += reaped;
701 return reaped;
702}
703
704static void *submitter_aio_fn(void *data)
705{
706 struct submitter *s = data;
707 int i, ret, prepped, nr_batch;
708 struct iocb **iocbsptr;
709 struct iocb *iocbs;
710 struct io_event *events;
711
712 nr_batch = submitter_init(s);
713
714 iocbsptr = calloc(depth, sizeof(struct iocb *));
715 iocbs = calloc(depth, sizeof(struct iocb));
716 events = calloc(depth, sizeof(struct io_event));
717
718 for (i = 0; i < depth; i++)
719 iocbsptr[i] = &iocbs[i];
720
721 prepped = 0;
722 do {
723 int to_wait, to_submit, to_prep;
724
725 if (!prepped && s->inflight < depth) {
726 to_prep = min(depth - s->inflight, batch_submit);
727 prepped = prep_more_ios_aio(s, to_prep, iocbs);
728#ifdef ARCH_HAVE_CPU_CLOCK
729 if (prepped && stats) {
730 s->clock_batch[s->clock_index] = get_cpu_clock();
731 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
732 }
733#endif
734 }
735 s->inflight += prepped;
736 to_submit = prepped;
737
738 if (to_submit && (s->inflight + to_submit <= depth))
739 to_wait = 0;
740 else
741 to_wait = min(s->inflight + to_submit, batch_complete);
742
743 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
744 s->calls++;
745 if (ret < 0) {
746 perror("io_submit");
747 break;
748 } else if (ret != to_submit) {
749 printf("submitted %d, wanted %d\n", ret, to_submit);
750 break;
751 }
752 prepped = 0;
753
24a24c12 754 while (to_wait) {
256714ea
JA
755 int r;
756
24a24c12
JA
757 s->calls++;
758 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
759 if (r < 0) {
760 perror("io_getevents");
761 break;
762 } else if (r != to_wait) {
763 printf("r=%d, wait=%d\n", r, to_wait);
764 break;
765 }
766 r = reap_events_aio(s, events, r);
767 s->reaps += r;
768 to_wait -= r;
256714ea
JA
769 }
770 } while (!s->finish);
771
772 free(iocbsptr);
773 free(iocbs);
774 free(events);
775 finish = 1;
776 return NULL;
777}
778#endif
779
780static void *submitter_uring_fn(void *data)
781{
782 struct submitter *s = data;
783 struct io_sq_ring *ring = &s->sq_ring;
784 int ret, prepped, nr_batch;
785
786 nr_batch = submitter_init(s);
787
c9fb4c5b
JA
788 prepped = 0;
789 do {
f310970e 790 int to_wait, to_submit, this_reap, to_prep;
fc2dc21b 791 unsigned ring_flags = 0;
c9fb4c5b 792
e39863e3
KB
793 if (!prepped && s->inflight < depth) {
794 to_prep = min(depth - s->inflight, batch_submit);
256714ea 795 prepped = prep_more_ios_uring(s, to_prep);
932131c9
JA
796#ifdef ARCH_HAVE_CPU_CLOCK
797 if (prepped && stats) {
798 s->clock_batch[s->clock_index] = get_cpu_clock();
799 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
800 }
801#endif
f310970e 802 }
c9fb4c5b
JA
803 s->inflight += prepped;
804submit_more:
805 to_submit = prepped;
806submit:
e39863e3 807 if (to_submit && (s->inflight + to_submit <= depth))
c9fb4c5b
JA
808 to_wait = 0;
809 else
e39863e3 810 to_wait = min(s->inflight + to_submit, batch_complete);
c9fb4c5b 811
ce1705de
JA
812 /*
813 * Only need to call io_uring_enter if we're not using SQ thread
814 * poll, or if IORING_SQ_NEED_WAKEUP is set.
815 */
fc2dc21b
JA
816 if (sq_thread_poll)
817 ring_flags = atomic_load_acquire(ring->flags);
818 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
e0abe388
JA
819 unsigned flags = 0;
820
821 if (to_wait)
822 flags = IORING_ENTER_GETEVENTS;
fc2dc21b 823 if (ring_flags & IORING_SQ_NEED_WAKEUP)
b532dd6d 824 flags |= IORING_ENTER_SQ_WAKEUP;
e0abe388 825 ret = io_uring_enter(s, to_submit, to_wait, flags);
ce1705de 826 s->calls++;
fc2dc21b
JA
827 } else {
828 /* for SQPOLL, we submitted it all effectively */
829 ret = to_submit;
ce1705de 830 }
c9fb4c5b 831
ce1705de
JA
832 /*
833 * For non SQ thread poll, we already got the events we needed
834 * through the io_uring_enter() above. For SQ thread poll, we
835 * need to loop here until we find enough events.
836 */
837 this_reap = 0;
838 do {
839 int r;
256714ea
JA
840
841 r = reap_events_uring(s);
7d1435e6
JA
842 if (r == -1) {
843 s->finish = 1;
ce1705de 844 break;
7d1435e6 845 } else if (r > 0)
ce1705de
JA
846 this_reap += r;
847 } while (sq_thread_poll && this_reap < to_wait);
c9fb4c5b
JA
848 s->reaps += this_reap;
849
850 if (ret >= 0) {
851 if (!ret) {
852 to_submit = 0;
853 if (s->inflight)
854 goto submit;
855 continue;
856 } else if (ret < to_submit) {
857 int diff = to_submit - ret;
858
859 s->done += ret;
860 prepped -= diff;
861 goto submit_more;
862 }
863 s->done += ret;
864 prepped = 0;
865 continue;
866 } else if (ret < 0) {
ac122fea 867 if (errno == EAGAIN) {
c9fb4c5b
JA
868 if (s->finish)
869 break;
870 if (this_reap)
871 goto submit;
c9fb4c5b
JA
872 to_submit = 0;
873 goto submit;
874 }
ac122fea 875 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
876 break;
877 }
878 } while (!s->finish);
a7086591 879
c9fb4c5b
JA
880 finish = 1;
881 return NULL;
882}
883
54319661
JA
884static struct submitter *get_submitter(int offset)
885{
886 void *ret;
887
888 ret = submitter;
889 if (offset)
890 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
891 return ret;
892}
893
65e1a5e8 894static void do_finish(const char *reason)
c9fb4c5b 895{
54319661 896 int j;
65e1a5e8 897 printf("Exiting on %s\n", reason);
54319661
JA
898 for (j = 0; j < nthreads; j++) {
899 struct submitter *s = get_submitter(j);
900 s->finish = 1;
901 }
16d25711 902 if (max_iops > 100000)
18b557a0 903 printf("Maximum IOPS=%luK\n", max_iops / 1000);
16d25711 904 else if (max_iops)
18b557a0 905 printf("Maximum IOPS=%lu\n", max_iops);
c9fb4c5b
JA
906 finish = 1;
907}
908
65e1a5e8
EV
909static void sig_int(int sig)
910{
911 do_finish("signal");
912}
913
c9fb4c5b
JA
914static void arm_sig_int(void)
915{
916 struct sigaction act;
917
918 memset(&act, 0, sizeof(act));
919 act.sa_handler = sig_int;
920 act.sa_flags = SA_RESTART;
921 sigaction(SIGINT, &act, NULL);
2cf71009
BP
922
923 /* Windows uses SIGBREAK as a quit signal from other applications */
924#ifdef WIN32
925 sigaction(SIGBREAK, &act, NULL);
926#endif
c9fb4c5b
JA
927}
928
256714ea
JA
929static int setup_aio(struct submitter *s)
930{
931#ifdef CONFIG_LIBAIO
932 if (polled) {
933 fprintf(stderr, "aio does not support polled IO\n");
934 polled = 0;
935 }
936 if (sq_thread_poll) {
937 fprintf(stderr, "aio does not support SQPOLL IO\n");
938 sq_thread_poll = 0;
939 }
940 if (do_nop) {
941 fprintf(stderr, "aio does not support polled IO\n");
942 do_nop = 0;
943 }
944 if (fixedbufs || register_files) {
945 fprintf(stderr, "aio does not support registered files or buffers\n");
946 fixedbufs = register_files = 0;
947 }
948
949 return io_queue_init(depth, &s->aio_ctx);
950#else
951 fprintf(stderr, "Legacy AIO not available on this system/build\n");
952 errno = EINVAL;
953 return -1;
954#endif
955}
956
c3e2fc25
JA
957static int setup_ring(struct submitter *s)
958{
e31b8288
JA
959 struct io_sq_ring *sring = &s->sq_ring;
960 struct io_cq_ring *cring = &s->cq_ring;
961 struct io_uring_params p;
2ea53ca3 962 int ret, fd;
c3e2fc25 963 void *ptr;
c3e2fc25
JA
964
965 memset(&p, 0, sizeof(p));
966
7d1435e6 967 if (polled && !do_nop)
0e47f11b 968 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3 969 if (sq_thread_poll) {
2ea53ca3 970 p.flags |= IORING_SETUP_SQPOLL;
3ade18a3 971 if (sq_thread_cpu != -1) {
3d7d00a3 972 p.flags |= IORING_SETUP_SQ_AFF;
3ade18a3
JA
973 p.sq_thread_cpu = sq_thread_cpu;
974 }
c3e2fc25
JA
975 }
976
e39863e3 977 fd = io_uring_setup(depth, &p);
c3e2fc25
JA
978 if (fd < 0) {
979 perror("io_uring_setup");
980 return 1;
981 }
f310970e 982 s->ring_fd = fd;
2ea53ca3 983
b3915995
JA
984 io_uring_probe(fd);
985
2ea53ca3 986 if (fixedbufs) {
15130e3f
JA
987 struct rlimit rlim;
988
989 rlim.rlim_cur = RLIM_INFINITY;
990 rlim.rlim_max = RLIM_INFINITY;
991 /* ignore potential error, not needed on newer kernels */
992 setrlimit(RLIMIT_MEMLOCK, &rlim);
993
2ea53ca3
JA
994 ret = io_uring_register_buffers(s);
995 if (ret < 0) {
a7abc9fb 996 perror("io_uring_register_buffers");
2ea53ca3
JA
997 return 1;
998 }
a71ad043
JA
999
1000 if (dma_map) {
1001 ret = io_uring_map_buffers(s);
1002 if (ret < 0) {
1003 perror("io_uring_map_buffers");
1004 return 1;
1005 }
1006 }
2ea53ca3
JA
1007 }
1008
8c5fa755
JA
1009 if (register_files) {
1010 ret = io_uring_register_files(s);
1011 if (ret < 0) {
1012 perror("io_uring_register_files");
1013 return 1;
1014 }
a7abc9fb
JA
1015 }
1016
e2239016 1017 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
f310970e
JA
1018 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1019 IORING_OFF_SQ_RING);
c3e2fc25
JA
1020 sring->head = ptr + p.sq_off.head;
1021 sring->tail = ptr + p.sq_off.tail;
1022 sring->ring_mask = ptr + p.sq_off.ring_mask;
1023 sring->ring_entries = ptr + p.sq_off.ring_entries;
ce1705de 1024 sring->flags = ptr + p.sq_off.flags;
ac122fea 1025 sring->array = ptr + p.sq_off.array;
c3e2fc25
JA
1026 sq_ring_mask = *sring->ring_mask;
1027
f0403f94 1028 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
f310970e 1029 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
f0403f94 1030 IORING_OFF_SQES);
c3e2fc25 1031
f0403f94 1032 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
f310970e
JA
1033 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1034 IORING_OFF_CQ_RING);
c3e2fc25
JA
1035 cring->head = ptr + p.cq_off.head;
1036 cring->tail = ptr + p.cq_off.tail;
1037 cring->ring_mask = ptr + p.cq_off.ring_mask;
1038 cring->ring_entries = ptr + p.cq_off.ring_entries;
f0403f94 1039 cring->cqes = ptr + p.cq_off.cqes;
c3e2fc25
JA
1040 cq_ring_mask = *cring->ring_mask;
1041 return 0;
1042}
1043
2e7888ef
JA
1044static void file_depths(char *buf)
1045{
26976a13 1046 bool prev = false;
2e7888ef 1047 char *p;
54319661 1048 int i, j;
2e7888ef 1049
ac21bfab 1050 buf[0] = '\0';
2e7888ef 1051 p = buf;
54319661
JA
1052 for (j = 0; j < nthreads; j++) {
1053 struct submitter *s = get_submitter(j);
2e7888ef 1054
54319661
JA
1055 for (i = 0; i < s->nr_files; i++) {
1056 struct file *f = &s->files[i];
1057
26976a13
JA
1058 if (prev)
1059 p += sprintf(p, " %d", f->pending_ios);
54319661 1060 else
4f215227 1061 p += sprintf(p, "%d", f->pending_ios);
26976a13 1062 prev = true;
54319661 1063 }
2e7888ef
JA
1064 }
1065}
1066
d79ff8c9 1067static void usage(char *argv, int status)
e39863e3 1068{
65e1a5e8
EV
1069 char runtime_str[16];
1070 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
e39863e3 1071 printf("%s [options] -- [filenames]\n"
35268e11
AJ
1072 " -d <int> : IO Depth, default %d\n"
1073 " -s <int> : Batch submit, default %d\n"
1074 " -c <int> : Batch complete, default %d\n"
1075 " -b <int> : Block size, default %d\n"
1076 " -p <bool> : Polled IO, default %d\n"
1077 " -B <bool> : Fixed buffers, default %d\n"
dd1f1ba0 1078 " -D <bool> : DMA map fixed buffers, default %d\n"
35268e11 1079 " -F <bool> : Register files, default %d\n"
0862f718 1080 " -n <int> : Number of threads, default %d\n"
2686fc22 1081 " -O <bool> : Use O_DIRECT, default %d\n"
4a39c524
EV
1082 " -N <bool> : Perform just no-op requests, default %d\n"
1083 " -t <bool> : Track IO latencies, default %d\n"
256714ea 1084 " -T <int> : TSC rate in HZ\n"
beda9d8d
JA
1085 " -r <int> : Runtime in seconds, default %s\n"
1086 " -R <bool> : Use random IO, default %d\n"
1087 " -a <bool> : Use legacy aio, default %d\n",
35268e11 1088 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
a71ad043 1089 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
beda9d8d 1090 stats, runtime == 0 ? "unlimited" : runtime_str, aio, random_io);
d79ff8c9 1091 exit(status);
e39863e3
KB
1092}
1093
203e4c26
JA
1094static void read_tsc_rate(void)
1095{
1096 char buffer[32];
1097 int fd, ret;
1098
1099 if (tsc_rate)
1100 return;
1101
1102 fd = open(TSC_RATE_FILE, O_RDONLY);
1103 if (fd < 0)
1104 return;
1105
1106 ret = read(fd, buffer, sizeof(buffer));
1107 if (ret < 0) {
1108 close(fd);
1109 return;
1110 }
1111
1112 tsc_rate = strtoul(buffer, NULL, 10);
1113 printf("Using TSC rate %luHz\n", tsc_rate);
1114 close(fd);
1115}
1116
1117static void write_tsc_rate(void)
1118{
1119 char buffer[32];
1120 struct stat sb;
1121 int fd, ret;
1122
1123 if (!stat(TSC_RATE_FILE, &sb))
1124 return;
1125
1126 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1127 if (fd < 0)
1128 return;
1129
1130 memset(buffer, 0, sizeof(buffer));
1131 sprintf(buffer, "%lu", tsc_rate);
1132 ret = write(fd, buffer, strlen(buffer));
1133 if (ret < 0)
1134 perror("write");
1135 close(fd);
1136}
1137
c9fb4c5b
JA
1138int main(int argc, char *argv[])
1139{
e39863e3 1140 struct submitter *s;
05138221 1141 unsigned long done, calls, reap;
d79ff8c9
AJ
1142 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1143 struct file f;
2e7888ef 1144 char *fdepths;
c3e2fc25 1145 void *ret;
c9fb4c5b 1146
0862f718
JA
1147 if (!do_nop && argc < 2)
1148 usage(argv[0], 1);
c9fb4c5b 1149
beda9d8d 1150 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:r:D:R:h?")) != -1) {
e39863e3 1151 switch (opt) {
256714ea
JA
1152 case 'a':
1153 aio = !!atoi(optarg);
1154 break;
e39863e3
KB
1155 case 'd':
1156 depth = atoi(optarg);
1157 break;
1158 case 's':
1159 batch_submit = atoi(optarg);
932131c9
JA
1160 if (!batch_submit)
1161 batch_submit = 1;
e39863e3
KB
1162 break;
1163 case 'c':
1164 batch_complete = atoi(optarg);
932131c9
JA
1165 if (!batch_complete)
1166 batch_complete = 1;
e39863e3 1167 break;
5bd526f2
JA
1168 case 'b':
1169 bs = atoi(optarg);
1170 break;
1171 case 'p':
1172 polled = !!atoi(optarg);
1173 break;
6a87c3b0
JA
1174 case 'B':
1175 fixedbufs = !!atoi(optarg);
1176 break;
1177 case 'F':
1178 register_files = !!atoi(optarg);
1179 break;
54319661
JA
1180 case 'n':
1181 nthreads = atoi(optarg);
caf2b9ac
JA
1182 if (!nthreads) {
1183 printf("Threads must be non-zero\n");
1184 usage(argv[0], 1);
1185 }
54319661 1186 break;
0862f718
JA
1187 case 'N':
1188 do_nop = !!atoi(optarg);
1189 break;
2686fc22
JA
1190 case 'O':
1191 buffered = !atoi(optarg);
1192 break;
932131c9
JA
1193 case 't':
1194#ifndef ARCH_HAVE_CPU_CLOCK
1195 fprintf(stderr, "Stats not supported on this CPU\n");
1196 return 1;
1197#endif
1198 stats = !!atoi(optarg);
1199 break;
1200 case 'T':
1201#ifndef ARCH_HAVE_CPU_CLOCK
1202 fprintf(stderr, "Stats not supported on this CPU\n");
1203 return 1;
1204#endif
1205 tsc_rate = strtoul(optarg, NULL, 10);
203e4c26 1206 write_tsc_rate();
932131c9 1207 break;
65e1a5e8
EV
1208 case 'r':
1209 runtime = atoi(optarg);
1210 break;
a71ad043
JA
1211 case 'D':
1212 dma_map = !!atoi(optarg);
1213 break;
beda9d8d
JA
1214 case 'R':
1215 random_io = !!atoi(optarg);
1216 break;
e39863e3
KB
1217 case 'h':
1218 case '?':
1219 default:
d79ff8c9 1220 usage(argv[0], 0);
e39863e3
KB
1221 break;
1222 }
1223 }
1224
203e4c26
JA
1225 if (stats)
1226 read_tsc_rate();
1227
c5347611
JA
1228 if (batch_complete > depth)
1229 batch_complete = depth;
1230 if (batch_submit > depth)
1231 batch_submit = depth;
a71ad043
JA
1232 if (!fixedbufs && dma_map)
1233 dma_map = 0;
c5347611 1234
54319661
JA
1235 submitter = calloc(nthreads, sizeof(*submitter) +
1236 depth * sizeof(struct iovec));
1237 for (j = 0; j < nthreads; j++) {
1238 s = get_submitter(j);
1239 s->index = j;
1240 s->done = s->calls = s->reaps = 0;
1241 }
e39863e3 1242
701d1277 1243 flags = O_RDONLY | O_NOATIME;
a7086591
JA
1244 if (!buffered)
1245 flags |= O_DIRECT;
1246
54319661 1247 j = 0;
e39863e3 1248 i = optind;
d79ff8c9 1249 nfiles = argc - i;
2ac00585
JA
1250 if (!do_nop) {
1251 if (!nfiles) {
1252 printf("No files specified\n");
1253 usage(argv[0], 1);
1254 }
1255 threads_per_f = nthreads / nfiles;
1256 /* make sure each thread gets assigned files */
1257 if (threads_per_f == 0) {
1258 threads_per_f = 1;
1259 } else {
1260 threads_rem = nthreads - threads_per_f * nfiles;
1261 }
d79ff8c9 1262 }
8025517d 1263 while (!do_nop && i < argc) {
d79ff8c9
AJ
1264 int k, limit;
1265
1266 memset(&f, 0, sizeof(f));
a7086591
JA
1267
1268 fd = open(argv[i], flags);
1269 if (fd < 0) {
1270 perror("open");
1271 return 1;
1272 }
d79ff8c9
AJ
1273 f.real_fd = fd;
1274 if (get_file_size(&f)) {
a7086591
JA
1275 printf("failed getting size of device/file\n");
1276 return 1;
1277 }
d79ff8c9 1278 if (f.max_blocks <= 1) {
a7086591
JA
1279 printf("Zero file/device size?\n");
1280 return 1;
1281 }
d79ff8c9
AJ
1282 f.max_blocks--;
1283
1284 limit = threads_per_f;
1285 limit += threads_rem > 0 ? 1 : 0;
1286 for (k = 0; k < limit; k++) {
1287 s = get_submitter((j + k) % nthreads);
a7086591 1288
d79ff8c9
AJ
1289 if (s->nr_files == MAX_FDS) {
1290 printf("Max number of files (%d) reached\n", MAX_FDS);
1291 break;
1292 }
1293
1294 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1295
1296 printf("Added file %s (submitter %d)\n", argv[i], s->index);
1297 s->nr_files++;
1298 }
1299 threads_rem--;
a7086591 1300 i++;
d79ff8c9 1301 j += limit;
a7086591
JA
1302 }
1303
c9fb4c5b
JA
1304 arm_sig_int();
1305
54319661
JA
1306 for (j = 0; j < nthreads; j++) {
1307 s = get_submitter(j);
1308 for (i = 0; i < depth; i++) {
1309 void *buf;
c9fb4c5b 1310
54319661
JA
1311 if (posix_memalign(&buf, bs, bs)) {
1312 printf("failed alloc\n");
1313 return 1;
1314 }
1315 s->iovecs[i].iov_base = buf;
1316 s->iovecs[i].iov_len = bs;
c9fb4c5b 1317 }
c9fb4c5b
JA
1318 }
1319
54319661
JA
1320 for (j = 0; j < nthreads; j++) {
1321 s = get_submitter(j);
1322
256714ea
JA
1323 if (!aio)
1324 err = setup_ring(s);
1325 else
1326 err = setup_aio(s);
54319661
JA
1327 if (err) {
1328 printf("ring setup failed: %s, %d\n", strerror(errno), err);
1329 return 1;
1330 }
c9fb4c5b 1331 }
54319661 1332 s = get_submitter(0);
a71ad043 1333 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
256714ea
JA
1334 if (!aio)
1335 printf("Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
256714ea 1336 else
09ee86fa 1337 printf("Engine=aio\n");
c9fb4c5b 1338
54319661
JA
1339 for (j = 0; j < nthreads; j++) {
1340 s = get_submitter(j);
256714ea
JA
1341 if (!aio)
1342 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1343#ifdef CONFIG_LIBAIO
1344 else
1345 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1346#endif
54319661 1347 }
c9fb4c5b 1348
54319661 1349 fdepths = malloc(8 * s->nr_files * nthreads);
05138221 1350 reap = calls = done = 0;
c9fb4c5b
JA
1351 do {
1352 unsigned long this_done = 0;
1353 unsigned long this_reap = 0;
1354 unsigned long this_call = 0;
1355 unsigned long rpc = 0, ipc = 0;
f3057d26 1356 unsigned long iops, bw;
c9fb4c5b
JA
1357
1358 sleep(1);
65e1a5e8
EV
1359 if (runtime && !--runtime)
1360 do_finish("timeout");
a1f17100
JA
1361
1362 /* don't print partial run, if interrupted by signal */
1363 if (finish)
1364 break;
52479d8b
JA
1365
1366 /* one second in to the run, enable stats */
1367 if (stats)
1368 stats_running = 1;
1369
54319661 1370 for (j = 0; j < nthreads; j++) {
7d1ce4b7 1371 s = get_submitter(j);
54319661
JA
1372 this_done += s->done;
1373 this_call += s->calls;
1374 this_reap += s->reaps;
1375 }
c9fb4c5b
JA
1376 if (this_call - calls) {
1377 rpc = (this_done - done) / (this_call - calls);
1378 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
1379 } else
1380 rpc = ipc = -1;
2e7888ef 1381 file_depths(fdepths);
22fd3501 1382 iops = this_done - done;
f3057d26
JA
1383 if (bs > 1048576)
1384 bw = iops * (bs / 1048576);
1385 else
1386 bw = iops / (1048576 / bs);
dc10c23a
JA
1387 if (iops > 100000)
1388 printf("IOPS=%luK, ", iops / 1000);
1389 else
53b5fa1e 1390 printf("IOPS=%lu, ", iops);
16d25711 1391 max_iops = max(max_iops, iops);
6c5d3a1c
JA
1392 if (!do_nop)
1393 printf("BW=%luMiB/s, ", bw);
1394 printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths);
c9fb4c5b
JA
1395 done = this_done;
1396 calls = this_call;
1397 reap = this_reap;
1398 } while (!finish);
1399
54319661
JA
1400 for (j = 0; j < nthreads; j++) {
1401 s = get_submitter(j);
1402 pthread_join(s->thread, &ret);
1403 close(s->ring_fd);
932131c9
JA
1404
1405 if (stats) {
1406 unsigned long nr;
1407
1408 printf("%d: Latency percentiles:\n", s->tid);
1409 for (i = 0, nr = 0; i < PLAT_NR; i++)
1410 nr += s->plat[i];
1411 show_clat_percentiles(s->plat, nr, 4);
1412 free(s->clock_batch);
1413 free(s->plat);
1414 }
54319661 1415 }
932131c9 1416
2e7888ef 1417 free(fdepths);
54319661 1418 free(submitter);
c9fb4c5b
JA
1419 return 0;
1420}