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))
64 struct io_event events[0];
67 #define IORING_FLAG_SUBMIT (1 << 0)
68 #define IORING_FLAG_GETEVENTS (1 << 1)
71 #define RING_SIZE (DEPTH + 1)
73 #define BATCH_SUBMIT 8
74 #define BATCH_COMPLETE 8
80 unsigned long max_blocks;
82 struct drand48_data rand;
83 struct aio_sq_ring *sq_ring;
85 struct aio_cq_ring *cq_ring;
94 static struct submitter submitters[1];
95 static volatile int finish;
97 static int polled = 1; /* use IO polling */
98 static int fixedbufs = 1; /* use fixed user buffers */
99 static int buffered = 0; /* use buffered IO, not O_DIRECT */
100 static int sq_thread = 0; /* use kernel submission thread */
101 static int sq_thread_cpu = 0; /* pin above thread to this CPU */
103 static int io_setup2(unsigned int nr_events, unsigned int flags,
104 struct aio_sq_ring *sq_ring, struct aio_cq_ring *cq_ring,
105 io_context_t *ctx_idp)
107 return syscall(335, nr_events, flags, sq_ring, cq_ring, ctx_idp);
110 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
111 unsigned int min_complete, unsigned int flags)
113 return syscall(336, ctx, to_submit, min_complete, flags);
116 static int gettid(void)
118 return syscall(__NR_gettid);
121 static void init_io(struct submitter *s, int fd, struct iocb *iocb)
123 unsigned long offset;
126 lrand48_r(&s->rand, &r);
127 offset = (r % (s->max_blocks - 1)) * BS;
129 iocb->aio_fildes = fd;
130 iocb->aio_lio_opcode = IO_CMD_PREAD;
131 iocb->u.c.offset = offset;
133 iocb->u.c.flags = IOCB_FLAG_HIPRI;
135 iocb->u.c.nbytes = BS;
138 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
140 struct aio_sq_ring *ring = s->sq_ring;
141 u32 tail, next_tail, prepped = 0;
143 next_tail = tail = ring->tail;
146 if (next_tail == ring->nr_events)
150 if (next_tail == ring->head)
153 init_io(s, fd, &s->iocbs[tail]);
154 s->sq_ring->array[tail] = tail;
157 } while (prepped < max_ios);
159 if (ring->tail != tail) {
160 /* order tail store with writes to iocbs above */
168 static int get_file_size(int fd, unsigned long *blocks)
172 if (fstat(fd, &st) < 0)
174 if (S_ISBLK(st.st_mode)) {
175 unsigned long long bytes;
177 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
180 *blocks = bytes / BS;
182 } else if (S_ISREG(st.st_mode)) {
183 *blocks = st.st_size / BS;
190 static int reap_events(struct submitter *s)
192 struct aio_cq_ring *ring = s->cq_ring;
194 u32 head, reaped = 0;
199 if (head == ring->tail)
201 ev = &ring->events[head];
203 struct iocb *iocb = ev->obj;
205 printf("io: unexpected ret=%ld\n", ev->res);
206 printf("offset=%lu, size=%lu\n", (unsigned long) iocb->u.c.offset, (unsigned long) iocb->u.c.nbytes);
211 if (head == ring->nr_events)
215 s->inflight -= reaped;
221 static void *submitter_fn(void *data)
223 struct submitter *s = data;
224 int fd, ret, prepped, flags;
226 printf("submitter=%d\n", gettid());
231 fd = open(s->filename, flags);
237 if (get_file_size(fd, &s->max_blocks)) {
238 printf("failed getting size of device/file\n");
241 if (!s->max_blocks) {
242 printf("Zero file/device size?\n");
248 srand48_r(pthread_self(), &s->rand);
252 int to_wait, flags, to_submit, this_reap;
254 if (!prepped && s->inflight < DEPTH)
255 prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
256 s->inflight += prepped;
260 if (s->inflight + BATCH_SUBMIT < DEPTH)
263 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
265 flags = IORING_FLAG_GETEVENTS;
267 flags |= IORING_FLAG_SUBMIT;
269 ret = io_ring_enter(s->ioc, to_submit, to_wait, flags);
272 this_reap = reap_events(s);
275 s->reaps += this_reap;
283 } else if (ret < to_submit) {
284 int diff = to_submit - ret;
293 } else if (ret < 0) {
294 if ((ret == -1 && errno == EAGAIN) || ret == -EAGAIN) {
303 printf("io_submit: %s\n", strerror(errno));
305 printf("io_submit: %s\n", strerror(-ret));
308 } while (!s->finish);
316 static void sig_int(int sig)
318 printf("Exiting on signal %d\n", sig);
319 submitters[0].finish = 1;
323 static void arm_sig_int(void)
325 struct sigaction act;
327 memset(&act, 0, sizeof(act));
328 act.sa_handler = sig_int;
329 act.sa_flags = SA_RESTART;
330 sigaction(SIGINT, &act, NULL);
333 int main(int argc, char *argv[])
335 struct submitter *s = &submitters[0];
336 unsigned long done, calls, reap;
344 printf("%s: filename\n", argv[0]);
348 rlim.rlim_cur = RLIM_INFINITY;
349 rlim.rlim_max = RLIM_INFINITY;
350 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
357 size = sizeof(struct iocb) * RING_SIZE;
358 if (posix_memalign(&p, 4096, size))
363 size = sizeof(struct aio_sq_ring) + RING_SIZE * sizeof(u32);
364 if (posix_memalign(&p, 4096, size))
368 s->sq_ring->nr_events = RING_SIZE;
369 s->sq_ring->iocbs = (u64) s->iocbs;
371 /* CQ ring must be twice as big */
372 size = sizeof(struct aio_cq_ring) +
373 2 * RING_SIZE * sizeof(struct io_event);
374 if (posix_memalign(&p, 4096, size))
378 s->cq_ring->nr_events = 2 * RING_SIZE;
380 for (j = 0; j < RING_SIZE; j++) {
381 struct iocb *iocb = &s->iocbs[j];
383 if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
384 printf("failed alloc\n");
387 iocb->u.c.nbytes = BS;
390 flags = IOCTX_FLAG_SCQRING;
392 flags |= IOCTX_FLAG_IOPOLL;
394 flags |= IOCTX_FLAG_FIXEDBUFS;
396 flags |= IOCTX_FLAG_SQWQ;
397 else if (sq_thread) {
398 flags |= IOCTX_FLAG_SQTHREAD;
399 s->sq_ring->sq_thread_cpu = sq_thread_cpu;
402 err = io_setup2(RING_SIZE, flags, s->sq_ring, s->cq_ring, &s->ioc);
404 printf("ctx_init failed: %s, %d\n", strerror(errno), err);
407 printf("polled=%d, fixedbufs=%d, buffered=%d\n", polled, fixedbufs, buffered);
408 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, s->sq_ring->nr_events, s->cq_ring->nr_events);
409 strcpy(s->filename, argv[1]);
411 pthread_create(&s->thread, NULL, submitter_fn, s);
413 reap = calls = done = 0;
415 unsigned long this_done = 0;
416 unsigned long this_reap = 0;
417 unsigned long this_call = 0;
418 unsigned long rpc = 0, ipc = 0;
421 this_done += s->done;
422 this_call += s->calls;
423 this_reap += s->reaps;
424 if (this_call - calls) {
425 rpc = (this_done - done) / (this_call - calls);
426 ipc = (this_reap - reap) / (this_call - calls);
428 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%d tail=%d), %lu, %lu\n",
429 this_done - done, rpc, ipc, s->inflight,
430 s->cq_ring->head, s->cq_ring->tail, s->reaps, s->done);
436 pthread_join(s->thread, &ret);