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