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>
26 #define IOCTX_FLAG_SCQRING (1 << 0) /* Use SQ/CQ rings */
27 #define IOCTX_FLAG_IOPOLL (1 << 1)
28 #define IOCTX_FLAG_FIXEDBUFS (1 << 2)
29 #define IOCTX_FLAG_SQTHREAD (1 << 3) /* Use SQ thread */
30 #define IOCTX_FLAG_SQWQ (1 << 4) /* Use SQ wq */
31 #define IOCTX_FLAG_SQPOLL (1 << 5)
33 #define IOEV_RES2_CACHEHIT (1 << 0)
35 #define barrier() __asm__ __volatile__("": : :"memory")
37 #define min(a, b) ((a < b) ? (a) : (b))
43 #define IORING_OFF_SQ_RING 0ULL
44 #define IORING_OFF_CQ_RING 0x8000000ULL
45 #define IORING_OFF_IOCB 0x10000000ULL
47 struct aio_uring_offsets {
56 struct aio_uring_params {
62 struct aio_uring_offsets sq_off;
63 struct aio_uring_offsets cq_off;
79 struct io_event *events;
82 #define IORING_ENTER_GETEVENTS (1 << 0)
86 #define BATCH_SUBMIT 8
87 #define BATCH_COMPLETE 8
91 static unsigned sq_ring_mask, cq_ring_mask;
95 unsigned long max_blocks;
97 struct drand48_data rand;
98 struct aio_sq_ring sq_ring;
100 struct iovec iovecs[DEPTH];
101 struct aio_cq_ring cq_ring;
106 unsigned long cachehit, cachemiss;
111 static struct submitter submitters[1];
112 static volatile int finish;
114 static int polled = 0; /* use IO polling */
115 static int fixedbufs = 0; /* use fixed user buffers */
116 static int buffered = 1; /* use buffered IO, not O_DIRECT */
117 static int sq_thread = 0; /* use kernel submission thread */
118 static int sq_thread_cpu = 0; /* pin above thread to this CPU */
120 static int io_uring_setup(unsigned entries, struct iovec *iovecs,
121 struct aio_uring_params *p)
123 return syscall(335, entries, iovecs, p);
126 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
127 unsigned int min_complete, unsigned int flags)
129 return syscall(336, s->fd, to_submit, min_complete, flags);
132 static int gettid(void)
134 return syscall(__NR_gettid);
137 static void init_io(struct submitter *s, int fd, unsigned index)
139 struct iocb *iocb = &s->iocbs[index];
140 unsigned long offset;
143 lrand48_r(&s->rand, &r);
144 offset = (r % (s->max_blocks - 1)) * BS;
146 iocb->aio_fildes = fd;
147 iocb->aio_lio_opcode = IO_CMD_PREAD;
148 iocb->u.c.buf = s->iovecs[index].iov_base;
149 iocb->u.c.nbytes = BS;
150 iocb->u.c.offset = offset;
153 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
155 struct aio_sq_ring *ring = &s->sq_ring;
156 u32 index, tail, next_tail, prepped = 0;
158 next_tail = tail = *ring->tail;
162 if (next_tail == *ring->head)
165 index = tail & sq_ring_mask;
166 init_io(s, fd, index);
167 ring->array[index] = index;
170 } while (prepped < max_ios);
172 if (*ring->tail != tail) {
173 /* order tail store with writes to iocbs above */
181 static int get_file_size(int fd, unsigned long *blocks)
185 if (fstat(fd, &st) < 0)
187 if (S_ISBLK(st.st_mode)) {
188 unsigned long long bytes;
190 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
193 *blocks = bytes / BS;
195 } else if (S_ISREG(st.st_mode)) {
196 *blocks = st.st_size / BS;
203 static int reap_events(struct submitter *s)
205 struct aio_cq_ring *ring = &s->cq_ring;
207 u32 head, reaped = 0;
212 if (head == *ring->tail)
214 ev = &ring->events[head & cq_ring_mask];
216 struct iocb *iocb = ev->obj;
218 printf("io: unexpected ret=%ld\n", ev->res);
219 printf("offset=%lu, size=%lu\n", (unsigned long) iocb->u.c.offset, (unsigned long) iocb->u.c.nbytes);
222 if (ev->res2 & IOEV_RES2_CACHEHIT)
230 s->inflight -= reaped;
236 static void *submitter_fn(void *data)
238 struct submitter *s = data;
239 int fd, ret, prepped, flags;
241 printf("submitter=%d\n", gettid());
246 fd = open(s->filename, flags);
252 if (get_file_size(fd, &s->max_blocks)) {
253 printf("failed getting size of device/file\n");
256 if (!s->max_blocks) {
257 printf("Zero file/device size?\n");
263 srand48_r(pthread_self(), &s->rand);
267 int to_wait, to_submit, this_reap;
269 if (!prepped && s->inflight < DEPTH)
270 prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
271 s->inflight += prepped;
275 if (s->inflight + BATCH_SUBMIT < DEPTH)
278 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
280 ret = io_uring_enter(s, to_submit, to_wait, IORING_ENTER_GETEVENTS);
283 this_reap = reap_events(s);
286 s->reaps += this_reap;
294 } else if (ret < to_submit) {
295 int diff = to_submit - ret;
304 } else if (ret < 0) {
305 if ((ret == -1 && errno == EAGAIN) || ret == -EAGAIN) {
314 printf("io_submit: %s\n", strerror(errno));
316 printf("io_submit: %s\n", strerror(-ret));
319 } while (!s->finish);
327 static void sig_int(int sig)
329 printf("Exiting on signal %d\n", sig);
330 submitters[0].finish = 1;
334 static void arm_sig_int(void)
336 struct sigaction act;
338 memset(&act, 0, sizeof(act));
339 act.sa_handler = sig_int;
340 act.sa_flags = SA_RESTART;
341 sigaction(SIGINT, &act, NULL);
344 static int setup_ring(struct submitter *s)
346 struct aio_sq_ring *sring = &s->sq_ring;
347 struct aio_cq_ring *cring = &s->cq_ring;
348 struct aio_uring_params p;
352 memset(&p, 0, sizeof(p));
354 p.flags = IOCTX_FLAG_SCQRING;
356 p.flags |= IOCTX_FLAG_IOPOLL;
358 p.flags |= IOCTX_FLAG_FIXEDBUFS;
360 p.flags |= IOCTX_FLAG_SQWQ;
361 else if (sq_thread) {
362 p.flags |= IOCTX_FLAG_SQTHREAD;
363 p.sq_thread_cpu = sq_thread_cpu;
367 fd = io_uring_setup(DEPTH, s->iovecs, &p);
369 fd = io_uring_setup(DEPTH, NULL, &p);
371 perror("io_uring_setup");
377 ptr = mmap(0, p.sq_off.elems + p.sq_entries * sizeof(u32),
378 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
379 fd, IORING_OFF_SQ_RING);
380 printf("sq_ring ptr = 0x%p\n", ptr);
381 sring->head = ptr + p.sq_off.head;
382 sring->tail = ptr + p.sq_off.tail;
383 sring->ring_mask = ptr + p.sq_off.ring_mask;
384 sring->ring_entries = ptr + p.sq_off.ring_entries;
385 sring->array = ptr + p.sq_off.elems;
386 sq_ring_mask = *sring->ring_mask;
388 s->iocbs = mmap(0, p.sq_entries * sizeof(struct iocb), PROT_READ | PROT_WRITE,
389 MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_IOCB);
390 printf("iocbs ptr = 0x%p\n", s->iocbs);
392 ptr = mmap(0, p.cq_off.elems + p.cq_entries * sizeof(struct io_event),
393 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
394 fd, IORING_OFF_CQ_RING);
395 printf("cq_ring ptr = 0x%p\n", ptr);
396 cring->head = ptr + p.cq_off.head;
397 cring->tail = ptr + p.cq_off.tail;
398 cring->ring_mask = ptr + p.cq_off.ring_mask;
399 cring->ring_entries = ptr + p.cq_off.ring_entries;
400 cring->events = ptr + p.cq_off.elems;
401 cq_ring_mask = *cring->ring_mask;
405 int main(int argc, char *argv[])
407 struct submitter *s = &submitters[0];
408 unsigned long done, calls, reap, cache_hit, cache_miss;
414 printf("%s: filename\n", argv[0]);
418 rlim.rlim_cur = RLIM_INFINITY;
419 rlim.rlim_max = RLIM_INFINITY;
420 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
427 for (i = 0; i < DEPTH; i++) {
430 if (posix_memalign(&buf, BS, BS)) {
431 printf("failed alloc\n");
434 s->iovecs[i].iov_base = buf;
435 s->iovecs[i].iov_len = BS;
440 printf("ring setup failed: %s, %d\n", strerror(errno), err);
443 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
444 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
445 strcpy(s->filename, argv[1]);
447 pthread_create(&s->thread, NULL, submitter_fn, s);
449 cache_hit = cache_miss = reap = calls = done = 0;
451 unsigned long this_done = 0;
452 unsigned long this_reap = 0;
453 unsigned long this_call = 0;
454 unsigned long this_cache_hit = 0;
455 unsigned long this_cache_miss = 0;
456 unsigned long rpc = 0, ipc = 0;
460 this_done += s->done;
461 this_call += s->calls;
462 this_reap += s->reaps;
463 this_cache_hit += s->cachehit;
464 this_cache_miss += s->cachemiss;
465 if (this_cache_hit && this_cache_miss) {
466 unsigned long hits, total;
468 hits = this_cache_hit - cache_hit;
469 total = hits + this_cache_miss - cache_miss;
470 hit = (double) hits / (double) total;
473 if (this_call - calls) {
474 rpc = (this_done - done) / (this_call - calls);
475 ipc = (this_reap - reap) / (this_call - calls);
477 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
478 this_done - done, rpc, ipc, s->inflight,
479 *s->cq_ring.head, *s->cq_ring.tail, hit);
483 cache_hit = s->cachehit;
484 cache_miss = s->cachemiss;
487 pthread_join(s->thread, &ret);