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