Rename t/aio-ring to t/io_uring
[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
25 typedef uint64_t u64;
26 typedef uint32_t u32;
27 typedef int32_t s32;
28 typedef uint16_t u16;
29 typedef uint8_t u8;
30
31 #include "../os/io_uring.h"
32
33 #define barrier()       __asm__ __volatile__("": : :"memory")
34
35 #define min(a, b)               ((a < b) ? (a) : (b))
36
37 struct io_sq_ring {
38         u32 *head;
39         u32 *tail;
40         u32 *ring_mask;
41         u32 *ring_entries;
42         u32 *array;
43 };
44
45 struct io_cq_ring {
46         u32 *head;
47         u32 *tail;
48         u32 *ring_mask;
49         u32 *ring_entries;
50         struct io_uring_event *events;
51 };
52
53 #define DEPTH                   32
54
55 #define BATCH_SUBMIT            8
56 #define BATCH_COMPLETE          8
57
58 #define BS                      4096
59
60 static unsigned sq_ring_mask, cq_ring_mask;
61
62 struct submitter {
63         pthread_t thread;
64         unsigned long max_blocks;
65         int ring_fd;
66         struct drand48_data rand;
67         struct io_sq_ring sq_ring;
68         struct io_uring_iocb *iocbs;
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         char filename[128];
78 };
79
80 static struct submitter submitters[1];
81 static volatile int finish;
82
83 static int polled = 0;          /* use IO polling */
84 static int fixedbufs = 0;       /* use fixed user buffers */
85 static int buffered = 1;        /* use buffered IO, not O_DIRECT */
86 static int sq_thread = 0;       /* use kernel submission thread */
87 static int sq_thread_cpu = 0;   /* pin above thread to this CPU */
88
89 static int io_uring_setup(unsigned entries, struct iovec *iovecs,
90                           struct io_uring_params *p)
91 {
92         return syscall(__NR_sys_io_uring_setup, entries, iovecs, p);
93 }
94
95 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
96                           unsigned int min_complete, unsigned int flags)
97 {
98         return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
99                         min_complete, flags);
100 }
101
102 static int gettid(void)
103 {
104         return syscall(__NR_gettid);
105 }
106
107 static void init_io(struct submitter *s, int fd, unsigned index)
108 {
109         struct io_uring_iocb *iocb = &s->iocbs[index];
110         unsigned long offset;
111         long r;
112
113         lrand48_r(&s->rand, &r);
114         offset = (r % (s->max_blocks - 1)) * BS;
115
116         iocb->opcode = IORING_OP_READ;
117         iocb->flags = 0;
118         iocb->ioprio = 0;
119         iocb->fd = fd;
120         iocb->off = offset;
121         iocb->addr = s->iovecs[index].iov_base;
122         iocb->len = BS;
123 }
124
125 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
126 {
127         struct io_sq_ring *ring = &s->sq_ring;
128         u32 index, tail, next_tail, prepped = 0;
129
130         next_tail = tail = *ring->tail;
131         do {
132                 next_tail++;
133                 barrier();
134                 if (next_tail == *ring->head)
135                         break;
136
137                 index = tail & sq_ring_mask;
138                 init_io(s, fd, index);
139                 ring->array[index] = index;
140                 prepped++;
141                 tail = next_tail;
142         } while (prepped < max_ios);
143
144         if (*ring->tail != tail) {
145                 /* order tail store with writes to iocbs above */
146                 barrier();
147                 *ring->tail = tail;
148                 barrier();
149         }
150         return prepped;
151 }
152
153 static int get_file_size(int fd, unsigned long *blocks)
154 {
155         struct stat st;
156
157         if (fstat(fd, &st) < 0)
158                 return -1;
159         if (S_ISBLK(st.st_mode)) {
160                 unsigned long long bytes;
161
162                 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
163                         return -1;
164
165                 *blocks = bytes / BS;
166                 return 0;
167         } else if (S_ISREG(st.st_mode)) {
168                 *blocks = st.st_size / BS;
169                 return 0;
170         }
171
172         return -1;
173 }
174
175 static int reap_events(struct submitter *s)
176 {
177         struct io_cq_ring *ring = &s->cq_ring;
178         struct io_uring_event *ev;
179         u32 head, reaped = 0;
180
181         head = *ring->head;
182         do {
183                 barrier();
184                 if (head == *ring->tail)
185                         break;
186                 ev = &ring->events[head & cq_ring_mask];
187                 if (ev->res != BS) {
188                         struct io_uring_iocb *iocb = &s->iocbs[ev->index];
189
190                         printf("io: unexpected ret=%d\n", ev->res);
191                         printf("offset=%lu, size=%lu\n",
192                                         (unsigned long) iocb->off,
193                                         (unsigned long) iocb->len);
194                         return -1;
195                 }
196                 if (ev->flags & IOEV_FLAG_CACHEHIT)
197                         s->cachehit++;
198                 else
199                         s->cachemiss++;
200                 reaped++;
201                 head++;
202         } while (1);
203
204         s->inflight -= reaped;
205         *ring->head = head;
206         barrier();
207         return reaped;
208 }
209
210 static void *submitter_fn(void *data)
211 {
212         struct submitter *s = data;
213         int fd, ret, prepped, flags;
214
215         printf("submitter=%d\n", gettid());
216
217         flags = O_RDONLY;
218         if (!buffered)
219                 flags |= O_DIRECT;
220         fd = open(s->filename, flags);
221         if (fd < 0) {
222                 perror("open");
223                 goto done;
224         }
225
226         if (get_file_size(fd, &s->max_blocks)) {
227                 printf("failed getting size of device/file\n");
228                 goto err;
229         }
230         if (s->max_blocks <= 1) {
231                 printf("Zero file/device size?\n");
232                 goto err;
233         }
234         s->max_blocks--;
235
236         srand48_r(pthread_self(), &s->rand);
237
238         prepped = 0;
239         do {
240                 int to_wait, to_submit, this_reap, to_prep;
241
242                 if (!prepped && s->inflight < DEPTH) {
243                         to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
244                         prepped = prep_more_ios(s, fd, to_prep);
245                 }
246                 s->inflight += prepped;
247 submit_more:
248                 to_submit = prepped;
249 submit:
250                 if (s->inflight + BATCH_SUBMIT < DEPTH)
251                         to_wait = 0;
252                 else
253                         to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
254
255                 ret = io_uring_enter(s, to_submit, to_wait,
256                                         IORING_ENTER_GETEVENTS);
257                 s->calls++;
258
259                 this_reap = reap_events(s);
260                 if (this_reap == -1)
261                         break;
262                 s->reaps += this_reap;
263
264                 if (ret >= 0) {
265                         if (!ret) {
266                                 to_submit = 0;
267                                 if (s->inflight)
268                                         goto submit;
269                                 continue;
270                         } else if (ret < to_submit) {
271                                 int diff = to_submit - ret;
272
273                                 s->done += ret;
274                                 prepped -= diff;
275                                 goto submit_more;
276                         }
277                         s->done += ret;
278                         prepped = 0;
279                         continue;
280                 } else if (ret < 0) {
281                         if (errno == EAGAIN) {
282                                 if (s->finish)
283                                         break;
284                                 if (this_reap)
285                                         goto submit;
286                                 to_submit = 0;
287                                 goto submit;
288                         }
289                         printf("io_submit: %s\n", strerror(errno));
290                         break;
291                 }
292         } while (!s->finish);
293 err:
294         close(fd);
295 done:
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         void *ptr;
323         int fd;
324
325         memset(&p, 0, sizeof(p));
326
327         if (polled)
328                 p.flags |= IORING_SETUP_IOPOLL;
329         if (fixedbufs)
330                 p.flags |= IORING_SETUP_FIXEDBUFS;
331         if (buffered)
332                 p.flags |= IORING_SETUP_SQWQ;
333         else if (sq_thread) {
334                 p.flags |= IORING_SETUP_SQTHREAD;
335                 p.sq_thread_cpu = sq_thread_cpu;
336         }
337
338         if (fixedbufs)
339                 fd = io_uring_setup(DEPTH, s->iovecs, &p);
340         else
341                 fd = io_uring_setup(DEPTH, NULL, &p);
342         if (fd < 0) {
343                 perror("io_uring_setup");
344                 return 1;
345         }
346
347         s->ring_fd = fd;
348         ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(u32),
349                         PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
350                         IORING_OFF_SQ_RING);
351         printf("sq_ring ptr = 0x%p\n", ptr);
352         sring->head = ptr + p.sq_off.head;
353         sring->tail = ptr + p.sq_off.tail;
354         sring->ring_mask = ptr + p.sq_off.ring_mask;
355         sring->ring_entries = ptr + p.sq_off.ring_entries;
356         sring->array = ptr + p.sq_off.array;
357         sq_ring_mask = *sring->ring_mask;
358
359         s->iocbs = mmap(0, p.sq_entries * sizeof(struct io_uring_iocb),
360                         PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
361                         IORING_OFF_IOCB);
362         printf("iocbs ptr   = 0x%p\n", s->iocbs);
363
364         ptr = mmap(0, p.cq_off.events + p.cq_entries * sizeof(struct io_uring_event),
365                         PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
366                         IORING_OFF_CQ_RING);
367         printf("cq_ring ptr = 0x%p\n", ptr);
368         cring->head = ptr + p.cq_off.head;
369         cring->tail = ptr + p.cq_off.tail;
370         cring->ring_mask = ptr + p.cq_off.ring_mask;
371         cring->ring_entries = ptr + p.cq_off.ring_entries;
372         cring->events = ptr + p.cq_off.events;
373         cq_ring_mask = *cring->ring_mask;
374         return 0;
375 }
376
377 int main(int argc, char *argv[])
378 {
379         struct submitter *s = &submitters[0];
380         unsigned long done, calls, reap, cache_hit, cache_miss;
381         int err, i;
382         struct rlimit rlim;
383         void *ret;
384
385         if (argc < 2) {
386                 printf("%s: filename\n", argv[0]);
387                 return 1;
388         }
389
390         rlim.rlim_cur = RLIM_INFINITY;
391         rlim.rlim_max = RLIM_INFINITY;
392         if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
393                 perror("setrlimit");
394                 return 1;
395         }
396
397         arm_sig_int();
398
399         for (i = 0; i < DEPTH; i++) {
400                 void *buf;
401
402                 if (posix_memalign(&buf, BS, BS)) {
403                         printf("failed alloc\n");
404                         return 1;
405                 }
406                 s->iovecs[i].iov_base = buf;
407                 s->iovecs[i].iov_len = BS;
408         }
409
410         err = setup_ring(s);
411         if (err) {
412                 printf("ring setup failed: %s, %d\n", strerror(errno), err);
413                 return 1;
414         }
415         printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
416         printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
417         strcpy(s->filename, argv[1]);
418
419         pthread_create(&s->thread, NULL, submitter_fn, s);
420
421         cache_hit = cache_miss = reap = calls = done = 0;
422         do {
423                 unsigned long this_done = 0;
424                 unsigned long this_reap = 0;
425                 unsigned long this_call = 0;
426                 unsigned long this_cache_hit = 0;
427                 unsigned long this_cache_miss = 0;
428                 unsigned long rpc = 0, ipc = 0;
429                 double hit = 0.0;
430
431                 sleep(1);
432                 this_done += s->done;
433                 this_call += s->calls;
434                 this_reap += s->reaps;
435                 this_cache_hit += s->cachehit;
436                 this_cache_miss += s->cachemiss;
437                 if (this_cache_hit && this_cache_miss) {
438                         unsigned long hits, total;
439
440                         hits = this_cache_hit - cache_hit;
441                         total = hits + this_cache_miss - cache_miss;
442                         hit = (double) hits / (double) total;
443                         hit *= 100.0;
444                 }
445                 if (this_call - calls) {
446                         rpc = (this_done - done) / (this_call - calls);
447                         ipc = (this_reap - reap) / (this_call - calls);
448                 }
449                 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
450                                 this_done - done, rpc, ipc, s->inflight,
451                                 *s->cq_ring.head, *s->cq_ring.tail, hit);
452                 done = this_done;
453                 calls = this_call;
454                 reap = this_reap;
455                 cache_hit = s->cachehit;
456                 cache_miss = s->cachemiss;
457         } while (!finish);
458
459         pthread_join(s->thread, &ret);
460         close(s->ring_fd);
461         return 0;
462 }