1a4fe44b75a9feae40f152540ce952e289d9e111
[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 ring_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->ring_fd, to_submit,
144                         min_complete, 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",
235                                         (unsigned long) iocb->u.c.offset,
236                                         (unsigned long) iocb->u.c.nbytes);
237                         return -1;
238                 }
239                 if (ev->res2 & IOEV_RES2_CACHEHIT)
240                         s->cachehit++;
241                 else
242                         s->cachemiss++;
243                 reaped++;
244                 head++;
245         } while (1);
246
247         s->inflight -= reaped;
248         *ring->head = head;
249         barrier();
250         return reaped;
251 }
252
253 static void *submitter_fn(void *data)
254 {
255         struct submitter *s = data;
256         int fd, ret, prepped, flags;
257
258         printf("submitter=%d\n", gettid());
259
260         flags = O_RDONLY;
261         if (!buffered)
262                 flags |= O_DIRECT;
263         fd = open(s->filename, flags);
264         if (fd < 0) {
265                 perror("open");
266                 goto done;
267         }
268
269         if (get_file_size(fd, &s->max_blocks)) {
270                 printf("failed getting size of device/file\n");
271                 goto err;
272         }
273         if (s->max_blocks <= 1) {
274                 printf("Zero file/device size?\n");
275                 goto err;
276         }
277         s->max_blocks--;
278
279         srand48_r(pthread_self(), &s->rand);
280
281         prepped = 0;
282         do {
283                 int to_wait, to_submit, this_reap, to_prep;
284
285                 if (!prepped && s->inflight < DEPTH) {
286                         to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
287                         prepped = prep_more_ios(s, fd, to_prep);
288                 }
289                 s->inflight += prepped;
290 submit_more:
291                 to_submit = prepped;
292 submit:
293                 if (s->inflight + BATCH_SUBMIT < DEPTH)
294                         to_wait = 0;
295                 else
296                         to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
297
298                 ret = io_uring_enter(s, to_submit, to_wait,
299                                         IORING_ENTER_GETEVENTS);
300                 s->calls++;
301
302                 this_reap = reap_events(s);
303                 if (this_reap == -1)
304                         break;
305                 s->reaps += this_reap;
306
307                 if (ret >= 0) {
308                         if (!ret) {
309                                 to_submit = 0;
310                                 if (s->inflight)
311                                         goto submit;
312                                 continue;
313                         } else if (ret < to_submit) {
314                                 int diff = to_submit - ret;
315
316                                 s->done += ret;
317                                 prepped -= diff;
318                                 goto submit_more;
319                         }
320                         s->done += ret;
321                         prepped = 0;
322                         continue;
323                 } else if (ret < 0) {
324                         if (errno == EAGAIN) {
325                                 if (s->finish)
326                                         break;
327                                 if (this_reap)
328                                         goto submit;
329                                 to_submit = 0;
330                                 goto submit;
331                         }
332                         printf("io_submit: %s\n", strerror(errno));
333                         break;
334                 }
335         } while (!s->finish);
336 err:
337         close(fd);
338 done:
339         finish = 1;
340         return NULL;
341 }
342
343 static void sig_int(int sig)
344 {
345         printf("Exiting on signal %d\n", sig);
346         submitters[0].finish = 1;
347         finish = 1;
348 }
349
350 static void arm_sig_int(void)
351 {
352         struct sigaction act;
353
354         memset(&act, 0, sizeof(act));
355         act.sa_handler = sig_int;
356         act.sa_flags = SA_RESTART;
357         sigaction(SIGINT, &act, NULL);
358 }
359
360 static int setup_ring(struct submitter *s)
361 {
362         struct aio_sq_ring *sring = &s->sq_ring;
363         struct aio_cq_ring *cring = &s->cq_ring;
364         struct aio_uring_params p;
365         void *ptr;
366         int fd;
367
368         memset(&p, 0, sizeof(p));
369
370         p.flags = IOCTX_FLAG_SCQRING;
371         if (polled)
372                 p.flags |= IOCTX_FLAG_IOPOLL;
373         if (fixedbufs)
374                 p.flags |= IOCTX_FLAG_FIXEDBUFS;
375         if (buffered)
376                 p.flags |= IOCTX_FLAG_SQWQ;
377         else if (sq_thread) {
378                 p.flags |= IOCTX_FLAG_SQTHREAD;
379                 p.sq_thread_cpu = sq_thread_cpu;
380         }
381
382         if (fixedbufs)
383                 fd = io_uring_setup(DEPTH, s->iovecs, &p);
384         else
385                 fd = io_uring_setup(DEPTH, NULL, &p);
386         if (fd < 0) {
387                 perror("io_uring_setup");
388                 return 1;
389         }
390
391         s->ring_fd = fd;
392         ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(u32),
393                         PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
394                         IORING_OFF_SQ_RING);
395         printf("sq_ring ptr = 0x%p\n", ptr);
396         sring->head = ptr + p.sq_off.head;
397         sring->tail = ptr + p.sq_off.tail;
398         sring->ring_mask = ptr + p.sq_off.ring_mask;
399         sring->ring_entries = ptr + p.sq_off.ring_entries;
400         sring->array = ptr + p.sq_off.array;
401         sq_ring_mask = *sring->ring_mask;
402
403         s->iocbs = mmap(0, p.sq_entries * sizeof(struct iocb),
404                         PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
405                         IORING_OFF_IOCB);
406         printf("iocbs ptr   = 0x%p\n", s->iocbs);
407
408         ptr = mmap(0, p.cq_off.events + p.cq_entries * sizeof(struct io_event),
409                         PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
410                         IORING_OFF_CQ_RING);
411         printf("cq_ring ptr = 0x%p\n", ptr);
412         cring->head = ptr + p.cq_off.head;
413         cring->tail = ptr + p.cq_off.tail;
414         cring->ring_mask = ptr + p.cq_off.ring_mask;
415         cring->ring_entries = ptr + p.cq_off.ring_entries;
416         cring->events = ptr + p.cq_off.events;
417         cq_ring_mask = *cring->ring_mask;
418         return 0;
419 }
420
421 int main(int argc, char *argv[])
422 {
423         struct submitter *s = &submitters[0];
424         unsigned long done, calls, reap, cache_hit, cache_miss;
425         int err, i;
426         struct rlimit rlim;
427         void *ret;
428
429         if (argc < 2) {
430                 printf("%s: filename\n", argv[0]);
431                 return 1;
432         }
433
434         rlim.rlim_cur = RLIM_INFINITY;
435         rlim.rlim_max = RLIM_INFINITY;
436         if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
437                 perror("setrlimit");
438                 return 1;
439         }
440
441         arm_sig_int();
442
443         for (i = 0; i < DEPTH; i++) {
444                 void *buf;
445
446                 if (posix_memalign(&buf, BS, BS)) {
447                         printf("failed alloc\n");
448                         return 1;
449                 }
450                 s->iovecs[i].iov_base = buf;
451                 s->iovecs[i].iov_len = BS;
452         }
453
454         err = setup_ring(s);
455         if (err) {
456                 printf("ring setup failed: %s, %d\n", strerror(errno), err);
457                 return 1;
458         }
459         printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
460         printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
461         strcpy(s->filename, argv[1]);
462
463         pthread_create(&s->thread, NULL, submitter_fn, s);
464
465         cache_hit = cache_miss = reap = calls = done = 0;
466         do {
467                 unsigned long this_done = 0;
468                 unsigned long this_reap = 0;
469                 unsigned long this_call = 0;
470                 unsigned long this_cache_hit = 0;
471                 unsigned long this_cache_miss = 0;
472                 unsigned long rpc = 0, ipc = 0;
473                 double hit = 0.0;
474
475                 sleep(1);
476                 this_done += s->done;
477                 this_call += s->calls;
478                 this_reap += s->reaps;
479                 this_cache_hit += s->cachehit;
480                 this_cache_miss += s->cachemiss;
481                 if (this_cache_hit && this_cache_miss) {
482                         unsigned long hits, total;
483
484                         hits = this_cache_hit - cache_hit;
485                         total = hits + this_cache_miss - cache_miss;
486                         hit = (double) hits / (double) total;
487                         hit *= 100.0;
488                 }
489                 if (this_call - calls) {
490                         rpc = (this_done - done) / (this_call - calls);
491                         ipc = (this_reap - reap) / (this_call - calls);
492                 }
493                 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
494                                 this_done - done, rpc, ipc, s->inflight,
495                                 *s->cq_ring.head, *s->cq_ring.tail, hit);
496                 done = this_done;
497                 calls = this_call;
498                 reap = this_reap;
499                 cache_hit = s->cachehit;
500                 cache_miss = s->cachemiss;
501         } while (!finish);
502
503         pthread_join(s->thread, &ret);
504         return 0;
505 }