t/io_uring: prep for including engines/nvme.h in t/io_uring
[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
4b9e13dc
JA
14#ifdef CONFIG_LIBNUMA
15#include <numa.h>
16#endif
17
c9fb4c5b
JA
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <sys/ioctl.h>
21#include <sys/syscall.h>
22#include <sys/resource.h>
c3e2fc25 23#include <sys/mman.h>
e31b8288 24#include <sys/uio.h>
c9fb4c5b
JA
25#include <linux/fs.h>
26#include <fcntl.h>
27#include <unistd.h>
c9fb4c5b
JA
28#include <string.h>
29#include <pthread.h>
30#include <sched.h>
31
74efb029 32#include "../arch/arch.h"
57fa61f0 33#include "../lib/types.h"
932131c9 34#include "../lib/roundup.h"
9eff5320 35#include "../lib/rand.h"
932131c9 36#include "../minmax.h"
f3e769a4 37#include "../os/linux/io_uring.h"
ac122fea 38
e31b8288 39struct io_sq_ring {
e2239016
JA
40 unsigned *head;
41 unsigned *tail;
42 unsigned *ring_mask;
43 unsigned *ring_entries;
ce1705de 44 unsigned *flags;
e2239016 45 unsigned *array;
c9fb4c5b
JA
46};
47
e31b8288 48struct io_cq_ring {
e2239016
JA
49 unsigned *head;
50 unsigned *tail;
51 unsigned *ring_mask;
52 unsigned *ring_entries;
f0403f94 53 struct io_uring_cqe *cqes;
c9fb4c5b
JA
54};
55
701d1277 56#define DEPTH 128
2e7888ef
JA
57#define BATCH_SUBMIT 32
58#define BATCH_COMPLETE 32
c9fb4c5b
JA
59#define BS 4096
60
a7086591
JA
61#define MAX_FDS 16
62
c3e2fc25 63static unsigned sq_ring_mask, cq_ring_mask;
e39c34dc 64
a7086591
JA
65struct file {
66 unsigned long max_blocks;
beda9d8d
JA
67 unsigned long max_size;
68 unsigned long cur_off;
701d1277 69 unsigned pending_ios;
48e698fa
JA
70 int real_fd;
71 int fixed_fd;
932131c9 72 int fileno;
a7086591
JA
73};
74
932131c9
JA
75#define PLAT_BITS 6
76#define PLAT_VAL (1 << PLAT_BITS)
77#define PLAT_GROUP_NR 29
78#define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
79
c9fb4c5b
JA
80struct submitter {
81 pthread_t thread;
f310970e 82 int ring_fd;
ca8c91c5 83 int enter_ring_fd;
54319661 84 int index;
e31b8288 85 struct io_sq_ring sq_ring;
f0403f94 86 struct io_uring_sqe *sqes;
e31b8288 87 struct io_cq_ring cq_ring;
c9fb4c5b 88 int inflight;
932131c9 89 int tid;
c9fb4c5b
JA
90 unsigned long reaps;
91 unsigned long done;
92 unsigned long calls;
93 volatile int finish;
701d1277 94
48e698fa
JA
95 __s32 *fds;
96
9eff5320
JA
97 struct taus258_state rand_state;
98
932131c9
JA
99 unsigned long *clock_batch;
100 int clock_index;
101 unsigned long *plat;
102
256714ea
JA
103#ifdef CONFIG_LIBAIO
104 io_context_t aio_ctx;
105#endif
106
4b9e13dc
JA
107 int numa_node;
108 const char *filename;
109
a7086591
JA
110 struct file files[MAX_FDS];
111 unsigned nr_files;
112 unsigned cur_file;
e39863e3 113 struct iovec iovecs[];
c9fb4c5b
JA
114};
115
e39863e3 116static struct submitter *submitter;
c9fb4c5b 117static volatile int finish;
52479d8b 118static int stats_running;
16d25711 119static unsigned long max_iops;
c409e4c2 120static long t_io_uring_page_size;
c9fb4c5b 121
e39863e3
KB
122static int depth = DEPTH;
123static int batch_submit = BATCH_SUBMIT;
124static int batch_complete = BATCH_COMPLETE;
5bd526f2 125static int bs = BS;
f0403f94 126static int polled = 1; /* use IO polling */
701d1277 127static int fixedbufs = 1; /* use fixed user buffers */
a71ad043 128static int dma_map; /* pre-map DMA buffers */
8c5fa755 129static int register_files = 1; /* use fixed files */
f0403f94 130static int buffered = 0; /* use buffered IO, not O_DIRECT */
3d7d00a3
JA
131static int sq_thread_poll = 0; /* use kernel submission/poller thread */
132static int sq_thread_cpu = -1; /* pin above thread to this CPU */
8025517d 133static int do_nop = 0; /* no-op SQ ring commands */
54319661 134static int nthreads = 1;
932131c9 135static int stats = 0; /* generate IO stats */
256714ea 136static int aio = 0; /* use libaio */
beda9d8d
JA
137static int runtime = 0; /* runtime */
138static int random_io = 1; /* random or sequential IO */
ca8c91c5 139static int register_ring = 1; /* register ring */
379406bc 140static int use_sync = 0; /* use preadv2 */
4b9e13dc 141static int numa_placement = 0; /* set to node of device */
256714ea 142
932131c9 143static unsigned long tsc_rate;
c9fb4c5b 144
203e4c26
JA
145#define TSC_RATE_FILE "tsc-rate"
146
84106576 147static int vectored = 1;
b3915995 148
932131c9 149static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0,
ad45a465 150 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 };
932131c9
JA
151static int plist_len = 17;
152
a71ad043 153#ifndef IORING_REGISTER_MAP_BUFFERS
8538256c 154#define IORING_REGISTER_MAP_BUFFERS 22
a71ad043
JA
155struct io_uring_map_buffers {
156 __s32 fd;
157 __u32 buf_start;
158 __u32 buf_end;
702b0be2
JA
159 __u32 flags;
160 __u64 rsvd[2];
a71ad043
JA
161};
162#endif
163
932131c9
JA
164static unsigned long cycles_to_nsec(unsigned long cycles)
165{
166 uint64_t val;
167
168 if (!tsc_rate)
169 return cycles;
170
171 val = cycles * 1000000000ULL;
172 return val / tsc_rate;
173}
174
175static unsigned long plat_idx_to_val(unsigned int idx)
176{
177 unsigned int error_bits;
178 unsigned long k, base;
179
180 assert(idx < PLAT_NR);
181
182 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
183 * all bits of the sample as index */
184 if (idx < (PLAT_VAL << 1))
185 return cycles_to_nsec(idx);
186
187 /* Find the group and compute the minimum value of that group */
188 error_bits = (idx >> PLAT_BITS) - 1;
189 base = ((unsigned long) 1) << (error_bits + PLAT_BITS);
190
191 /* Find its bucket number of the group */
192 k = idx % PLAT_VAL;
193
194 /* Return the mean of the range of the bucket */
195 return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits)));
196}
197
c409e4c2
AG
198unsigned int calculate_clat_percentiles(unsigned long *io_u_plat,
199 unsigned long nr, unsigned long **output,
200 unsigned long *maxv, unsigned long *minv)
932131c9
JA
201{
202 unsigned long sum = 0;
203 unsigned int len = plist_len, i, j = 0;
204 unsigned long *ovals = NULL;
205 bool is_last;
206
bb209d68 207 *minv = -1UL;
932131c9
JA
208 *maxv = 0;
209
210 ovals = malloc(len * sizeof(*ovals));
211 if (!ovals)
212 return 0;
213
214 /*
215 * Calculate bucket values, note down max and min values
216 */
217 is_last = false;
218 for (i = 0; i < PLAT_NR && !is_last; i++) {
219 sum += io_u_plat[i];
220 while (sum >= ((long double) plist[j] / 100.0 * nr)) {
221 assert(plist[j] <= 100.0);
222
223 ovals[j] = plat_idx_to_val(i);
224 if (ovals[j] < *minv)
225 *minv = ovals[j];
226 if (ovals[j] > *maxv)
227 *maxv = ovals[j];
228
229 is_last = (j == len - 1) != 0;
230 if (is_last)
231 break;
232
233 j++;
234 }
235 }
236
237 if (!is_last)
238 fprintf(stderr, "error calculating latency percentiles\n");
239
240 *output = ovals;
241 return len;
242}
243
244static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr,
245 unsigned int precision)
246{
247 unsigned int divisor, len, i, j = 0;
248 unsigned long minv, maxv;
249 unsigned long *ovals;
250 int per_line, scale_down, time_width;
251 bool is_last;
252 char fmt[32];
253
c409e4c2 254 len = calculate_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv);
932131c9
JA
255 if (!len || !ovals)
256 goto out;
257
258 if (!tsc_rate) {
259 scale_down = 0;
260 divisor = 1;
261 printf(" percentiles (tsc ticks):\n |");
262 } else if (minv > 2000 && maxv > 99999) {
263 scale_down = 1;
264 divisor = 1000;
265 printf(" percentiles (usec):\n |");
266 } else {
267 scale_down = 0;
268 divisor = 1;
269 printf(" percentiles (nsec):\n |");
270 }
271
272 time_width = max(5, (int) (log10(maxv / divisor) + 1));
273 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
274 precision, time_width);
275 /* fmt will be something like " %5.2fth=[%4llu]%c" */
276 per_line = (80 - 7) / (precision + 10 + time_width);
277
278 for (j = 0; j < len; j++) {
279 /* for formatting */
280 if (j != 0 && (j % per_line) == 0)
281 printf(" |");
282
283 /* end of the list */
284 is_last = (j == len - 1) != 0;
285
286 for (i = 0; i < scale_down; i++)
287 ovals[j] = (ovals[j] + 999) / 1000;
288
289 printf(fmt, plist[j], ovals[j], is_last ? '\n' : ',');
290
291 if (is_last)
292 break;
293
294 if ((j % per_line) == per_line - 1) /* for formatting */
295 printf("\n");
296 }
297
298out:
299 free(ovals);
300}
301
b65c1fc0 302#ifdef ARCH_HAVE_CPU_CLOCK
932131c9
JA
303static unsigned int plat_val_to_idx(unsigned long val)
304{
305 unsigned int msb, error_bits, base, offset, idx;
306
307 /* Find MSB starting from bit 0 */
308 if (val == 0)
309 msb = 0;
310 else
311 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
312
313 /*
314 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
315 * all bits of the sample as index
316 */
317 if (msb <= PLAT_BITS)
318 return val;
319
320 /* Compute the number of error bits to discard*/
321 error_bits = msb - PLAT_BITS;
322
323 /* Compute the number of buckets before the group */
324 base = (error_bits + 1) << PLAT_BITS;
325
326 /*
327 * Discard the error bits and apply the mask to find the
328 * index for the buckets in the group
329 */
330 offset = (PLAT_VAL - 1) & (val >> error_bits);
331
332 /* Make sure the index does not exceed (array size - 1) */
333 idx = (base + offset) < (PLAT_NR - 1) ?
334 (base + offset) : (PLAT_NR - 1);
335
336 return idx;
337}
b65c1fc0 338#endif
932131c9
JA
339
340static void add_stat(struct submitter *s, int clock_index, int nr)
341{
342#ifdef ARCH_HAVE_CPU_CLOCK
343 unsigned long cycles;
344 unsigned int pidx;
345
265697fc
JA
346 if (!s->finish && clock_index) {
347 cycles = get_cpu_clock();
348 cycles -= s->clock_batch[clock_index];
349 pidx = plat_val_to_idx(cycles);
350 s->plat[pidx] += nr;
351 }
932131c9
JA
352#endif
353}
354
a71ad043
JA
355static int io_uring_map_buffers(struct submitter *s)
356{
357 struct io_uring_map_buffers map = {
358 .fd = s->files[0].real_fd,
a71ad043 359 .buf_end = depth,
a71ad043
JA
360 };
361
362 if (do_nop)
363 return 0;
d49885aa
JA
364 if (s->nr_files > 1)
365 fprintf(stdout, "Mapping buffers may not work with multiple files\n");
a71ad043
JA
366
367 return syscall(__NR_io_uring_register, s->ring_fd,
368 IORING_REGISTER_MAP_BUFFERS, &map, 1);
369}
370
2ea53ca3 371static int io_uring_register_buffers(struct submitter *s)
c9fb4c5b 372{
8025517d
JA
373 if (do_nop)
374 return 0;
375
bfed648c 376 return syscall(__NR_io_uring_register, s->ring_fd,
55845033 377 IORING_REGISTER_BUFFERS, s->iovecs, roundup_pow2(depth));
2ea53ca3
JA
378}
379
a7abc9fb
JA
380static int io_uring_register_files(struct submitter *s)
381{
48e698fa 382 int i;
a7abc9fb 383
8025517d
JA
384 if (do_nop)
385 return 0;
386
48e698fa
JA
387 s->fds = calloc(s->nr_files, sizeof(__s32));
388 for (i = 0; i < s->nr_files; i++) {
389 s->fds[i] = s->files[i].real_fd;
390 s->files[i].fixed_fd = i;
391 }
a7abc9fb 392
bfed648c 393 return syscall(__NR_io_uring_register, s->ring_fd,
919850d2 394 IORING_REGISTER_FILES, s->fds, s->nr_files);
a7abc9fb
JA
395}
396
2ea53ca3
JA
397static int io_uring_setup(unsigned entries, struct io_uring_params *p)
398{
1db268db
JA
399 /*
400 * Clamp CQ ring size at our SQ ring size, we don't need more entries
401 * than that.
402 */
403 p->flags |= IORING_SETUP_CQSIZE;
404 p->cq_entries = entries;
405
bfed648c 406 return syscall(__NR_io_uring_setup, entries, p);
c9fb4c5b
JA
407}
408
b3915995
JA
409static void io_uring_probe(int fd)
410{
411 struct io_uring_probe *p;
412 int ret;
413
414 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
415 if (!p)
416 return;
417
418 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
419 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
420 if (ret < 0)
421 goto out;
422
423 if (IORING_OP_READ > p->ops_len)
424 goto out;
425
426 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
84106576 427 vectored = 0;
b3915995
JA
428out:
429 free(p);
430}
431
c3e2fc25
JA
432static int io_uring_enter(struct submitter *s, unsigned int to_submit,
433 unsigned int min_complete, unsigned int flags)
c9fb4c5b 434{
ca8c91c5
JA
435 if (register_ring)
436 flags |= IORING_ENTER_REGISTERED_RING;
c377f4f8 437#ifdef FIO_ARCH_HAS_SYSCALL
ca8c91c5 438 return __do_syscall6(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
c377f4f8
JA
439 min_complete, flags, NULL, 0);
440#else
ca8c91c5
JA
441 return syscall(__NR_io_uring_enter, s->enter_ring_fd, to_submit,
442 min_complete, flags, NULL, 0);
c377f4f8 443#endif
c9fb4c5b
JA
444}
445
77d814a1 446#ifndef CONFIG_HAVE_GETTID
c9fb4c5b
JA
447static int gettid(void)
448{
449 return syscall(__NR_gettid);
450}
77d814a1 451#endif
c9fb4c5b 452
701d1277
JA
453static unsigned file_depth(struct submitter *s)
454{
e39863e3 455 return (depth + s->nr_files - 1) / s->nr_files;
701d1277
JA
456}
457
a7086591 458static void init_io(struct submitter *s, unsigned index)
c9fb4c5b 459{
f0403f94 460 struct io_uring_sqe *sqe = &s->sqes[index];
c9fb4c5b 461 unsigned long offset;
a7086591 462 struct file *f;
c9fb4c5b
JA
463 long r;
464
8025517d
JA
465 if (do_nop) {
466 sqe->opcode = IORING_OP_NOP;
467 return;
468 }
469
701d1277
JA
470 if (s->nr_files == 1) {
471 f = &s->files[0];
472 } else {
473 f = &s->files[s->cur_file];
474 if (f->pending_ios >= file_depth(s)) {
475 s->cur_file++;
476 if (s->cur_file == s->nr_files)
477 s->cur_file = 0;
93d1811c 478 f = &s->files[s->cur_file];
701d1277
JA
479 }
480 }
481 f->pending_ios++;
a7086591 482
beda9d8d
JA
483 if (random_io) {
484 r = __rand64(&s->rand_state);
485 offset = (r % (f->max_blocks - 1)) * bs;
486 } else {
487 offset = f->cur_off;
488 f->cur_off += bs;
489 if (f->cur_off + bs > f->max_size)
490 f->cur_off = 0;
491 }
c9fb4c5b 492
8c5fa755
JA
493 if (register_files) {
494 sqe->flags = IOSQE_FIXED_FILE;
495 sqe->fd = f->fixed_fd;
496 } else {
497 sqe->flags = 0;
498 sqe->fd = f->real_fd;
499 }
f0403f94 500 if (fixedbufs) {
48e698fa 501 sqe->opcode = IORING_OP_READ_FIXED;
919850d2 502 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
5bd526f2 503 sqe->len = bs;
2ea53ca3 504 sqe->buf_index = index;
84106576 505 } else if (!vectored) {
b3915995
JA
506 sqe->opcode = IORING_OP_READ;
507 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
508 sqe->len = bs;
509 sqe->buf_index = 0;
84106576
JA
510 } else {
511 sqe->opcode = IORING_OP_READV;
512 sqe->addr = (unsigned long) &s->iovecs[index];
513 sqe->len = 1;
514 sqe->buf_index = 0;
f0403f94 515 }
f0403f94 516 sqe->ioprio = 0;
f0403f94 517 sqe->off = offset;
932131c9 518 sqe->user_data = (unsigned long) f->fileno;
52479d8b 519 if (stats && stats_running)
bb209d68 520 sqe->user_data |= ((uint64_t)s->clock_index << 32);
c9fb4c5b
JA
521}
522
256714ea 523static int prep_more_ios_uring(struct submitter *s, int max_ios)
c9fb4c5b 524{
e31b8288 525 struct io_sq_ring *ring = &s->sq_ring;
e2239016 526 unsigned index, tail, next_tail, prepped = 0;
c9fb4c5b 527
c3e2fc25 528 next_tail = tail = *ring->tail;
c9fb4c5b
JA
529 do {
530 next_tail++;
fc2dc21b 531 if (next_tail == atomic_load_acquire(ring->head))
c9fb4c5b
JA
532 break;
533
e39c34dc 534 index = tail & sq_ring_mask;
a7086591 535 init_io(s, index);
c3e2fc25 536 ring->array[index] = index;
c9fb4c5b
JA
537 prepped++;
538 tail = next_tail;
539 } while (prepped < max_ios);
540
fc2dc21b
JA
541 if (prepped)
542 atomic_store_release(ring->tail, tail);
c9fb4c5b
JA
543 return prepped;
544}
545
a7086591 546static int get_file_size(struct file *f)
c9fb4c5b
JA
547{
548 struct stat st;
549
48e698fa 550 if (fstat(f->real_fd, &st) < 0)
c9fb4c5b
JA
551 return -1;
552 if (S_ISBLK(st.st_mode)) {
553 unsigned long long bytes;
554
48e698fa 555 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
c9fb4c5b
JA
556 return -1;
557
5bd526f2 558 f->max_blocks = bytes / bs;
beda9d8d 559 f->max_size = bytes;
c9fb4c5b
JA
560 return 0;
561 } else if (S_ISREG(st.st_mode)) {
5bd526f2 562 f->max_blocks = st.st_size / bs;
beda9d8d 563 f->max_size = st.st_size;
c9fb4c5b
JA
564 return 0;
565 }
566
567 return -1;
568}
569
256714ea 570static int reap_events_uring(struct submitter *s)
c9fb4c5b 571{
e31b8288 572 struct io_cq_ring *ring = &s->cq_ring;
f0403f94 573 struct io_uring_cqe *cqe;
e2239016 574 unsigned head, reaped = 0;
ab85494f 575 int last_idx = -1, stat_nr = 0;
c9fb4c5b 576
c3e2fc25 577 head = *ring->head;
c9fb4c5b 578 do {
701d1277
JA
579 struct file *f;
580
679d8352 581 read_barrier();
fc2dc21b 582 if (head == atomic_load_acquire(ring->tail))
c9fb4c5b 583 break;
f0403f94 584 cqe = &ring->cqes[head & cq_ring_mask];
8025517d 585 if (!do_nop) {
932131c9
JA
586 int fileno = cqe->user_data & 0xffffffff;
587
588 f = &s->files[fileno];
8025517d 589 f->pending_ios--;
5bd526f2 590 if (cqe->res != bs) {
8025517d 591 printf("io: unexpected ret=%d\n", cqe->res);
154a9582 592 if (polled && cqe->res == -EOPNOTSUPP)
8066f6b6 593 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
8025517d
JA
594 return -1;
595 }
c9fb4c5b 596 }
932131c9
JA
597 if (stats) {
598 int clock_index = cqe->user_data >> 32;
599
ab85494f
JA
600 if (last_idx != clock_index) {
601 if (last_idx != -1) {
602 add_stat(s, last_idx, stat_nr);
603 stat_nr = 0;
604 }
605 last_idx = clock_index;
d4af2ece
JA
606 }
607 stat_nr++;
932131c9 608 }
c9fb4c5b
JA
609 reaped++;
610 head++;
c9fb4c5b
JA
611 } while (1);
612
ab85494f
JA
613 if (stat_nr)
614 add_stat(s, last_idx, stat_nr);
615
fc2dc21b
JA
616 if (reaped) {
617 s->inflight -= reaped;
618 atomic_store_release(ring->head, head);
619 }
c9fb4c5b
JA
620 return reaped;
621}
622
4b9e13dc
JA
623static void set_affinity(struct submitter *s)
624{
625#ifdef CONFIG_LIBNUMA
626 struct bitmask *mask;
627
628 if (s->numa_node == -1)
629 return;
630
631 numa_set_preferred(s->numa_node);
632
633 mask = numa_allocate_cpumask();
634 numa_node_to_cpus(s->numa_node, mask);
635 numa_sched_setaffinity(s->tid, mask);
636#endif
637}
638
639static int detect_node(struct submitter *s, const char *name)
640{
641#ifdef CONFIG_LIBNUMA
642 const char *base = basename(name);
643 char str[128];
644 int ret, fd, node;
645
646 sprintf(str, "/sys/block/%s/device/numa_node", base);
647 fd = open(str, O_RDONLY);
648 if (fd < 0)
649 return -1;
650
651 ret = read(fd, str, sizeof(str));
652 if (ret < 0) {
653 close(fd);
654 return -1;
655 }
656 node = atoi(str);
657 s->numa_node = node;
658 close(fd);
659#else
660 s->numa_node = -1;
661#endif
662 return 0;
663}
664
665static int setup_aio(struct submitter *s)
666{
667#ifdef CONFIG_LIBAIO
668 if (polled) {
669 fprintf(stderr, "aio does not support polled IO\n");
670 polled = 0;
671 }
672 if (sq_thread_poll) {
673 fprintf(stderr, "aio does not support SQPOLL IO\n");
674 sq_thread_poll = 0;
675 }
676 if (do_nop) {
677 fprintf(stderr, "aio does not support polled IO\n");
678 do_nop = 0;
679 }
680 if (fixedbufs || register_files) {
681 fprintf(stderr, "aio does not support registered files or buffers\n");
682 fixedbufs = register_files = 0;
683 }
684
685 return io_queue_init(roundup_pow2(depth), &s->aio_ctx);
686#else
687 fprintf(stderr, "Legacy AIO not available on this system/build\n");
688 errno = EINVAL;
689 return -1;
690#endif
691}
692
693static int setup_ring(struct submitter *s)
694{
695 struct io_sq_ring *sring = &s->sq_ring;
696 struct io_cq_ring *cring = &s->cq_ring;
697 struct io_uring_params p;
698 int ret, fd;
699 void *ptr;
700
701 memset(&p, 0, sizeof(p));
702
703 if (polled && !do_nop)
704 p.flags |= IORING_SETUP_IOPOLL;
705 if (sq_thread_poll) {
706 p.flags |= IORING_SETUP_SQPOLL;
707 if (sq_thread_cpu != -1) {
708 p.flags |= IORING_SETUP_SQ_AFF;
709 p.sq_thread_cpu = sq_thread_cpu;
710 }
711 }
712
713 fd = io_uring_setup(depth, &p);
714 if (fd < 0) {
715 perror("io_uring_setup");
716 return 1;
717 }
718 s->ring_fd = s->enter_ring_fd = fd;
719
720 io_uring_probe(fd);
721
722 if (fixedbufs) {
723 struct rlimit rlim;
724
725 rlim.rlim_cur = RLIM_INFINITY;
726 rlim.rlim_max = RLIM_INFINITY;
727 /* ignore potential error, not needed on newer kernels */
728 setrlimit(RLIMIT_MEMLOCK, &rlim);
729
730 ret = io_uring_register_buffers(s);
731 if (ret < 0) {
732 perror("io_uring_register_buffers");
733 return 1;
734 }
735
736 if (dma_map) {
737 ret = io_uring_map_buffers(s);
738 if (ret < 0) {
739 perror("io_uring_map_buffers");
740 return 1;
741 }
742 }
743 }
744
745 if (register_files) {
746 ret = io_uring_register_files(s);
747 if (ret < 0) {
748 perror("io_uring_register_files");
749 return 1;
750 }
751 }
752
753 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
754 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
755 IORING_OFF_SQ_RING);
756 sring->head = ptr + p.sq_off.head;
757 sring->tail = ptr + p.sq_off.tail;
758 sring->ring_mask = ptr + p.sq_off.ring_mask;
759 sring->ring_entries = ptr + p.sq_off.ring_entries;
760 sring->flags = ptr + p.sq_off.flags;
761 sring->array = ptr + p.sq_off.array;
762 sq_ring_mask = *sring->ring_mask;
763
764 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
765 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
766 IORING_OFF_SQES);
767
768 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
769 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
770 IORING_OFF_CQ_RING);
771 cring->head = ptr + p.cq_off.head;
772 cring->tail = ptr + p.cq_off.tail;
773 cring->ring_mask = ptr + p.cq_off.ring_mask;
774 cring->ring_entries = ptr + p.cq_off.ring_entries;
775 cring->cqes = ptr + p.cq_off.cqes;
776 cq_ring_mask = *cring->ring_mask;
777 return 0;
778}
779
780static void *allocate_mem(struct submitter *s, int size)
781{
782 void *buf;
783
784#ifdef CONFIG_LIBNUMA
785 if (s->numa_node != -1)
786 return numa_alloc_onnode(size, s->numa_node);
787#endif
788
c409e4c2 789 if (posix_memalign(&buf, t_io_uring_page_size, bs)) {
4b9e13dc
JA
790 printf("failed alloc\n");
791 return NULL;
792 }
793
794 return buf;
795}
796
256714ea 797static int submitter_init(struct submitter *s)
c9fb4c5b 798{
4b9e13dc
JA
799 int i, nr_batch, err;
800 static int init_printed;
801 char buf[80];
932131c9 802 s->tid = gettid();
4b9e13dc
JA
803 printf("submitter=%d, tid=%d, file=%s, node=%d\n", s->index, s->tid,
804 s->filename, s->numa_node);
805
806 set_affinity(s);
c9fb4c5b 807
6243766b
KR
808 __init_rand64(&s->rand_state, s->tid);
809 srand48(s->tid);
c9fb4c5b 810
932131c9
JA
811 for (i = 0; i < MAX_FDS; i++)
812 s->files[i].fileno = i;
813
4b9e13dc
JA
814 for (i = 0; i < roundup_pow2(depth); i++) {
815 void *buf;
816
817 buf = allocate_mem(s, bs);
818 if (!buf)
819 return 1;
820 s->iovecs[i].iov_base = buf;
821 s->iovecs[i].iov_len = bs;
822 }
823
824 if (use_sync) {
825 sprintf(buf, "Engine=preadv2\n");
826 err = 0;
827 } else if (!aio) {
828 err = setup_ring(s);
829 sprintf(buf, "Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
830 } else {
831 sprintf(buf, "Engine=aio\n");
832 err = setup_aio(s);
833 }
834 if (err) {
835 printf("queue setup failed: %s, %d\n", strerror(errno), err);
836 return 1;
837 }
838
839 if (!init_printed) {
840 printf("polled=%d, fixedbufs=%d/%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, dma_map, register_files, buffered, depth);
841 printf("%s", buf);
842 init_printed = 1;
843 }
844
932131c9
JA
845 if (stats) {
846 nr_batch = roundup_pow2(depth / batch_submit);
d4af2ece
JA
847 if (nr_batch < 2)
848 nr_batch = 2;
932131c9 849 s->clock_batch = calloc(nr_batch, sizeof(unsigned long));
52479d8b 850 s->clock_index = 1;
932131c9
JA
851
852 s->plat = calloc(PLAT_NR, sizeof(unsigned long));
853 } else {
854 s->clock_batch = NULL;
855 s->plat = NULL;
856 nr_batch = 0;
857 }
858
256714ea
JA
859 return nr_batch;
860}
861
862#ifdef CONFIG_LIBAIO
863static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs)
864{
8310c570
VF
865 uint64_t data;
866 long long offset;
256714ea
JA
867 struct file *f;
868 unsigned index;
869 long r;
870
871 index = 0;
872 while (index < max_ios) {
873 struct iocb *iocb = &iocbs[index];
874
875 if (s->nr_files == 1) {
876 f = &s->files[0];
877 } else {
878 f = &s->files[s->cur_file];
879 if (f->pending_ios >= file_depth(s)) {
880 s->cur_file++;
881 if (s->cur_file == s->nr_files)
882 s->cur_file = 0;
883 f = &s->files[s->cur_file];
884 }
885 }
886 f->pending_ios++;
887
888 r = lrand48();
889 offset = (r % (f->max_blocks - 1)) * bs;
890 io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base,
891 s->iovecs[index].iov_len, offset);
892
893 data = f->fileno;
52479d8b 894 if (stats && stats_running)
8310c570 895 data |= (((uint64_t) s->clock_index) << 32);
256714ea
JA
896 iocb->data = (void *) (uintptr_t) data;
897 index++;
898 }
899 return index;
900}
901
902static int reap_events_aio(struct submitter *s, struct io_event *events, int evs)
903{
904 int last_idx = -1, stat_nr = 0;
905 int reaped = 0;
906
907 while (evs) {
8310c570 908 uint64_t data = (uintptr_t) events[reaped].data;
256714ea
JA
909 struct file *f = &s->files[data & 0xffffffff];
910
911 f->pending_ios--;
912 if (events[reaped].res != bs) {
913 printf("io: unexpected ret=%ld\n", events[reaped].res);
914 return -1;
915 }
916 if (stats) {
917 int clock_index = data >> 32;
918
919 if (last_idx != clock_index) {
920 if (last_idx != -1) {
921 add_stat(s, last_idx, stat_nr);
922 stat_nr = 0;
923 }
924 last_idx = clock_index;
d4af2ece
JA
925 }
926 stat_nr++;
256714ea
JA
927 }
928 reaped++;
929 evs--;
930 }
931
932 if (stat_nr)
933 add_stat(s, last_idx, stat_nr);
934
935 s->inflight -= reaped;
936 s->done += reaped;
937 return reaped;
938}
939
940static void *submitter_aio_fn(void *data)
941{
942 struct submitter *s = data;
71989c1b 943 int i, ret, prepped;
256714ea
JA
944 struct iocb **iocbsptr;
945 struct iocb *iocbs;
946 struct io_event *events;
71989c1b
JA
947#ifdef ARCH_HAVE_CPU_CLOCK
948 int nr_batch = submitter_init(s);
949#else
950 submitter_init(s);
951#endif
256714ea
JA
952
953 iocbsptr = calloc(depth, sizeof(struct iocb *));
954 iocbs = calloc(depth, sizeof(struct iocb));
955 events = calloc(depth, sizeof(struct io_event));
956
957 for (i = 0; i < depth; i++)
958 iocbsptr[i] = &iocbs[i];
959
960 prepped = 0;
961 do {
962 int to_wait, to_submit, to_prep;
963
964 if (!prepped && s->inflight < depth) {
965 to_prep = min(depth - s->inflight, batch_submit);
966 prepped = prep_more_ios_aio(s, to_prep, iocbs);
967#ifdef ARCH_HAVE_CPU_CLOCK
968 if (prepped && stats) {
969 s->clock_batch[s->clock_index] = get_cpu_clock();
970 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
971 }
972#endif
973 }
974 s->inflight += prepped;
975 to_submit = prepped;
976
977 if (to_submit && (s->inflight + to_submit <= depth))
978 to_wait = 0;
979 else
980 to_wait = min(s->inflight + to_submit, batch_complete);
981
982 ret = io_submit(s->aio_ctx, to_submit, iocbsptr);
983 s->calls++;
984 if (ret < 0) {
985 perror("io_submit");
986 break;
987 } else if (ret != to_submit) {
988 printf("submitted %d, wanted %d\n", ret, to_submit);
989 break;
990 }
991 prepped = 0;
992
24a24c12 993 while (to_wait) {
256714ea
JA
994 int r;
995
24a24c12
JA
996 s->calls++;
997 r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL);
998 if (r < 0) {
999 perror("io_getevents");
1000 break;
1001 } else if (r != to_wait) {
1002 printf("r=%d, wait=%d\n", r, to_wait);
1003 break;
1004 }
1005 r = reap_events_aio(s, events, r);
1006 s->reaps += r;
1007 to_wait -= r;
256714ea
JA
1008 }
1009 } while (!s->finish);
1010
1011 free(iocbsptr);
1012 free(iocbs);
1013 free(events);
1014 finish = 1;
1015 return NULL;
1016}
1017#endif
1018
ca8c91c5
JA
1019static void io_uring_unregister_ring(struct submitter *s)
1020{
1021 struct io_uring_rsrc_update up = {
1022 .offset = s->enter_ring_fd,
1023 };
1024
1025 syscall(__NR_io_uring_register, s->ring_fd, IORING_UNREGISTER_RING_FDS,
1026 &up, 1);
1027}
1028
1029static int io_uring_register_ring(struct submitter *s)
1030{
1031 struct io_uring_rsrc_update up = {
1032 .data = s->ring_fd,
1033 .offset = -1U,
1034 };
1035 int ret;
1036
1037 ret = syscall(__NR_io_uring_register, s->ring_fd,
1038 IORING_REGISTER_RING_FDS, &up, 1);
1039 if (ret == 1) {
1040 s->enter_ring_fd = up.offset;
1041 return 0;
1042 }
1043 register_ring = 0;
1044 return -1;
1045}
1046
256714ea
JA
1047static void *submitter_uring_fn(void *data)
1048{
1049 struct submitter *s = data;
1050 struct io_sq_ring *ring = &s->sq_ring;
b65c1fc0
JA
1051 int ret, prepped;
1052#ifdef ARCH_HAVE_CPU_CLOCK
1053 int nr_batch = submitter_init(s);
1054#else
1055 submitter_init(s);
1056#endif
256714ea 1057
ca8c91c5
JA
1058 if (register_ring)
1059 io_uring_register_ring(s);
1060
c9fb4c5b
JA
1061 prepped = 0;
1062 do {
f310970e 1063 int to_wait, to_submit, this_reap, to_prep;
fc2dc21b 1064 unsigned ring_flags = 0;
c9fb4c5b 1065
e39863e3
KB
1066 if (!prepped && s->inflight < depth) {
1067 to_prep = min(depth - s->inflight, batch_submit);
256714ea 1068 prepped = prep_more_ios_uring(s, to_prep);
932131c9
JA
1069#ifdef ARCH_HAVE_CPU_CLOCK
1070 if (prepped && stats) {
1071 s->clock_batch[s->clock_index] = get_cpu_clock();
1072 s->clock_index = (s->clock_index + 1) & (nr_batch - 1);
1073 }
1074#endif
f310970e 1075 }
c9fb4c5b
JA
1076 s->inflight += prepped;
1077submit_more:
1078 to_submit = prepped;
1079submit:
e39863e3 1080 if (to_submit && (s->inflight + to_submit <= depth))
c9fb4c5b
JA
1081 to_wait = 0;
1082 else
e39863e3 1083 to_wait = min(s->inflight + to_submit, batch_complete);
c9fb4c5b 1084
ce1705de
JA
1085 /*
1086 * Only need to call io_uring_enter if we're not using SQ thread
1087 * poll, or if IORING_SQ_NEED_WAKEUP is set.
1088 */
fc2dc21b
JA
1089 if (sq_thread_poll)
1090 ring_flags = atomic_load_acquire(ring->flags);
1091 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
e0abe388
JA
1092 unsigned flags = 0;
1093
1094 if (to_wait)
1095 flags = IORING_ENTER_GETEVENTS;
fc2dc21b 1096 if (ring_flags & IORING_SQ_NEED_WAKEUP)
b532dd6d 1097 flags |= IORING_ENTER_SQ_WAKEUP;
e0abe388 1098 ret = io_uring_enter(s, to_submit, to_wait, flags);
ce1705de 1099 s->calls++;
fc2dc21b
JA
1100 } else {
1101 /* for SQPOLL, we submitted it all effectively */
1102 ret = to_submit;
ce1705de 1103 }
c9fb4c5b 1104
ce1705de
JA
1105 /*
1106 * For non SQ thread poll, we already got the events we needed
1107 * through the io_uring_enter() above. For SQ thread poll, we
1108 * need to loop here until we find enough events.
1109 */
1110 this_reap = 0;
1111 do {
1112 int r;
256714ea
JA
1113
1114 r = reap_events_uring(s);
7d1435e6
JA
1115 if (r == -1) {
1116 s->finish = 1;
ce1705de 1117 break;
7d1435e6 1118 } else if (r > 0)
ce1705de
JA
1119 this_reap += r;
1120 } while (sq_thread_poll && this_reap < to_wait);
c9fb4c5b
JA
1121 s->reaps += this_reap;
1122
1123 if (ret >= 0) {
1124 if (!ret) {
1125 to_submit = 0;
1126 if (s->inflight)
1127 goto submit;
1128 continue;
1129 } else if (ret < to_submit) {
1130 int diff = to_submit - ret;
1131
1132 s->done += ret;
1133 prepped -= diff;
1134 goto submit_more;
1135 }
1136 s->done += ret;
1137 prepped = 0;
1138 continue;
1139 } else if (ret < 0) {
ac122fea 1140 if (errno == EAGAIN) {
c9fb4c5b
JA
1141 if (s->finish)
1142 break;
1143 if (this_reap)
1144 goto submit;
c9fb4c5b
JA
1145 to_submit = 0;
1146 goto submit;
1147 }
ac122fea 1148 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
1149 break;
1150 }
1151 } while (!s->finish);
a7086591 1152
ca8c91c5
JA
1153 if (register_ring)
1154 io_uring_unregister_ring(s);
1155
c9fb4c5b
JA
1156 finish = 1;
1157 return NULL;
1158}
1159
a7648136 1160#ifdef CONFIG_PWRITEV2
379406bc
JA
1161static void *submitter_sync_fn(void *data)
1162{
1163 struct submitter *s = data;
1164 int ret;
1165
1166 submitter_init(s);
1167
1168 do {
1169 uint64_t offset;
1170 struct file *f;
1171 long r;
1172
1173 if (s->nr_files == 1) {
1174 f = &s->files[0];
1175 } else {
1176 f = &s->files[s->cur_file];
1177 if (f->pending_ios >= file_depth(s)) {
1178 s->cur_file++;
1179 if (s->cur_file == s->nr_files)
1180 s->cur_file = 0;
1181 f = &s->files[s->cur_file];
1182 }
1183 }
1184 f->pending_ios++;
1185
1186 if (random_io) {
1187 r = __rand64(&s->rand_state);
1188 offset = (r % (f->max_blocks - 1)) * bs;
1189 } else {
1190 offset = f->cur_off;
1191 f->cur_off += bs;
1192 if (f->cur_off + bs > f->max_size)
1193 f->cur_off = 0;
1194 }
1195
1196#ifdef ARCH_HAVE_CPU_CLOCK
1197 if (stats)
1198 s->clock_batch[s->clock_index] = get_cpu_clock();
1199#endif
1200
1201 s->inflight++;
1202 s->calls++;
1203
1204 if (polled)
1205 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, RWF_HIPRI);
1206 else
1207 ret = preadv2(f->real_fd, &s->iovecs[0], 1, offset, 0);
1208
1209 if (ret < 0) {
1210 perror("preadv2");
1211 break;
1212 } else if (ret != bs) {
1213 break;
1214 }
1215
1216 s->done++;
1217 s->inflight--;
1218 f->pending_ios--;
1219 if (stats)
1220 add_stat(s, s->clock_index, 1);
1221 } while (!s->finish);
1222
1223 finish = 1;
1224 return NULL;
1225}
a7648136
JA
1226#else
1227static void *submitter_sync_fn(void *data)
1228{
1229 finish = 1;
1230 return NULL;
1231}
1232#endif
379406bc 1233
54319661
JA
1234static struct submitter *get_submitter(int offset)
1235{
1236 void *ret;
1237
1238 ret = submitter;
1239 if (offset)
1240 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
1241 return ret;
1242}
1243
65e1a5e8 1244static void do_finish(const char *reason)
c9fb4c5b 1245{
54319661 1246 int j;
4b9e13dc 1247
65e1a5e8 1248 printf("Exiting on %s\n", reason);
54319661
JA
1249 for (j = 0; j < nthreads; j++) {
1250 struct submitter *s = get_submitter(j);
1251 s->finish = 1;
1252 }
4b9e13dc
JA
1253 if (max_iops > 1000000) {
1254 double miops = (double) max_iops / 1000000.0;
1255 printf("Maximum IOPS=%.2fM\n", miops);
1256 } else if (max_iops > 100000) {
1257 double kiops = (double) max_iops / 1000.0;
1258 printf("Maximum IOPS=%.2fK\n", kiops);
1259 } else {
18b557a0 1260 printf("Maximum IOPS=%lu\n", max_iops);
4b9e13dc 1261 }
c9fb4c5b
JA
1262 finish = 1;
1263}
1264
65e1a5e8
EV
1265static void sig_int(int sig)
1266{
1267 do_finish("signal");
1268}
1269
c9fb4c5b
JA
1270static void arm_sig_int(void)
1271{
1272 struct sigaction act;
1273
1274 memset(&act, 0, sizeof(act));
1275 act.sa_handler = sig_int;
1276 act.sa_flags = SA_RESTART;
1277 sigaction(SIGINT, &act, NULL);
2cf71009
BP
1278
1279 /* Windows uses SIGBREAK as a quit signal from other applications */
1280#ifdef WIN32
1281 sigaction(SIGBREAK, &act, NULL);
1282#endif
c9fb4c5b
JA
1283}
1284
d79ff8c9 1285static void usage(char *argv, int status)
e39863e3 1286{
65e1a5e8
EV
1287 char runtime_str[16];
1288 snprintf(runtime_str, sizeof(runtime_str), "%d", runtime);
e39863e3 1289 printf("%s [options] -- [filenames]\n"
35268e11
AJ
1290 " -d <int> : IO Depth, default %d\n"
1291 " -s <int> : Batch submit, default %d\n"
1292 " -c <int> : Batch complete, default %d\n"
1293 " -b <int> : Block size, default %d\n"
1294 " -p <bool> : Polled IO, default %d\n"
1295 " -B <bool> : Fixed buffers, default %d\n"
dd1f1ba0 1296 " -D <bool> : DMA map fixed buffers, default %d\n"
35268e11 1297 " -F <bool> : Register files, default %d\n"
0862f718 1298 " -n <int> : Number of threads, default %d\n"
2686fc22 1299 " -O <bool> : Use O_DIRECT, default %d\n"
4a39c524
EV
1300 " -N <bool> : Perform just no-op requests, default %d\n"
1301 " -t <bool> : Track IO latencies, default %d\n"
256714ea 1302 " -T <int> : TSC rate in HZ\n"
beda9d8d
JA
1303 " -r <int> : Runtime in seconds, default %s\n"
1304 " -R <bool> : Use random IO, default %d\n"
ca8c91c5 1305 " -a <bool> : Use legacy aio, default %d\n"
3be2f0ca 1306 " -S <bool> : Use sync IO (preadv2), default %d\n"
4b9e13dc
JA
1307 " -X <bool> : Use registered ring %d\n"
1308 " -P <bool> : Automatically place on device home node %d\n",
35268e11 1309 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
a71ad043 1310 fixedbufs, dma_map, register_files, nthreads, !buffered, do_nop,
ca8c91c5 1311 stats, runtime == 0 ? "unlimited" : runtime_str, random_io, aio,
4b9e13dc 1312 use_sync, register_ring, numa_placement);
d79ff8c9 1313 exit(status);
e39863e3
KB
1314}
1315
203e4c26
JA
1316static void read_tsc_rate(void)
1317{
1318 char buffer[32];
1319 int fd, ret;
1320
1321 if (tsc_rate)
1322 return;
1323
1324 fd = open(TSC_RATE_FILE, O_RDONLY);
1325 if (fd < 0)
1326 return;
1327
1328 ret = read(fd, buffer, sizeof(buffer));
1329 if (ret < 0) {
1330 close(fd);
1331 return;
1332 }
1333
1334 tsc_rate = strtoul(buffer, NULL, 10);
1335 printf("Using TSC rate %luHz\n", tsc_rate);
1336 close(fd);
1337}
1338
1339static void write_tsc_rate(void)
1340{
1341 char buffer[32];
1342 struct stat sb;
1343 int fd, ret;
1344
1345 if (!stat(TSC_RATE_FILE, &sb))
1346 return;
1347
1348 fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644);
1349 if (fd < 0)
1350 return;
1351
1352 memset(buffer, 0, sizeof(buffer));
1353 sprintf(buffer, "%lu", tsc_rate);
1354 ret = write(fd, buffer, strlen(buffer));
1355 if (ret < 0)
1356 perror("write");
1357 close(fd);
1358}
1359
c9fb4c5b
JA
1360int main(int argc, char *argv[])
1361{
e39863e3 1362 struct submitter *s;
05138221 1363 unsigned long done, calls, reap;
4b9e13dc 1364 int i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
d79ff8c9 1365 struct file f;
c3e2fc25 1366 void *ret;
c9fb4c5b 1367
0862f718
JA
1368 if (!do_nop && argc < 2)
1369 usage(argv[0], 1);
c9fb4c5b 1370
4b9e13dc 1371 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:r:D:R:X:S:P:h?")) != -1) {
e39863e3 1372 switch (opt) {
256714ea
JA
1373 case 'a':
1374 aio = !!atoi(optarg);
1375 break;
e39863e3
KB
1376 case 'd':
1377 depth = atoi(optarg);
1378 break;
1379 case 's':
1380 batch_submit = atoi(optarg);
932131c9
JA
1381 if (!batch_submit)
1382 batch_submit = 1;
e39863e3
KB
1383 break;
1384 case 'c':
1385 batch_complete = atoi(optarg);
932131c9
JA
1386 if (!batch_complete)
1387 batch_complete = 1;
e39863e3 1388 break;
5bd526f2
JA
1389 case 'b':
1390 bs = atoi(optarg);
1391 break;
1392 case 'p':
1393 polled = !!atoi(optarg);
1394 break;
6a87c3b0
JA
1395 case 'B':
1396 fixedbufs = !!atoi(optarg);
1397 break;
1398 case 'F':
1399 register_files = !!atoi(optarg);
1400 break;
54319661
JA
1401 case 'n':
1402 nthreads = atoi(optarg);
caf2b9ac
JA
1403 if (!nthreads) {
1404 printf("Threads must be non-zero\n");
1405 usage(argv[0], 1);
1406 }
54319661 1407 break;
0862f718
JA
1408 case 'N':
1409 do_nop = !!atoi(optarg);
1410 break;
2686fc22
JA
1411 case 'O':
1412 buffered = !atoi(optarg);
1413 break;
932131c9
JA
1414 case 't':
1415#ifndef ARCH_HAVE_CPU_CLOCK
1416 fprintf(stderr, "Stats not supported on this CPU\n");
1417 return 1;
1418#endif
1419 stats = !!atoi(optarg);
1420 break;
1421 case 'T':
1422#ifndef ARCH_HAVE_CPU_CLOCK
1423 fprintf(stderr, "Stats not supported on this CPU\n");
1424 return 1;
1425#endif
1426 tsc_rate = strtoul(optarg, NULL, 10);
203e4c26 1427 write_tsc_rate();
932131c9 1428 break;
65e1a5e8
EV
1429 case 'r':
1430 runtime = atoi(optarg);
1431 break;
a71ad043
JA
1432 case 'D':
1433 dma_map = !!atoi(optarg);
1434 break;
beda9d8d
JA
1435 case 'R':
1436 random_io = !!atoi(optarg);
1437 break;
ca8c91c5
JA
1438 case 'X':
1439 register_ring = !!atoi(optarg);
1440 break;
379406bc 1441 case 'S':
a7648136 1442#ifdef CONFIG_PWRITEV2
379406bc 1443 use_sync = !!atoi(optarg);
a7648136
JA
1444#else
1445 fprintf(stderr, "preadv2 not supported\n");
1446 exit(1);
1447#endif
379406bc 1448 break;
4b9e13dc
JA
1449 case 'P':
1450 numa_placement = !!atoi(optarg);
1451 break;
e39863e3
KB
1452 case 'h':
1453 case '?':
1454 default:
d79ff8c9 1455 usage(argv[0], 0);
e39863e3
KB
1456 break;
1457 }
1458 }
1459
203e4c26
JA
1460 if (stats)
1461 read_tsc_rate();
1462
c5347611
JA
1463 if (batch_complete > depth)
1464 batch_complete = depth;
1465 if (batch_submit > depth)
1466 batch_submit = depth;
a71ad043
JA
1467 if (!fixedbufs && dma_map)
1468 dma_map = 0;
c5347611 1469
54319661 1470 submitter = calloc(nthreads, sizeof(*submitter) +
55845033 1471 roundup_pow2(depth) * sizeof(struct iovec));
54319661
JA
1472 for (j = 0; j < nthreads; j++) {
1473 s = get_submitter(j);
4b9e13dc 1474 s->numa_node = -1;
54319661
JA
1475 s->index = j;
1476 s->done = s->calls = s->reaps = 0;
1477 }
e39863e3 1478
701d1277 1479 flags = O_RDONLY | O_NOATIME;
a7086591
JA
1480 if (!buffered)
1481 flags |= O_DIRECT;
1482
54319661 1483 j = 0;
e39863e3 1484 i = optind;
d79ff8c9 1485 nfiles = argc - i;
2ac00585
JA
1486 if (!do_nop) {
1487 if (!nfiles) {
1488 printf("No files specified\n");
1489 usage(argv[0], 1);
1490 }
1491 threads_per_f = nthreads / nfiles;
1492 /* make sure each thread gets assigned files */
1493 if (threads_per_f == 0) {
1494 threads_per_f = 1;
1495 } else {
1496 threads_rem = nthreads - threads_per_f * nfiles;
1497 }
d79ff8c9 1498 }
8025517d 1499 while (!do_nop && i < argc) {
d79ff8c9
AJ
1500 int k, limit;
1501
1502 memset(&f, 0, sizeof(f));
a7086591
JA
1503
1504 fd = open(argv[i], flags);
1505 if (fd < 0) {
1506 perror("open");
1507 return 1;
1508 }
d79ff8c9
AJ
1509 f.real_fd = fd;
1510 if (get_file_size(&f)) {
a7086591
JA
1511 printf("failed getting size of device/file\n");
1512 return 1;
1513 }
d79ff8c9 1514 if (f.max_blocks <= 1) {
a7086591
JA
1515 printf("Zero file/device size?\n");
1516 return 1;
1517 }
d79ff8c9
AJ
1518 f.max_blocks--;
1519
1520 limit = threads_per_f;
1521 limit += threads_rem > 0 ? 1 : 0;
1522 for (k = 0; k < limit; k++) {
1523 s = get_submitter((j + k) % nthreads);
a7086591 1524
d79ff8c9
AJ
1525 if (s->nr_files == MAX_FDS) {
1526 printf("Max number of files (%d) reached\n", MAX_FDS);
1527 break;
1528 }
1529
1530 memcpy(&s->files[s->nr_files], &f, sizeof(f));
1531
4b9e13dc
JA
1532 if (numa_placement)
1533 detect_node(s, argv[i]);
1534
1535 s->filename = argv[i];
d79ff8c9
AJ
1536 s->nr_files++;
1537 }
1538 threads_rem--;
a7086591 1539 i++;
d79ff8c9 1540 j += limit;
a7086591
JA
1541 }
1542
c9fb4c5b
JA
1543 arm_sig_int();
1544
c409e4c2
AG
1545 t_io_uring_page_size = sysconf(_SC_PAGESIZE);
1546 if (t_io_uring_page_size < 0)
1547 t_io_uring_page_size = 4096;
a0639afe 1548
54319661
JA
1549 for (j = 0; j < nthreads; j++) {
1550 s = get_submitter(j);
379406bc
JA
1551 if (use_sync)
1552 pthread_create(&s->thread, NULL, submitter_sync_fn, s);
1553 else if (!aio)
256714ea
JA
1554 pthread_create(&s->thread, NULL, submitter_uring_fn, s);
1555#ifdef CONFIG_LIBAIO
1556 else
1557 pthread_create(&s->thread, NULL, submitter_aio_fn, s);
1558#endif
54319661 1559 }
c9fb4c5b 1560
05138221 1561 reap = calls = done = 0;
c9fb4c5b
JA
1562 do {
1563 unsigned long this_done = 0;
1564 unsigned long this_reap = 0;
1565 unsigned long this_call = 0;
1566 unsigned long rpc = 0, ipc = 0;
f3057d26 1567 unsigned long iops, bw;
c9fb4c5b
JA
1568
1569 sleep(1);
65e1a5e8
EV
1570 if (runtime && !--runtime)
1571 do_finish("timeout");
a1f17100
JA
1572
1573 /* don't print partial run, if interrupted by signal */
1574 if (finish)
1575 break;
52479d8b
JA
1576
1577 /* one second in to the run, enable stats */
1578 if (stats)
1579 stats_running = 1;
1580
54319661 1581 for (j = 0; j < nthreads; j++) {
7d1ce4b7 1582 s = get_submitter(j);
54319661
JA
1583 this_done += s->done;
1584 this_call += s->calls;
1585 this_reap += s->reaps;
1586 }
c9fb4c5b
JA
1587 if (this_call - calls) {
1588 rpc = (this_done - done) / (this_call - calls);
1589 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
1590 } else
1591 rpc = ipc = -1;
22fd3501 1592 iops = this_done - done;
f3057d26
JA
1593 if (bs > 1048576)
1594 bw = iops * (bs / 1048576);
1595 else
1596 bw = iops / (1048576 / bs);
4b9e13dc
JA
1597 if (iops > 1000000) {
1598 double miops = (double) iops / 1000000.0;
1599 printf("IOPS=%.2fM, ", miops);
1600 } else if (iops > 100000) {
1601 double kiops = (double) iops / 1000.0;
1602 printf("IOPS=%.2fK, ", kiops);
1603 } else {
53b5fa1e 1604 printf("IOPS=%lu, ", iops);
4b9e13dc 1605 }
16d25711 1606 max_iops = max(max_iops, iops);
55037c48
JA
1607 if (!do_nop) {
1608 if (bw > 2000) {
1609 double bw_g = (double) bw / 1000.0;
1610
1611 printf("BW=%.2fGiB/s, ", bw_g);
1612 } else {
1613 printf("BW=%luMiB/s, ", bw);
1614 }
1615 }
4b9e13dc 1616 printf("IOS/call=%ld/%ld\n", rpc, ipc);
c9fb4c5b
JA
1617 done = this_done;
1618 calls = this_call;
1619 reap = this_reap;
1620 } while (!finish);
1621
54319661
JA
1622 for (j = 0; j < nthreads; j++) {
1623 s = get_submitter(j);
1624 pthread_join(s->thread, &ret);
1625 close(s->ring_fd);
932131c9
JA
1626
1627 if (stats) {
1628 unsigned long nr;
1629
1630 printf("%d: Latency percentiles:\n", s->tid);
1631 for (i = 0, nr = 0; i < PLAT_NR; i++)
1632 nr += s->plat[i];
1633 show_clat_percentiles(s->plat, nr, 4);
1634 free(s->clock_batch);
1635 free(s->plat);
1636 }
54319661 1637 }
932131c9 1638
54319661 1639 free(submitter);
c9fb4c5b
JA
1640 return 0;
1641}