engines/aioring: update for continually rolling ring
[fio.git] / t / aio-ring.c
CommitLineData
c9fb4c5b
JA
1/*
2 * gcc -D_GNU_SOURCE -Wall -O2 -o aio-ring aio-ring.c -lpthread -laio
3 */
4#include <stdio.h>
5#include <errno.h>
6#include <assert.h>
7#include <stdlib.h>
8#include <stddef.h>
9#include <signal.h>
10#include <inttypes.h>
11
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <sys/ioctl.h>
15#include <sys/syscall.h>
16#include <sys/resource.h>
17#include <linux/fs.h>
18#include <fcntl.h>
19#include <unistd.h>
20#include <libaio.h>
21#include <string.h>
22#include <pthread.h>
23#include <sched.h>
24
25#define IOCB_FLAG_HIPRI (1 << 2)
26
478a96a8
JA
27#define IOCTX_FLAG_IOPOLL (1 << 0)
28#define IOCTX_FLAG_SCQRING (1 << 1) /* Use SQ/CQ rings */
c9fb4c5b 29#define IOCTX_FLAG_FIXEDBUFS (1 << 2)
478a96a8
JA
30#define IOCTX_FLAG_SQTHREAD (1 << 3) /* Use SQ thread */
31#define IOCTX_FLAG_SQWQ (1 << 4) /* Use SQ wq */
c9fb4c5b 32
305c06ce
JA
33#define IOEV_RES2_CACHEHIT (1 << 0)
34
c9fb4c5b
JA
35#define barrier() __asm__ __volatile__("": : :"memory")
36
37#define min(a, b) ((a < b) ? (a) : (b))
38
fa3e6c25 39typedef uint64_t u64;
c9fb4c5b 40typedef uint32_t u32;
478a96a8 41typedef uint16_t u16;
c9fb4c5b 42
fa3e6c25 43struct aio_sq_ring {
c9fb4c5b
JA
44 union {
45 struct {
fa3e6c25
JA
46 u32 head;
47 u32 tail;
c9fb4c5b 48 u32 nr_events;
478a96a8 49 u16 sq_thread_cpu;
fa3e6c25 50 u64 iocbs;
c9fb4c5b 51 };
fa3e6c25 52 u32 pad[16];
c9fb4c5b 53 };
fa3e6c25 54 u32 array[0];
c9fb4c5b
JA
55};
56
fa3e6c25 57struct aio_cq_ring {
c9fb4c5b
JA
58 union {
59 struct {
fa3e6c25
JA
60 u32 head;
61 u32 tail;
c9fb4c5b
JA
62 u32 nr_events;
63 };
fa3e6c25 64 struct io_event pad;
c9fb4c5b
JA
65 };
66 struct io_event events[0];
67};
68
69#define IORING_FLAG_SUBMIT (1 << 0)
70#define IORING_FLAG_GETEVENTS (1 << 1)
71
c9fb4c5b
JA
72#define DEPTH 32
73#define RING_SIZE (DEPTH + 1)
74
75#define BATCH_SUBMIT 8
76#define BATCH_COMPLETE 8
77
78#define BS 4096
79
80struct submitter {
81 pthread_t thread;
82 unsigned long max_blocks;
83 io_context_t ioc;
84 struct drand48_data rand;
fa3e6c25
JA
85 struct aio_sq_ring *sq_ring;
86 struct iocb *iocbs;
87 struct aio_cq_ring *cq_ring;
c9fb4c5b
JA
88 int inflight;
89 unsigned long reaps;
90 unsigned long done;
91 unsigned long calls;
305c06ce 92 unsigned long cachehit, cachemiss;
c9fb4c5b
JA
93 volatile int finish;
94 char filename[128];
95};
96
97static struct submitter submitters[1];
98static volatile int finish;
99
100static int polled = 1; /* use IO polling */
101static int fixedbufs = 1; /* use fixed user buffers */
102static int buffered = 0; /* use buffered IO, not O_DIRECT */
0527b23f
JA
103static int sq_thread = 0; /* use kernel submission thread */
104static int sq_thread_cpu = 0; /* pin above thread to this CPU */
c9fb4c5b
JA
105
106static int io_setup2(unsigned int nr_events, unsigned int flags,
fa3e6c25
JA
107 struct aio_sq_ring *sq_ring, struct aio_cq_ring *cq_ring,
108 io_context_t *ctx_idp)
c9fb4c5b 109{
bf5dc389 110 return syscall(335, nr_events, flags, sq_ring, cq_ring, ctx_idp);
c9fb4c5b
JA
111}
112
113static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
114 unsigned int min_complete, unsigned int flags)
115{
116 return syscall(336, ctx, to_submit, min_complete, flags);
117}
118
119static int gettid(void)
120{
121 return syscall(__NR_gettid);
122}
123
124static void init_io(struct submitter *s, int fd, struct iocb *iocb)
125{
126 unsigned long offset;
127 long r;
128
129 lrand48_r(&s->rand, &r);
130 offset = (r % (s->max_blocks - 1)) * BS;
131
132 iocb->aio_fildes = fd;
133 iocb->aio_lio_opcode = IO_CMD_PREAD;
134 iocb->u.c.offset = offset;
135 if (polled)
136 iocb->u.c.flags = IOCB_FLAG_HIPRI;
137 if (!fixedbufs)
138 iocb->u.c.nbytes = BS;
139}
140
141static int prep_more_ios(struct submitter *s, int fd, int max_ios)
142{
fa3e6c25 143 struct aio_sq_ring *ring = s->sq_ring;
c9fb4c5b
JA
144 u32 tail, next_tail, prepped = 0;
145
146 next_tail = tail = ring->tail;
147 do {
148 next_tail++;
149 if (next_tail == ring->nr_events)
150 next_tail = 0;
151
152 barrier();
153 if (next_tail == ring->head)
154 break;
155
fa3e6c25
JA
156 init_io(s, fd, &s->iocbs[tail]);
157 s->sq_ring->array[tail] = tail;
c9fb4c5b
JA
158 prepped++;
159 tail = next_tail;
160 } while (prepped < max_ios);
161
162 if (ring->tail != tail) {
163 /* order tail store with writes to iocbs above */
164 barrier();
165 ring->tail = tail;
166 barrier();
167 }
168 return prepped;
169}
170
171static int get_file_size(int fd, unsigned long *blocks)
172{
173 struct stat st;
174
175 if (fstat(fd, &st) < 0)
176 return -1;
177 if (S_ISBLK(st.st_mode)) {
178 unsigned long long bytes;
179
180 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
181 return -1;
182
183 *blocks = bytes / BS;
184 return 0;
185 } else if (S_ISREG(st.st_mode)) {
186 *blocks = st.st_size / BS;
187 return 0;
188 }
189
190 return -1;
191}
192
193static int reap_events(struct submitter *s)
194{
fa3e6c25 195 struct aio_cq_ring *ring = s->cq_ring;
c9fb4c5b
JA
196 struct io_event *ev;
197 u32 head, reaped = 0;
198
199 head = ring->head;
200 do {
201 barrier();
202 if (head == ring->tail)
203 break;
204 ev = &ring->events[head];
205 if (ev->res != BS) {
fa3e6c25 206 struct iocb *iocb = ev->obj;
c9fb4c5b
JA
207
208 printf("io: unexpected ret=%ld\n", ev->res);
209 printf("offset=%lu, size=%lu\n", (unsigned long) iocb->u.c.offset, (unsigned long) iocb->u.c.nbytes);
210 return -1;
211 }
305c06ce
JA
212 if (ev->res2 & IOEV_RES2_CACHEHIT)
213 s->cachehit++;
214 else
215 s->cachemiss++;
c9fb4c5b
JA
216 reaped++;
217 head++;
218 if (head == ring->nr_events)
219 head = 0;
220 } while (1);
221
222 s->inflight -= reaped;
223 ring->head = head;
224 barrier();
225 return reaped;
226}
227
228static void *submitter_fn(void *data)
229{
230 struct submitter *s = data;
231 int fd, ret, prepped, flags;
232
233 printf("submitter=%d\n", gettid());
234
235 flags = O_RDONLY;
236 if (!buffered)
237 flags |= O_DIRECT;
238 fd = open(s->filename, flags);
239 if (fd < 0) {
240 perror("open");
241 goto done;
242 }
243
244 if (get_file_size(fd, &s->max_blocks)) {
245 printf("failed getting size of device/file\n");
246 goto err;
247 }
248 if (!s->max_blocks) {
249 printf("Zero file/device size?\n");
250 goto err;
251 }
252
253 s->max_blocks--;
254
255 srand48_r(pthread_self(), &s->rand);
256
257 prepped = 0;
258 do {
259 int to_wait, flags, to_submit, this_reap;
260
261 if (!prepped && s->inflight < DEPTH)
262 prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
263 s->inflight += prepped;
264submit_more:
265 to_submit = prepped;
266submit:
267 if (s->inflight + BATCH_SUBMIT < DEPTH)
268 to_wait = 0;
269 else
270 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
271
272 flags = IORING_FLAG_GETEVENTS;
273 if (to_submit)
274 flags |= IORING_FLAG_SUBMIT;
275
276 ret = io_ring_enter(s->ioc, to_submit, to_wait, flags);
277 s->calls++;
278
279 this_reap = reap_events(s);
280 if (this_reap == -1)
281 break;
282 s->reaps += this_reap;
283
284 if (ret >= 0) {
285 if (!ret) {
286 to_submit = 0;
287 if (s->inflight)
288 goto submit;
289 continue;
290 } else if (ret < to_submit) {
291 int diff = to_submit - ret;
292
293 s->done += ret;
294 prepped -= diff;
295 goto submit_more;
296 }
297 s->done += ret;
298 prepped = 0;
299 continue;
300 } else if (ret < 0) {
301 if ((ret == -1 && errno == EAGAIN) || ret == -EAGAIN) {
302 if (s->finish)
303 break;
304 if (this_reap)
305 goto submit;
c9fb4c5b
JA
306 to_submit = 0;
307 goto submit;
308 }
309 if (ret == -1)
310 printf("io_submit: %s\n", strerror(errno));
311 else
312 printf("io_submit: %s\n", strerror(-ret));
313 break;
314 }
315 } while (!s->finish);
316err:
317 close(fd);
318done:
319 finish = 1;
320 return NULL;
321}
322
323static void sig_int(int sig)
324{
325 printf("Exiting on signal %d\n", sig);
326 submitters[0].finish = 1;
327 finish = 1;
328}
329
330static void arm_sig_int(void)
331{
332 struct sigaction act;
333
334 memset(&act, 0, sizeof(act));
335 act.sa_handler = sig_int;
336 act.sa_flags = SA_RESTART;
337 sigaction(SIGINT, &act, NULL);
338}
339
340int main(int argc, char *argv[])
341{
342 struct submitter *s = &submitters[0];
305c06ce 343 unsigned long done, calls, reap, cache_hit, cache_miss;
c9fb4c5b
JA
344 int flags = 0, err;
345 int j;
346 size_t size;
347 void *p, *ret;
348 struct rlimit rlim;
349
350 if (argc < 2) {
351 printf("%s: filename\n", argv[0]);
352 return 1;
353 }
354
355 rlim.rlim_cur = RLIM_INFINITY;
356 rlim.rlim_max = RLIM_INFINITY;
357 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
358 perror("setrlimit");
359 return 1;
360 }
361
362 arm_sig_int();
363
fa3e6c25
JA
364 size = sizeof(struct iocb) * RING_SIZE;
365 if (posix_memalign(&p, 4096, size))
366 return 1;
367 memset(p, 0, size);
368 s->iocbs = p;
369
370 size = sizeof(struct aio_sq_ring) + RING_SIZE * sizeof(u32);
c9fb4c5b
JA
371 if (posix_memalign(&p, 4096, size))
372 return 1;
373 s->sq_ring = p;
374 memset(p, 0, size);
0f75d092 375 s->sq_ring->nr_events = RING_SIZE;
fa3e6c25 376 s->sq_ring->iocbs = (u64) s->iocbs;
c9fb4c5b 377
478a96a8 378 /* CQ ring must be twice as big */
fa3e6c25 379 size = sizeof(struct aio_cq_ring) +
478a96a8 380 2 * RING_SIZE * sizeof(struct io_event);
c9fb4c5b
JA
381 if (posix_memalign(&p, 4096, size))
382 return 1;
383 s->cq_ring = p;
384 memset(p, 0, size);
0f75d092 385 s->cq_ring->nr_events = 2 * RING_SIZE;
c9fb4c5b
JA
386
387 for (j = 0; j < RING_SIZE; j++) {
fa3e6c25 388 struct iocb *iocb = &s->iocbs[j];
c9fb4c5b
JA
389
390 if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
391 printf("failed alloc\n");
392 return 1;
393 }
394 iocb->u.c.nbytes = BS;
395 }
396
397 flags = IOCTX_FLAG_SCQRING;
398 if (polled)
399 flags |= IOCTX_FLAG_IOPOLL;
400 if (fixedbufs)
401 flags |= IOCTX_FLAG_FIXEDBUFS;
402 if (buffered)
403 flags |= IOCTX_FLAG_SQWQ;
404 else if (sq_thread) {
405 flags |= IOCTX_FLAG_SQTHREAD;
406 s->sq_ring->sq_thread_cpu = sq_thread_cpu;
407 }
408
bf5dc389 409 err = io_setup2(RING_SIZE, flags, s->sq_ring, s->cq_ring, &s->ioc);
c9fb4c5b
JA
410 if (err) {
411 printf("ctx_init failed: %s, %d\n", strerror(errno), err);
412 return 1;
413 }
414 printf("polled=%d, fixedbufs=%d, buffered=%d\n", polled, fixedbufs, buffered);
415 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, s->sq_ring->nr_events, s->cq_ring->nr_events);
416 strcpy(s->filename, argv[1]);
417
418 pthread_create(&s->thread, NULL, submitter_fn, s);
419
305c06ce 420 cache_hit = cache_miss = reap = calls = done = 0;
c9fb4c5b
JA
421 do {
422 unsigned long this_done = 0;
423 unsigned long this_reap = 0;
424 unsigned long this_call = 0;
305c06ce
JA
425 unsigned long this_cache_hit = 0;
426 unsigned long this_cache_miss = 0;
c9fb4c5b 427 unsigned long rpc = 0, ipc = 0;
305c06ce 428 double hit = 0.0;
c9fb4c5b
JA
429
430 sleep(1);
431 this_done += s->done;
432 this_call += s->calls;
433 this_reap += s->reaps;
305c06ce
JA
434 this_cache_hit += s->cachehit;
435 this_cache_miss += s->cachemiss;
436 if (this_cache_hit && this_cache_miss) {
437 unsigned long hits, total;
438
439 hits = this_cache_hit - cache_hit;
440 total = hits + this_cache_miss - cache_miss;
441 hit = (double) hits / (double) total;
442 hit *= 100.0;
443 }
c9fb4c5b
JA
444 if (this_call - calls) {
445 rpc = (this_done - done) / (this_call - calls);
446 ipc = (this_reap - reap) / (this_call - calls);
447 }
305c06ce 448 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%d tail=%d), Cachehit=%0.2f%%\n",
c9fb4c5b 449 this_done - done, rpc, ipc, s->inflight,
305c06ce 450 s->cq_ring->head, s->cq_ring->tail, hit);
c9fb4c5b
JA
451 done = this_done;
452 calls = this_call;
453 reap = this_reap;
305c06ce
JA
454 cache_hit = s->cachehit;
455 cache_miss = s->cachemiss;
c9fb4c5b
JA
456 } while (!finish);
457
458 pthread_join(s->thread, &ret);
459 return 0;
460}