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_GETEVENTS (1 << 0)
73 #define BATCH_SUBMIT 8
74 #define BATCH_COMPLETE 8
78 static unsigned sq_ring_mask = DEPTH - 1;
79 static unsigned cq_ring_mask = (2 * DEPTH) - 1;
83 unsigned long max_blocks;
85 struct drand48_data rand;
86 struct aio_sq_ring *sq_ring;
88 struct aio_cq_ring *cq_ring;
93 unsigned long cachehit, cachemiss;
98 static struct submitter submitters[1];
99 static volatile int finish;
101 static int polled = 1; /* use IO polling */
102 static int fixedbufs = 1; /* use fixed user buffers */
103 static int buffered = 0; /* use buffered IO, not O_DIRECT */
104 static int sq_thread = 0; /* use kernel submission thread */
105 static int sq_thread_cpu = 0; /* pin above thread to this CPU */
107 static int io_setup2(unsigned int nr_events, unsigned int flags,
108 struct aio_sq_ring *sq_ring, struct aio_cq_ring *cq_ring,
109 io_context_t *ctx_idp)
111 return syscall(335, nr_events, flags, sq_ring, cq_ring, ctx_idp);
114 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
115 unsigned int min_complete, unsigned int flags)
117 return syscall(336, ctx, to_submit, min_complete, flags);
120 static int gettid(void)
122 return syscall(__NR_gettid);
125 static void init_io(struct submitter *s, int fd, struct iocb *iocb)
127 unsigned long offset;
130 lrand48_r(&s->rand, &r);
131 offset = (r % (s->max_blocks - 1)) * BS;
133 iocb->aio_fildes = fd;
134 iocb->aio_lio_opcode = IO_CMD_PREAD;
135 iocb->u.c.offset = offset;
137 iocb->u.c.flags = IOCB_FLAG_HIPRI;
139 iocb->u.c.nbytes = BS;
142 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
144 struct aio_sq_ring *ring = s->sq_ring;
145 u32 index, tail, next_tail, prepped = 0;
147 next_tail = tail = ring->tail;
151 if (next_tail == ring->head)
154 index = tail & sq_ring_mask;
155 init_io(s, fd, &s->iocbs[index]);
156 s->sq_ring->array[index] = index;
159 } while (prepped < max_ios);
161 if (ring->tail != tail) {
162 /* order tail store with writes to iocbs above */
170 static int get_file_size(int fd, unsigned long *blocks)
174 if (fstat(fd, &st) < 0)
176 if (S_ISBLK(st.st_mode)) {
177 unsigned long long bytes;
179 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
182 *blocks = bytes / BS;
184 } else if (S_ISREG(st.st_mode)) {
185 *blocks = st.st_size / BS;
192 static int reap_events(struct submitter *s)
194 struct aio_cq_ring *ring = s->cq_ring;
196 u32 head, reaped = 0;
201 if (head == ring->tail)
203 ev = &ring->events[head & cq_ring_mask];
205 struct iocb *iocb = ev->obj;
207 printf("io: unexpected ret=%ld\n", ev->res);
208 printf("offset=%lu, size=%lu\n", (unsigned long) iocb->u.c.offset, (unsigned long) iocb->u.c.nbytes);
211 if (ev->res2 & IOEV_RES2_CACHEHIT)
219 s->inflight -= reaped;
225 static void *submitter_fn(void *data)
227 struct submitter *s = data;
228 int fd, ret, prepped, flags;
230 printf("submitter=%d\n", gettid());
235 fd = open(s->filename, flags);
241 if (get_file_size(fd, &s->max_blocks)) {
242 printf("failed getting size of device/file\n");
245 if (!s->max_blocks) {
246 printf("Zero file/device size?\n");
252 srand48_r(pthread_self(), &s->rand);
256 int to_wait, to_submit, this_reap;
258 if (!prepped && s->inflight < DEPTH)
259 prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
260 s->inflight += prepped;
264 if (s->inflight + BATCH_SUBMIT < DEPTH)
267 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
269 ret = io_ring_enter(s->ioc, to_submit, to_wait,
270 IORING_FLAG_GETEVENTS);
273 this_reap = reap_events(s);
276 s->reaps += this_reap;
284 } else if (ret < to_submit) {
285 int diff = to_submit - ret;
294 } else if (ret < 0) {
295 if ((ret == -1 && errno == EAGAIN) || ret == -EAGAIN) {
304 printf("io_submit: %s\n", strerror(errno));
306 printf("io_submit: %s\n", strerror(-ret));
309 } while (!s->finish);
317 static void sig_int(int sig)
319 printf("Exiting on signal %d\n", sig);
320 submitters[0].finish = 1;
324 static void arm_sig_int(void)
326 struct sigaction act;
328 memset(&act, 0, sizeof(act));
329 act.sa_handler = sig_int;
330 act.sa_flags = SA_RESTART;
331 sigaction(SIGINT, &act, NULL);
334 int main(int argc, char *argv[])
336 struct submitter *s = &submitters[0];
337 unsigned long done, calls, reap, cache_hit, cache_miss;
345 printf("%s: filename\n", argv[0]);
349 rlim.rlim_cur = RLIM_INFINITY;
350 rlim.rlim_max = RLIM_INFINITY;
351 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
358 size = sizeof(struct iocb) * DEPTH;
359 if (posix_memalign(&p, 4096, size))
364 size = sizeof(struct aio_sq_ring) + DEPTH * sizeof(u32);
365 if (posix_memalign(&p, 4096, size))
369 s->sq_ring->nr_events = DEPTH;
370 s->sq_ring->iocbs = (u64) s->iocbs;
372 /* CQ ring must be twice as big */
373 size = sizeof(struct aio_cq_ring) +
374 2 * DEPTH * sizeof(struct io_event);
375 if (posix_memalign(&p, 4096, size))
379 s->cq_ring->nr_events = 2 * DEPTH;
381 for (j = 0; j < DEPTH; j++) {
382 struct iocb *iocb = &s->iocbs[j];
384 if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
385 printf("failed alloc\n");
388 iocb->u.c.nbytes = BS;
391 flags = IOCTX_FLAG_SCQRING;
393 flags |= IOCTX_FLAG_IOPOLL;
395 flags |= IOCTX_FLAG_FIXEDBUFS;
397 flags |= IOCTX_FLAG_SQWQ;
398 else if (sq_thread) {
399 flags |= IOCTX_FLAG_SQTHREAD;
400 s->sq_ring->sq_thread_cpu = sq_thread_cpu;
403 err = io_setup2(DEPTH, flags, s->sq_ring, s->cq_ring, &s->ioc);
405 printf("ctx_init failed: %s, %d\n", strerror(errno), err);
408 printf("polled=%d, fixedbufs=%d, buffered=%d\n", polled, fixedbufs, buffered);
409 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, s->sq_ring->nr_events, s->cq_ring->nr_events);
410 strcpy(s->filename, argv[1]);
412 pthread_create(&s->thread, NULL, submitter_fn, s);
414 cache_hit = cache_miss = reap = calls = done = 0;
416 unsigned long this_done = 0;
417 unsigned long this_reap = 0;
418 unsigned long this_call = 0;
419 unsigned long this_cache_hit = 0;
420 unsigned long this_cache_miss = 0;
421 unsigned long rpc = 0, ipc = 0;
425 this_done += s->done;
426 this_call += s->calls;
427 this_reap += s->reaps;
428 this_cache_hit += s->cachehit;
429 this_cache_miss += s->cachemiss;
430 if (this_cache_hit && this_cache_miss) {
431 unsigned long hits, total;
433 hits = this_cache_hit - cache_hit;
434 total = hits + this_cache_miss - cache_miss;
435 hit = (double) hits / (double) total;
438 if (this_call - calls) {
439 rpc = (this_done - done) / (this_call - calls);
440 ipc = (this_reap - reap) / (this_call - calls);
442 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
443 this_done - done, rpc, ipc, s->inflight,
444 s->cq_ring->head, s->cq_ring->tail, hit);
448 cache_hit = s->cachehit;
449 cache_miss = s->cachemiss;
452 pthread_join(s->thread, &ret);