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 #include "../arch/arch.h"
28 #define IOCTX_FLAG_SCQRING (1 << 0) /* Use SQ/CQ rings */
29 #define IOCTX_FLAG_IOPOLL (1 << 1)
30 #define IOCTX_FLAG_FIXEDBUFS (1 << 2)
31 #define IOCTX_FLAG_SQTHREAD (1 << 3) /* Use SQ thread */
32 #define IOCTX_FLAG_SQWQ (1 << 4) /* Use SQ wq */
33 #define IOCTX_FLAG_SQPOLL (1 << 5)
35 #define IOEV_RES2_CACHEHIT (1 << 0)
37 #define barrier() __asm__ __volatile__("": : :"memory")
39 #define min(a, b) ((a < b) ? (a) : (b))
45 #define IORING_OFF_SQ_RING 0ULL
46 #define IORING_OFF_CQ_RING 0x8000000ULL
47 #define IORING_OFF_IOCB 0x10000000ULL
49 struct aio_sqring_offsets {
60 struct aio_cqring_offsets {
70 struct aio_uring_params {
76 struct aio_sqring_offsets sq_off;
77 struct aio_cqring_offsets cq_off;
93 struct io_event *events;
96 #define IORING_ENTER_GETEVENTS (1 << 0)
100 #define BATCH_SUBMIT 8
101 #define BATCH_COMPLETE 8
105 static unsigned sq_ring_mask, cq_ring_mask;
109 unsigned long max_blocks;
111 struct drand48_data rand;
112 struct aio_sq_ring sq_ring;
114 struct iovec iovecs[DEPTH];
115 struct aio_cq_ring cq_ring;
120 unsigned long cachehit, cachemiss;
125 static struct submitter submitters[1];
126 static volatile int finish;
128 static int polled = 0; /* use IO polling */
129 static int fixedbufs = 0; /* use fixed user buffers */
130 static int buffered = 1; /* use buffered IO, not O_DIRECT */
131 static int sq_thread = 0; /* use kernel submission thread */
132 static int sq_thread_cpu = 0; /* pin above thread to this CPU */
134 static int io_uring_setup(unsigned entries, struct iovec *iovecs,
135 struct aio_uring_params *p)
137 return syscall(__NR_sys_io_uring_setup, entries, iovecs, p);
140 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
141 unsigned int min_complete, unsigned int flags)
143 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
144 min_complete, flags);
147 static int gettid(void)
149 return syscall(__NR_gettid);
152 static void init_io(struct submitter *s, int fd, unsigned index)
154 struct iocb *iocb = &s->iocbs[index];
155 unsigned long offset;
158 lrand48_r(&s->rand, &r);
159 offset = (r % (s->max_blocks - 1)) * BS;
161 iocb->aio_fildes = fd;
162 iocb->aio_lio_opcode = IO_CMD_PREAD;
163 iocb->u.c.buf = s->iovecs[index].iov_base;
164 iocb->u.c.nbytes = BS;
165 iocb->u.c.offset = offset;
168 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
170 struct aio_sq_ring *ring = &s->sq_ring;
171 u32 index, tail, next_tail, prepped = 0;
173 next_tail = tail = *ring->tail;
177 if (next_tail == *ring->head)
180 index = tail & sq_ring_mask;
181 init_io(s, fd, index);
182 ring->array[index] = index;
185 } while (prepped < max_ios);
187 if (*ring->tail != tail) {
188 /* order tail store with writes to iocbs above */
196 static int get_file_size(int fd, unsigned long *blocks)
200 if (fstat(fd, &st) < 0)
202 if (S_ISBLK(st.st_mode)) {
203 unsigned long long bytes;
205 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
208 *blocks = bytes / BS;
210 } else if (S_ISREG(st.st_mode)) {
211 *blocks = st.st_size / BS;
218 static int reap_events(struct submitter *s)
220 struct aio_cq_ring *ring = &s->cq_ring;
222 u32 head, reaped = 0;
227 if (head == *ring->tail)
229 ev = &ring->events[head & cq_ring_mask];
231 struct iocb *iocb = ev->obj;
233 printf("io: unexpected ret=%ld\n", ev->res);
234 printf("offset=%lu, size=%lu\n",
235 (unsigned long) iocb->u.c.offset,
236 (unsigned long) iocb->u.c.nbytes);
239 if (ev->res2 & IOEV_RES2_CACHEHIT)
247 s->inflight -= reaped;
253 static void *submitter_fn(void *data)
255 struct submitter *s = data;
256 int fd, ret, prepped, flags;
258 printf("submitter=%d\n", gettid());
263 fd = open(s->filename, flags);
269 if (get_file_size(fd, &s->max_blocks)) {
270 printf("failed getting size of device/file\n");
273 if (s->max_blocks <= 1) {
274 printf("Zero file/device size?\n");
279 srand48_r(pthread_self(), &s->rand);
283 int to_wait, to_submit, this_reap, to_prep;
285 if (!prepped && s->inflight < DEPTH) {
286 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
287 prepped = prep_more_ios(s, fd, to_prep);
289 s->inflight += prepped;
293 if (s->inflight + BATCH_SUBMIT < DEPTH)
296 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
298 ret = io_uring_enter(s, to_submit, to_wait,
299 IORING_ENTER_GETEVENTS);
302 this_reap = reap_events(s);
305 s->reaps += this_reap;
313 } else if (ret < to_submit) {
314 int diff = to_submit - ret;
323 } else if (ret < 0) {
324 if (errno == EAGAIN) {
332 printf("io_submit: %s\n", strerror(errno));
335 } while (!s->finish);
343 static void sig_int(int sig)
345 printf("Exiting on signal %d\n", sig);
346 submitters[0].finish = 1;
350 static void arm_sig_int(void)
352 struct sigaction act;
354 memset(&act, 0, sizeof(act));
355 act.sa_handler = sig_int;
356 act.sa_flags = SA_RESTART;
357 sigaction(SIGINT, &act, NULL);
360 static int setup_ring(struct submitter *s)
362 struct aio_sq_ring *sring = &s->sq_ring;
363 struct aio_cq_ring *cring = &s->cq_ring;
364 struct aio_uring_params p;
368 memset(&p, 0, sizeof(p));
370 p.flags = IOCTX_FLAG_SCQRING;
372 p.flags |= IOCTX_FLAG_IOPOLL;
374 p.flags |= IOCTX_FLAG_FIXEDBUFS;
376 p.flags |= IOCTX_FLAG_SQWQ;
377 else if (sq_thread) {
378 p.flags |= IOCTX_FLAG_SQTHREAD;
379 p.sq_thread_cpu = sq_thread_cpu;
383 fd = io_uring_setup(DEPTH, s->iovecs, &p);
385 fd = io_uring_setup(DEPTH, NULL, &p);
387 perror("io_uring_setup");
392 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(u32),
393 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
395 printf("sq_ring ptr = 0x%p\n", ptr);
396 sring->head = ptr + p.sq_off.head;
397 sring->tail = ptr + p.sq_off.tail;
398 sring->ring_mask = ptr + p.sq_off.ring_mask;
399 sring->ring_entries = ptr + p.sq_off.ring_entries;
400 sring->array = ptr + p.sq_off.array;
401 sq_ring_mask = *sring->ring_mask;
403 s->iocbs = mmap(0, p.sq_entries * sizeof(struct iocb),
404 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
406 printf("iocbs ptr = 0x%p\n", s->iocbs);
408 ptr = mmap(0, p.cq_off.events + p.cq_entries * sizeof(struct io_event),
409 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
411 printf("cq_ring ptr = 0x%p\n", ptr);
412 cring->head = ptr + p.cq_off.head;
413 cring->tail = ptr + p.cq_off.tail;
414 cring->ring_mask = ptr + p.cq_off.ring_mask;
415 cring->ring_entries = ptr + p.cq_off.ring_entries;
416 cring->events = ptr + p.cq_off.events;
417 cq_ring_mask = *cring->ring_mask;
421 int main(int argc, char *argv[])
423 struct submitter *s = &submitters[0];
424 unsigned long done, calls, reap, cache_hit, cache_miss;
430 printf("%s: filename\n", argv[0]);
434 rlim.rlim_cur = RLIM_INFINITY;
435 rlim.rlim_max = RLIM_INFINITY;
436 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
443 for (i = 0; i < DEPTH; i++) {
446 if (posix_memalign(&buf, BS, BS)) {
447 printf("failed alloc\n");
450 s->iovecs[i].iov_base = buf;
451 s->iovecs[i].iov_len = BS;
456 printf("ring setup failed: %s, %d\n", strerror(errno), err);
459 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
460 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
461 strcpy(s->filename, argv[1]);
463 pthread_create(&s->thread, NULL, submitter_fn, s);
465 cache_hit = cache_miss = reap = calls = done = 0;
467 unsigned long this_done = 0;
468 unsigned long this_reap = 0;
469 unsigned long this_call = 0;
470 unsigned long this_cache_hit = 0;
471 unsigned long this_cache_miss = 0;
472 unsigned long rpc = 0, ipc = 0;
476 this_done += s->done;
477 this_call += s->calls;
478 this_reap += s->reaps;
479 this_cache_hit += s->cachehit;
480 this_cache_miss += s->cachemiss;
481 if (this_cache_hit && this_cache_miss) {
482 unsigned long hits, total;
484 hits = this_cache_hit - cache_hit;
485 total = hits + this_cache_miss - cache_miss;
486 hit = (double) hits / (double) total;
489 if (this_call - calls) {
490 rpc = (this_done - done) / (this_call - calls);
491 ipc = (this_reap - reap) / (this_call - calls);
493 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
494 this_done - done, rpc, ipc, s->inflight,
495 *s->cq_ring.head, *s->cq_ring.tail, hit);
499 cache_hit = s->cachehit;
500 cache_miss = s->cachemiss;
503 pthread_join(s->thread, &ret);