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