Add cache hit stats
[fio.git] / t / aio-ring.c
... / ...
CommitLineData
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
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 */
32
33#define barrier() __asm__ __volatile__("": : :"memory")
34
35#define min(a, b) ((a < b) ? (a) : (b))
36
37typedef uint64_t u64;
38typedef uint32_t u32;
39typedef uint16_t u16;
40
41struct aio_sq_ring {
42 union {
43 struct {
44 u32 head;
45 u32 tail;
46 u32 nr_events;
47 u16 sq_thread_cpu;
48 u64 iocbs;
49 };
50 u32 pad[16];
51 };
52 u32 array[0];
53};
54
55struct aio_cq_ring {
56 union {
57 struct {
58 u32 head;
59 u32 tail;
60 u32 nr_events;
61 };
62 struct io_event pad;
63 };
64 struct io_event events[0];
65};
66
67#define IORING_FLAG_SUBMIT (1 << 0)
68#define IORING_FLAG_GETEVENTS (1 << 1)
69
70#define DEPTH 32
71#define RING_SIZE (DEPTH + 1)
72
73#define BATCH_SUBMIT 8
74#define BATCH_COMPLETE 8
75
76#define BS 4096
77
78struct submitter {
79 pthread_t thread;
80 unsigned long max_blocks;
81 io_context_t ioc;
82 struct drand48_data rand;
83 struct aio_sq_ring *sq_ring;
84 struct iocb *iocbs;
85 struct aio_cq_ring *cq_ring;
86 int inflight;
87 unsigned long reaps;
88 unsigned long done;
89 unsigned long calls;
90 volatile int finish;
91 char filename[128];
92};
93
94static struct submitter submitters[1];
95static volatile int finish;
96
97static int polled = 1; /* use IO polling */
98static int fixedbufs = 1; /* use fixed user buffers */
99static int buffered = 0; /* use buffered IO, not O_DIRECT */
100static int sq_thread = 0; /* use kernel submission thread */
101static int sq_thread_cpu = 0; /* pin above thread to this CPU */
102
103static 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)
106{
107 return syscall(335, nr_events, flags, sq_ring, cq_ring, ctx_idp);
108}
109
110static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
111 unsigned int min_complete, unsigned int flags)
112{
113 return syscall(336, ctx, to_submit, min_complete, flags);
114}
115
116static int gettid(void)
117{
118 return syscall(__NR_gettid);
119}
120
121static void init_io(struct submitter *s, int fd, struct iocb *iocb)
122{
123 unsigned long offset;
124 long r;
125
126 lrand48_r(&s->rand, &r);
127 offset = (r % (s->max_blocks - 1)) * BS;
128
129 iocb->aio_fildes = fd;
130 iocb->aio_lio_opcode = IO_CMD_PREAD;
131 iocb->u.c.offset = offset;
132 if (polled)
133 iocb->u.c.flags = IOCB_FLAG_HIPRI;
134 if (!fixedbufs)
135 iocb->u.c.nbytes = BS;
136}
137
138static int prep_more_ios(struct submitter *s, int fd, int max_ios)
139{
140 struct aio_sq_ring *ring = s->sq_ring;
141 u32 tail, next_tail, prepped = 0;
142
143 next_tail = tail = ring->tail;
144 do {
145 next_tail++;
146 if (next_tail == ring->nr_events)
147 next_tail = 0;
148
149 barrier();
150 if (next_tail == ring->head)
151 break;
152
153 init_io(s, fd, &s->iocbs[tail]);
154 s->sq_ring->array[tail] = tail;
155 prepped++;
156 tail = next_tail;
157 } while (prepped < max_ios);
158
159 if (ring->tail != tail) {
160 /* order tail store with writes to iocbs above */
161 barrier();
162 ring->tail = tail;
163 barrier();
164 }
165 return prepped;
166}
167
168static int get_file_size(int fd, unsigned long *blocks)
169{
170 struct stat st;
171
172 if (fstat(fd, &st) < 0)
173 return -1;
174 if (S_ISBLK(st.st_mode)) {
175 unsigned long long bytes;
176
177 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
178 return -1;
179
180 *blocks = bytes / BS;
181 return 0;
182 } else if (S_ISREG(st.st_mode)) {
183 *blocks = st.st_size / BS;
184 return 0;
185 }
186
187 return -1;
188}
189
190static int reap_events(struct submitter *s)
191{
192 struct aio_cq_ring *ring = s->cq_ring;
193 struct io_event *ev;
194 u32 head, reaped = 0;
195
196 head = ring->head;
197 do {
198 barrier();
199 if (head == ring->tail)
200 break;
201 ev = &ring->events[head];
202 if (ev->res != BS) {
203 struct iocb *iocb = ev->obj;
204
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);
207 return -1;
208 }
209 reaped++;
210 head++;
211 if (head == ring->nr_events)
212 head = 0;
213 } while (1);
214
215 s->inflight -= reaped;
216 ring->head = head;
217 barrier();
218 return reaped;
219}
220
221static void *submitter_fn(void *data)
222{
223 struct submitter *s = data;
224 int fd, ret, prepped, flags;
225
226 printf("submitter=%d\n", gettid());
227
228 flags = O_RDONLY;
229 if (!buffered)
230 flags |= O_DIRECT;
231 fd = open(s->filename, flags);
232 if (fd < 0) {
233 perror("open");
234 goto done;
235 }
236
237 if (get_file_size(fd, &s->max_blocks)) {
238 printf("failed getting size of device/file\n");
239 goto err;
240 }
241 if (!s->max_blocks) {
242 printf("Zero file/device size?\n");
243 goto err;
244 }
245
246 s->max_blocks--;
247
248 srand48_r(pthread_self(), &s->rand);
249
250 prepped = 0;
251 do {
252 int to_wait, flags, to_submit, this_reap;
253
254 if (!prepped && s->inflight < DEPTH)
255 prepped = prep_more_ios(s, fd, min(DEPTH - s->inflight, BATCH_SUBMIT));
256 s->inflight += prepped;
257submit_more:
258 to_submit = prepped;
259submit:
260 if (s->inflight + BATCH_SUBMIT < DEPTH)
261 to_wait = 0;
262 else
263 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
264
265 flags = IORING_FLAG_GETEVENTS;
266 if (to_submit)
267 flags |= IORING_FLAG_SUBMIT;
268
269 ret = io_ring_enter(s->ioc, to_submit, to_wait, flags);
270 s->calls++;
271
272 this_reap = reap_events(s);
273 if (this_reap == -1)
274 break;
275 s->reaps += this_reap;
276
277 if (ret >= 0) {
278 if (!ret) {
279 to_submit = 0;
280 if (s->inflight)
281 goto submit;
282 continue;
283 } else if (ret < to_submit) {
284 int diff = to_submit - ret;
285
286 s->done += ret;
287 prepped -= diff;
288 goto submit_more;
289 }
290 s->done += ret;
291 prepped = 0;
292 continue;
293 } else if (ret < 0) {
294 if ((ret == -1 && errno == EAGAIN) || ret == -EAGAIN) {
295 if (s->finish)
296 break;
297 if (this_reap)
298 goto submit;
299 to_submit = 0;
300 goto submit;
301 }
302 if (ret == -1)
303 printf("io_submit: %s\n", strerror(errno));
304 else
305 printf("io_submit: %s\n", strerror(-ret));
306 break;
307 }
308 } while (!s->finish);
309err:
310 close(fd);
311done:
312 finish = 1;
313 return NULL;
314}
315
316static void sig_int(int sig)
317{
318 printf("Exiting on signal %d\n", sig);
319 submitters[0].finish = 1;
320 finish = 1;
321}
322
323static void arm_sig_int(void)
324{
325 struct sigaction act;
326
327 memset(&act, 0, sizeof(act));
328 act.sa_handler = sig_int;
329 act.sa_flags = SA_RESTART;
330 sigaction(SIGINT, &act, NULL);
331}
332
333int main(int argc, char *argv[])
334{
335 struct submitter *s = &submitters[0];
336 unsigned long done, calls, reap;
337 int flags = 0, err;
338 int j;
339 size_t size;
340 void *p, *ret;
341 struct rlimit rlim;
342
343 if (argc < 2) {
344 printf("%s: filename\n", argv[0]);
345 return 1;
346 }
347
348 rlim.rlim_cur = RLIM_INFINITY;
349 rlim.rlim_max = RLIM_INFINITY;
350 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
351 perror("setrlimit");
352 return 1;
353 }
354
355 arm_sig_int();
356
357 size = sizeof(struct iocb) * RING_SIZE;
358 if (posix_memalign(&p, 4096, size))
359 return 1;
360 memset(p, 0, size);
361 s->iocbs = p;
362
363 size = sizeof(struct aio_sq_ring) + RING_SIZE * sizeof(u32);
364 if (posix_memalign(&p, 4096, size))
365 return 1;
366 s->sq_ring = p;
367 memset(p, 0, size);
368 s->sq_ring->nr_events = RING_SIZE;
369 s->sq_ring->iocbs = (u64) s->iocbs;
370
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))
375 return 1;
376 s->cq_ring = p;
377 memset(p, 0, size);
378 s->cq_ring->nr_events = 2 * RING_SIZE;
379
380 for (j = 0; j < RING_SIZE; j++) {
381 struct iocb *iocb = &s->iocbs[j];
382
383 if (posix_memalign(&iocb->u.c.buf, BS, BS)) {
384 printf("failed alloc\n");
385 return 1;
386 }
387 iocb->u.c.nbytes = BS;
388 }
389
390 flags = IOCTX_FLAG_SCQRING;
391 if (polled)
392 flags |= IOCTX_FLAG_IOPOLL;
393 if (fixedbufs)
394 flags |= IOCTX_FLAG_FIXEDBUFS;
395 if (buffered)
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;
400 }
401
402 err = io_setup2(RING_SIZE, flags, s->sq_ring, s->cq_ring, &s->ioc);
403 if (err) {
404 printf("ctx_init failed: %s, %d\n", strerror(errno), err);
405 return 1;
406 }
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]);
410
411 pthread_create(&s->thread, NULL, submitter_fn, s);
412
413 reap = calls = done = 0;
414 do {
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;
419
420 sleep(1);
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);
427 }
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);
431 done = this_done;
432 calls = this_call;
433 reap = this_reap;
434 } while (!finish);
435
436 pthread_join(s->thread, &ret);
437 return 0;
438}