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