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 {
58 struct aio_cqring_offsets {
67 struct aio_uring_params {
73 struct aio_sqring_offsets sq_off;
74 struct aio_cqring_offsets cq_off;
90 struct io_event *events;
93 #define IORING_ENTER_GETEVENTS (1 << 0)
97 #define BATCH_SUBMIT 8
98 #define BATCH_COMPLETE 8
102 static unsigned sq_ring_mask, cq_ring_mask;
106 unsigned long max_blocks;
108 struct drand48_data rand;
109 struct aio_sq_ring sq_ring;
111 struct iovec iovecs[DEPTH];
112 struct aio_cq_ring cq_ring;
117 unsigned long cachehit, cachemiss;
122 static struct submitter submitters[1];
123 static volatile int finish;
125 static int polled = 0; /* use IO polling */
126 static int fixedbufs = 0; /* use fixed user buffers */
127 static int buffered = 1; /* use buffered IO, not O_DIRECT */
128 static int sq_thread = 0; /* use kernel submission thread */
129 static int sq_thread_cpu = 0; /* pin above thread to this CPU */
131 static int io_uring_setup(unsigned entries, struct iovec *iovecs,
132 struct aio_uring_params *p)
134 return syscall(__NR_sys_io_uring_setup, entries, iovecs, p);
137 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
138 unsigned int min_complete, unsigned int flags)
140 return syscall(__NR_sys_io_uring_enter, s->fd, to_submit, min_complete,
144 static int gettid(void)
146 return syscall(__NR_gettid);
149 static void init_io(struct submitter *s, int fd, unsigned index)
151 struct iocb *iocb = &s->iocbs[index];
152 unsigned long offset;
155 lrand48_r(&s->rand, &r);
156 offset = (r % (s->max_blocks - 1)) * BS;
158 iocb->aio_fildes = fd;
159 iocb->aio_lio_opcode = IO_CMD_PREAD;
160 iocb->u.c.buf = s->iovecs[index].iov_base;
161 iocb->u.c.nbytes = BS;
162 iocb->u.c.offset = offset;
165 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
167 struct aio_sq_ring *ring = &s->sq_ring;
168 u32 index, tail, next_tail, prepped = 0;
170 next_tail = tail = *ring->tail;
174 if (next_tail == *ring->head)
177 index = tail & sq_ring_mask;
178 init_io(s, fd, index);
179 ring->array[index] = index;
182 } while (prepped < max_ios);
184 if (*ring->tail != tail) {
185 /* order tail store with writes to iocbs above */
193 static int get_file_size(int fd, unsigned long *blocks)
197 if (fstat(fd, &st) < 0)
199 if (S_ISBLK(st.st_mode)) {
200 unsigned long long bytes;
202 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
205 *blocks = bytes / BS;
207 } else if (S_ISREG(st.st_mode)) {
208 *blocks = st.st_size / BS;
215 static int reap_events(struct submitter *s)
217 struct aio_cq_ring *ring = &s->cq_ring;
219 u32 head, reaped = 0;
224 if (head == *ring->tail)
226 ev = &ring->events[head & cq_ring_mask];
228 struct iocb *iocb = ev->obj;
230 printf("io: unexpected ret=%ld\n", ev->res);
231 printf("offset=%lu, size=%lu\n", (unsigned long) iocb->u.c.offset, (unsigned long) iocb->u.c.nbytes);
234 if (ev->res2 & IOEV_RES2_CACHEHIT)
242 s->inflight -= reaped;
248 static void *submitter_fn(void *data)
250 struct submitter *s = data;
251 int fd, ret, prepped, flags;
253 printf("submitter=%d\n", gettid());
258 fd = open(s->filename, flags);
264 if (get_file_size(fd, &s->max_blocks)) {
265 printf("failed getting size of device/file\n");
268 if (!s->max_blocks) {
269 printf("Zero file/device size?\n");
275 srand48_r(pthread_self(), &s->rand);
279 int to_wait, to_submit, this_reap;
281 if (!prepped && s->inflight < DEPTH)
282 prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
283 s->inflight += prepped;
287 if (s->inflight + BATCH_SUBMIT < DEPTH)
290 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
292 ret = io_uring_enter(s, to_submit, to_wait, IORING_ENTER_GETEVENTS);
295 this_reap = reap_events(s);
298 s->reaps += this_reap;
306 } else if (ret < to_submit) {
307 int diff = to_submit - ret;
316 } else if (ret < 0) {
317 if (errno == EAGAIN) {
325 printf("io_submit: %s\n", strerror(errno));
328 } while (!s->finish);
336 static void sig_int(int sig)
338 printf("Exiting on signal %d\n", sig);
339 submitters[0].finish = 1;
343 static void arm_sig_int(void)
345 struct sigaction act;
347 memset(&act, 0, sizeof(act));
348 act.sa_handler = sig_int;
349 act.sa_flags = SA_RESTART;
350 sigaction(SIGINT, &act, NULL);
353 static int setup_ring(struct submitter *s)
355 struct aio_sq_ring *sring = &s->sq_ring;
356 struct aio_cq_ring *cring = &s->cq_ring;
357 struct aio_uring_params p;
361 memset(&p, 0, sizeof(p));
363 p.flags = IOCTX_FLAG_SCQRING;
365 p.flags |= IOCTX_FLAG_IOPOLL;
367 p.flags |= IOCTX_FLAG_FIXEDBUFS;
369 p.flags |= IOCTX_FLAG_SQWQ;
370 else if (sq_thread) {
371 p.flags |= IOCTX_FLAG_SQTHREAD;
372 p.sq_thread_cpu = sq_thread_cpu;
376 fd = io_uring_setup(DEPTH, s->iovecs, &p);
378 fd = io_uring_setup(DEPTH, NULL, &p);
380 perror("io_uring_setup");
386 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(u32),
387 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
388 fd, IORING_OFF_SQ_RING);
389 printf("sq_ring ptr = 0x%p\n", ptr);
390 sring->head = ptr + p.sq_off.head;
391 sring->tail = ptr + p.sq_off.tail;
392 sring->ring_mask = ptr + p.sq_off.ring_mask;
393 sring->ring_entries = ptr + p.sq_off.ring_entries;
394 sring->array = ptr + p.sq_off.array;
395 sq_ring_mask = *sring->ring_mask;
397 s->iocbs = mmap(0, p.sq_entries * sizeof(struct iocb), PROT_READ | PROT_WRITE,
398 MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_IOCB);
399 printf("iocbs ptr = 0x%p\n", s->iocbs);
401 ptr = mmap(0, p.cq_off.events + p.cq_entries * sizeof(struct io_event),
402 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
403 fd, IORING_OFF_CQ_RING);
404 printf("cq_ring ptr = 0x%p\n", ptr);
405 cring->head = ptr + p.cq_off.head;
406 cring->tail = ptr + p.cq_off.tail;
407 cring->ring_mask = ptr + p.cq_off.ring_mask;
408 cring->ring_entries = ptr + p.cq_off.ring_entries;
409 cring->events = ptr + p.cq_off.events;
410 cq_ring_mask = *cring->ring_mask;
414 int main(int argc, char *argv[])
416 struct submitter *s = &submitters[0];
417 unsigned long done, calls, reap, cache_hit, cache_miss;
423 printf("%s: filename\n", argv[0]);
427 rlim.rlim_cur = RLIM_INFINITY;
428 rlim.rlim_max = RLIM_INFINITY;
429 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
436 for (i = 0; i < DEPTH; i++) {
439 if (posix_memalign(&buf, BS, BS)) {
440 printf("failed alloc\n");
443 s->iovecs[i].iov_base = buf;
444 s->iovecs[i].iov_len = BS;
449 printf("ring setup failed: %s, %d\n", strerror(errno), err);
452 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
453 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
454 strcpy(s->filename, argv[1]);
456 pthread_create(&s->thread, NULL, submitter_fn, s);
458 cache_hit = cache_miss = reap = calls = done = 0;
460 unsigned long this_done = 0;
461 unsigned long this_reap = 0;
462 unsigned long this_call = 0;
463 unsigned long this_cache_hit = 0;
464 unsigned long this_cache_miss = 0;
465 unsigned long rpc = 0, ipc = 0;
469 this_done += s->done;
470 this_call += s->calls;
471 this_reap += s->reaps;
472 this_cache_hit += s->cachehit;
473 this_cache_miss += s->cachemiss;
474 if (this_cache_hit && this_cache_miss) {
475 unsigned long hits, total;
477 hits = this_cache_hit - cache_hit;
478 total = hits + this_cache_miss - cache_miss;
479 hit = (double) hits / (double) total;
482 if (this_call - calls) {
483 rpc = (this_done - done) / (this_call - calls);
484 ipc = (this_reap - reap) / (this_call - calls);
486 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
487 this_done - done, rpc, ipc, s->inflight,
488 *s->cq_ring.head, *s->cq_ring.tail, hit);
492 cache_hit = s->cachehit;
493 cache_miss = s->cachemiss;
496 pthread_join(s->thread, &ret);