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