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