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