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 IOCB_FLAG_HIPRI (1 << 2)
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 */
33 #define IOEV_RES2_CACHEHIT (1 << 0)
35 #define barrier() __asm__ __volatile__("": : :"memory")
37 #define min(a, b) ((a < b) ? (a) : (b))
66 struct io_event events[0];
69 #define IORING_FLAG_SUBMIT (1 << 0)
70 #define IORING_FLAG_GETEVENTS (1 << 1)
74 #define BATCH_SUBMIT 8
75 #define BATCH_COMPLETE 8
79 static unsigned sq_ring_mask = DEPTH - 1;
80 static unsigned cq_ring_mask = (2 * DEPTH) - 1;
84 unsigned long max_blocks;
86 struct drand48_data rand;
87 struct aio_sq_ring *sq_ring;
89 struct aio_cq_ring *cq_ring;
94 unsigned long cachehit, cachemiss;
99 static struct submitter submitters[1];
100 static volatile int finish;
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 */
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)
112 return syscall(335, nr_events, flags, sq_ring, cq_ring, ctx_idp);
115 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
116 unsigned int min_complete, unsigned int flags)
118 return syscall(336, ctx, to_submit, min_complete, flags);
121 static int gettid(void)
123 return syscall(__NR_gettid);
126 static void init_io(struct submitter *s, int fd, struct iocb *iocb)
128 unsigned long offset;
131 lrand48_r(&s->rand, &r);
132 offset = (r % (s->max_blocks - 1)) * BS;
134 iocb->aio_fildes = fd;
135 iocb->aio_lio_opcode = IO_CMD_PREAD;
136 iocb->u.c.offset = offset;
138 iocb->u.c.flags = IOCB_FLAG_HIPRI;
140 iocb->u.c.nbytes = BS;
143 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
145 struct aio_sq_ring *ring = s->sq_ring;
146 u32 index, tail, next_tail, prepped = 0;
148 next_tail = tail = ring->tail;
152 if (next_tail == ring->head)
155 index = tail & sq_ring_mask;
156 init_io(s, fd, &s->iocbs[index]);
157 s->sq_ring->array[index] = index;
160 } while (prepped < max_ios);
162 if (ring->tail != tail) {
163 /* order tail store with writes to iocbs above */
171 static int get_file_size(int fd, unsigned long *blocks)
175 if (fstat(fd, &st) < 0)
177 if (S_ISBLK(st.st_mode)) {
178 unsigned long long bytes;
180 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
183 *blocks = bytes / BS;
185 } else if (S_ISREG(st.st_mode)) {
186 *blocks = st.st_size / BS;
193 static int reap_events(struct submitter *s)
195 struct aio_cq_ring *ring = s->cq_ring;
197 u32 head, reaped = 0;
202 if (head == ring->tail)
204 ev = &ring->events[head & cq_ring_mask];
206 struct iocb *iocb = ev->obj;
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);
212 if (ev->res2 & IOEV_RES2_CACHEHIT)
220 s->inflight -= reaped;
226 static void *submitter_fn(void *data)
228 struct submitter *s = data;
229 int fd, ret, prepped, flags;
231 printf("submitter=%d\n", gettid());
236 fd = open(s->filename, flags);
242 if (get_file_size(fd, &s->max_blocks)) {
243 printf("failed getting size of device/file\n");
246 if (!s->max_blocks) {
247 printf("Zero file/device size?\n");
253 srand48_r(pthread_self(), &s->rand);
257 int to_wait, flags, to_submit, this_reap;
259 if (!prepped && s->inflight < DEPTH)
260 prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
261 s->inflight += prepped;
265 if (s->inflight + BATCH_SUBMIT < DEPTH)
268 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
270 flags = IORING_FLAG_GETEVENTS;
272 flags |= IORING_FLAG_SUBMIT;
274 ret = io_ring_enter(s->ioc, to_submit, to_wait, flags);
277 this_reap = reap_events(s);
280 s->reaps += this_reap;
288 } else if (ret < to_submit) {
289 int diff = to_submit - ret;
298 } else if (ret < 0) {
299 if ((ret == -1 && errno == EAGAIN) || ret == -EAGAIN) {
308 printf("io_submit: %s\n", strerror(errno));
310 printf("io_submit: %s\n", strerror(-ret));
313 } while (!s->finish);
321 static void sig_int(int sig)
323 printf("Exiting on signal %d\n", sig);
324 submitters[0].finish = 1;
328 static void arm_sig_int(void)
330 struct sigaction act;
332 memset(&act, 0, sizeof(act));
333 act.sa_handler = sig_int;
334 act.sa_flags = SA_RESTART;
335 sigaction(SIGINT, &act, NULL);
338 int main(int argc, char *argv[])
340 struct submitter *s = &submitters[0];
341 unsigned long done, calls, reap, cache_hit, cache_miss;
349 printf("%s: filename\n", argv[0]);
353 rlim.rlim_cur = RLIM_INFINITY;
354 rlim.rlim_max = RLIM_INFINITY;
355 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
362 size = sizeof(struct iocb) * DEPTH;
363 if (posix_memalign(&p, 4096, size))
368 size = sizeof(struct aio_sq_ring) + DEPTH * sizeof(u32);
369 if (posix_memalign(&p, 4096, size))
373 s->sq_ring->nr_events = DEPTH;
374 s->sq_ring->iocbs = (u64) s->iocbs;
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))
383 s->cq_ring->nr_events = 2 * DEPTH;
385 for (j = 0; j < DEPTH; j++) {
386 struct iocb *iocb = &s->iocbs[j];
388 if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
389 printf("failed alloc\n");
392 iocb->u.c.nbytes = BS;
395 flags = IOCTX_FLAG_SCQRING;
397 flags |= IOCTX_FLAG_IOPOLL;
399 flags |= IOCTX_FLAG_FIXEDBUFS;
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;
407 err = io_setup2(DEPTH, flags, s->sq_ring, s->cq_ring, &s->ioc);
409 printf("ctx_init failed: %s, %d\n", strerror(errno), err);
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]);
416 pthread_create(&s->thread, NULL, submitter_fn, s);
418 cache_hit = cache_miss = reap = calls = done = 0;
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;
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;
437 hits = this_cache_hit - cache_hit;
438 total = hits + this_cache_miss - cache_miss;
439 hit = (double) hits / (double) total;
442 if (this_call - calls) {
443 rpc = (this_done - done) / (this_call - calls);
444 ipc = (this_reap - reap) / (this_call - calls);
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);
452 cache_hit = s->cachehit;
453 cache_miss = s->cachemiss;
456 pthread_join(s->thread, &ret);