2 * gcc -D_GNU_SOURCE -Wall -O2 -o aio-ring aio-ring.c -lpthread -laio
12 #include <sys/types.h>
14 #include <sys/ioctl.h>
15 #include <sys/syscall.h>
16 #include <sys/resource.h>
25 #define IOCTX_FLAG_IOPOLL (1 << 0)
26 #define IOCTX_FLAG_SCQRING (1 << 1) /* Use SQ/CQ rings */
27 #define IOCTX_FLAG_FIXEDBUFS (1 << 2)
28 #define IOCTX_FLAG_SQTHREAD (1 << 3) /* Use SQ thread */
29 #define IOCTX_FLAG_SQWQ (1 << 4) /* Use SQ wq */
31 #define IOEV_RES2_CACHEHIT (1 << 0)
33 #define barrier() __asm__ __volatile__("": : :"memory")
35 #define min(a, b) ((a < b) ? (a) : (b))
64 struct io_event events[0];
67 #define IORING_FLAG_GETEVENTS (1 << 0)
71 #define BATCH_SUBMIT 8
72 #define BATCH_COMPLETE 8
76 static unsigned sq_ring_mask = DEPTH - 1;
77 static unsigned cq_ring_mask = (2 * DEPTH) - 1;
81 unsigned long max_blocks;
83 struct drand48_data rand;
84 struct aio_sq_ring *sq_ring;
86 struct aio_cq_ring *cq_ring;
91 unsigned long cachehit, cachemiss;
96 static struct submitter submitters[1];
97 static volatile int finish;
99 static int polled = 1; /* use IO polling */
100 static int fixedbufs = 1; /* use fixed user buffers */
101 static int buffered = 0; /* use buffered IO, not O_DIRECT */
102 static int sq_thread = 0; /* use kernel submission thread */
103 static int sq_thread_cpu = 0; /* pin above thread to this CPU */
105 static int io_setup2(unsigned int nr_events, unsigned int flags,
106 struct aio_sq_ring *sq_ring, struct aio_cq_ring *cq_ring,
107 io_context_t *ctx_idp)
109 return syscall(335, nr_events, flags, sq_ring, cq_ring, ctx_idp);
112 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
113 unsigned int min_complete, unsigned int flags)
115 return syscall(336, ctx, to_submit, min_complete, flags);
118 static int gettid(void)
120 return syscall(__NR_gettid);
123 static void init_io(struct submitter *s, int fd, struct iocb *iocb)
125 unsigned long offset;
128 lrand48_r(&s->rand, &r);
129 offset = (r % (s->max_blocks - 1)) * BS;
131 iocb->aio_fildes = fd;
132 iocb->aio_lio_opcode = IO_CMD_PREAD;
133 iocb->u.c.offset = offset;
135 iocb->u.c.nbytes = BS;
138 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
140 struct aio_sq_ring *ring = s->sq_ring;
141 u32 index, tail, next_tail, prepped = 0;
143 next_tail = tail = ring->tail;
147 if (next_tail == ring->head)
150 index = tail & sq_ring_mask;
151 init_io(s, fd, &s->iocbs[index]);
152 s->sq_ring->array[index] = index;
155 } while (prepped < max_ios);
157 if (ring->tail != tail) {
158 /* order tail store with writes to iocbs above */
166 static int get_file_size(int fd, unsigned long *blocks)
170 if (fstat(fd, &st) < 0)
172 if (S_ISBLK(st.st_mode)) {
173 unsigned long long bytes;
175 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
178 *blocks = bytes / BS;
180 } else if (S_ISREG(st.st_mode)) {
181 *blocks = st.st_size / BS;
188 static int reap_events(struct submitter *s)
190 struct aio_cq_ring *ring = s->cq_ring;
192 u32 head, reaped = 0;
197 if (head == ring->tail)
199 ev = &ring->events[head & cq_ring_mask];
201 struct iocb *iocb = ev->obj;
203 printf("io: unexpected ret=%ld\n", ev->res);
204 printf("offset=%lu, size=%lu\n", (unsigned long) iocb->u.c.offset, (unsigned long) iocb->u.c.nbytes);
207 if (ev->res2 & IOEV_RES2_CACHEHIT)
215 s->inflight -= reaped;
221 static void *submitter_fn(void *data)
223 struct submitter *s = data;
224 int fd, ret, prepped, flags;
226 printf("submitter=%d\n", gettid());
231 fd = open(s->filename, flags);
237 if (get_file_size(fd, &s->max_blocks)) {
238 printf("failed getting size of device/file\n");
241 if (!s->max_blocks) {
242 printf("Zero file/device size?\n");
248 srand48_r(pthread_self(), &s->rand);
252 int to_wait, to_submit, this_reap;
254 if (!prepped && s->inflight < DEPTH)
255 prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
256 s->inflight += prepped;
260 if (s->inflight + BATCH_SUBMIT < DEPTH)
263 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
265 ret = io_ring_enter(s->ioc, to_submit, to_wait,
266 IORING_FLAG_GETEVENTS);
269 this_reap = reap_events(s);
272 s->reaps += this_reap;
280 } else if (ret < to_submit) {
281 int diff = to_submit - ret;
290 } else if (ret < 0) {
291 if ((ret == -1 && errno == EAGAIN) || ret == -EAGAIN) {
300 printf("io_submit: %s\n", strerror(errno));
302 printf("io_submit: %s\n", strerror(-ret));
305 } while (!s->finish);
313 static void sig_int(int sig)
315 printf("Exiting on signal %d\n", sig);
316 submitters[0].finish = 1;
320 static void arm_sig_int(void)
322 struct sigaction act;
324 memset(&act, 0, sizeof(act));
325 act.sa_handler = sig_int;
326 act.sa_flags = SA_RESTART;
327 sigaction(SIGINT, &act, NULL);
330 int main(int argc, char *argv[])
332 struct submitter *s = &submitters[0];
333 unsigned long done, calls, reap, cache_hit, cache_miss;
341 printf("%s: filename\n", argv[0]);
345 rlim.rlim_cur = RLIM_INFINITY;
346 rlim.rlim_max = RLIM_INFINITY;
347 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
354 size = sizeof(struct iocb) * DEPTH;
355 if (posix_memalign(&p, 4096, size))
360 size = sizeof(struct aio_sq_ring) + DEPTH * sizeof(u32);
361 if (posix_memalign(&p, 4096, size))
365 s->sq_ring->nr_events = DEPTH;
366 s->sq_ring->iocbs = (u64) s->iocbs;
368 /* CQ ring must be twice as big */
369 size = sizeof(struct aio_cq_ring) +
370 2 * DEPTH * sizeof(struct io_event);
371 if (posix_memalign(&p, 4096, size))
375 s->cq_ring->nr_events = 2 * DEPTH;
377 for (j = 0; j < DEPTH; j++) {
378 struct iocb *iocb = &s->iocbs[j];
380 if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
381 printf("failed alloc\n");
384 iocb->u.c.nbytes = BS;
387 flags = IOCTX_FLAG_SCQRING;
389 flags |= IOCTX_FLAG_IOPOLL;
391 flags |= IOCTX_FLAG_FIXEDBUFS;
393 flags |= IOCTX_FLAG_SQWQ;
394 else if (sq_thread) {
395 flags |= IOCTX_FLAG_SQTHREAD;
396 s->sq_ring->sq_thread_cpu = sq_thread_cpu;
399 err = io_setup2(DEPTH, flags, s->sq_ring, s->cq_ring, &s->ioc);
401 printf("ctx_init failed: %s, %d\n", strerror(errno), err);
404 printf("polled=%d, fixedbufs=%d, buffered=%d\n", polled, fixedbufs, buffered);
405 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, s->sq_ring->nr_events, s->cq_ring->nr_events);
406 strcpy(s->filename, argv[1]);
408 pthread_create(&s->thread, NULL, submitter_fn, s);
410 cache_hit = cache_miss = reap = calls = done = 0;
412 unsigned long this_done = 0;
413 unsigned long this_reap = 0;
414 unsigned long this_call = 0;
415 unsigned long this_cache_hit = 0;
416 unsigned long this_cache_miss = 0;
417 unsigned long rpc = 0, ipc = 0;
421 this_done += s->done;
422 this_call += s->calls;
423 this_reap += s->reaps;
424 this_cache_hit += s->cachehit;
425 this_cache_miss += s->cachemiss;
426 if (this_cache_hit && this_cache_miss) {
427 unsigned long hits, total;
429 hits = this_cache_hit - cache_hit;
430 total = hits + this_cache_miss - cache_miss;
431 hit = (double) hits / (double) total;
434 if (this_call - calls) {
435 rpc = (this_done - done) / (this_call - calls);
436 ipc = (this_reap - reap) / (this_call - calls);
438 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
439 this_done - done, rpc, ipc, s->inflight,
440 s->cq_ring->head, s->cq_ring->tail, hit);
444 cache_hit = s->cachehit;
445 cache_miss = s->cachemiss;
448 pthread_join(s->thread, &ret);