Merge branch 'tsc' of https://github.com/ErwanAliasr1/fio
[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;
ab85494f 487 int last_idx = -1, stat_nr = 0;
c9fb4c5b 488
c3e2fc25 489 head = *ring->head;
c9fb4c5b 490 do {
701d1277
JA
491 struct file *f;
492
679d8352 493 read_barrier();
fc2dc21b 494 if (head == atomic_load_acquire(ring->tail))
c9fb4c5b 495 break;
f0403f94 496 cqe = &ring->cqes[head & cq_ring_mask];
8025517d 497 if (!do_nop) {
932131c9
JA
498 int fileno = cqe->user_data & 0xffffffff;
499
500 f = &s->files[fileno];
8025517d 501 f->pending_ios--;
5bd526f2 502 if (cqe->res != bs) {
8025517d 503 printf("io: unexpected ret=%d\n", cqe->res);
154a9582 504 if (polled && cqe->res == -EOPNOTSUPP)
8066f6b6 505 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
8025517d
JA
506 return -1;
507 }
c9fb4c5b 508 }
932131c9
JA
509 if (stats) {
510 int clock_index = cqe->user_data >> 32;
511
ab85494f
JA
512 if (last_idx != clock_index) {
513 if (last_idx != -1) {
514 add_stat(s, last_idx, stat_nr);
515 stat_nr = 0;
516 }
517 last_idx = clock_index;
518 }
519 stat_nr++;
932131c9
JA
520 add_stat(s, clock_index, 1);
521 }
c9fb4c5b
JA
522 reaped++;
523 head++;
c9fb4c5b
JA
524 } while (1);
525
ab85494f
JA
526 if (stat_nr)
527 add_stat(s, last_idx, stat_nr);
528
fc2dc21b
JA
529 if (reaped) {
530 s->inflight -= reaped;
531 atomic_store_release(ring->head, head);
532 }
c9fb4c5b
JA
533 return reaped;
534}
535
536static void *submitter_fn(void *data)
537{
538 struct submitter *s = data;
ce1705de 539 struct io_sq_ring *ring = &s->sq_ring;
932131c9 540 int i, ret, prepped, nr_batch;
c9fb4c5b 541
932131c9
JA
542 s->tid = gettid();
543 printf("submitter=%d\n", s->tid);
c9fb4c5b 544
5e8865c0 545 srand48(pthread_self());
c9fb4c5b 546
932131c9
JA
547 for (i = 0; i < MAX_FDS; i++)
548 s->files[i].fileno = i;
549
550 if (stats) {
551 nr_batch = roundup_pow2(depth / batch_submit);
552 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
553 s->clock_index = 0;
554
555 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
556 } else {
557 s->clock_batch = NULL;
558 s->plat = NULL;
559 nr_batch = 0;
560 }
561
c9fb4c5b
JA
562 prepped = 0;
563 do {
f310970e 564 int to_wait, to_submit, this_reap, to_prep;
fc2dc21b 565 unsigned ring_flags = 0;
c9fb4c5b 566
e39863e3
KB
567 if (!prepped && s->inflight < depth) {
568 to_prep = min(depth - s->inflight, batch_submit);
a7086591 569 prepped = prep_more_ios(s, to_prep);
932131c9
JA
570#ifdef ARCH_HAVE_CPU_CLOCK
571 if (prepped && stats) {
572 s->clock_batch[s->clock_index] = get_cpu_clock();
573 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
574 }
575#endif
f310970e 576 }
c9fb4c5b
JA
577 s->inflight += prepped;
578submit_more:
579 to_submit = prepped;
580submit:
e39863e3 581 if (to_submit && (s->inflight + to_submit <= depth))
c9fb4c5b
JA
582 to_wait = 0;
583 else
e39863e3 584 to_wait = min(s->inflight + to_submit, batch_complete);
c9fb4c5b 585
ce1705de
JA
586 /*
587 * Only need to call io_uring_enter if we're not using SQ thread
588 * poll, or if IORING_SQ_NEED_WAKEUP is set.
589 */
fc2dc21b
JA
590 if (sq_thread_poll)
591 ring_flags = atomic_load_acquire(ring->flags);
592 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
e0abe388
JA
593 unsigned flags = 0;
594
595 if (to_wait)
596 flags = IORING_ENTER_GETEVENTS;
fc2dc21b 597 if (ring_flags & IORING_SQ_NEED_WAKEUP)
b532dd6d 598 flags |= IORING_ENTER_SQ_WAKEUP;
e0abe388 599 ret = io_uring_enter(s, to_submit, to_wait, flags);
ce1705de 600 s->calls++;
fc2dc21b
JA
601 } else {
602 /* for SQPOLL, we submitted it all effectively */
603 ret = to_submit;
ce1705de 604 }
c9fb4c5b 605
ce1705de
JA
606 /*
607 * For non SQ thread poll, we already got the events we needed
608 * through the io_uring_enter() above. For SQ thread poll, we
609 * need to loop here until we find enough events.
610 */
611 this_reap = 0;
612 do {
613 int r;
614 r = reap_events(s);
7d1435e6
JA
615 if (r == -1) {
616 s->finish = 1;
ce1705de 617 break;
7d1435e6 618 } else if (r > 0)
ce1705de
JA
619 this_reap += r;
620 } while (sq_thread_poll && this_reap < to_wait);
c9fb4c5b
JA
621 s->reaps += this_reap;
622
623 if (ret >= 0) {
624 if (!ret) {
625 to_submit = 0;
626 if (s->inflight)
627 goto submit;
628 continue;
629 } else if (ret < to_submit) {
630 int diff = to_submit - ret;
631
632 s->done += ret;
633 prepped -= diff;
634 goto submit_more;
635 }
636 s->done += ret;
637 prepped = 0;
638 continue;
639 } else if (ret < 0) {
ac122fea 640 if (errno == EAGAIN) {
c9fb4c5b
JA
641 if (s->finish)
642 break;
643 if (this_reap)
644 goto submit;
c9fb4c5b
JA
645 to_submit = 0;
646 goto submit;
647 }
ac122fea 648 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
649 break;
650 }
651 } while (!s->finish);
a7086591 652
c9fb4c5b
JA
653 finish = 1;
654 return NULL;
655}
656
54319661
JA
657static struct submitter *get_submitter(int offset)
658{
659 void *ret;
660
661 ret = submitter;
662 if (offset)
663 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
664 return ret;
665}
666
c9fb4c5b
JA
667static void sig_int(int sig)
668{
54319661
JA
669 int j;
670
c9fb4c5b 671 printf("Exiting on signal %d\n", sig);
54319661
JA
672 for (j = 0; j < nthreads; j++) {
673 struct submitter *s = get_submitter(j);
674 s->finish = 1;
675 }
c9fb4c5b
JA
676 finish = 1;
677}
678
679static void arm_sig_int(void)
680{
681 struct sigaction act;
682
683 memset(&act, 0, sizeof(act));
684 act.sa_handler = sig_int;
685 act.sa_flags = SA_RESTART;
686 sigaction(SIGINT, &act, NULL);
687}
688
c3e2fc25
JA
689static int setup_ring(struct submitter *s)
690{
e31b8288
JA
691 struct io_sq_ring *sring = &s->sq_ring;
692 struct io_cq_ring *cring = &s->cq_ring;
693 struct io_uring_params p;
2ea53ca3 694 int ret, fd;
c3e2fc25 695 void *ptr;
c3e2fc25
JA
696
697 memset(&p, 0, sizeof(p));
698
7d1435e6 699 if (polled && !do_nop)
0e47f11b 700 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3 701 if (sq_thread_poll) {
2ea53ca3 702 p.flags |= IORING_SETUP_SQPOLL;
3ade18a3 703 if (sq_thread_cpu != -1) {
3d7d00a3 704 p.flags |= IORING_SETUP_SQ_AFF;
3ade18a3
JA
705 p.sq_thread_cpu = sq_thread_cpu;
706 }
c3e2fc25
JA
707 }
708
e39863e3 709 fd = io_uring_setup(depth, &p);
c3e2fc25
JA
710 if (fd < 0) {
711 perror("io_uring_setup");
712 return 1;
713 }
f310970e 714 s->ring_fd = fd;
2ea53ca3 715
b3915995
JA
716 io_uring_probe(fd);
717
2ea53ca3 718 if (fixedbufs) {
15130e3f
JA
719 struct rlimit rlim;
720
721 rlim.rlim_cur = RLIM_INFINITY;
722 rlim.rlim_max = RLIM_INFINITY;
723 /* ignore potential error, not needed on newer kernels */
724 setrlimit(RLIMIT_MEMLOCK, &rlim);
725
2ea53ca3
JA
726 ret = io_uring_register_buffers(s);
727 if (ret < 0) {
a7abc9fb 728 perror("io_uring_register_buffers");
2ea53ca3
JA
729 return 1;
730 }
731 }
732
8c5fa755
JA
733 if (register_files) {
734 ret = io_uring_register_files(s);
735 if (ret < 0) {
736 perror("io_uring_register_files");
737 return 1;
738 }
a7abc9fb
JA
739 }
740
e2239016 741 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
f310970e
JA
742 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
743 IORING_OFF_SQ_RING);
c3e2fc25
JA
744 printf("sq_ring ptr = 0x%p\n", ptr);
745 sring->head = ptr + p.sq_off.head;
746 sring->tail = ptr + p.sq_off.tail;
747 sring->ring_mask = ptr + p.sq_off.ring_mask;
748 sring->ring_entries = ptr + p.sq_off.ring_entries;
ce1705de 749 sring->flags = ptr + p.sq_off.flags;
ac122fea 750 sring->array = ptr + p.sq_off.array;
c3e2fc25
JA
751 sq_ring_mask = *sring->ring_mask;
752
f0403f94 753 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
f310970e 754 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
f0403f94
JA
755 IORING_OFF_SQES);
756 printf("sqes ptr = 0x%p\n", s->sqes);
c3e2fc25 757
f0403f94 758 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
f310970e
JA
759 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
760 IORING_OFF_CQ_RING);
c3e2fc25
JA
761 printf("cq_ring ptr = 0x%p\n", ptr);
762 cring->head = ptr + p.cq_off.head;
763 cring->tail = ptr + p.cq_off.tail;
764 cring->ring_mask = ptr + p.cq_off.ring_mask;
765 cring->ring_entries = ptr + p.cq_off.ring_entries;
f0403f94 766 cring->cqes = ptr + p.cq_off.cqes;
c3e2fc25
JA
767 cq_ring_mask = *cring->ring_mask;
768 return 0;
769}
770
2e7888ef
JA
771static void file_depths(char *buf)
772{
26976a13 773 bool prev = false;
2e7888ef 774 char *p;
54319661 775 int i, j;
2e7888ef 776
ac21bfab 777 buf[0] = '\0';
2e7888ef 778 p = buf;
54319661
JA
779 for (j = 0; j < nthreads; j++) {
780 struct submitter *s = get_submitter(j);
2e7888ef 781
54319661
JA
782 for (i = 0; i < s->nr_files; i++) {
783 struct file *f = &s->files[i];
784
26976a13
JA
785 if (prev)
786 p += sprintf(p, " %d", f->pending_ios);
54319661 787 else
4f215227 788 p += sprintf(p, "%d", f->pending_ios);
26976a13 789 prev = true;
54319661 790 }
2e7888ef
JA
791 }
792}
793
d79ff8c9 794static void usage(char *argv, int status)
e39863e3
KB
795{
796 printf("%s [options] -- [filenames]\n"
35268e11
AJ
797 " -d <int> : IO Depth, default %d\n"
798 " -s <int> : Batch submit, default %d\n"
799 " -c <int> : Batch complete, default %d\n"
800 " -b <int> : Block size, default %d\n"
801 " -p <bool> : Polled IO, default %d\n"
802 " -B <bool> : Fixed buffers, default %d\n"
803 " -F <bool> : Register files, default %d\n"
0862f718 804 " -n <int> : Number of threads, default %d\n"
2686fc22 805 " -O <bool> : Use O_DIRECT, default %d\n"
4a39c524
EV
806 " -N <bool> : Perform just no-op requests, default %d\n"
807 " -t <bool> : Track IO latencies, default %d\n"
932131c9 808 " -T <int> : TSC rate in HZ\n",
35268e11 809 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
932131c9 810 fixedbufs, register_files, nthreads, !buffered, do_nop, stats);
d79ff8c9 811 exit(status);
e39863e3
KB
812}
813
c9fb4c5b
JA
814int main(int argc, char *argv[])
815{
e39863e3 816 struct submitter *s;
05138221 817 unsigned long done, calls, reap;
d79ff8c9
AJ
818 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
819 struct file f;
2e7888ef 820 char *fdepths;
c3e2fc25 821 void *ret;
c9fb4c5b 822
0862f718
JA
823 if (!do_nop && argc < 2)
824 usage(argv[0], 1);
c9fb4c5b 825
932131c9 826 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:h?")) != -1) {
e39863e3
KB
827 switch (opt) {
828 case 'd':
829 depth = atoi(optarg);
830 break;
831 case 's':
832 batch_submit = atoi(optarg);
932131c9
JA
833 if (!batch_submit)
834 batch_submit = 1;
e39863e3
KB
835 break;
836 case 'c':
837 batch_complete = atoi(optarg);
932131c9
JA
838 if (!batch_complete)
839 batch_complete = 1;
e39863e3 840 break;
5bd526f2
JA
841 case 'b':
842 bs = atoi(optarg);
843 break;
844 case 'p':
845 polled = !!atoi(optarg);
846 break;
6a87c3b0
JA
847 case 'B':
848 fixedbufs = !!atoi(optarg);
849 break;
850 case 'F':
851 register_files = !!atoi(optarg);
852 break;
54319661
JA
853 case 'n':
854 nthreads = atoi(optarg);
caf2b9ac
JA
855 if (!nthreads) {
856 printf("Threads must be non-zero\n");
857 usage(argv[0], 1);
858 }
54319661 859 break;
0862f718
JA
860 case 'N':
861 do_nop = !!atoi(optarg);
862 break;
2686fc22
JA
863 case 'O':
864 buffered = !atoi(optarg);
865 break;
932131c9
JA
866 case 't':
867#ifndef ARCH_HAVE_CPU_CLOCK
868 fprintf(stderr, "Stats not supported on this CPU\n");
869 return 1;
870#endif
871 stats = !!atoi(optarg);
872 break;
873 case 'T':
874#ifndef ARCH_HAVE_CPU_CLOCK
875 fprintf(stderr, "Stats not supported on this CPU\n");
876 return 1;
877#endif
878 tsc_rate = strtoul(optarg, NULL, 10);
879 break;
e39863e3
KB
880 case 'h':
881 case '?':
882 default:
d79ff8c9 883 usage(argv[0], 0);
e39863e3
KB
884 break;
885 }
886 }
887
c5347611
JA
888 if (batch_complete > depth)
889 batch_complete = depth;
890 if (batch_submit > depth)
891 batch_submit = depth;
892
54319661
JA
893 submitter = calloc(nthreads, sizeof(*submitter) +
894 depth * sizeof(struct iovec));
895 for (j = 0; j < nthreads; j++) {
896 s = get_submitter(j);
897 s->index = j;
898 s->done = s->calls = s->reaps = 0;
899 }
e39863e3 900
701d1277 901 flags = O_RDONLY | O_NOATIME;
a7086591
JA
902 if (!buffered)
903 flags |= O_DIRECT;
904
54319661 905 j = 0;
e39863e3 906 i = optind;
d79ff8c9 907 nfiles = argc - i;
2ac00585
JA
908 if (!do_nop) {
909 if (!nfiles) {
910 printf("No files specified\n");
911 usage(argv[0], 1);
912 }
913 threads_per_f = nthreads / nfiles;
914 /* make sure each thread gets assigned files */
915 if (threads_per_f == 0) {
916 threads_per_f = 1;
917 } else {
918 threads_rem = nthreads - threads_per_f * nfiles;
919 }
d79ff8c9 920 }
8025517d 921 while (!do_nop && i < argc) {
d79ff8c9
AJ
922 int k, limit;
923
924 memset(&f, 0, sizeof(f));
a7086591
JA
925
926 fd = open(argv[i], flags);
927 if (fd < 0) {
928 perror("open");
929 return 1;
930 }
d79ff8c9
AJ
931 f.real_fd = fd;
932 if (get_file_size(&f)) {
a7086591
JA
933 printf("failed getting size of device/file\n");
934 return 1;
935 }
d79ff8c9 936 if (f.max_blocks <= 1) {
a7086591
JA
937 printf("Zero file/device size?\n");
938 return 1;
939 }
d79ff8c9
AJ
940 f.max_blocks--;
941
942 limit = threads_per_f;
943 limit += threads_rem > 0 ? 1 : 0;
944 for (k = 0; k < limit; k++) {
945 s = get_submitter((j + k) % nthreads);
a7086591 946
d79ff8c9
AJ
947 if (s->nr_files == MAX_FDS) {
948 printf("Max number of files (%d) reached\n", MAX_FDS);
949 break;
950 }
951
952 memcpy(&s->files[s->nr_files], &f, sizeof(f));
953
954 printf("Added file %s (submitter %d)\n", argv[i], s->index);
955 s->nr_files++;
956 }
957 threads_rem--;
a7086591 958 i++;
d79ff8c9 959 j += limit;
a7086591
JA
960 }
961
c9fb4c5b
JA
962 arm_sig_int();
963
54319661
JA
964 for (j = 0; j < nthreads; j++) {
965 s = get_submitter(j);
966 for (i = 0; i < depth; i++) {
967 void *buf;
c9fb4c5b 968
54319661
JA
969 if (posix_memalign(&buf, bs, bs)) {
970 printf("failed alloc\n");
971 return 1;
972 }
973 s->iovecs[i].iov_base = buf;
974 s->iovecs[i].iov_len = bs;
c9fb4c5b 975 }
c9fb4c5b
JA
976 }
977
54319661
JA
978 for (j = 0; j < nthreads; j++) {
979 s = get_submitter(j);
980
981 err = setup_ring(s);
982 if (err) {
983 printf("ring setup failed: %s, %d\n", strerror(errno), err);
984 return 1;
985 }
c9fb4c5b 986 }
54319661 987 s = get_submitter(0);
6a87c3b0 988 printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d", polled, fixedbufs, register_files, buffered);
e39863e3 989 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
c9fb4c5b 990
54319661
JA
991 for (j = 0; j < nthreads; j++) {
992 s = get_submitter(j);
993 pthread_create(&s->thread, NULL, submitter_fn, s);
994 }
c9fb4c5b 995
54319661 996 fdepths = malloc(8 * s->nr_files * nthreads);
05138221 997 reap = calls = done = 0;
c9fb4c5b
JA
998 do {
999 unsigned long this_done = 0;
1000 unsigned long this_reap = 0;
1001 unsigned long this_call = 0;
1002 unsigned long rpc = 0, ipc = 0;
f3057d26 1003 unsigned long iops, bw;
c9fb4c5b
JA
1004
1005 sleep(1);
54319661
JA
1006 for (j = 0; j < nthreads; j++) {
1007 this_done += s->done;
1008 this_call += s->calls;
1009 this_reap += s->reaps;
1010 }
c9fb4c5b
JA
1011 if (this_call - calls) {
1012 rpc = (this_done - done) / (this_call - calls);
1013 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
1014 } else
1015 rpc = ipc = -1;
2e7888ef 1016 file_depths(fdepths);
22fd3501 1017 iops = this_done - done;
f3057d26
JA
1018 if (bs > 1048576)
1019 bw = iops * (bs / 1048576);
1020 else
1021 bw = iops / (1048576 / bs);
6c5d3a1c
JA
1022 printf("IOPS=%lu, ", iops);
1023 if (!do_nop)
1024 printf("BW=%luMiB/s, ", bw);
1025 printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths);
c9fb4c5b
JA
1026 done = this_done;
1027 calls = this_call;
1028 reap = this_reap;
1029 } while (!finish);
1030
54319661
JA
1031 for (j = 0; j < nthreads; j++) {
1032 s = get_submitter(j);
1033 pthread_join(s->thread, &ret);
1034 close(s->ring_fd);
932131c9
JA
1035
1036 if (stats) {
1037 unsigned long nr;
1038
1039 printf("%d: Latency percentiles:\n", s->tid);
1040 for (i = 0, nr = 0; i < PLAT_NR; i++)
1041 nr += s->plat[i];
1042 show_clat_percentiles(s->plat, nr, 4);
1043 free(s->clock_batch);
1044 free(s->plat);
1045 }
54319661 1046 }
932131c9 1047
2e7888ef 1048 free(fdepths);
54319661 1049 free(submitter);
c9fb4c5b
JA
1050 return 0;
1051}