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