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