Unify architecture io_uring syscall numbers
[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>
8
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <sys/ioctl.h>
12#include <sys/syscall.h>
13#include <sys/resource.h>
c3e2fc25 14#include <sys/mman.h>
e31b8288 15#include <sys/uio.h>
c9fb4c5b
JA
16#include <linux/fs.h>
17#include <fcntl.h>
18#include <unistd.h>
c9fb4c5b
JA
19#include <string.h>
20#include <pthread.h>
21#include <sched.h>
22
74efb029 23#include "../arch/arch.h"
57fa61f0 24#include "../lib/types.h"
f3e769a4 25#include "../os/linux/io_uring.h"
ac122fea 26
e31b8288 27#define min(a, b) ((a < b) ? (a) : (b))
c3e2fc25 28
e31b8288 29struct io_sq_ring {
e2239016
JA
30 unsigned *head;
31 unsigned *tail;
32 unsigned *ring_mask;
33 unsigned *ring_entries;
ce1705de 34 unsigned *flags;
e2239016 35 unsigned *array;
c9fb4c5b
JA
36};
37
e31b8288 38struct io_cq_ring {
e2239016
JA
39 unsigned *head;
40 unsigned *tail;
41 unsigned *ring_mask;
42 unsigned *ring_entries;
f0403f94 43 struct io_uring_cqe *cqes;
c9fb4c5b
JA
44};
45
701d1277 46#define DEPTH 128
2e7888ef
JA
47#define BATCH_SUBMIT 32
48#define BATCH_COMPLETE 32
c9fb4c5b
JA
49
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;
a7086591
JA
61};
62
c9fb4c5b
JA
63struct submitter {
64 pthread_t thread;
f310970e 65 int ring_fd;
c9fb4c5b 66 struct drand48_data rand;
e31b8288 67 struct io_sq_ring sq_ring;
f0403f94 68 struct io_uring_sqe *sqes;
e31b8288 69 struct io_cq_ring cq_ring;
c9fb4c5b
JA
70 int inflight;
71 unsigned long reaps;
72 unsigned long done;
73 unsigned long calls;
74 volatile int finish;
701d1277 75
48e698fa
JA
76 __s32 *fds;
77
a7086591
JA
78 struct file files[MAX_FDS];
79 unsigned nr_files;
80 unsigned cur_file;
e39863e3 81 struct iovec iovecs[];
c9fb4c5b
JA
82};
83
e39863e3 84static struct submitter *submitter;
c9fb4c5b
JA
85static volatile int finish;
86
e39863e3
KB
87static int depth = DEPTH;
88static int batch_submit = BATCH_SUBMIT;
89static int batch_complete = BATCH_COMPLETE;
f0403f94 90static int polled = 1; /* use IO polling */
701d1277 91static int fixedbufs = 1; /* use fixed user buffers */
8c5fa755 92static int register_files = 1; /* use fixed files */
f0403f94 93static int buffered = 0; /* use buffered IO, not O_DIRECT */
3d7d00a3
JA
94static int sq_thread_poll = 0; /* use kernel submission/poller thread */
95static int sq_thread_cpu = -1; /* pin above thread to this CPU */
8025517d 96static int do_nop = 0; /* no-op SQ ring commands */
c9fb4c5b 97
2ea53ca3 98static int io_uring_register_buffers(struct submitter *s)
c9fb4c5b 99{
8025517d
JA
100 if (do_nop)
101 return 0;
102
bfed648c 103 return syscall(__NR_io_uring_register, s->ring_fd,
e39863e3 104 IORING_REGISTER_BUFFERS, s->iovecs, depth);
2ea53ca3
JA
105}
106
a7abc9fb
JA
107static int io_uring_register_files(struct submitter *s)
108{
48e698fa 109 int i;
a7abc9fb 110
8025517d
JA
111 if (do_nop)
112 return 0;
113
48e698fa
JA
114 s->fds = calloc(s->nr_files, sizeof(__s32));
115 for (i = 0; i < s->nr_files; i++) {
116 s->fds[i] = s->files[i].real_fd;
117 s->files[i].fixed_fd = i;
118 }
a7abc9fb 119
bfed648c 120 return syscall(__NR_io_uring_register, s->ring_fd,
919850d2 121 IORING_REGISTER_FILES, s->fds, s->nr_files);
a7abc9fb
JA
122}
123
2ea53ca3
JA
124static int io_uring_setup(unsigned entries, struct io_uring_params *p)
125{
bfed648c 126 return syscall(__NR_io_uring_setup, entries, p);
c9fb4c5b
JA
127}
128
c3e2fc25
JA
129static int io_uring_enter(struct submitter *s, unsigned int to_submit,
130 unsigned int min_complete, unsigned int flags)
c9fb4c5b 131{
bfed648c
JA
132 return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete,
133 flags, NULL, 0);
c9fb4c5b
JA
134}
135
77d814a1 136#ifndef CONFIG_HAVE_GETTID
c9fb4c5b
JA
137static int gettid(void)
138{
139 return syscall(__NR_gettid);
140}
77d814a1 141#endif
c9fb4c5b 142
701d1277
JA
143static unsigned file_depth(struct submitter *s)
144{
e39863e3 145 return (depth + s->nr_files - 1) / s->nr_files;
701d1277
JA
146}
147
a7086591 148static void init_io(struct submitter *s, unsigned index)
c9fb4c5b 149{
f0403f94 150 struct io_uring_sqe *sqe = &s->sqes[index];
c9fb4c5b 151 unsigned long offset;
a7086591 152 struct file *f;
c9fb4c5b
JA
153 long r;
154
8025517d
JA
155 if (do_nop) {
156 sqe->opcode = IORING_OP_NOP;
157 return;
158 }
159
701d1277
JA
160 if (s->nr_files == 1) {
161 f = &s->files[0];
162 } else {
163 f = &s->files[s->cur_file];
164 if (f->pending_ios >= file_depth(s)) {
165 s->cur_file++;
166 if (s->cur_file == s->nr_files)
167 s->cur_file = 0;
93d1811c 168 f = &s->files[s->cur_file];
701d1277
JA
169 }
170 }
171 f->pending_ios++;
a7086591 172
c9fb4c5b 173 lrand48_r(&s->rand, &r);
a7086591 174 offset = (r % (f->max_blocks - 1)) * BS;
c9fb4c5b 175
8c5fa755
JA
176 if (register_files) {
177 sqe->flags = IOSQE_FIXED_FILE;
178 sqe->fd = f->fixed_fd;
179 } else {
180 sqe->flags = 0;
181 sqe->fd = f->real_fd;
182 }
f0403f94 183 if (fixedbufs) {
48e698fa 184 sqe->opcode = IORING_OP_READ_FIXED;
919850d2 185 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
f0403f94 186 sqe->len = BS;
2ea53ca3 187 sqe->buf_index = index;
f0403f94 188 } else {
48e698fa 189 sqe->opcode = IORING_OP_READV;
919850d2 190 sqe->addr = (unsigned long) &s->iovecs[index];
f0403f94 191 sqe->len = 1;
2ea53ca3 192 sqe->buf_index = 0;
f0403f94 193 }
f0403f94 194 sqe->ioprio = 0;
f0403f94 195 sqe->off = offset;
48e698fa 196 sqe->user_data = (unsigned long) f;
c9fb4c5b
JA
197}
198
a7086591 199static int prep_more_ios(struct submitter *s, int max_ios)
c9fb4c5b 200{
e31b8288 201 struct io_sq_ring *ring = &s->sq_ring;
e2239016 202 unsigned index, tail, next_tail, prepped = 0;
c9fb4c5b 203
c3e2fc25 204 next_tail = tail = *ring->tail;
c9fb4c5b
JA
205 do {
206 next_tail++;
679d8352 207 read_barrier();
c3e2fc25 208 if (next_tail == *ring->head)
c9fb4c5b
JA
209 break;
210
e39c34dc 211 index = tail & sq_ring_mask;
a7086591 212 init_io(s, index);
c3e2fc25 213 ring->array[index] = index;
c9fb4c5b
JA
214 prepped++;
215 tail = next_tail;
216 } while (prepped < max_ios);
217
c3e2fc25 218 if (*ring->tail != tail) {
f0403f94 219 /* order tail store with writes to sqes above */
679d8352 220 write_barrier();
c3e2fc25 221 *ring->tail = tail;
679d8352 222 write_barrier();
c9fb4c5b
JA
223 }
224 return prepped;
225}
226
a7086591 227static int get_file_size(struct file *f)
c9fb4c5b
JA
228{
229 struct stat st;
230
48e698fa 231 if (fstat(f->real_fd, &st) < 0)
c9fb4c5b
JA
232 return -1;
233 if (S_ISBLK(st.st_mode)) {
234 unsigned long long bytes;
235
48e698fa 236 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
c9fb4c5b
JA
237 return -1;
238
a7086591 239 f->max_blocks = bytes / BS;
c9fb4c5b
JA
240 return 0;
241 } else if (S_ISREG(st.st_mode)) {
a7086591 242 f->max_blocks = st.st_size / BS;
c9fb4c5b
JA
243 return 0;
244 }
245
246 return -1;
247}
248
249static int reap_events(struct submitter *s)
250{
e31b8288 251 struct io_cq_ring *ring = &s->cq_ring;
f0403f94 252 struct io_uring_cqe *cqe;
e2239016 253 unsigned head, reaped = 0;
c9fb4c5b 254
c3e2fc25 255 head = *ring->head;
c9fb4c5b 256 do {
701d1277
JA
257 struct file *f;
258
679d8352 259 read_barrier();
c3e2fc25 260 if (head == *ring->tail)
c9fb4c5b 261 break;
f0403f94 262 cqe = &ring->cqes[head & cq_ring_mask];
8025517d 263 if (!do_nop) {
e3466352 264 f = (struct file *) (uintptr_t) cqe->user_data;
8025517d
JA
265 f->pending_ios--;
266 if (cqe->res != BS) {
267 printf("io: unexpected ret=%d\n", cqe->res);
154a9582 268 if (polled && cqe->res == -EOPNOTSUPP)
8066f6b6 269 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
8025517d
JA
270 return -1;
271 }
c9fb4c5b
JA
272 }
273 reaped++;
274 head++;
c9fb4c5b
JA
275 } while (1);
276
277 s->inflight -= reaped;
c3e2fc25 278 *ring->head = head;
679d8352 279 write_barrier();
c9fb4c5b
JA
280 return reaped;
281}
282
283static void *submitter_fn(void *data)
284{
285 struct submitter *s = data;
ce1705de 286 struct io_sq_ring *ring = &s->sq_ring;
a7086591 287 int ret, prepped;
c9fb4c5b
JA
288
289 printf("submitter=%d\n", gettid());
290
c9fb4c5b
JA
291 srand48_r(pthread_self(), &s->rand);
292
293 prepped = 0;
294 do {
f310970e 295 int to_wait, to_submit, this_reap, to_prep;
c9fb4c5b 296
e39863e3
KB
297 if (!prepped && s->inflight < depth) {
298 to_prep = min(depth - s->inflight, batch_submit);
a7086591 299 prepped = prep_more_ios(s, to_prep);
f310970e 300 }
c9fb4c5b
JA
301 s->inflight += prepped;
302submit_more:
303 to_submit = prepped;
304submit:
e39863e3 305 if (to_submit && (s->inflight + to_submit <= depth))
c9fb4c5b
JA
306 to_wait = 0;
307 else
e39863e3 308 to_wait = min(s->inflight + to_submit, batch_complete);
c9fb4c5b 309
ce1705de
JA
310 /*
311 * Only need to call io_uring_enter if we're not using SQ thread
312 * poll, or if IORING_SQ_NEED_WAKEUP is set.
313 */
314 if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
e0abe388
JA
315 unsigned flags = 0;
316
317 if (to_wait)
318 flags = IORING_ENTER_GETEVENTS;
e2f309e6 319 if ((*ring->flags & IORING_SQ_NEED_WAKEUP))
b532dd6d 320 flags |= IORING_ENTER_SQ_WAKEUP;
e0abe388 321 ret = io_uring_enter(s, to_submit, to_wait, flags);
ce1705de
JA
322 s->calls++;
323 }
c9fb4c5b 324
ce1705de
JA
325 /*
326 * For non SQ thread poll, we already got the events we needed
327 * through the io_uring_enter() above. For SQ thread poll, we
328 * need to loop here until we find enough events.
329 */
330 this_reap = 0;
331 do {
332 int r;
333 r = reap_events(s);
7d1435e6
JA
334 if (r == -1) {
335 s->finish = 1;
ce1705de 336 break;
7d1435e6 337 } else if (r > 0)
ce1705de
JA
338 this_reap += r;
339 } while (sq_thread_poll && this_reap < to_wait);
c9fb4c5b
JA
340 s->reaps += this_reap;
341
342 if (ret >= 0) {
343 if (!ret) {
344 to_submit = 0;
345 if (s->inflight)
346 goto submit;
347 continue;
348 } else if (ret < to_submit) {
349 int diff = to_submit - ret;
350
351 s->done += ret;
352 prepped -= diff;
353 goto submit_more;
354 }
355 s->done += ret;
356 prepped = 0;
357 continue;
358 } else if (ret < 0) {
ac122fea 359 if (errno == EAGAIN) {
c9fb4c5b
JA
360 if (s->finish)
361 break;
362 if (this_reap)
363 goto submit;
c9fb4c5b
JA
364 to_submit = 0;
365 goto submit;
366 }
ac122fea 367 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
368 break;
369 }
370 } while (!s->finish);
a7086591 371
c9fb4c5b
JA
372 finish = 1;
373 return NULL;
374}
375
376static void sig_int(int sig)
377{
378 printf("Exiting on signal %d\n", sig);
e39863e3 379 submitter->finish = 1;
c9fb4c5b
JA
380 finish = 1;
381}
382
383static void arm_sig_int(void)
384{
385 struct sigaction act;
386
387 memset(&act, 0, sizeof(act));
388 act.sa_handler = sig_int;
389 act.sa_flags = SA_RESTART;
390 sigaction(SIGINT, &act, NULL);
391}
392
c3e2fc25
JA
393static int setup_ring(struct submitter *s)
394{
e31b8288
JA
395 struct io_sq_ring *sring = &s->sq_ring;
396 struct io_cq_ring *cring = &s->cq_ring;
397 struct io_uring_params p;
2ea53ca3 398 int ret, fd;
c3e2fc25 399 void *ptr;
c3e2fc25
JA
400
401 memset(&p, 0, sizeof(p));
402
7d1435e6 403 if (polled && !do_nop)
0e47f11b 404 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3 405 if (sq_thread_poll) {
2ea53ca3 406 p.flags |= IORING_SETUP_SQPOLL;
3ade18a3 407 if (sq_thread_cpu != -1) {
3d7d00a3 408 p.flags |= IORING_SETUP_SQ_AFF;
3ade18a3
JA
409 p.sq_thread_cpu = sq_thread_cpu;
410 }
c3e2fc25
JA
411 }
412
e39863e3 413 fd = io_uring_setup(depth, &p);
c3e2fc25
JA
414 if (fd < 0) {
415 perror("io_uring_setup");
416 return 1;
417 }
f310970e 418 s->ring_fd = fd;
2ea53ca3
JA
419
420 if (fixedbufs) {
421 ret = io_uring_register_buffers(s);
422 if (ret < 0) {
a7abc9fb 423 perror("io_uring_register_buffers");
2ea53ca3
JA
424 return 1;
425 }
426 }
427
8c5fa755
JA
428 if (register_files) {
429 ret = io_uring_register_files(s);
430 if (ret < 0) {
431 perror("io_uring_register_files");
432 return 1;
433 }
a7abc9fb
JA
434 }
435
e2239016 436 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
f310970e
JA
437 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
438 IORING_OFF_SQ_RING);
c3e2fc25
JA
439 printf("sq_ring ptr = 0x%p\n", ptr);
440 sring->head = ptr + p.sq_off.head;
441 sring->tail = ptr + p.sq_off.tail;
442 sring->ring_mask = ptr + p.sq_off.ring_mask;
443 sring->ring_entries = ptr + p.sq_off.ring_entries;
ce1705de 444 sring->flags = ptr + p.sq_off.flags;
ac122fea 445 sring->array = ptr + p.sq_off.array;
c3e2fc25
JA
446 sq_ring_mask = *sring->ring_mask;
447
f0403f94 448 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
f310970e 449 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
f0403f94
JA
450 IORING_OFF_SQES);
451 printf("sqes ptr = 0x%p\n", s->sqes);
c3e2fc25 452
f0403f94 453 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
f310970e
JA
454 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
455 IORING_OFF_CQ_RING);
c3e2fc25
JA
456 printf("cq_ring ptr = 0x%p\n", ptr);
457 cring->head = ptr + p.cq_off.head;
458 cring->tail = ptr + p.cq_off.tail;
459 cring->ring_mask = ptr + p.cq_off.ring_mask;
460 cring->ring_entries = ptr + p.cq_off.ring_entries;
f0403f94 461 cring->cqes = ptr + p.cq_off.cqes;
c3e2fc25
JA
462 cq_ring_mask = *cring->ring_mask;
463 return 0;
464}
465
2e7888ef
JA
466static void file_depths(char *buf)
467{
e39863e3 468 struct submitter *s = submitter;
2e7888ef
JA
469 char *p;
470 int i;
471
ac21bfab 472 buf[0] = '\0';
2e7888ef
JA
473 p = buf;
474 for (i = 0; i < s->nr_files; i++) {
475 struct file *f = &s->files[i];
476
477 if (i + 1 == s->nr_files)
478 p += sprintf(p, "%d", f->pending_ios);
479 else
480 p += sprintf(p, "%d, ", f->pending_ios);
481 }
482}
483
e39863e3
KB
484static void usage(char *argv)
485{
486 printf("%s [options] -- [filenames]\n"
487 " -d <int> : IO Depth, default %d\n"
488 " -s <int> : Batch submit, default %d\n"
489 " -c <int> : Batch complete, default %d\n",
490 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE);
491 exit(0);
492}
493
c9fb4c5b
JA
494int main(int argc, char *argv[])
495{
e39863e3 496 struct submitter *s;
05138221 497 unsigned long done, calls, reap;
e39863e3 498 int err, i, flags, fd, opt;
2e7888ef 499 char *fdepths;
c3e2fc25 500 void *ret;
c9fb4c5b 501
8025517d 502 if (!do_nop && argc < 2) {
e39863e3 503 printf("%s: filename [options]\n", argv[0]);
c9fb4c5b
JA
504 return 1;
505 }
506
e39863e3
KB
507 while ((opt = getopt(argc, argv, "d:s:c:h?")) != -1) {
508 switch (opt) {
509 case 'd':
510 depth = atoi(optarg);
511 break;
512 case 's':
513 batch_submit = atoi(optarg);
514 break;
515 case 'c':
516 batch_complete = atoi(optarg);
517 break;
518 case 'h':
519 case '?':
520 default:
521 usage(argv[0]);
522 break;
523 }
524 }
525
d9c50de7
KB
526 submitter = malloc(sizeof(*submitter) + depth * sizeof(struct iovec));
527 memset(submitter, 0, sizeof(*submitter) + depth * sizeof(struct iovec));
e39863e3
KB
528 s = submitter;
529
701d1277 530 flags = O_RDONLY | O_NOATIME;
a7086591
JA
531 if (!buffered)
532 flags |= O_DIRECT;
533
e39863e3 534 i = optind;
8025517d 535 while (!do_nop && i < argc) {
be26a982 536 struct file *f;
a7086591 537
be26a982
JA
538 if (s->nr_files == MAX_FDS) {
539 printf("Max number of files (%d) reached\n", MAX_FDS);
540 break;
541 }
a7086591
JA
542 fd = open(argv[i], flags);
543 if (fd < 0) {
544 perror("open");
545 return 1;
546 }
be26a982
JA
547
548 f = &s->files[s->nr_files];
48e698fa 549 f->real_fd = fd;
a7086591
JA
550 if (get_file_size(f)) {
551 printf("failed getting size of device/file\n");
552 return 1;
553 }
554 if (f->max_blocks <= 1) {
555 printf("Zero file/device size?\n");
556 return 1;
557 }
558 f->max_blocks--;
559
560 printf("Added file %s\n", argv[i]);
561 s->nr_files++;
562 i++;
563 }
564
b3ce355d
JA
565 if (fixedbufs) {
566 struct rlimit rlim;
567
568 rlim.rlim_cur = RLIM_INFINITY;
569 rlim.rlim_max = RLIM_INFINITY;
570 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
571 perror("setrlimit");
572 return 1;
573 }
c9fb4c5b
JA
574 }
575
576 arm_sig_int();
577
e39863e3 578 for (i = 0; i < depth; i++) {
c3e2fc25 579 void *buf;
c9fb4c5b 580
c3e2fc25 581 if (posix_memalign(&buf, BS, BS)) {
c9fb4c5b
JA
582 printf("failed alloc\n");
583 return 1;
584 }
c3e2fc25
JA
585 s->iovecs[i].iov_base = buf;
586 s->iovecs[i].iov_len = BS;
c9fb4c5b
JA
587 }
588
c3e2fc25 589 err = setup_ring(s);
c9fb4c5b 590 if (err) {
c3e2fc25 591 printf("ring setup failed: %s, %d\n", strerror(errno), err);
c9fb4c5b
JA
592 return 1;
593 }
c3e2fc25 594 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
e39863e3 595 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
c9fb4c5b
JA
596
597 pthread_create(&s->thread, NULL, submitter_fn, s);
598
2e7888ef 599 fdepths = malloc(8 * s->nr_files);
05138221 600 reap = calls = done = 0;
c9fb4c5b
JA
601 do {
602 unsigned long this_done = 0;
603 unsigned long this_reap = 0;
604 unsigned long this_call = 0;
605 unsigned long rpc = 0, ipc = 0;
606
607 sleep(1);
608 this_done += s->done;
609 this_call += s->calls;
610 this_reap += s->reaps;
611 if (this_call - calls) {
612 rpc = (this_done - done) / (this_call - calls);
613 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
614 } else
615 rpc = ipc = -1;
2e7888ef 616 file_depths(fdepths);
05138221 617 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (%s)\n",
c9fb4c5b 618 this_done - done, rpc, ipc, s->inflight,
05138221 619 fdepths);
c9fb4c5b
JA
620 done = this_done;
621 calls = this_call;
622 reap = this_reap;
623 } while (!finish);
624
625 pthread_join(s->thread, &ret);
e31b8288 626 close(s->ring_fd);
2e7888ef 627 free(fdepths);
c9fb4c5b
JA
628 return 0;
629}