stat: convert json output to a new per priority granularity format
[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
bb209d68 195 *minv = -1UL;
932131c9
JA
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{
1db268db
JA
387 /*
388 * Clamp CQ ring size at our SQ ring size, we don't need more entries
389 * than that.
390 */
391 p->flags |= IORING_SETUP_CQSIZE;
392 p->cq_entries = entries;
393
bfed648c 394 return syscall(__NR_io_uring_setup, entries, p);
c9fb4c5b
JA
395}
396
b3915995
JA
397static void io_uring_probe(int fd)
398{
399 struct io_uring_probe *p;
400 int ret;
401
402 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
403 if (!p)
404 return;
405
406 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
407 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
408 if (ret < 0)
409 goto out;
410
411 if (IORING_OP_READ > p->ops_len)
412 goto out;
413
414 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
84106576 415 vectored = 0;
b3915995
JA
416out:
417 free(p);
418}
419
c3e2fc25
JA
420static int io_uring_enter(struct submitter *s, unsigned int to_submit,
421 unsigned int min_complete, unsigned int flags)
c9fb4c5b 422{
bfed648c
JA
423 return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete,
424 flags, NULL, 0);
c9fb4c5b
JA
425}
426
77d814a1 427#ifndef CONFIG_HAVE_GETTID
c9fb4c5b
JA
428static int gettid(void)
429{
430 return syscall(__NR_gettid);
431}
77d814a1 432#endif
c9fb4c5b 433
701d1277
JA
434static unsigned file_depth(struct submitter *s)
435{
e39863e3 436 return (depth + s->nr_files - 1) / s->nr_files;
701d1277
JA
437}
438
a7086591 439static void init_io(struct submitter *s, unsigned index)
c9fb4c5b 440{
f0403f94 441 struct io_uring_sqe *sqe = &s->sqes[index];
c9fb4c5b 442 unsigned long offset;
a7086591 443 struct file *f;
c9fb4c5b
JA
444 long r;
445
8025517d
JA
446 if (do_nop) {
447 sqe->opcode = IORING_OP_NOP;
448 return;
449 }
450
701d1277
JA
451 if (s->nr_files == 1) {
452 f = &s->files[0];
453 } else {
454 f = &s->files[s->cur_file];
455 if (f->pending_ios >= file_depth(s)) {
456 s->cur_file++;
457 if (s->cur_file == s->nr_files)
458 s->cur_file = 0;
93d1811c 459 f = &s->files[s->cur_file];
701d1277
JA
460 }
461 }
462 f->pending_ios++;
a7086591 463
beda9d8d
JA
464 if (random_io) {
465 r = __rand64(&s->rand_state);
466 offset = (r % (f->max_blocks - 1)) * bs;
467 } else {
468 offset = f->cur_off;
469 f->cur_off += bs;
470 if (f->cur_off + bs > f->max_size)
471 f->cur_off = 0;
472 }
c9fb4c5b 473
8c5fa755
JA
474 if (register_files) {
475 sqe->flags = IOSQE_FIXED_FILE;
476 sqe->fd = f->fixed_fd;
477 } else {
478 sqe->flags = 0;
479 sqe->fd = f->real_fd;
480 }
f0403f94 481 if (fixedbufs) {
48e698fa 482 sqe->opcode = IORING_OP_READ_FIXED;
919850d2 483 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
5bd526f2 484 sqe->len = bs;
2ea53ca3 485 sqe->buf_index = index;
84106576 486 } else if (!vectored) {
b3915995
JA
487 sqe->opcode = IORING_OP_READ;
488 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
489 sqe->len = bs;
490 sqe->buf_index = 0;
84106576
JA
491 } else {
492 sqe->opcode = IORING_OP_READV;
493 sqe->addr = (unsigned long) &s->iovecs[index];
494 sqe->len = 1;
495 sqe->buf_index = 0;
f0403f94 496 }
f0403f94 497 sqe->ioprio = 0;
f0403f94 498 sqe->off = offset;
932131c9 499 sqe->user_data = (unsigned long) f->fileno;
52479d8b 500 if (stats && stats_running)
bb209d68 501 sqe->user_data |= ((uint64_t)s->clock_index << 32);
c9fb4c5b
JA
502}
503
256714ea 504static int prep_more_ios_uring(struct submitter *s, int max_ios)
c9fb4c5b 505{
e31b8288 506 struct io_sq_ring *ring = &s->sq_ring;
e2239016 507 unsigned index, tail, next_tail, prepped = 0;
c9fb4c5b 508
c3e2fc25 509 next_tail = tail = *ring->tail;
c9fb4c5b
JA
510 do {
511 next_tail++;
fc2dc21b 512 if (next_tail == atomic_load_acquire(ring->head))
c9fb4c5b
JA
513 break;
514
e39c34dc 515 index = tail & sq_ring_mask;
a7086591 516 init_io(s, index);
c3e2fc25 517 ring->array[index] = index;
c9fb4c5b
JA
518 prepped++;
519 tail = next_tail;
520 } while (prepped < max_ios);
521
fc2dc21b
JA
522 if (prepped)
523 atomic_store_release(ring->tail, tail);
c9fb4c5b
JA
524 return prepped;
525}
526
a7086591 527static int get_file_size(struct file *f)
c9fb4c5b
JA
528{
529 struct stat st;
530
48e698fa 531 if (fstat(f->real_fd, &st) < 0)
c9fb4c5b
JA
532 return -1;
533 if (S_ISBLK(st.st_mode)) {
534 unsigned long long bytes;
535
48e698fa 536 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
c9fb4c5b
JA
537 return -1;
538
5bd526f2 539 f->max_blocks = bytes / bs;
beda9d8d 540 f->max_size = bytes;
c9fb4c5b
JA
541 return 0;
542 } else if (S_ISREG(st.st_mode)) {
5bd526f2 543 f->max_blocks = st.st_size / bs;
beda9d8d 544 f->max_size = st.st_size;
c9fb4c5b
JA
545 return 0;
546 }
547
548 return -1;
549}
550
256714ea 551static int reap_events_uring(struct submitter *s)
c9fb4c5b 552{
e31b8288 553 struct io_cq_ring *ring = &s->cq_ring;
f0403f94 554 struct io_uring_cqe *cqe;
e2239016 555 unsigned head, reaped = 0;
ab85494f 556 int last_idx = -1, stat_nr = 0;
c9fb4c5b 557
c3e2fc25 558 head = *ring->head;
c9fb4c5b 559 do {
701d1277
JA
560 struct file *f;
561
679d8352 562 read_barrier();
fc2dc21b 563 if (head == atomic_load_acquire(ring->tail))
c9fb4c5b 564 break;
f0403f94 565 cqe = &ring->cqes[head & cq_ring_mask];
8025517d 566 if (!do_nop) {
932131c9
JA
567 int fileno = cqe->user_data & 0xffffffff;
568
569 f = &s->files[fileno];
8025517d 570 f->pending_ios--;
5bd526f2 571 if (cqe->res != bs) {
8025517d 572 printf("io: unexpected ret=%d\n", cqe->res);
154a9582 573 if (polled && cqe->res == -EOPNOTSUPP)
8066f6b6 574 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
8025517d
JA
575 return -1;
576 }
c9fb4c5b 577 }
932131c9
JA
578 if (stats) {
579 int clock_index = cqe->user_data >> 32;
580
ab85494f
JA
581 if (last_idx != clock_index) {
582 if (last_idx != -1) {
583 add_stat(s, last_idx, stat_nr);
584 stat_nr = 0;
585 }
586 last_idx = clock_index;
d4af2ece
JA
587 }
588 stat_nr++;
932131c9 589 }
c9fb4c5b
JA
590 reaped++;
591 head++;
c9fb4c5b
JA
592 } while (1);
593
ab85494f
JA
594 if (stat_nr)
595 add_stat(s, last_idx, stat_nr);
596
fc2dc21b
JA
597 if (reaped) {
598 s->inflight -= reaped;
599 atomic_store_release(ring->head, head);
600 }
c9fb4c5b
JA
601 return reaped;
602}
603
256714ea 604static int submitter_init(struct submitter *s)
c9fb4c5b 605{
256714ea 606 int i, nr_batch;
c9fb4c5b 607
932131c9 608 s->tid = gettid();
7e847fd0 609 printf("submitter=%d, tid=%d\n", s->index, s->tid);
c9fb4c5b 610
9eff5320 611 __init_rand64(&s->rand_state, pthread_self());
5e8865c0 612 srand48(pthread_self());
c9fb4c5b 613
932131c9
JA
614 for (i = 0; i < MAX_FDS; i++)
615 s->files[i].fileno = i;
616
617 if (stats) {
618 nr_batch = roundup_pow2(depth / batch_submit);
d4af2ece
JA
619 if (nr_batch < 2)
620 nr_batch = 2;
932131c9 621 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
52479d8b 622 s->clock_index = 1;
932131c9
JA
623
624 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
625 } else {
626 s->clock_batch = NULL;
627 s->plat = NULL;
628 nr_batch = 0;
629 }
630
256714ea
JA
631 return nr_batch;
632}
633
634#ifdef CONFIG_LIBAIO
635static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
636{
8310c570
VF
637 uint64_t data;
638 long long offset;
256714ea
JA
639 struct file *f;
640 unsigned index;
641 long r;
642
643 index = 0;
644 while (index < max_ios) {
645 struct iocb *iocb = &iocbs[index];
646
647 if (s->nr_files == 1) {
648 f = &s->files[0];
649 } else {
650 f = &s->files[s->cur_file];
651 if (f->pending_ios >= file_depth(s)) {
652 s->cur_file++;
653 if (s->cur_file == s->nr_files)
654 s->cur_file = 0;
655 f = &s->files[s->cur_file];
656 }
657 }
658 f->pending_ios++;
659
660 r = lrand48();
661 offset = (r % (f->max_blocks - 1)) * bs;
662 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
663 s->iovecs[index].iov_len, offset);
664
665 data = f->fileno;
52479d8b 666 if (stats && stats_running)
8310c570 667 data |= (((uint64_t) s->clock_index) << 32);
256714ea
JA
668 iocb->data = (void *) (uintptr_t) data;
669 index++;
670 }
671 return index;
672}
673
674static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
675{
676 int last_idx = -1, stat_nr = 0;
677 int reaped = 0;
678
679 while (evs) {
8310c570 680 uint64_t data = (uintptr_t) events[reaped].data;
256714ea
JA
681 struct file *f = &s->files[data & 0xffffffff];
682
683 f->pending_ios--;
684 if (events[reaped].res != bs) {
685 printf("io: unexpected ret=%ld\n", events[reaped].res);
686 return -1;
687 }
688 if (stats) {
689 int clock_index = data >> 32;
690
691 if (last_idx != clock_index) {
692 if (last_idx != -1) {
693 add_stat(s, last_idx, stat_nr);
694 stat_nr = 0;
695 }
696 last_idx = clock_index;
d4af2ece
JA
697 }
698 stat_nr++;
256714ea
JA
699 }
700 reaped++;
701 evs--;
702 }
703
704 if (stat_nr)
705 add_stat(s, last_idx, stat_nr);
706
707 s->inflight -= reaped;
708 s->done += reaped;
709 return reaped;
710}
711
712static void *submitter_aio_fn(void *data)
713{
714 struct submitter *s = data;
715 int i, ret, prepped, nr_batch;
716 struct iocb **iocbsptr;
717 struct iocb *iocbs;
718 struct io_event *events;
719
720 nr_batch = submitter_init(s);
721
722 iocbsptr = calloc(depth, sizeof(struct iocb *));
723 iocbs = calloc(depth, sizeof(struct iocb));
724 events = calloc(depth, sizeof(struct io_event));
725
726 for (i = 0; i < depth; i++)
727 iocbsptr[i] = &iocbs[i];
728
729 prepped = 0;
730 do {
731 int to_wait, to_submit, to_prep;
732
733 if (!prepped && s->inflight < depth) {
734 to_prep = min(depth - s->inflight, batch_submit);
735 prepped = prep_more_ios_aio(s, to_prep, iocbs);
736#ifdef ARCH_HAVE_CPU_CLOCK
737 if (prepped && stats) {
738 s->clock_batch[s->clock_index] = get_cpu_clock();
739 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
740 }
741#endif
742 }
743 s->inflight += prepped;
744 to_submit = prepped;
745
746 if (to_submit && (s->inflight + to_submit <= depth))
747 to_wait = 0;
748 else
749 to_wait = min(s->inflight + to_submit, batch_complete);
750
751 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
752 s->calls++;
753 if (ret < 0) {
754 perror("io_submit");
755 break;
756 } else if (ret != to_submit) {
757 printf("submitted %d, wanted %d\n", ret, to_submit);
758 break;
759 }
760 prepped = 0;
761
24a24c12 762 while (to_wait) {
256714ea
JA
763 int r;
764
24a24c12
JA
765 s->calls++;
766 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
767 if (r < 0) {
768 perror("io_getevents");
769 break;
770 } else if (r != to_wait) {
771 printf("r=%d, wait=%d\n", r, to_wait);
772 break;
773 }
774 r = reap_events_aio(s, events, r);
775 s->reaps += r;
776 to_wait -= r;
256714ea
JA
777 }
778 } while (!s->finish);
779
780 free(iocbsptr);
781 free(iocbs);
782 free(events);
783 finish = 1;
784 return NULL;
785}
786#endif
787
788static void *submitter_uring_fn(void *data)
789{
790 struct submitter *s = data;
791 struct io_sq_ring *ring = &s->sq_ring;
792 int ret, prepped, nr_batch;
793
794 nr_batch = submitter_init(s);
795
c9fb4c5b
JA
796 prepped = 0;
797 do {
f310970e 798 int to_wait, to_submit, this_reap, to_prep;
fc2dc21b 799 unsigned ring_flags = 0;
c9fb4c5b 800
e39863e3
KB
801 if (!prepped && s->inflight < depth) {
802 to_prep = min(depth - s->inflight, batch_submit);
256714ea 803 prepped = prep_more_ios_uring(s, to_prep);
932131c9
JA
804#ifdef ARCH_HAVE_CPU_CLOCK
805 if (prepped && stats) {
806 s->clock_batch[s->clock_index] = get_cpu_clock();
807 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
808 }
809#endif
f310970e 810 }
c9fb4c5b
JA
811 s->inflight += prepped;
812submit_more:
813 to_submit = prepped;
814submit:
e39863e3 815 if (to_submit && (s->inflight + to_submit <= depth))
c9fb4c5b
JA
816 to_wait = 0;
817 else
e39863e3 818 to_wait = min(s->inflight + to_submit, batch_complete);
c9fb4c5b 819
ce1705de
JA
820 /*
821 * Only need to call io_uring_enter if we're not using SQ thread
822 * poll, or if IORING_SQ_NEED_WAKEUP is set.
823 */
fc2dc21b
JA
824 if (sq_thread_poll)
825 ring_flags = atomic_load_acquire(ring->flags);
826 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
e0abe388
JA
827 unsigned flags = 0;
828
829 if (to_wait)
830 flags = IORING_ENTER_GETEVENTS;
fc2dc21b 831 if (ring_flags & IORING_SQ_NEED_WAKEUP)
b532dd6d 832 flags |= IORING_ENTER_SQ_WAKEUP;
e0abe388 833 ret = io_uring_enter(s, to_submit, to_wait, flags);
ce1705de 834 s->calls++;
fc2dc21b
JA
835 } else {
836 /* for SQPOLL, we submitted it all effectively */
837 ret = to_submit;
ce1705de 838 }
c9fb4c5b 839
ce1705de
JA
840 /*
841 * For non SQ thread poll, we already got the events we needed
842 * through the io_uring_enter() above. For SQ thread poll, we
843 * need to loop here until we find enough events.
844 */
845 this_reap = 0;
846 do {
847 int r;
256714ea
JA
848
849 r = reap_events_uring(s);
7d1435e6
JA
850 if (r == -1) {
851 s->finish = 1;
ce1705de 852 break;
7d1435e6 853 } else if (r > 0)
ce1705de
JA
854 this_reap += r;
855 } while (sq_thread_poll && this_reap < to_wait);
c9fb4c5b
JA
856 s->reaps += this_reap;
857
858 if (ret >= 0) {
859 if (!ret) {
860 to_submit = 0;
861 if (s->inflight)
862 goto submit;
863 continue;
864 } else if (ret < to_submit) {
865 int diff = to_submit - ret;
866
867 s->done += ret;
868 prepped -= diff;
869 goto submit_more;
870 }
871 s->done += ret;
872 prepped = 0;
873 continue;
874 } else if (ret < 0) {
ac122fea 875 if (errno == EAGAIN) {
c9fb4c5b
JA
876 if (s->finish)
877 break;
878 if (this_reap)
879 goto submit;
c9fb4c5b
JA
880 to_submit = 0;
881 goto submit;
882 }
ac122fea 883 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
884 break;
885 }
886 } while (!s->finish);
a7086591 887
c9fb4c5b
JA
888 finish = 1;
889 return NULL;
890}
891
54319661
JA
892static struct submitter *get_submitter(int offset)
893{
894 void *ret;
895
896 ret = submitter;
897 if (offset)
898 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
899 return ret;
900}
901
65e1a5e8 902static void do_finish(const char *reason)
c9fb4c5b 903{
54319661 904 int j;
65e1a5e8 905 printf("Exiting on %s\n", reason);
54319661
JA
906 for (j = 0; j < nthreads; j++) {
907 struct submitter *s = get_submitter(j);
908 s->finish = 1;
909 }
16d25711 910 if (max_iops > 100000)
18b557a0 911 printf("Maximum IOPS=%luK\n", max_iops / 1000);
16d25711 912 else if (max_iops)
18b557a0 913 printf("Maximum IOPS=%lu\n", max_iops);
c9fb4c5b
JA
914 finish = 1;
915}
916
65e1a5e8
EV
917static void sig_int(int sig)
918{
919 do_finish("signal");
920}
921
c9fb4c5b
JA
922static void arm_sig_int(void)
923{
924 struct sigaction act;
925
926 memset(&act, 0, sizeof(act));
927 act.sa_handler = sig_int;
928 act.sa_flags = SA_RESTART;
929 sigaction(SIGINT, &act, NULL);
2cf71009
BP
930
931 /* Windows uses SIGBREAK as a quit signal from other applications */
932#ifdef WIN32
933 sigaction(SIGBREAK, &act, NULL);
934#endif
c9fb4c5b
JA
935}
936
256714ea
JA
937static int setup_aio(struct submitter *s)
938{
939#ifdef CONFIG_LIBAIO
940 if (polled) {
941 fprintf(stderr, "aio does not support polled IO\n");
942 polled = 0;
943 }
944 if (sq_thread_poll) {
945 fprintf(stderr, "aio does not support SQPOLL IO\n");
946 sq_thread_poll = 0;
947 }
948 if (do_nop) {
949 fprintf(stderr, "aio does not support polled IO\n");
950 do_nop = 0;
951 }
952 if (fixedbufs || register_files) {
953 fprintf(stderr, "aio does not support registered files or buffers\n");
954 fixedbufs = register_files = 0;
955 }
956
957 return io_queue_init(depth, &s->aio_ctx);
958#else
959 fprintf(stderr, "Legacy AIO not available on this system/build\n");
960 errno = EINVAL;
961 return -1;
962#endif
963}
964
c3e2fc25
JA
965static int setup_ring(struct submitter *s)
966{
e31b8288
JA
967 struct io_sq_ring *sring = &s->sq_ring;
968 struct io_cq_ring *cring = &s->cq_ring;
969 struct io_uring_params p;
2ea53ca3 970 int ret, fd;
c3e2fc25 971 void *ptr;
c3e2fc25
JA
972
973 memset(&p, 0, sizeof(p));
974
7d1435e6 975 if (polled && !do_nop)
0e47f11b 976 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3 977 if (sq_thread_poll) {
2ea53ca3 978 p.flags |= IORING_SETUP_SQPOLL;
3ade18a3 979 if (sq_thread_cpu != -1) {
3d7d00a3 980 p.flags |= IORING_SETUP_SQ_AFF;
3ade18a3
JA
981 p.sq_thread_cpu = sq_thread_cpu;
982 }
c3e2fc25
JA
983 }
984
e39863e3 985 fd = io_uring_setup(depth, &p);
c3e2fc25
JA
986 if (fd < 0) {
987 perror("io_uring_setup");
988 return 1;
989 }
f310970e 990 s->ring_fd = fd;
2ea53ca3 991
b3915995
JA
992 io_uring_probe(fd);
993
2ea53ca3 994 if (fixedbufs) {
15130e3f
JA
995 struct rlimit rlim;
996
997 rlim.rlim_cur = RLIM_INFINITY;
998 rlim.rlim_max = RLIM_INFINITY;
999 /* ignore potential error, not needed on newer kernels */
1000 setrlimit(RLIMIT_MEMLOCK, &rlim);
1001
2ea53ca3
JA
1002 ret = io_uring_register_buffers(s);
1003 if (ret < 0) {
a7abc9fb 1004 perror("io_uring_register_buffers");
2ea53ca3
JA
1005 return 1;
1006 }
a71ad043
JA
1007
1008 if (dma_map) {
1009 ret = io_uring_map_buffers(s);
1010 if (ret < 0) {
1011 perror("io_uring_map_buffers");
1012 return 1;
1013 }
1014 }
2ea53ca3
JA
1015 }
1016
8c5fa755
JA
1017 if (register_files) {
1018 ret = io_uring_register_files(s);
1019 if (ret < 0) {
1020 perror("io_uring_register_files");
1021 return 1;
1022 }
a7abc9fb
JA
1023 }
1024
e2239016 1025 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
f310970e
JA
1026 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1027 IORING_OFF_SQ_RING);
c3e2fc25
JA
1028 sring->head = ptr + p.sq_off.head;
1029 sring->tail = ptr + p.sq_off.tail;
1030 sring->ring_mask = ptr + p.sq_off.ring_mask;
1031 sring->ring_entries = ptr + p.sq_off.ring_entries;
ce1705de 1032 sring->flags = ptr + p.sq_off.flags;
ac122fea 1033 sring->array = ptr + p.sq_off.array;
c3e2fc25
JA
1034 sq_ring_mask = *sring->ring_mask;
1035
f0403f94 1036 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
f310970e 1037 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
f0403f94 1038 IORING_OFF_SQES);
c3e2fc25 1039
f0403f94 1040 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
f310970e
JA
1041 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
1042 IORING_OFF_CQ_RING);
c3e2fc25
JA
1043 cring->head = ptr + p.cq_off.head;
1044 cring->tail = ptr + p.cq_off.tail;
1045 cring->ring_mask = ptr + p.cq_off.ring_mask;
1046 cring->ring_entries = ptr + p.cq_off.ring_entries;
f0403f94 1047 cring->cqes = ptr + p.cq_off.cqes;
c3e2fc25
JA
1048 cq_ring_mask = *cring->ring_mask;
1049 return 0;
1050}
1051
2e7888ef
JA
1052static void file_depths(char *buf)
1053{
26976a13 1054 bool prev = false;
2e7888ef 1055 char *p;
54319661 1056 int i, j;
2e7888ef 1057
ac21bfab 1058 buf[0] = '\0';
2e7888ef 1059 p = buf;
54319661
JA
1060 for (j = 0; j < nthreads; j++) {
1061 struct submitter *s = get_submitter(j);
2e7888ef 1062
54319661
JA
1063 for (i = 0; i < s->nr_files; i++) {
1064 struct file *f = &s->files[i];
1065
26976a13
JA
1066 if (prev)
1067 p += sprintf(p, " %d", f->pending_ios);
54319661 1068 else
4f215227 1069 p += sprintf(p, "%d", f->pending_ios);
26976a13 1070 prev = true;
54319661 1071 }
2e7888ef
JA
1072 }
1073}
1074
d79ff8c9 1075static void usage(char *argv, int status)
e39863e3 1076{
65e1a5e8
EV
1077 char runtime_str[16];
1078 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
e39863e3 1079 printf("%s [options] -- [filenames]\n"
35268e11
AJ
1080 " -d <int> : IO Depth, default %d\n"
1081 " -s <int> : Batch submit, default %d\n"
1082 " -c <int> : Batch complete, default %d\n"
1083 " -b <int> : Block size, default %d\n"
1084 " -p <bool> : Polled IO, default %d\n"
1085 " -B <bool> : Fixed buffers, default %d\n"
dd1f1ba0 1086 " -D <bool> : DMA map fixed buffers, default %d\n"
35268e11 1087 " -F <bool> : Register files, default %d\n"
0862f718 1088 " -n <int> : Number of threads, default %d\n"
2686fc22 1089 " -O <bool> : Use O_DIRECT, default %d\n"
4a39c524
EV
1090 " -N <bool> : Perform just no-op requests, default %d\n"
1091 " -t <bool> : Track IO latencies, default %d\n"
256714ea 1092 " -T <int> : TSC rate in HZ\n"
beda9d8d
JA
1093 " -r <int> : Runtime in seconds, default %s\n"
1094 " -R <bool> : Use random IO, default %d\n"
1095 " -a <bool> : Use legacy aio, default %d\n",
35268e11 1096 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
a71ad043 1097 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
5d3ea96a 1098 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio);
d79ff8c9 1099 exit(status);
e39863e3
KB
1100}
1101
203e4c26
JA
1102static void read_tsc_rate(void)
1103{
1104 char buffer[32];
1105 int fd, ret;
1106
1107 if (tsc_rate)
1108 return;
1109
1110 fd = open(TSC_RATE_FILE, O_RDONLY);
1111 if (fd < 0)
1112 return;
1113
1114 ret = read(fd, buffer, sizeof(buffer));
1115 if (ret < 0) {
1116 close(fd);
1117 return;
1118 }
1119
1120 tsc_rate = strtoul(buffer, NULL, 10);
1121 printf("Using TSC rate %luHz\n", tsc_rate);
1122 close(fd);
1123}
1124
1125static void write_tsc_rate(void)
1126{
1127 char buffer[32];
1128 struct stat sb;
1129 int fd, ret;
1130
1131 if (!stat(TSC_RATE_FILE, &sb))
1132 return;
1133
1134 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1135 if (fd < 0)
1136 return;
1137
1138 memset(buffer, 0, sizeof(buffer));
1139 sprintf(buffer, "%lu", tsc_rate);
1140 ret = write(fd, buffer, strlen(buffer));
1141 if (ret < 0)
1142 perror("write");
1143 close(fd);
1144}
1145
c9fb4c5b
JA
1146int main(int argc, char *argv[])
1147{
e39863e3 1148 struct submitter *s;
05138221 1149 unsigned long done, calls, reap;
d79ff8c9
AJ
1150 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
1151 struct file f;
2e7888ef 1152 char *fdepths;
c3e2fc25 1153 void *ret;
c9fb4c5b 1154
0862f718
JA
1155 if (!do_nop && argc < 2)
1156 usage(argv[0], 1);
c9fb4c5b 1157
beda9d8d 1158 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:r:D:R:h?")) != -1) {
e39863e3 1159 switch (opt) {
256714ea
JA
1160 case 'a':
1161 aio = !!atoi(optarg);
1162 break;
e39863e3
KB
1163 case 'd':
1164 depth = atoi(optarg);
1165 break;
1166 case 's':
1167 batch_submit = atoi(optarg);
932131c9
JA
1168 if (!batch_submit)
1169 batch_submit = 1;
e39863e3
KB
1170 break;
1171 case 'c':
1172 batch_complete = atoi(optarg);
932131c9
JA
1173 if (!batch_complete)
1174 batch_complete = 1;
e39863e3 1175 break;
5bd526f2
JA
1176 case 'b':
1177 bs = atoi(optarg);
1178 break;
1179 case 'p':
1180 polled = !!atoi(optarg);
1181 break;
6a87c3b0
JA
1182 case 'B':
1183 fixedbufs = !!atoi(optarg);
1184 break;
1185 case 'F':
1186 register_files = !!atoi(optarg);
1187 break;
54319661
JA
1188 case 'n':
1189 nthreads = atoi(optarg);
caf2b9ac
JA
1190 if (!nthreads) {
1191 printf("Threads must be non-zero\n");
1192 usage(argv[0], 1);
1193 }
54319661 1194 break;
0862f718
JA
1195 case 'N':
1196 do_nop = !!atoi(optarg);
1197 break;
2686fc22
JA
1198 case 'O':
1199 buffered = !atoi(optarg);
1200 break;
932131c9
JA
1201 case 't':
1202#ifndef ARCH_HAVE_CPU_CLOCK
1203 fprintf(stderr, "Stats not supported on this CPU\n");
1204 return 1;
1205#endif
1206 stats = !!atoi(optarg);
1207 break;
1208 case 'T':
1209#ifndef ARCH_HAVE_CPU_CLOCK
1210 fprintf(stderr, "Stats not supported on this CPU\n");
1211 return 1;
1212#endif
1213 tsc_rate = strtoul(optarg, NULL, 10);
203e4c26 1214 write_tsc_rate();
932131c9 1215 break;
65e1a5e8
EV
1216 case 'r':
1217 runtime = atoi(optarg);
1218 break;
a71ad043
JA
1219 case 'D':
1220 dma_map = !!atoi(optarg);
1221 break;
beda9d8d
JA
1222 case 'R':
1223 random_io = !!atoi(optarg);
1224 break;
e39863e3
KB
1225 case 'h':
1226 case '?':
1227 default:
d79ff8c9 1228 usage(argv[0], 0);
e39863e3
KB
1229 break;
1230 }
1231 }
1232
203e4c26
JA
1233 if (stats)
1234 read_tsc_rate();
1235
c5347611
JA
1236 if (batch_complete > depth)
1237 batch_complete = depth;
1238 if (batch_submit > depth)
1239 batch_submit = depth;
a71ad043
JA
1240 if (!fixedbufs && dma_map)
1241 dma_map = 0;
c5347611 1242
54319661
JA
1243 submitter = calloc(nthreads, sizeof(*submitter) +
1244 depth * sizeof(struct iovec));
1245 for (j = 0; j < nthreads; j++) {
1246 s = get_submitter(j);
1247 s->index = j;
1248 s->done = s->calls = s->reaps = 0;
1249 }
e39863e3 1250
701d1277 1251 flags = O_RDONLY | O_NOATIME;
a7086591
JA
1252 if (!buffered)
1253 flags |= O_DIRECT;
1254
54319661 1255 j = 0;
e39863e3 1256 i = optind;
d79ff8c9 1257 nfiles = argc - i;
2ac00585
JA
1258 if (!do_nop) {
1259 if (!nfiles) {
1260 printf("No files specified\n");
1261 usage(argv[0], 1);
1262 }
1263 threads_per_f = nthreads / nfiles;
1264 /* make sure each thread gets assigned files */
1265 if (threads_per_f == 0) {
1266 threads_per_f = 1;
1267 } else {
1268 threads_rem = nthreads - threads_per_f * nfiles;
1269 }
d79ff8c9 1270 }
8025517d 1271 while (!do_nop && i < argc) {
d79ff8c9
AJ
1272 int k, limit;
1273
1274 memset(&f, 0, sizeof(f));
a7086591
JA
1275
1276 fd = open(argv[i], flags);
1277 if (fd < 0) {
1278 perror("open");
1279 return 1;
1280 }
d79ff8c9
AJ
1281 f.real_fd = fd;
1282 if (get_file_size(&f)) {
a7086591
JA
1283 printf("failed getting size of device/file\n");
1284 return 1;
1285 }
d79ff8c9 1286 if (f.max_blocks <= 1) {
a7086591
JA
1287 printf("Zero file/device size?\n");
1288 return 1;
1289 }
d79ff8c9
AJ
1290 f.max_blocks--;
1291
1292 limit = threads_per_f;
1293 limit += threads_rem > 0 ? 1 : 0;
1294 for (k = 0; k < limit; k++) {
1295 s = get_submitter((j + k) % nthreads);
a7086591 1296
d79ff8c9
AJ
1297 if (s->nr_files == MAX_FDS) {
1298 printf("Max number of files (%d) reached\n", MAX_FDS);
1299 break;
1300 }
1301
1302 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1303
1304 printf("Added file %s (submitter %d)\n", argv[i], s->index);
1305 s->nr_files++;
1306 }
1307 threads_rem--;
a7086591 1308 i++;
d79ff8c9 1309 j += limit;
a7086591
JA
1310 }
1311
c9fb4c5b
JA
1312 arm_sig_int();
1313
54319661
JA
1314 for (j = 0; j < nthreads; j++) {
1315 s = get_submitter(j);
1316 for (i = 0; i < depth; i++) {
1317 void *buf;
c9fb4c5b 1318
54319661
JA
1319 if (posix_memalign(&buf, bs, bs)) {
1320 printf("failed alloc\n");
1321 return 1;
1322 }
1323 s->iovecs[i].iov_base = buf;
1324 s->iovecs[i].iov_len = bs;
c9fb4c5b 1325 }
c9fb4c5b
JA
1326 }
1327
54319661
JA
1328 for (j = 0; j < nthreads; j++) {
1329 s = get_submitter(j);
1330
256714ea
JA
1331 if (!aio)
1332 err = setup_ring(s);
1333 else
1334 err = setup_aio(s);
54319661
JA
1335 if (err) {
1336 printf("ring setup failed: %s, %d\n", strerror(errno), err);
1337 return 1;
1338 }
c9fb4c5b 1339 }
54319661 1340 s = get_submitter(0);
a71ad043 1341 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
256714ea
JA
1342 if (!aio)
1343 printf("Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
256714ea 1344 else
09ee86fa 1345 printf("Engine=aio\n");
c9fb4c5b 1346
54319661
JA
1347 for (j = 0; j < nthreads; j++) {
1348 s = get_submitter(j);
256714ea
JA
1349 if (!aio)
1350 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1351#ifdef CONFIG_LIBAIO
1352 else
1353 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1354#endif
54319661 1355 }
c9fb4c5b 1356
54319661 1357 fdepths = malloc(8 * s->nr_files * nthreads);
05138221 1358 reap = calls = done = 0;
c9fb4c5b
JA
1359 do {
1360 unsigned long this_done = 0;
1361 unsigned long this_reap = 0;
1362 unsigned long this_call = 0;
1363 unsigned long rpc = 0, ipc = 0;
f3057d26 1364 unsigned long iops, bw;
c9fb4c5b
JA
1365
1366 sleep(1);
65e1a5e8
EV
1367 if (runtime && !--runtime)
1368 do_finish("timeout");
a1f17100
JA
1369
1370 /* don't print partial run, if interrupted by signal */
1371 if (finish)
1372 break;
52479d8b
JA
1373
1374 /* one second in to the run, enable stats */
1375 if (stats)
1376 stats_running = 1;
1377
54319661 1378 for (j = 0; j < nthreads; j++) {
7d1ce4b7 1379 s = get_submitter(j);
54319661
JA
1380 this_done += s->done;
1381 this_call += s->calls;
1382 this_reap += s->reaps;
1383 }
c9fb4c5b
JA
1384 if (this_call - calls) {
1385 rpc = (this_done - done) / (this_call - calls);
1386 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
1387 } else
1388 rpc = ipc = -1;
2e7888ef 1389 file_depths(fdepths);
22fd3501 1390 iops = this_done - done;
f3057d26
JA
1391 if (bs > 1048576)
1392 bw = iops * (bs / 1048576);
1393 else
1394 bw = iops / (1048576 / bs);
dc10c23a
JA
1395 if (iops > 100000)
1396 printf("IOPS=%luK, ", iops / 1000);
1397 else
53b5fa1e 1398 printf("IOPS=%lu, ", iops);
16d25711 1399 max_iops = max(max_iops, iops);
6c5d3a1c
JA
1400 if (!do_nop)
1401 printf("BW=%luMiB/s, ", bw);
1402 printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths);
c9fb4c5b
JA
1403 done = this_done;
1404 calls = this_call;
1405 reap = this_reap;
1406 } while (!finish);
1407
54319661
JA
1408 for (j = 0; j < nthreads; j++) {
1409 s = get_submitter(j);
1410 pthread_join(s->thread, &ret);
1411 close(s->ring_fd);
932131c9
JA
1412
1413 if (stats) {
1414 unsigned long nr;
1415
1416 printf("%d: Latency percentiles:\n", s->tid);
1417 for (i = 0, nr = 0; i < PLAT_NR; i++)
1418 nr += s->plat[i];
1419 show_clat_percentiles(s->plat, nr, 4);
1420 free(s->clock_batch);
1421 free(s->plat);
1422 }
54319661 1423 }
932131c9 1424
2e7888ef 1425 free(fdepths);
54319661 1426 free(submitter);
c9fb4c5b
JA
1427 return 0;
1428}