aioring: make sq/cqring_offsets a bit more future proof
[fio.git] / t / aio-ring.c
1 /*
2  * gcc -D_GNU_SOURCE -Wall -O2 -o aio-ring aio-ring.c  -lpthread -laio
3  */
4 #include <stdio.h>
5 #include <errno.h>
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <stddef.h>
9 #include <signal.h>
10 #include <inttypes.h>
11
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <sys/ioctl.h>
15 #include <sys/syscall.h>
16 #include <sys/resource.h>
17 #include <sys/mman.h>
18 #include <linux/fs.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <libaio.h>
22 #include <string.h>
23 #include <pthread.h>
24 #include <sched.h>
25
26 #include "../arch/arch.h"
27
28 #define IOCTX_FLAG_SCQRING      (1 << 0)        /* Use SQ/CQ rings */
29 #define IOCTX_FLAG_IOPOLL       (1 << 1)
30 #define IOCTX_FLAG_FIXEDBUFS    (1 << 2)
31 #define IOCTX_FLAG_SQTHREAD     (1 << 3)        /* Use SQ thread */
32 #define IOCTX_FLAG_SQWQ         (1 << 4)        /* Use SQ wq */
33 #define IOCTX_FLAG_SQPOLL       (1 << 5)
34
35 #define IOEV_RES2_CACHEHIT      (1 << 0)
36
37 #define barrier()       __asm__ __volatile__("": : :"memory")
38
39 #define min(a, b)               ((a < b) ? (a) : (b))
40
41 typedef uint64_t u64;
42 typedef uint32_t u32;
43 typedef uint16_t u16;
44
45 #define IORING_OFF_SQ_RING      0ULL
46 #define IORING_OFF_CQ_RING      0x8000000ULL
47 #define IORING_OFF_IOCB         0x10000000ULL
48
49 struct aio_sqring_offsets {
50         u32 head;
51         u32 tail;
52         u32 ring_mask;
53         u32 ring_entries;
54         u32 flags;
55         u32 dropped;
56         u32 array;
57         u32 resv[3];
58 };
59
60 struct aio_cqring_offsets {
61         u32 head;
62         u32 tail;
63         u32 ring_mask;
64         u32 ring_entries;
65         u32 overflow;
66         u32 events;
67         u32 resv[4];
68 };
69
70 struct aio_uring_params {
71         u32 sq_entries;
72         u32 cq_entries;
73         u32 flags;
74         u16 sq_thread_cpu;
75         u16 resv[9];
76         struct aio_sqring_offsets sq_off;
77         struct aio_cqring_offsets cq_off;
78 };
79
80 struct aio_sq_ring {
81         u32 *head;
82         u32 *tail;
83         u32 *ring_mask;
84         u32 *ring_entries;
85         u32 *array;
86 };
87
88 struct aio_cq_ring {
89         u32 *head;
90         u32 *tail;
91         u32 *ring_mask;
92         u32 *ring_entries;
93         struct io_event *events;
94 };
95
96 #define IORING_ENTER_GETEVENTS  (1 << 0)
97
98 #define DEPTH                   32
99
100 #define BATCH_SUBMIT            8
101 #define BATCH_COMPLETE          8
102
103 #define BS                      4096
104
105 static unsigned sq_ring_mask, cq_ring_mask;
106
107 struct submitter {
108         pthread_t thread;
109         unsigned long max_blocks;
110         int fd;
111         struct drand48_data rand;
112         struct aio_sq_ring sq_ring;
113         struct iocb *iocbs;
114         struct iovec iovecs[DEPTH];
115         struct aio_cq_ring cq_ring;
116         int inflight;
117         unsigned long reaps;
118         unsigned long done;
119         unsigned long calls;
120         unsigned long cachehit, cachemiss;
121         volatile int finish;
122         char filename[128];
123 };
124
125 static struct submitter submitters[1];
126 static volatile int finish;
127
128 static int polled = 0;          /* use IO polling */
129 static int fixedbufs = 0;       /* use fixed user buffers */
130 static int buffered = 1;        /* use buffered IO, not O_DIRECT */
131 static int sq_thread = 0;       /* use kernel submission thread */
132 static int sq_thread_cpu = 0;   /* pin above thread to this CPU */
133
134 static int io_uring_setup(unsigned entries, struct iovec *iovecs,
135                           struct aio_uring_params *p)
136 {
137         return syscall(__NR_sys_io_uring_setup, entries, iovecs, p);
138 }
139
140 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
141                           unsigned int min_complete, unsigned int flags)
142 {
143         return syscall(__NR_sys_io_uring_enter, s->fd, to_submit, min_complete,
144                         flags);
145 }
146
147 static int gettid(void)
148 {
149         return syscall(__NR_gettid);
150 }
151
152 static void init_io(struct submitter *s, int fd, unsigned index)
153 {
154         struct iocb *iocb = &s->iocbs[index];
155         unsigned long offset;
156         long r;
157
158         lrand48_r(&s->rand, &r);
159         offset = (r % (s->max_blocks - 1)) * BS;
160
161         iocb->aio_fildes = fd;
162         iocb->aio_lio_opcode = IO_CMD_PREAD;
163         iocb->u.c.buf = s->iovecs[index].iov_base;
164         iocb->u.c.nbytes = BS;
165         iocb->u.c.offset = offset;
166 }
167
168 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
169 {
170         struct aio_sq_ring *ring = &s->sq_ring;
171         u32 index, tail, next_tail, prepped = 0;
172
173         next_tail = tail = *ring->tail;
174         do {
175                 next_tail++;
176                 barrier();
177                 if (next_tail == *ring->head)
178                         break;
179
180                 index = tail & sq_ring_mask;
181                 init_io(s, fd, index);
182                 ring->array[index] = index;
183                 prepped++;
184                 tail = next_tail;
185         } while (prepped < max_ios);
186
187         if (*ring->tail != tail) {
188                 /* order tail store with writes to iocbs above */
189                 barrier();
190                 *ring->tail = tail;
191                 barrier();
192         }
193         return prepped;
194 }
195
196 static int get_file_size(int fd, unsigned long *blocks)
197 {
198         struct stat st;
199
200         if (fstat(fd, &st) < 0)
201                 return -1;
202         if (S_ISBLK(st.st_mode)) {
203                 unsigned long long bytes;
204
205                 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
206                         return -1;
207
208                 *blocks = bytes / BS;
209                 return 0;
210         } else if (S_ISREG(st.st_mode)) {
211                 *blocks = st.st_size / BS;
212                 return 0;
213         }
214
215         return -1;
216 }
217
218 static int reap_events(struct submitter *s)
219 {
220         struct aio_cq_ring *ring = &s->cq_ring;
221         struct io_event *ev;
222         u32 head, reaped = 0;
223
224         head = *ring->head;
225         do {
226                 barrier();
227                 if (head == *ring->tail)
228                         break;
229                 ev = &ring->events[head & cq_ring_mask];
230                 if (ev->res != BS) {
231                         struct iocb *iocb = ev->obj;
232
233                         printf("io: unexpected ret=%ld\n", ev->res);
234                         printf("offset=%lu, size=%lu\n", (unsigned long) iocb->u.c.offset, (unsigned long) iocb->u.c.nbytes);
235                         return -1;
236                 }
237                 if (ev->res2 & IOEV_RES2_CACHEHIT)
238                         s->cachehit++;
239                 else
240                         s->cachemiss++;
241                 reaped++;
242                 head++;
243         } while (1);
244
245         s->inflight -= reaped;
246         *ring->head = head;
247         barrier();
248         return reaped;
249 }
250
251 static void *submitter_fn(void *data)
252 {
253         struct submitter *s = data;
254         int fd, ret, prepped, flags;
255
256         printf("submitter=%d\n", gettid());
257
258         flags = O_RDONLY;
259         if (!buffered)
260                 flags |= O_DIRECT;
261         fd = open(s->filename, flags);
262         if (fd < 0) {
263                 perror("open");
264                 goto done;
265         }
266
267         if (get_file_size(fd, &s->max_blocks)) {
268                 printf("failed getting size of device/file\n");
269                 goto err;
270         }
271         if (!s->max_blocks) {
272                 printf("Zero file/device size?\n");
273                 goto err;
274         }
275
276         s->max_blocks--;
277
278         srand48_r(pthread_self(), &s->rand);
279
280         prepped = 0;
281         do {
282                 int to_wait, to_submit, this_reap;
283
284                 if (!prepped && s->inflight < DEPTH)
285                         prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
286                 s->inflight += prepped;
287 submit_more:
288                 to_submit = prepped;
289 submit:
290                 if (s->inflight + BATCH_SUBMIT < DEPTH)
291                         to_wait = 0;
292                 else
293                         to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
294
295                 ret = io_uring_enter(s, to_submit, to_wait, IORING_ENTER_GETEVENTS);
296                 s->calls++;
297
298                 this_reap = reap_events(s);
299                 if (this_reap == -1)
300                         break;
301                 s->reaps += this_reap;
302
303                 if (ret >= 0) {
304                         if (!ret) {
305                                 to_submit = 0;
306                                 if (s->inflight)
307                                         goto submit;
308                                 continue;
309                         } else if (ret < to_submit) {
310                                 int diff = to_submit - ret;
311
312                                 s->done += ret;
313                                 prepped -= diff;
314                                 goto submit_more;
315                         }
316                         s->done += ret;
317                         prepped = 0;
318                         continue;
319                 } else if (ret < 0) {
320                         if (errno == EAGAIN) {
321                                 if (s->finish)
322                                         break;
323                                 if (this_reap)
324                                         goto submit;
325                                 to_submit = 0;
326                                 goto submit;
327                         }
328                         printf("io_submit: %s\n", strerror(errno));
329                         break;
330                 }
331         } while (!s->finish);
332 err:
333         close(fd);
334 done:
335         finish = 1;
336         return NULL;
337 }
338
339 static void sig_int(int sig)
340 {
341         printf("Exiting on signal %d\n", sig);
342         submitters[0].finish = 1;
343         finish = 1;
344 }
345
346 static void arm_sig_int(void)
347 {
348         struct sigaction act;
349
350         memset(&act, 0, sizeof(act));
351         act.sa_handler = sig_int;
352         act.sa_flags = SA_RESTART;
353         sigaction(SIGINT, &act, NULL);
354 }
355
356 static int setup_ring(struct submitter *s)
357 {
358         struct aio_sq_ring *sring = &s->sq_ring;
359         struct aio_cq_ring *cring = &s->cq_ring;
360         struct aio_uring_params p;
361         void *ptr;
362         int fd;
363
364         memset(&p, 0, sizeof(p));
365
366         p.flags = IOCTX_FLAG_SCQRING;
367         if (polled)
368                 p.flags |= IOCTX_FLAG_IOPOLL;
369         if (fixedbufs)
370                 p.flags |= IOCTX_FLAG_FIXEDBUFS;
371         if (buffered)
372                 p.flags |= IOCTX_FLAG_SQWQ;
373         else if (sq_thread) {
374                 p.flags |= IOCTX_FLAG_SQTHREAD;
375                 p.sq_thread_cpu = sq_thread_cpu;
376         }
377
378         if (fixedbufs)
379                 fd = io_uring_setup(DEPTH, s->iovecs, &p);
380         else
381                 fd = io_uring_setup(DEPTH, NULL, &p);
382         if (fd < 0) {
383                 perror("io_uring_setup");
384                 return 1;
385         }
386
387         s->fd = fd;
388
389         ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(u32),
390                         PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
391                         fd, IORING_OFF_SQ_RING);
392         printf("sq_ring ptr = 0x%p\n", ptr);
393         sring->head = ptr + p.sq_off.head;
394         sring->tail = ptr + p.sq_off.tail;
395         sring->ring_mask = ptr + p.sq_off.ring_mask;
396         sring->ring_entries = ptr + p.sq_off.ring_entries;
397         sring->array = ptr + p.sq_off.array;
398         sq_ring_mask = *sring->ring_mask;
399
400         s->iocbs = mmap(0, p.sq_entries * sizeof(struct iocb), PROT_READ | PROT_WRITE,
401                         MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_IOCB);
402         printf("iocbs ptr   = 0x%p\n", s->iocbs);
403
404         ptr = mmap(0, p.cq_off.events + p.cq_entries * sizeof(struct io_event),
405                         PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
406                         fd, IORING_OFF_CQ_RING);
407         printf("cq_ring ptr = 0x%p\n", ptr);
408         cring->head = ptr + p.cq_off.head;
409         cring->tail = ptr + p.cq_off.tail;
410         cring->ring_mask = ptr + p.cq_off.ring_mask;
411         cring->ring_entries = ptr + p.cq_off.ring_entries;
412         cring->events = ptr + p.cq_off.events;
413         cq_ring_mask = *cring->ring_mask;
414         return 0;
415 }
416
417 int main(int argc, char *argv[])
418 {
419         struct submitter *s = &submitters[0];
420         unsigned long done, calls, reap, cache_hit, cache_miss;
421         int err, i;
422         struct rlimit rlim;
423         void *ret;
424
425         if (argc < 2) {
426                 printf("%s: filename\n", argv[0]);
427                 return 1;
428         }
429
430         rlim.rlim_cur = RLIM_INFINITY;
431         rlim.rlim_max = RLIM_INFINITY;
432         if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
433                 perror("setrlimit");
434                 return 1;
435         }
436
437         arm_sig_int();
438
439         for (i = 0; i < DEPTH; i++) {
440                 void *buf;
441
442                 if (posix_memalign(&buf, BS, BS)) {
443                         printf("failed alloc\n");
444                         return 1;
445                 }
446                 s->iovecs[i].iov_base = buf;
447                 s->iovecs[i].iov_len = BS;
448         }
449
450         err = setup_ring(s);
451         if (err) {
452                 printf("ring setup failed: %s, %d\n", strerror(errno), err);
453                 return 1;
454         }
455         printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
456         printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
457         strcpy(s->filename, argv[1]);
458
459         pthread_create(&s->thread, NULL, submitter_fn, s);
460
461         cache_hit = cache_miss = reap = calls = done = 0;
462         do {
463                 unsigned long this_done = 0;
464                 unsigned long this_reap = 0;
465                 unsigned long this_call = 0;
466                 unsigned long this_cache_hit = 0;
467                 unsigned long this_cache_miss = 0;
468                 unsigned long rpc = 0, ipc = 0;
469                 double hit = 0.0;
470
471                 sleep(1);
472                 this_done += s->done;
473                 this_call += s->calls;
474                 this_reap += s->reaps;
475                 this_cache_hit += s->cachehit;
476                 this_cache_miss += s->cachemiss;
477                 if (this_cache_hit && this_cache_miss) {
478                         unsigned long hits, total;
479
480                         hits = this_cache_hit - cache_hit;
481                         total = hits + this_cache_miss - cache_miss;
482                         hit = (double) hits / (double) total;
483                         hit *= 100.0;
484                 }
485                 if (this_call - calls) {
486                         rpc = (this_done - done) / (this_call - calls);
487                         ipc = (this_reap - reap) / (this_call - calls);
488                 }
489                 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
490                                 this_done - done, rpc, ipc, s->inflight,
491                                 *s->cq_ring.head, *s->cq_ring.tail, hit);
492                 done = this_done;
493                 calls = this_call;
494                 reap = this_reap;
495                 cache_hit = s->cachehit;
496                 cache_miss = s->cachemiss;
497         } while (!finish);
498
499         pthread_join(s->thread, &ret);
500         return 0;
501 }