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