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 << 1)
28 #define IOCTX_FLAG_USERIOCB (1 << 0)
29 #define IOCTX_FLAG_FIXEDBUFS (1 << 2)
30 #define IOCTX_FLAG_SCQRING (1 << 3) /* Use SQ/CQ rings */
31 #define IOCTX_FLAG_SQTHREAD (1 << 4) /* Use SQ thread */
32 #define IOCTX_FLAG_SQWQ (1 << 5) /* Use SQ wq */
34 #define barrier() __asm__ __volatile__("": : :"memory")
36 #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)
66 #define container_of(ptr, type, member) ({ \
67 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
68 (type *)( (char *)__mptr - offsetof(type,member) );})
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_iocb_ring *sq_ring;
84 struct aio_io_event_ring *cq_ring;
93 static struct submitter submitters[1];
94 static volatile int finish;
96 static int polled = 1; /* use IO polling */
97 static int fixedbufs = 1; /* use fixed user buffers */
98 static int buffered = 0; /* use buffered IO, not O_DIRECT */
99 static int sq_thread = 0;
100 static int sq_thread_cpu = 0;
102 static int io_setup2(unsigned int nr_events, unsigned int flags,
103 struct iocb *iocbs, struct aio_iocb_ring *sq_ring,
104 struct aio_io_event_ring *cq_ring, io_context_t *ctx_idp)
106 return syscall(335, nr_events, flags, iocbs, sq_ring, cq_ring, ctx_idp);
109 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
110 unsigned int min_complete, unsigned int flags)
112 return syscall(336, ctx, to_submit, min_complete, flags);
115 static int gettid(void)
117 return syscall(__NR_gettid);
120 static void init_io(struct submitter *s, int fd, struct iocb *iocb)
122 unsigned long offset;
125 lrand48_r(&s->rand, &r);
126 offset = (r % (s->max_blocks - 1)) * BS;
128 iocb->aio_fildes = fd;
129 iocb->aio_lio_opcode = IO_CMD_PREAD;
130 iocb->u.c.offset = offset;
132 iocb->u.c.flags = IOCB_FLAG_HIPRI;
134 iocb->u.c.nbytes = BS;
137 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
139 struct aio_iocb_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 iocb = &s->sq_ring->iocbs[tail];
154 init_io(s, fd, iocb);
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_io_event_ring *ring = s->cq_ring;
194 u32 head, reaped = 0;
199 if (head == ring->tail)
201 ev = &ring->events[head];
203 int index = (int) (uintptr_t) ev->obj;
204 struct iocb *iocb = &s->sq_ring->iocbs[index];
206 printf("io: unexpected ret=%ld\n", ev->res);
207 printf("offset=%lu, size=%lu\n", (unsigned long) iocb->u.c.offset, (unsigned long) iocb->u.c.nbytes);
212 if (head == ring->nr_events)
216 s->inflight -= reaped;
222 static void *submitter_fn(void *data)
224 struct submitter *s = data;
225 int fd, ret, prepped, flags;
227 printf("submitter=%d\n", gettid());
232 fd = open(s->filename, flags);
238 if (get_file_size(fd, &s->max_blocks)) {
239 printf("failed getting size of device/file\n");
242 if (!s->max_blocks) {
243 printf("Zero file/device size?\n");
249 srand48_r(pthread_self(), &s->rand);
253 int to_wait, flags, to_submit, this_reap;
255 if (!prepped && s->inflight < DEPTH)
256 prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
257 s->inflight += prepped;
261 if (s->inflight + BATCH_SUBMIT < DEPTH)
264 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
266 flags = IORING_FLAG_GETEVENTS;
268 flags |= IORING_FLAG_SUBMIT;
270 ret = io_ring_enter(s->ioc, to_submit, to_wait, flags);
273 this_reap = reap_events(s);
276 s->reaps += this_reap;
284 } else if (ret < to_submit) {
285 int diff = to_submit - ret;
294 } else if (ret < 0) {
295 if ((ret == -1 && errno == EAGAIN) || ret == -EAGAIN) {
300 printf("EAGAIN reap=%d\n", reap_events(s));
301 printf("%d/%d, %d/%d\n", s->sq_ring->head, s->sq_ring->tail, s->cq_ring->head, s->cq_ring->tail);
306 printf("io_submit: %s\n", strerror(errno));
308 printf("io_submit: %s\n", strerror(-ret));
311 } while (!s->finish);
319 static void sig_int(int sig)
321 printf("Exiting on signal %d\n", sig);
322 submitters[0].finish = 1;
326 static void arm_sig_int(void)
328 struct sigaction act;
330 memset(&act, 0, sizeof(act));
331 act.sa_handler = sig_int;
332 act.sa_flags = SA_RESTART;
333 sigaction(SIGINT, &act, NULL);
336 int main(int argc, char *argv[])
338 struct submitter *s = &submitters[0];
339 unsigned long done, calls, reap;
347 printf("%s: filename\n", argv[0]);
351 rlim.rlim_cur = RLIM_INFINITY;
352 rlim.rlim_max = RLIM_INFINITY;
353 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
360 size = sizeof(struct aio_iocb_ring) + RING_SIZE * sizeof(struct iocb);
361 if (posix_memalign(&p, 4096, size))
366 size = sizeof(struct aio_io_event_ring) + RING_SIZE * sizeof(struct io_event);
367 if (posix_memalign(&p, 4096, size))
372 for (j = 0; j < RING_SIZE; j++) {
373 struct iocb *iocb = &s->sq_ring->iocbs[j];
375 if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
376 printf("failed alloc\n");
379 iocb->u.c.nbytes = BS;
382 flags = IOCTX_FLAG_SCQRING;
384 flags |= IOCTX_FLAG_IOPOLL;
386 flags |= IOCTX_FLAG_FIXEDBUFS;
388 flags |= IOCTX_FLAG_SQWQ;
389 else if (sq_thread) {
390 flags |= IOCTX_FLAG_SQTHREAD;
391 s->sq_ring->sq_thread_cpu = sq_thread_cpu;
394 err = io_setup2(RING_SIZE, flags, s->sq_ring->iocbs, s->sq_ring, s->cq_ring, &s->ioc);
396 printf("ctx_init failed: %s, %d\n", strerror(errno), err);
399 printf("polled=%d, fixedbufs=%d, buffered=%d\n", polled, fixedbufs, buffered);
400 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, s->sq_ring->nr_events, s->cq_ring->nr_events);
401 strcpy(s->filename, argv[1]);
403 pthread_create(&s->thread, NULL, submitter_fn, s);
405 reap = calls = done = 0;
407 unsigned long this_done = 0;
408 unsigned long this_reap = 0;
409 unsigned long this_call = 0;
410 unsigned long rpc = 0, ipc = 0;
413 this_done += s->done;
414 this_call += s->calls;
415 this_reap += s->reaps;
416 if (this_call - calls) {
417 rpc = (this_done - done) / (this_call - calls);
418 ipc = (this_reap - reap) / (this_call - calls);
420 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%d tail=%d), %lu, %lu\n",
421 this_done - done, rpc, ipc, s->inflight,
422 s->cq_ring->head, s->cq_ring->tail, s->reaps, s->done);
428 pthread_join(s->thread, &ret);