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