engines/aioring: fix harmless typo
[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 72#define DEPTH 32
c9fb4c5b
JA
73
74#define BATCH_SUBMIT 8
75#define BATCH_COMPLETE 8
76
77#define BS 4096
78
e39c34dc
JA
79static unsigned sq_ring_mask = DEPTH - 1;
80static unsigned cq_ring_mask = (2 * DEPTH) - 1;
81
c9fb4c5b
JA
82struct submitter {
83 pthread_t thread;
84 unsigned long max_blocks;
85 io_context_t ioc;
86 struct drand48_data rand;
fa3e6c25
JA
87 struct aio_sq_ring *sq_ring;
88 struct iocb *iocbs;
89 struct aio_cq_ring *cq_ring;
c9fb4c5b
JA
90 int inflight;
91 unsigned long reaps;
92 unsigned long done;
93 unsigned long calls;
305c06ce 94 unsigned long cachehit, cachemiss;
c9fb4c5b
JA
95 volatile int finish;
96 char filename[128];
97};
98
99static struct submitter submitters[1];
100static volatile int finish;
101
102static int polled = 1; /* use IO polling */
103static int fixedbufs = 1; /* use fixed user buffers */
104static int buffered = 0; /* use buffered IO, not O_DIRECT */
0527b23f
JA
105static int sq_thread = 0; /* use kernel submission thread */
106static int sq_thread_cpu = 0; /* pin above thread to this CPU */
c9fb4c5b
JA
107
108static int io_setup2(unsigned int nr_events, unsigned int flags,
fa3e6c25
JA
109 struct aio_sq_ring *sq_ring, struct aio_cq_ring *cq_ring,
110 io_context_t *ctx_idp)
c9fb4c5b 111{
bf5dc389 112 return syscall(335, nr_events, flags, sq_ring, cq_ring, ctx_idp);
c9fb4c5b
JA
113}
114
115static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
116 unsigned int min_complete, unsigned int flags)
117{
118 return syscall(336, ctx, to_submit, min_complete, flags);
119}
120
121static int gettid(void)
122{
123 return syscall(__NR_gettid);
124}
125
126static void init_io(struct submitter *s, int fd, struct iocb *iocb)
127{
128 unsigned long offset;
129 long r;
130
131 lrand48_r(&s->rand, &r);
132 offset = (r % (s->max_blocks - 1)) * BS;
133
134 iocb->aio_fildes = fd;
135 iocb->aio_lio_opcode = IO_CMD_PREAD;
136 iocb->u.c.offset = offset;
137 if (polled)
138 iocb->u.c.flags = IOCB_FLAG_HIPRI;
139 if (!fixedbufs)
140 iocb->u.c.nbytes = BS;
141}
142
143static int prep_more_ios(struct submitter *s, int fd, int max_ios)
144{
fa3e6c25 145 struct aio_sq_ring *ring = s->sq_ring;
e39c34dc 146 u32 index, tail, next_tail, prepped = 0;
c9fb4c5b
JA
147
148 next_tail = tail = ring->tail;
149 do {
150 next_tail++;
c9fb4c5b
JA
151 barrier();
152 if (next_tail == ring->head)
153 break;
154
e39c34dc
JA
155 index = tail & sq_ring_mask;
156 init_io(s, fd, &s->iocbs[index]);
157 s->sq_ring->array[index] = index;
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;
e39c34dc 204 ev = &ring->events[head & cq_ring_mask];
c9fb4c5b 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++;
c9fb4c5b
JA
218 } while (1);
219
220 s->inflight -= reaped;
221 ring->head = head;
222 barrier();
223 return reaped;
224}
225
226static void *submitter_fn(void *data)
227{
228 struct submitter *s = data;
229 int fd, ret, prepped, flags;
230
231 printf("submitter=%d\n", gettid());
232
233 flags = O_RDONLY;
234 if (!buffered)
235 flags |= O_DIRECT;
236 fd = open(s->filename, flags);
237 if (fd < 0) {
238 perror("open");
239 goto done;
240 }
241
242 if (get_file_size(fd, &s->max_blocks)) {
243 printf("failed getting size of device/file\n");
244 goto err;
245 }
246 if (!s->max_blocks) {
247 printf("Zero file/device size?\n");
248 goto err;
249 }
250
251 s->max_blocks--;
252
253 srand48_r(pthread_self(), &s->rand);
254
255 prepped = 0;
256 do {
257 int to_wait, flags, to_submit, this_reap;
258
259 if (!prepped && s->inflight < DEPTH)
260 prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
261 s->inflight += prepped;
262submit_more:
263 to_submit = prepped;
264submit:
265 if (s->inflight + BATCH_SUBMIT < DEPTH)
266 to_wait = 0;
267 else
268 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
269
270 flags = IORING_FLAG_GETEVENTS;
271 if (to_submit)
272 flags |= IORING_FLAG_SUBMIT;
273
274 ret = io_ring_enter(s->ioc, to_submit, to_wait, flags);
275 s->calls++;
276
277 this_reap = reap_events(s);
278 if (this_reap == -1)
279 break;
280 s->reaps += this_reap;
281
282 if (ret >= 0) {
283 if (!ret) {
284 to_submit = 0;
285 if (s->inflight)
286 goto submit;
287 continue;
288 } else if (ret < to_submit) {
289 int diff = to_submit - ret;
290
291 s->done += ret;
292 prepped -= diff;
293 goto submit_more;
294 }
295 s->done += ret;
296 prepped = 0;
297 continue;
298 } else if (ret < 0) {
299 if ((ret == -1 && errno == EAGAIN) || ret == -EAGAIN) {
300 if (s->finish)
301 break;
302 if (this_reap)
303 goto submit;
c9fb4c5b
JA
304 to_submit = 0;
305 goto submit;
306 }
307 if (ret == -1)
308 printf("io_submit: %s\n", strerror(errno));
309 else
310 printf("io_submit: %s\n", strerror(-ret));
311 break;
312 }
313 } while (!s->finish);
314err:
315 close(fd);
316done:
317 finish = 1;
318 return NULL;
319}
320
321static void sig_int(int sig)
322{
323 printf("Exiting on signal %d\n", sig);
324 submitters[0].finish = 1;
325 finish = 1;
326}
327
328static void arm_sig_int(void)
329{
330 struct sigaction act;
331
332 memset(&act, 0, sizeof(act));
333 act.sa_handler = sig_int;
334 act.sa_flags = SA_RESTART;
335 sigaction(SIGINT, &act, NULL);
336}
337
338int main(int argc, char *argv[])
339{
340 struct submitter *s = &submitters[0];
305c06ce 341 unsigned long done, calls, reap, cache_hit, cache_miss;
c9fb4c5b
JA
342 int flags = 0, err;
343 int j;
344 size_t size;
345 void *p, *ret;
346 struct rlimit rlim;
347
348 if (argc < 2) {
349 printf("%s: filename\n", argv[0]);
350 return 1;
351 }
352
353 rlim.rlim_cur = RLIM_INFINITY;
354 rlim.rlim_max = RLIM_INFINITY;
355 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
356 perror("setrlimit");
357 return 1;
358 }
359
360 arm_sig_int();
361
e39c34dc 362 size = sizeof(struct iocb) * DEPTH;
fa3e6c25
JA
363 if (posix_memalign(&p, 4096, size))
364 return 1;
365 memset(p, 0, size);
366 s->iocbs = p;
367
e39c34dc 368 size = sizeof(struct aio_sq_ring) + DEPTH * sizeof(u32);
c9fb4c5b
JA
369 if (posix_memalign(&p, 4096, size))
370 return 1;
371 s->sq_ring = p;
372 memset(p, 0, size);
e39c34dc 373 s->sq_ring->nr_events = DEPTH;
fa3e6c25 374 s->sq_ring->iocbs = (u64) s->iocbs;
c9fb4c5b 375
478a96a8 376 /* CQ ring must be twice as big */
fa3e6c25 377 size = sizeof(struct aio_cq_ring) +
e39c34dc 378 2 * DEPTH * sizeof(struct io_event);
c9fb4c5b
JA
379 if (posix_memalign(&p, 4096, size))
380 return 1;
381 s->cq_ring = p;
382 memset(p, 0, size);
e39c34dc 383 s->cq_ring->nr_events = 2 * DEPTH;
c9fb4c5b 384
e39c34dc 385 for (j = 0; j < DEPTH; j++) {
fa3e6c25 386 struct iocb *iocb = &s->iocbs[j];
c9fb4c5b
JA
387
388 if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
389 printf("failed alloc\n");
390 return 1;
391 }
392 iocb->u.c.nbytes = BS;
393 }
394
395 flags = IOCTX_FLAG_SCQRING;
396 if (polled)
397 flags |= IOCTX_FLAG_IOPOLL;
398 if (fixedbufs)
399 flags |= IOCTX_FLAG_FIXEDBUFS;
400 if (buffered)
401 flags |= IOCTX_FLAG_SQWQ;
402 else if (sq_thread) {
403 flags |= IOCTX_FLAG_SQTHREAD;
404 s->sq_ring->sq_thread_cpu = sq_thread_cpu;
405 }
406
e39c34dc 407 err = io_setup2(DEPTH, flags, s->sq_ring, s->cq_ring, &s->ioc);
c9fb4c5b
JA
408 if (err) {
409 printf("ctx_init failed: %s, %d\n", strerror(errno), err);
410 return 1;
411 }
412 printf("polled=%d, fixedbufs=%d, buffered=%d\n", polled, fixedbufs, buffered);
413 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, s->sq_ring->nr_events, s->cq_ring->nr_events);
414 strcpy(s->filename, argv[1]);
415
416 pthread_create(&s->thread, NULL, submitter_fn, s);
417
305c06ce 418 cache_hit = cache_miss = reap = calls = done = 0;
c9fb4c5b
JA
419 do {
420 unsigned long this_done = 0;
421 unsigned long this_reap = 0;
422 unsigned long this_call = 0;
305c06ce
JA
423 unsigned long this_cache_hit = 0;
424 unsigned long this_cache_miss = 0;
c9fb4c5b 425 unsigned long rpc = 0, ipc = 0;
305c06ce 426 double hit = 0.0;
c9fb4c5b
JA
427
428 sleep(1);
429 this_done += s->done;
430 this_call += s->calls;
431 this_reap += s->reaps;
305c06ce
JA
432 this_cache_hit += s->cachehit;
433 this_cache_miss += s->cachemiss;
434 if (this_cache_hit && this_cache_miss) {
435 unsigned long hits, total;
436
437 hits = this_cache_hit - cache_hit;
438 total = hits + this_cache_miss - cache_miss;
439 hit = (double) hits / (double) total;
440 hit *= 100.0;
441 }
c9fb4c5b
JA
442 if (this_call - calls) {
443 rpc = (this_done - done) / (this_call - calls);
444 ipc = (this_reap - reap) / (this_call - calls);
445 }
305c06ce 446 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%d tail=%d), Cachehit=%0.2f%%\n",
c9fb4c5b 447 this_done - done, rpc, ipc, s->inflight,
305c06ce 448 s->cq_ring->head, s->cq_ring->tail, hit);
c9fb4c5b
JA
449 done = this_done;
450 calls = this_call;
451 reap = this_reap;
305c06ce
JA
452 cache_hit = s->cachehit;
453 cache_miss = s->cachemiss;
c9fb4c5b
JA
454 } while (!finish);
455
456 pthread_join(s->thread, &ret);
457 return 0;
458}