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