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