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