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