t/io_uring: add support for latency tracking
[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
JA
9
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <sys/ioctl.h>
13#include <sys/syscall.h>
14#include <sys/resource.h>
c3e2fc25 15#include <sys/mman.h>
e31b8288 16#include <sys/uio.h>
c9fb4c5b
JA
17#include <linux/fs.h>
18#include <fcntl.h>
19#include <unistd.h>
c9fb4c5b
JA
20#include <string.h>
21#include <pthread.h>
22#include <sched.h>
23
74efb029 24#include "../arch/arch.h"
57fa61f0 25#include "../lib/types.h"
932131c9
JA
26#include "../lib/roundup.h"
27#include "../minmax.h"
f3e769a4 28#include "../os/linux/io_uring.h"
ac122fea 29
e31b8288 30struct io_sq_ring {
e2239016
JA
31 unsigned *head;
32 unsigned *tail;
33 unsigned *ring_mask;
34 unsigned *ring_entries;
ce1705de 35 unsigned *flags;
e2239016 36 unsigned *array;
c9fb4c5b
JA
37};
38
e31b8288 39struct io_cq_ring {
e2239016
JA
40 unsigned *head;
41 unsigned *tail;
42 unsigned *ring_mask;
43 unsigned *ring_entries;
f0403f94 44 struct io_uring_cqe *cqes;
c9fb4c5b
JA
45};
46
701d1277 47#define DEPTH 128
2e7888ef
JA
48#define BATCH_SUBMIT 32
49#define BATCH_COMPLETE 32
c9fb4c5b
JA
50#define BS 4096
51
a7086591
JA
52#define MAX_FDS 16
53
c3e2fc25 54static unsigned sq_ring_mask, cq_ring_mask;
e39c34dc 55
a7086591
JA
56struct file {
57 unsigned long max_blocks;
701d1277 58 unsigned pending_ios;
48e698fa
JA
59 int real_fd;
60 int fixed_fd;
932131c9 61 int fileno;
a7086591
JA
62};
63
932131c9
JA
64#define PLAT_BITS 6
65#define PLAT_VAL (1 << PLAT_BITS)
66#define PLAT_GROUP_NR 29
67#define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
68
c9fb4c5b
JA
69struct submitter {
70 pthread_t thread;
f310970e 71 int ring_fd;
54319661 72 int index;
e31b8288 73 struct io_sq_ring sq_ring;
f0403f94 74 struct io_uring_sqe *sqes;
e31b8288 75 struct io_cq_ring cq_ring;
c9fb4c5b 76 int inflight;
932131c9 77 int tid;
c9fb4c5b
JA
78 unsigned long reaps;
79 unsigned long done;
80 unsigned long calls;
81 volatile int finish;
701d1277 82
48e698fa
JA
83 __s32 *fds;
84
932131c9
JA
85 unsigned long *clock_batch;
86 int clock_index;
87 unsigned long *plat;
88
a7086591
JA
89 struct file files[MAX_FDS];
90 unsigned nr_files;
91 unsigned cur_file;
e39863e3 92 struct iovec iovecs[];
c9fb4c5b
JA
93};
94
e39863e3 95static struct submitter *submitter;
c9fb4c5b
JA
96static volatile int finish;
97
e39863e3
KB
98static int depth = DEPTH;
99static int batch_submit = BATCH_SUBMIT;
100static int batch_complete = BATCH_COMPLETE;
5bd526f2 101static int bs = BS;
f0403f94 102static int polled = 1; /* use IO polling */
701d1277 103static int fixedbufs = 1; /* use fixed user buffers */
8c5fa755 104static int register_files = 1; /* use fixed files */
f0403f94 105static int buffered = 0; /* use buffered IO, not O_DIRECT */
3d7d00a3
JA
106static int sq_thread_poll = 0; /* use kernel submission/poller thread */
107static int sq_thread_cpu = -1; /* pin above thread to this CPU */
8025517d 108static int do_nop = 0; /* no-op SQ ring commands */
54319661 109static int nthreads = 1;
932131c9
JA
110static int stats = 0; /* generate IO stats */
111static unsigned long tsc_rate;
c9fb4c5b 112
84106576 113static int vectored = 1;
b3915995 114
932131c9
JA
115static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
116 80.0, 90.0, 95.0, 99.9, 99.5, 99.9, 99.95, 99.99 };
117static int plist_len = 17;
118
119static unsigned long cycles_to_nsec(unsigned long cycles)
120{
121 uint64_t val;
122
123 if (!tsc_rate)
124 return cycles;
125
126 val = cycles * 1000000000ULL;
127 return val / tsc_rate;
128}
129
130static unsigned long plat_idx_to_val(unsigned int idx)
131{
132 unsigned int error_bits;
133 unsigned long k, base;
134
135 assert(idx < PLAT_NR);
136
137 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
138 * all bits of the sample as index */
139 if (idx < (PLAT_VAL << 1))
140 return cycles_to_nsec(idx);
141
142 /* Find the group and compute the minimum value of that group */
143 error_bits = (idx >> PLAT_BITS) - 1;
144 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
145
146 /* Find its bucket number of the group */
147 k = idx % PLAT_VAL;
148
149 /* Return the mean of the range of the bucket */
150 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
151}
152
153unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
154 unsigned long **output,
155 unsigned long *maxv, unsigned long *minv)
156{
157 unsigned long sum = 0;
158 unsigned int len = plist_len, i, j = 0;
159 unsigned long *ovals = NULL;
160 bool is_last;
161
162 *minv = -1ULL;
163 *maxv = 0;
164
165 ovals = malloc(len * sizeof(*ovals));
166 if (!ovals)
167 return 0;
168
169 /*
170 * Calculate bucket values, note down max and min values
171 */
172 is_last = false;
173 for (i = 0; i < PLAT_NR && !is_last; i++) {
174 sum += io_u_plat[i];
175 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
176 assert(plist[j] <= 100.0);
177
178 ovals[j] = plat_idx_to_val(i);
179 if (ovals[j] < *minv)
180 *minv = ovals[j];
181 if (ovals[j] > *maxv)
182 *maxv = ovals[j];
183
184 is_last = (j == len - 1) != 0;
185 if (is_last)
186 break;
187
188 j++;
189 }
190 }
191
192 if (!is_last)
193 fprintf(stderr, "error calculating latency percentiles\n");
194
195 *output = ovals;
196 return len;
197}
198
199static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
200 unsigned int precision)
201{
202 unsigned int divisor, len, i, j = 0;
203 unsigned long minv, maxv;
204 unsigned long *ovals;
205 int per_line, scale_down, time_width;
206 bool is_last;
207 char fmt[32];
208
209 len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
210 if (!len || !ovals)
211 goto out;
212
213 if (!tsc_rate) {
214 scale_down = 0;
215 divisor = 1;
216 printf(" percentiles (tsc ticks):\n |");
217 } else if (minv > 2000 && maxv > 99999) {
218 scale_down = 1;
219 divisor = 1000;
220 printf(" percentiles (usec):\n |");
221 } else {
222 scale_down = 0;
223 divisor = 1;
224 printf(" percentiles (nsec):\n |");
225 }
226
227 time_width = max(5, (int) (log10(maxv / divisor) + 1));
228 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
229 precision, time_width);
230 /* fmt will be something like " %5.2fth=[%4llu]%c" */
231 per_line = (80 - 7) / (precision + 10 + time_width);
232
233 for (j = 0; j < len; j++) {
234 /* for formatting */
235 if (j != 0 && (j % per_line) == 0)
236 printf(" |");
237
238 /* end of the list */
239 is_last = (j == len - 1) != 0;
240
241 for (i = 0; i < scale_down; i++)
242 ovals[j] = (ovals[j] + 999) / 1000;
243
244 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
245
246 if (is_last)
247 break;
248
249 if ((j % per_line) == per_line - 1) /* for formatting */
250 printf("\n");
251 }
252
253out:
254 free(ovals);
255}
256
257static unsigned int plat_val_to_idx(unsigned long val)
258{
259 unsigned int msb, error_bits, base, offset, idx;
260
261 /* Find MSB starting from bit 0 */
262 if (val == 0)
263 msb = 0;
264 else
265 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
266
267 /*
268 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
269 * all bits of the sample as index
270 */
271 if (msb <= PLAT_BITS)
272 return val;
273
274 /* Compute the number of error bits to discard*/
275 error_bits = msb - PLAT_BITS;
276
277 /* Compute the number of buckets before the group */
278 base = (error_bits + 1) << PLAT_BITS;
279
280 /*
281 * Discard the error bits and apply the mask to find the
282 * index for the buckets in the group
283 */
284 offset = (PLAT_VAL - 1) & (val >> error_bits);
285
286 /* Make sure the index does not exceed (array size - 1) */
287 idx = (base + offset) < (PLAT_NR - 1) ?
288 (base + offset) : (PLAT_NR - 1);
289
290 return idx;
291}
292
293static void add_stat(struct submitter *s, int clock_index, int nr)
294{
295#ifdef ARCH_HAVE_CPU_CLOCK
296 unsigned long cycles;
297 unsigned int pidx;
298
299 cycles = get_cpu_clock();
300 cycles -= s->clock_batch[clock_index];
301 pidx = plat_val_to_idx(cycles);
302 s->plat[pidx] += nr;
303#endif
304}
305
2ea53ca3 306static int io_uring_register_buffers(struct submitter *s)
c9fb4c5b 307{
8025517d
JA
308 if (do_nop)
309 return 0;
310
bfed648c 311 return syscall(__NR_io_uring_register, s->ring_fd,
e39863e3 312 IORING_REGISTER_BUFFERS, s->iovecs, depth);
2ea53ca3
JA
313}
314
a7abc9fb
JA
315static int io_uring_register_files(struct submitter *s)
316{
48e698fa 317 int i;
a7abc9fb 318
8025517d
JA
319 if (do_nop)
320 return 0;
321
48e698fa
JA
322 s->fds = calloc(s->nr_files, sizeof(__s32));
323 for (i = 0; i < s->nr_files; i++) {
324 s->fds[i] = s->files[i].real_fd;
325 s->files[i].fixed_fd = i;
326 }
a7abc9fb 327
bfed648c 328 return syscall(__NR_io_uring_register, s->ring_fd,
919850d2 329 IORING_REGISTER_FILES, s->fds, s->nr_files);
a7abc9fb
JA
330}
331
2ea53ca3
JA
332static int io_uring_setup(unsigned entries, struct io_uring_params *p)
333{
bfed648c 334 return syscall(__NR_io_uring_setup, entries, p);
c9fb4c5b
JA
335}
336
b3915995
JA
337static void io_uring_probe(int fd)
338{
339 struct io_uring_probe *p;
340 int ret;
341
342 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
343 if (!p)
344 return;
345
346 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
347 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
348 if (ret < 0)
349 goto out;
350
351 if (IORING_OP_READ > p->ops_len)
352 goto out;
353
354 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
84106576 355 vectored = 0;
b3915995
JA
356out:
357 free(p);
358}
359
c3e2fc25
JA
360static int io_uring_enter(struct submitter *s, unsigned int to_submit,
361 unsigned int min_complete, unsigned int flags)
c9fb4c5b 362{
bfed648c
JA
363 return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete,
364 flags, NULL, 0);
c9fb4c5b
JA
365}
366
77d814a1 367#ifndef CONFIG_HAVE_GETTID
c9fb4c5b
JA
368static int gettid(void)
369{
370 return syscall(__NR_gettid);
371}
77d814a1 372#endif
c9fb4c5b 373
701d1277
JA
374static unsigned file_depth(struct submitter *s)
375{
e39863e3 376 return (depth + s->nr_files - 1) / s->nr_files;
701d1277
JA
377}
378
a7086591 379static void init_io(struct submitter *s, unsigned index)
c9fb4c5b 380{
f0403f94 381 struct io_uring_sqe *sqe = &s->sqes[index];
c9fb4c5b 382 unsigned long offset;
a7086591 383 struct file *f;
c9fb4c5b
JA
384 long r;
385
8025517d
JA
386 if (do_nop) {
387 sqe->opcode = IORING_OP_NOP;
388 return;
389 }
390
701d1277
JA
391 if (s->nr_files == 1) {
392 f = &s->files[0];
393 } else {
394 f = &s->files[s->cur_file];
395 if (f->pending_ios >= file_depth(s)) {
396 s->cur_file++;
397 if (s->cur_file == s->nr_files)
398 s->cur_file = 0;
93d1811c 399 f = &s->files[s->cur_file];
701d1277
JA
400 }
401 }
402 f->pending_ios++;
a7086591 403
5e8865c0 404 r = lrand48();
5bd526f2 405 offset = (r % (f->max_blocks - 1)) * bs;
c9fb4c5b 406
8c5fa755
JA
407 if (register_files) {
408 sqe->flags = IOSQE_FIXED_FILE;
409 sqe->fd = f->fixed_fd;
410 } else {
411 sqe->flags = 0;
412 sqe->fd = f->real_fd;
413 }
f0403f94 414 if (fixedbufs) {
48e698fa 415 sqe->opcode = IORING_OP_READ_FIXED;
919850d2 416 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
5bd526f2 417 sqe->len = bs;
2ea53ca3 418 sqe->buf_index = index;
84106576 419 } else if (!vectored) {
b3915995
JA
420 sqe->opcode = IORING_OP_READ;
421 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
422 sqe->len = bs;
423 sqe->buf_index = 0;
84106576
JA
424 } else {
425 sqe->opcode = IORING_OP_READV;
426 sqe->addr = (unsigned long) &s->iovecs[index];
427 sqe->len = 1;
428 sqe->buf_index = 0;
f0403f94 429 }
f0403f94 430 sqe->ioprio = 0;
f0403f94 431 sqe->off = offset;
932131c9
JA
432 sqe->user_data = (unsigned long) f->fileno;
433 if (stats)
434 sqe->user_data |= ((unsigned long)s->clock_index << 32);
c9fb4c5b
JA
435}
436
a7086591 437static int prep_more_ios(struct submitter *s, int max_ios)
c9fb4c5b 438{
e31b8288 439 struct io_sq_ring *ring = &s->sq_ring;
e2239016 440 unsigned index, tail, next_tail, prepped = 0;
c9fb4c5b 441
c3e2fc25 442 next_tail = tail = *ring->tail;
c9fb4c5b
JA
443 do {
444 next_tail++;
fc2dc21b 445 if (next_tail == atomic_load_acquire(ring->head))
c9fb4c5b
JA
446 break;
447
e39c34dc 448 index = tail & sq_ring_mask;
a7086591 449 init_io(s, index);
c3e2fc25 450 ring->array[index] = index;
c9fb4c5b
JA
451 prepped++;
452 tail = next_tail;
453 } while (prepped < max_ios);
454
fc2dc21b
JA
455 if (prepped)
456 atomic_store_release(ring->tail, tail);
c9fb4c5b
JA
457 return prepped;
458}
459
a7086591 460static int get_file_size(struct file *f)
c9fb4c5b
JA
461{
462 struct stat st;
463
48e698fa 464 if (fstat(f->real_fd, &st) < 0)
c9fb4c5b
JA
465 return -1;
466 if (S_ISBLK(st.st_mode)) {
467 unsigned long long bytes;
468
48e698fa 469 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
c9fb4c5b
JA
470 return -1;
471
5bd526f2 472 f->max_blocks = bytes / bs;
c9fb4c5b
JA
473 return 0;
474 } else if (S_ISREG(st.st_mode)) {
5bd526f2 475 f->max_blocks = st.st_size / bs;
c9fb4c5b
JA
476 return 0;
477 }
478
479 return -1;
480}
481
482static int reap_events(struct submitter *s)
483{
e31b8288 484 struct io_cq_ring *ring = &s->cq_ring;
f0403f94 485 struct io_uring_cqe *cqe;
e2239016 486 unsigned head, reaped = 0;
c9fb4c5b 487
c3e2fc25 488 head = *ring->head;
c9fb4c5b 489 do {
701d1277
JA
490 struct file *f;
491
679d8352 492 read_barrier();
fc2dc21b 493 if (head == atomic_load_acquire(ring->tail))
c9fb4c5b 494 break;
f0403f94 495 cqe = &ring->cqes[head & cq_ring_mask];
8025517d 496 if (!do_nop) {
932131c9
JA
497 int fileno = cqe->user_data & 0xffffffff;
498
499 f = &s->files[fileno];
8025517d 500 f->pending_ios--;
5bd526f2 501 if (cqe->res != bs) {
8025517d 502 printf("io: unexpected ret=%d\n", cqe->res);
154a9582 503 if (polled && cqe->res == -EOPNOTSUPP)
8066f6b6 504 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
8025517d
JA
505 return -1;
506 }
c9fb4c5b 507 }
932131c9
JA
508 if (stats) {
509 int clock_index = cqe->user_data >> 32;
510
511 add_stat(s, clock_index, 1);
512 }
c9fb4c5b
JA
513 reaped++;
514 head++;
c9fb4c5b
JA
515 } while (1);
516
fc2dc21b
JA
517 if (reaped) {
518 s->inflight -= reaped;
519 atomic_store_release(ring->head, head);
520 }
c9fb4c5b
JA
521 return reaped;
522}
523
524static void *submitter_fn(void *data)
525{
526 struct submitter *s = data;
ce1705de 527 struct io_sq_ring *ring = &s->sq_ring;
932131c9 528 int i, ret, prepped, nr_batch;
c9fb4c5b 529
932131c9
JA
530 s->tid = gettid();
531 printf("submitter=%d\n", s->tid);
c9fb4c5b 532
5e8865c0 533 srand48(pthread_self());
c9fb4c5b 534
932131c9
JA
535 for (i = 0; i < MAX_FDS; i++)
536 s->files[i].fileno = i;
537
538 if (stats) {
539 nr_batch = roundup_pow2(depth / batch_submit);
540 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
541 s->clock_index = 0;
542
543 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
544 } else {
545 s->clock_batch = NULL;
546 s->plat = NULL;
547 nr_batch = 0;
548 }
549
c9fb4c5b
JA
550 prepped = 0;
551 do {
f310970e 552 int to_wait, to_submit, this_reap, to_prep;
fc2dc21b 553 unsigned ring_flags = 0;
c9fb4c5b 554
e39863e3
KB
555 if (!prepped && s->inflight < depth) {
556 to_prep = min(depth - s->inflight, batch_submit);
a7086591 557 prepped = prep_more_ios(s, to_prep);
932131c9
JA
558#ifdef ARCH_HAVE_CPU_CLOCK
559 if (prepped && stats) {
560 s->clock_batch[s->clock_index] = get_cpu_clock();
561 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
562 }
563#endif
f310970e 564 }
c9fb4c5b
JA
565 s->inflight += prepped;
566submit_more:
567 to_submit = prepped;
568submit:
e39863e3 569 if (to_submit && (s->inflight + to_submit <= depth))
c9fb4c5b
JA
570 to_wait = 0;
571 else
e39863e3 572 to_wait = min(s->inflight + to_submit, batch_complete);
c9fb4c5b 573
ce1705de
JA
574 /*
575 * Only need to call io_uring_enter if we're not using SQ thread
576 * poll, or if IORING_SQ_NEED_WAKEUP is set.
577 */
fc2dc21b
JA
578 if (sq_thread_poll)
579 ring_flags = atomic_load_acquire(ring->flags);
580 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
e0abe388
JA
581 unsigned flags = 0;
582
583 if (to_wait)
584 flags = IORING_ENTER_GETEVENTS;
fc2dc21b 585 if (ring_flags & IORING_SQ_NEED_WAKEUP)
b532dd6d 586 flags |= IORING_ENTER_SQ_WAKEUP;
e0abe388 587 ret = io_uring_enter(s, to_submit, to_wait, flags);
ce1705de 588 s->calls++;
fc2dc21b
JA
589 } else {
590 /* for SQPOLL, we submitted it all effectively */
591 ret = to_submit;
ce1705de 592 }
c9fb4c5b 593
ce1705de
JA
594 /*
595 * For non SQ thread poll, we already got the events we needed
596 * through the io_uring_enter() above. For SQ thread poll, we
597 * need to loop here until we find enough events.
598 */
599 this_reap = 0;
600 do {
601 int r;
602 r = reap_events(s);
7d1435e6
JA
603 if (r == -1) {
604 s->finish = 1;
ce1705de 605 break;
7d1435e6 606 } else if (r > 0)
ce1705de
JA
607 this_reap += r;
608 } while (sq_thread_poll && this_reap < to_wait);
c9fb4c5b
JA
609 s->reaps += this_reap;
610
611 if (ret >= 0) {
612 if (!ret) {
613 to_submit = 0;
614 if (s->inflight)
615 goto submit;
616 continue;
617 } else if (ret < to_submit) {
618 int diff = to_submit - ret;
619
620 s->done += ret;
621 prepped -= diff;
622 goto submit_more;
623 }
624 s->done += ret;
625 prepped = 0;
626 continue;
627 } else if (ret < 0) {
ac122fea 628 if (errno == EAGAIN) {
c9fb4c5b
JA
629 if (s->finish)
630 break;
631 if (this_reap)
632 goto submit;
c9fb4c5b
JA
633 to_submit = 0;
634 goto submit;
635 }
ac122fea 636 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
637 break;
638 }
639 } while (!s->finish);
a7086591 640
c9fb4c5b
JA
641 finish = 1;
642 return NULL;
643}
644
54319661
JA
645static struct submitter *get_submitter(int offset)
646{
647 void *ret;
648
649 ret = submitter;
650 if (offset)
651 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
652 return ret;
653}
654
c9fb4c5b
JA
655static void sig_int(int sig)
656{
54319661
JA
657 int j;
658
c9fb4c5b 659 printf("Exiting on signal %d\n", sig);
54319661
JA
660 for (j = 0; j < nthreads; j++) {
661 struct submitter *s = get_submitter(j);
662 s->finish = 1;
663 }
c9fb4c5b
JA
664 finish = 1;
665}
666
667static void arm_sig_int(void)
668{
669 struct sigaction act;
670
671 memset(&act, 0, sizeof(act));
672 act.sa_handler = sig_int;
673 act.sa_flags = SA_RESTART;
674 sigaction(SIGINT, &act, NULL);
675}
676
c3e2fc25
JA
677static int setup_ring(struct submitter *s)
678{
e31b8288
JA
679 struct io_sq_ring *sring = &s->sq_ring;
680 struct io_cq_ring *cring = &s->cq_ring;
681 struct io_uring_params p;
2ea53ca3 682 int ret, fd;
c3e2fc25 683 void *ptr;
c3e2fc25
JA
684
685 memset(&p, 0, sizeof(p));
686
7d1435e6 687 if (polled && !do_nop)
0e47f11b 688 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3 689 if (sq_thread_poll) {
2ea53ca3 690 p.flags |= IORING_SETUP_SQPOLL;
3ade18a3 691 if (sq_thread_cpu != -1) {
3d7d00a3 692 p.flags |= IORING_SETUP_SQ_AFF;
3ade18a3
JA
693 p.sq_thread_cpu = sq_thread_cpu;
694 }
c3e2fc25
JA
695 }
696
e39863e3 697 fd = io_uring_setup(depth, &p);
c3e2fc25
JA
698 if (fd < 0) {
699 perror("io_uring_setup");
700 return 1;
701 }
f310970e 702 s->ring_fd = fd;
2ea53ca3 703
b3915995
JA
704 io_uring_probe(fd);
705
2ea53ca3 706 if (fixedbufs) {
15130e3f
JA
707 struct rlimit rlim;
708
709 rlim.rlim_cur = RLIM_INFINITY;
710 rlim.rlim_max = RLIM_INFINITY;
711 /* ignore potential error, not needed on newer kernels */
712 setrlimit(RLIMIT_MEMLOCK, &rlim);
713
2ea53ca3
JA
714 ret = io_uring_register_buffers(s);
715 if (ret < 0) {
a7abc9fb 716 perror("io_uring_register_buffers");
2ea53ca3
JA
717 return 1;
718 }
719 }
720
8c5fa755
JA
721 if (register_files) {
722 ret = io_uring_register_files(s);
723 if (ret < 0) {
724 perror("io_uring_register_files");
725 return 1;
726 }
a7abc9fb
JA
727 }
728
e2239016 729 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
f310970e
JA
730 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
731 IORING_OFF_SQ_RING);
c3e2fc25
JA
732 printf("sq_ring ptr = 0x%p\n", ptr);
733 sring->head = ptr + p.sq_off.head;
734 sring->tail = ptr + p.sq_off.tail;
735 sring->ring_mask = ptr + p.sq_off.ring_mask;
736 sring->ring_entries = ptr + p.sq_off.ring_entries;
ce1705de 737 sring->flags = ptr + p.sq_off.flags;
ac122fea 738 sring->array = ptr + p.sq_off.array;
c3e2fc25
JA
739 sq_ring_mask = *sring->ring_mask;
740
f0403f94 741 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
f310970e 742 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
f0403f94
JA
743 IORING_OFF_SQES);
744 printf("sqes ptr = 0x%p\n", s->sqes);
c3e2fc25 745
f0403f94 746 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
f310970e
JA
747 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
748 IORING_OFF_CQ_RING);
c3e2fc25
JA
749 printf("cq_ring ptr = 0x%p\n", ptr);
750 cring->head = ptr + p.cq_off.head;
751 cring->tail = ptr + p.cq_off.tail;
752 cring->ring_mask = ptr + p.cq_off.ring_mask;
753 cring->ring_entries = ptr + p.cq_off.ring_entries;
f0403f94 754 cring->cqes = ptr + p.cq_off.cqes;
c3e2fc25
JA
755 cq_ring_mask = *cring->ring_mask;
756 return 0;
757}
758
2e7888ef
JA
759static void file_depths(char *buf)
760{
26976a13 761 bool prev = false;
2e7888ef 762 char *p;
54319661 763 int i, j;
2e7888ef 764
ac21bfab 765 buf[0] = '\0';
2e7888ef 766 p = buf;
54319661
JA
767 for (j = 0; j < nthreads; j++) {
768 struct submitter *s = get_submitter(j);
2e7888ef 769
54319661
JA
770 for (i = 0; i < s->nr_files; i++) {
771 struct file *f = &s->files[i];
772
26976a13
JA
773 if (prev)
774 p += sprintf(p, " %d", f->pending_ios);
54319661 775 else
4f215227 776 p += sprintf(p, "%d", f->pending_ios);
26976a13 777 prev = true;
54319661 778 }
2e7888ef
JA
779 }
780}
781
d79ff8c9 782static void usage(char *argv, int status)
e39863e3
KB
783{
784 printf("%s [options] -- [filenames]\n"
35268e11
AJ
785 " -d <int> : IO Depth, default %d\n"
786 " -s <int> : Batch submit, default %d\n"
787 " -c <int> : Batch complete, default %d\n"
788 " -b <int> : Block size, default %d\n"
789 " -p <bool> : Polled IO, default %d\n"
790 " -B <bool> : Fixed buffers, default %d\n"
791 " -F <bool> : Register files, default %d\n"
0862f718 792 " -n <int> : Number of threads, default %d\n"
2686fc22 793 " -O <bool> : Use O_DIRECT, default %d\n"
932131c9
JA
794 " -N <bool> : Perform just no-op requests, default %d"
795 " -t <bool> : Track IO latencies, default %d"
796 " -T <int> : TSC rate in HZ\n",
35268e11 797 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
932131c9 798 fixedbufs, register_files, nthreads, !buffered, do_nop, stats);
d79ff8c9 799 exit(status);
e39863e3
KB
800}
801
c9fb4c5b
JA
802int main(int argc, char *argv[])
803{
e39863e3 804 struct submitter *s;
05138221 805 unsigned long done, calls, reap;
d79ff8c9
AJ
806 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
807 struct file f;
2e7888ef 808 char *fdepths;
c3e2fc25 809 void *ret;
c9fb4c5b 810
0862f718
JA
811 if (!do_nop && argc < 2)
812 usage(argv[0], 1);
c9fb4c5b 813
932131c9 814 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:h?")) != -1) {
e39863e3
KB
815 switch (opt) {
816 case 'd':
817 depth = atoi(optarg);
818 break;
819 case 's':
820 batch_submit = atoi(optarg);
932131c9
JA
821 if (!batch_submit)
822 batch_submit = 1;
e39863e3
KB
823 break;
824 case 'c':
825 batch_complete = atoi(optarg);
932131c9
JA
826 if (!batch_complete)
827 batch_complete = 1;
e39863e3 828 break;
5bd526f2
JA
829 case 'b':
830 bs = atoi(optarg);
831 break;
832 case 'p':
833 polled = !!atoi(optarg);
834 break;
6a87c3b0
JA
835 case 'B':
836 fixedbufs = !!atoi(optarg);
837 break;
838 case 'F':
839 register_files = !!atoi(optarg);
840 break;
54319661
JA
841 case 'n':
842 nthreads = atoi(optarg);
caf2b9ac
JA
843 if (!nthreads) {
844 printf("Threads must be non-zero\n");
845 usage(argv[0], 1);
846 }
54319661 847 break;
0862f718
JA
848 case 'N':
849 do_nop = !!atoi(optarg);
850 break;
2686fc22
JA
851 case 'O':
852 buffered = !atoi(optarg);
853 break;
932131c9
JA
854 case 't':
855#ifndef ARCH_HAVE_CPU_CLOCK
856 fprintf(stderr, "Stats not supported on this CPU\n");
857 return 1;
858#endif
859 stats = !!atoi(optarg);
860 break;
861 case 'T':
862#ifndef ARCH_HAVE_CPU_CLOCK
863 fprintf(stderr, "Stats not supported on this CPU\n");
864 return 1;
865#endif
866 tsc_rate = strtoul(optarg, NULL, 10);
867 break;
e39863e3
KB
868 case 'h':
869 case '?':
870 default:
d79ff8c9 871 usage(argv[0], 0);
e39863e3
KB
872 break;
873 }
874 }
875
c5347611
JA
876 if (batch_complete > depth)
877 batch_complete = depth;
878 if (batch_submit > depth)
879 batch_submit = depth;
880
54319661
JA
881 submitter = calloc(nthreads, sizeof(*submitter) +
882 depth * sizeof(struct iovec));
883 for (j = 0; j < nthreads; j++) {
884 s = get_submitter(j);
885 s->index = j;
886 s->done = s->calls = s->reaps = 0;
887 }
e39863e3 888
701d1277 889 flags = O_RDONLY | O_NOATIME;
a7086591
JA
890 if (!buffered)
891 flags |= O_DIRECT;
892
54319661 893 j = 0;
e39863e3 894 i = optind;
d79ff8c9 895 nfiles = argc - i;
2ac00585
JA
896 if (!do_nop) {
897 if (!nfiles) {
898 printf("No files specified\n");
899 usage(argv[0], 1);
900 }
901 threads_per_f = nthreads / nfiles;
902 /* make sure each thread gets assigned files */
903 if (threads_per_f == 0) {
904 threads_per_f = 1;
905 } else {
906 threads_rem = nthreads - threads_per_f * nfiles;
907 }
d79ff8c9 908 }
8025517d 909 while (!do_nop && i < argc) {
d79ff8c9
AJ
910 int k, limit;
911
912 memset(&f, 0, sizeof(f));
a7086591
JA
913
914 fd = open(argv[i], flags);
915 if (fd < 0) {
916 perror("open");
917 return 1;
918 }
d79ff8c9
AJ
919 f.real_fd = fd;
920 if (get_file_size(&f)) {
a7086591
JA
921 printf("failed getting size of device/file\n");
922 return 1;
923 }
d79ff8c9 924 if (f.max_blocks <= 1) {
a7086591
JA
925 printf("Zero file/device size?\n");
926 return 1;
927 }
d79ff8c9
AJ
928 f.max_blocks--;
929
930 limit = threads_per_f;
931 limit += threads_rem > 0 ? 1 : 0;
932 for (k = 0; k < limit; k++) {
933 s = get_submitter((j + k) % nthreads);
a7086591 934
d79ff8c9
AJ
935 if (s->nr_files == MAX_FDS) {
936 printf("Max number of files (%d) reached\n", MAX_FDS);
937 break;
938 }
939
940 memcpy(&s->files[s->nr_files], &f, sizeof(f));
941
942 printf("Added file %s (submitter %d)\n", argv[i], s->index);
943 s->nr_files++;
944 }
945 threads_rem--;
a7086591 946 i++;
d79ff8c9 947 j += limit;
a7086591
JA
948 }
949
c9fb4c5b
JA
950 arm_sig_int();
951
54319661
JA
952 for (j = 0; j < nthreads; j++) {
953 s = get_submitter(j);
954 for (i = 0; i < depth; i++) {
955 void *buf;
c9fb4c5b 956
54319661
JA
957 if (posix_memalign(&buf, bs, bs)) {
958 printf("failed alloc\n");
959 return 1;
960 }
961 s->iovecs[i].iov_base = buf;
962 s->iovecs[i].iov_len = bs;
c9fb4c5b 963 }
c9fb4c5b
JA
964 }
965
54319661
JA
966 for (j = 0; j < nthreads; j++) {
967 s = get_submitter(j);
968
969 err = setup_ring(s);
970 if (err) {
971 printf("ring setup failed: %s, %d\n", strerror(errno), err);
972 return 1;
973 }
c9fb4c5b 974 }
54319661 975 s = get_submitter(0);
6a87c3b0 976 printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d", polled, fixedbufs, register_files, buffered);
e39863e3 977 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
c9fb4c5b 978
54319661
JA
979 for (j = 0; j < nthreads; j++) {
980 s = get_submitter(j);
981 pthread_create(&s->thread, NULL, submitter_fn, s);
982 }
c9fb4c5b 983
54319661 984 fdepths = malloc(8 * s->nr_files * nthreads);
05138221 985 reap = calls = done = 0;
c9fb4c5b
JA
986 do {
987 unsigned long this_done = 0;
988 unsigned long this_reap = 0;
989 unsigned long this_call = 0;
990 unsigned long rpc = 0, ipc = 0;
f3057d26 991 unsigned long iops, bw;
c9fb4c5b
JA
992
993 sleep(1);
54319661
JA
994 for (j = 0; j < nthreads; j++) {
995 this_done += s->done;
996 this_call += s->calls;
997 this_reap += s->reaps;
998 }
c9fb4c5b
JA
999 if (this_call - calls) {
1000 rpc = (this_done - done) / (this_call - calls);
1001 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
1002 } else
1003 rpc = ipc = -1;
2e7888ef 1004 file_depths(fdepths);
22fd3501 1005 iops = this_done - done;
f3057d26
JA
1006 if (bs > 1048576)
1007 bw = iops * (bs / 1048576);
1008 else
1009 bw = iops / (1048576 / bs);
6c5d3a1c
JA
1010 printf("IOPS=%lu, ", iops);
1011 if (!do_nop)
1012 printf("BW=%luMiB/s, ", bw);
1013 printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths);
c9fb4c5b
JA
1014 done = this_done;
1015 calls = this_call;
1016 reap = this_reap;
1017 } while (!finish);
1018
54319661
JA
1019 for (j = 0; j < nthreads; j++) {
1020 s = get_submitter(j);
1021 pthread_join(s->thread, &ret);
1022 close(s->ring_fd);
932131c9
JA
1023
1024 if (stats) {
1025 unsigned long nr;
1026
1027 printf("%d: Latency percentiles:\n", s->tid);
1028 for (i = 0, nr = 0; i < PLAT_NR; i++)
1029 nr += s->plat[i];
1030 show_clat_percentiles(s->plat, nr, 4);
1031 free(s->clock_batch);
1032 free(s->plat);
1033 }
54319661 1034 }
932131c9 1035
2e7888ef 1036 free(fdepths);
54319661 1037 free(submitter);
c9fb4c5b
JA
1038 return 0;
1039}