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