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