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)
73 #define RING_SIZE (DEPTH + 1)
75 #define BATCH_SUBMIT 8
76 #define BATCH_COMPLETE 8
82 unsigned long max_blocks;
84 struct drand48_data rand;
85 struct aio_sq_ring *sq_ring;
87 struct aio_cq_ring *cq_ring;
92 unsigned long cachehit, cachemiss;
97 static struct submitter submitters[1];
98 static volatile int finish;
100 static int polled = 1; /* use IO polling */
101 static int fixedbufs = 1; /* use fixed user buffers */
102 static int buffered = 0; /* use buffered IO, not O_DIRECT */
103 static int sq_thread = 0; /* use kernel submission thread */
104 static int sq_thread_cpu = 0; /* pin above thread to this CPU */
106 static int io_setup2(unsigned int nr_events, unsigned int flags,
107 struct aio_sq_ring *sq_ring, struct aio_cq_ring *cq_ring,
108 io_context_t *ctx_idp)
110 return syscall(335, nr_events, flags, sq_ring, cq_ring, ctx_idp);
113 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
114 unsigned int min_complete, unsigned int flags)
116 return syscall(336, ctx, to_submit, min_complete, flags);
119 static int gettid(void)
121 return syscall(__NR_gettid);
124 static void init_io(struct submitter *s, int fd, struct iocb *iocb)
126 unsigned long offset;
129 lrand48_r(&s->rand, &r);
130 offset = (r % (s->max_blocks - 1)) * BS;
132 iocb->aio_fildes = fd;
133 iocb->aio_lio_opcode = IO_CMD_PREAD;
134 iocb->u.c.offset = offset;
136 iocb->u.c.flags = IOCB_FLAG_HIPRI;
138 iocb->u.c.nbytes = BS;
141 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
143 struct aio_sq_ring *ring = s->sq_ring;
144 u32 tail, next_tail, prepped = 0;
146 next_tail = tail = ring->tail;
149 if (next_tail == ring->nr_events)
153 if (next_tail == ring->head)
156 init_io(s, fd, &s->iocbs[tail]);
157 s->sq_ring->array[tail] = tail;
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];
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)
218 if (head == ring->nr_events)
222 s->inflight -= reaped;
228 static void *submitter_fn(void *data)
230 struct submitter *s = data;
231 int fd, ret, prepped, flags;
233 printf("submitter=%d\n", gettid());
238 fd = open(s->filename, flags);
244 if (get_file_size(fd, &s->max_blocks)) {
245 printf("failed getting size of device/file\n");
248 if (!s->max_blocks) {
249 printf("Zero file/device size?\n");
255 srand48_r(pthread_self(), &s->rand);
259 int to_wait, flags, to_submit, this_reap;
261 if (!prepped && s->inflight < DEPTH)
262 prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
263 s->inflight += prepped;
267 if (s->inflight + BATCH_SUBMIT < DEPTH)
270 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
272 flags = IORING_FLAG_GETEVENTS;
274 flags |= IORING_FLAG_SUBMIT;
276 ret = io_ring_enter(s->ioc, to_submit, to_wait, flags);
279 this_reap = reap_events(s);
282 s->reaps += this_reap;
290 } else if (ret < to_submit) {
291 int diff = to_submit - ret;
300 } else if (ret < 0) {
301 if ((ret == -1 && errno == EAGAIN) || ret == -EAGAIN) {
310 printf("io_submit: %s\n", strerror(errno));
312 printf("io_submit: %s\n", strerror(-ret));
315 } while (!s->finish);
323 static void sig_int(int sig)
325 printf("Exiting on signal %d\n", sig);
326 submitters[0].finish = 1;
330 static void arm_sig_int(void)
332 struct sigaction act;
334 memset(&act, 0, sizeof(act));
335 act.sa_handler = sig_int;
336 act.sa_flags = SA_RESTART;
337 sigaction(SIGINT, &act, NULL);
340 int main(int argc, char *argv[])
342 struct submitter *s = &submitters[0];
343 unsigned long done, calls, reap, cache_hit, cache_miss;
351 printf("%s: filename\n", argv[0]);
355 rlim.rlim_cur = RLIM_INFINITY;
356 rlim.rlim_max = RLIM_INFINITY;
357 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
364 size = sizeof(struct iocb) * RING_SIZE;
365 if (posix_memalign(&p, 4096, size))
370 size = sizeof(struct aio_sq_ring) + RING_SIZE * sizeof(u32);
371 if (posix_memalign(&p, 4096, size))
375 s->sq_ring->nr_events = RING_SIZE;
376 s->sq_ring->iocbs = (u64) s->iocbs;
378 /* CQ ring must be twice as big */
379 size = sizeof(struct aio_cq_ring) +
380 2 * RING_SIZE * sizeof(struct io_event);
381 if (posix_memalign(&p, 4096, size))
385 s->cq_ring->nr_events = 2 * RING_SIZE;
387 for (j = 0; j < RING_SIZE; j++) {
388 struct iocb *iocb = &s->iocbs[j];
390 if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
391 printf("failed alloc\n");
394 iocb->u.c.nbytes = BS;
397 flags = IOCTX_FLAG_SCQRING;
399 flags |= IOCTX_FLAG_IOPOLL;
401 flags |= IOCTX_FLAG_FIXEDBUFS;
403 flags |= IOCTX_FLAG_SQWQ;
404 else if (sq_thread) {
405 flags |= IOCTX_FLAG_SQTHREAD;
406 s->sq_ring->sq_thread_cpu = sq_thread_cpu;
409 err = io_setup2(RING_SIZE, flags, s->sq_ring, s->cq_ring, &s->ioc);
411 printf("ctx_init failed: %s, %d\n", strerror(errno), err);
414 printf("polled=%d, fixedbufs=%d, buffered=%d\n", polled, fixedbufs, buffered);
415 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, s->sq_ring->nr_events, s->cq_ring->nr_events);
416 strcpy(s->filename, argv[1]);
418 pthread_create(&s->thread, NULL, submitter_fn, s);
420 cache_hit = cache_miss = reap = calls = done = 0;
422 unsigned long this_done = 0;
423 unsigned long this_reap = 0;
424 unsigned long this_call = 0;
425 unsigned long this_cache_hit = 0;
426 unsigned long this_cache_miss = 0;
427 unsigned long rpc = 0, ipc = 0;
431 this_done += s->done;
432 this_call += s->calls;
433 this_reap += s->reaps;
434 this_cache_hit += s->cachehit;
435 this_cache_miss += s->cachemiss;
436 if (this_cache_hit && this_cache_miss) {
437 unsigned long hits, total;
439 hits = this_cache_hit - cache_hit;
440 total = hits + this_cache_miss - cache_miss;
441 hit = (double) hits / (double) total;
444 if (this_call - calls) {
445 rpc = (this_done - done) / (this_call - calls);
446 ipc = (this_reap - reap) / (this_call - calls);
448 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%d tail=%d), Cachehit=%0.2f%%\n",
449 this_done - done, rpc, ipc, s->inflight,
450 s->cq_ring->head, s->cq_ring->tail, hit);
454 cache_hit = s->cachehit;
455 cache_miss = s->cachemiss;
458 pthread_join(s->thread, &ret);