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 barrier() __asm__ __volatile__("": : :"memory")
35 #define min(a, b) ((a < b) ? (a) : (b))
40 struct aio_iocb_ring {
52 struct aio_io_event_ring {
58 struct io_event pad_event;
60 struct io_event events[0];
63 #define IORING_FLAG_SUBMIT (1 << 0)
64 #define IORING_FLAG_GETEVENTS (1 << 1)
67 #define RING_SIZE (DEPTH + 1)
69 #define BATCH_SUBMIT 8
70 #define BATCH_COMPLETE 8
76 unsigned long max_blocks;
78 struct drand48_data rand;
79 struct aio_iocb_ring *sq_ring;
80 struct aio_io_event_ring *cq_ring;
89 static struct submitter submitters[1];
90 static volatile int finish;
92 static int polled = 1; /* use IO polling */
93 static int fixedbufs = 1; /* use fixed user buffers */
94 static int buffered = 0; /* use buffered IO, not O_DIRECT */
95 static int sq_thread = 0; /* use kernel submission thread */
96 static int sq_thread_cpu = 0; /* pin above thread to this CPU */
98 static int io_setup2(unsigned int nr_events, unsigned int flags,
99 struct iocb *iocbs, struct aio_iocb_ring *sq_ring,
100 struct aio_io_event_ring *cq_ring, io_context_t *ctx_idp)
102 return syscall(335, nr_events, flags, iocbs, sq_ring, cq_ring, ctx_idp);
105 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
106 unsigned int min_complete, unsigned int flags)
108 return syscall(336, ctx, to_submit, min_complete, flags);
111 static int gettid(void)
113 return syscall(__NR_gettid);
116 static void init_io(struct submitter *s, int fd, struct iocb *iocb)
118 unsigned long offset;
121 lrand48_r(&s->rand, &r);
122 offset = (r % (s->max_blocks - 1)) * BS;
124 iocb->aio_fildes = fd;
125 iocb->aio_lio_opcode = IO_CMD_PREAD;
126 iocb->u.c.offset = offset;
128 iocb->u.c.flags = IOCB_FLAG_HIPRI;
130 iocb->u.c.nbytes = BS;
133 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
135 struct aio_iocb_ring *ring = s->sq_ring;
137 u32 tail, next_tail, prepped = 0;
139 next_tail = tail = ring->tail;
142 if (next_tail == ring->nr_events)
146 if (next_tail == ring->head)
149 iocb = &s->sq_ring->iocbs[tail];
150 init_io(s, fd, iocb);
153 } while (prepped < max_ios);
155 if (ring->tail != tail) {
156 /* order tail store with writes to iocbs above */
164 static int get_file_size(int fd, unsigned long *blocks)
168 if (fstat(fd, &st) < 0)
170 if (S_ISBLK(st.st_mode)) {
171 unsigned long long bytes;
173 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
176 *blocks = bytes / BS;
178 } else if (S_ISREG(st.st_mode)) {
179 *blocks = st.st_size / BS;
186 static int reap_events(struct submitter *s)
188 struct aio_io_event_ring *ring = s->cq_ring;
190 u32 head, reaped = 0;
195 if (head == ring->tail)
197 ev = &ring->events[head];
199 int index = (int) (uintptr_t) ev->obj;
200 struct iocb *iocb = &s->sq_ring->iocbs[index];
202 printf("io: unexpected ret=%ld\n", ev->res);
203 printf("offset=%lu, size=%lu\n", (unsigned long) iocb->u.c.offset, (unsigned long) iocb->u.c.nbytes);
208 if (head == ring->nr_events)
212 s->inflight -= reaped;
218 static void *submitter_fn(void *data)
220 struct submitter *s = data;
221 int fd, ret, prepped, flags;
223 printf("submitter=%d\n", gettid());
228 fd = open(s->filename, flags);
234 if (get_file_size(fd, &s->max_blocks)) {
235 printf("failed getting size of device/file\n");
238 if (!s->max_blocks) {
239 printf("Zero file/device size?\n");
245 srand48_r(pthread_self(), &s->rand);
249 int to_wait, flags, to_submit, this_reap;
251 if (!prepped && s->inflight < DEPTH)
252 prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
253 s->inflight += prepped;
257 if (s->inflight + BATCH_SUBMIT < DEPTH)
260 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
262 flags = IORING_FLAG_GETEVENTS;
264 flags |= IORING_FLAG_SUBMIT;
266 ret = io_ring_enter(s->ioc, to_submit, to_wait, flags);
269 this_reap = reap_events(s);
272 s->reaps += this_reap;
280 } else if (ret < to_submit) {
281 int diff = to_submit - ret;
290 } else if (ret < 0) {
291 if ((ret == -1 && errno == EAGAIN) || ret == -EAGAIN) {
300 printf("io_submit: %s\n", strerror(errno));
302 printf("io_submit: %s\n", strerror(-ret));
305 } while (!s->finish);
313 static void sig_int(int sig)
315 printf("Exiting on signal %d\n", sig);
316 submitters[0].finish = 1;
320 static void arm_sig_int(void)
322 struct sigaction act;
324 memset(&act, 0, sizeof(act));
325 act.sa_handler = sig_int;
326 act.sa_flags = SA_RESTART;
327 sigaction(SIGINT, &act, NULL);
330 int main(int argc, char *argv[])
332 struct submitter *s = &submitters[0];
333 unsigned long done, calls, reap;
341 printf("%s: filename\n", argv[0]);
345 rlim.rlim_cur = RLIM_INFINITY;
346 rlim.rlim_max = RLIM_INFINITY;
347 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
354 size = sizeof(struct aio_iocb_ring) + RING_SIZE * sizeof(struct iocb);
355 if (posix_memalign(&p, 4096, size))
358 s->sq_ring->nr_events = RING_SIZE;
361 /* CQ ring must be twice as big */
362 size = sizeof(struct aio_io_event_ring) +
363 2 * RING_SIZE * sizeof(struct io_event);
364 if (posix_memalign(&p, 4096, size))
367 s->cq_ring->nr_events = 2 * RING_SIZE;
370 for (j = 0; j < RING_SIZE; j++) {
371 struct iocb *iocb = &s->sq_ring->iocbs[j];
373 if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
374 printf("failed alloc\n");
377 iocb->u.c.nbytes = BS;
380 flags = IOCTX_FLAG_SCQRING;
382 flags |= IOCTX_FLAG_IOPOLL;
384 flags |= IOCTX_FLAG_FIXEDBUFS;
386 flags |= IOCTX_FLAG_SQWQ;
387 else if (sq_thread) {
388 flags |= IOCTX_FLAG_SQTHREAD;
389 s->sq_ring->sq_thread_cpu = sq_thread_cpu;
392 err = io_setup2(RING_SIZE, flags, s->sq_ring->iocbs, s->sq_ring, s->cq_ring, &s->ioc);
394 printf("ctx_init failed: %s, %d\n", strerror(errno), err);
397 printf("polled=%d, fixedbufs=%d, buffered=%d\n", polled, fixedbufs, buffered);
398 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, s->sq_ring->nr_events, s->cq_ring->nr_events);
399 strcpy(s->filename, argv[1]);
401 pthread_create(&s->thread, NULL, submitter_fn, s);
403 reap = calls = done = 0;
405 unsigned long this_done = 0;
406 unsigned long this_reap = 0;
407 unsigned long this_call = 0;
408 unsigned long rpc = 0, ipc = 0;
411 this_done += s->done;
412 this_call += s->calls;
413 this_reap += s->reaps;
414 if (this_call - calls) {
415 rpc = (this_done - done) / (this_call - calls);
416 ipc = (this_reap - reap) / (this_call - calls);
418 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%d tail=%d), %lu, %lu\n",
419 this_done - done, rpc, ipc, s->inflight,
420 s->cq_ring->head, s->cq_ring->tail, s->reaps, s->done);
426 pthread_join(s->thread, &ret);