| 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 | #include <math.h> |
| 9 | |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <sys/ioctl.h> |
| 13 | #include <sys/syscall.h> |
| 14 | #include <sys/resource.h> |
| 15 | #include <sys/mman.h> |
| 16 | #include <sys/uio.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <unistd.h> |
| 20 | #include <string.h> |
| 21 | #include <pthread.h> |
| 22 | #include <sched.h> |
| 23 | |
| 24 | #include "../arch/arch.h" |
| 25 | #include "../lib/types.h" |
| 26 | #include "../lib/roundup.h" |
| 27 | #include "../minmax.h" |
| 28 | #include "../os/linux/io_uring.h" |
| 29 | |
| 30 | struct io_sq_ring { |
| 31 | unsigned *head; |
| 32 | unsigned *tail; |
| 33 | unsigned *ring_mask; |
| 34 | unsigned *ring_entries; |
| 35 | unsigned *flags; |
| 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 128 |
| 48 | #define BATCH_SUBMIT 32 |
| 49 | #define BATCH_COMPLETE 32 |
| 50 | #define BS 4096 |
| 51 | |
| 52 | #define MAX_FDS 16 |
| 53 | |
| 54 | static unsigned sq_ring_mask, cq_ring_mask; |
| 55 | |
| 56 | struct file { |
| 57 | unsigned long max_blocks; |
| 58 | unsigned pending_ios; |
| 59 | int real_fd; |
| 60 | int fixed_fd; |
| 61 | int fileno; |
| 62 | }; |
| 63 | |
| 64 | #define PLAT_BITS 6 |
| 65 | #define PLAT_VAL (1 << PLAT_BITS) |
| 66 | #define PLAT_GROUP_NR 29 |
| 67 | #define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL) |
| 68 | |
| 69 | struct submitter { |
| 70 | pthread_t thread; |
| 71 | int ring_fd; |
| 72 | int index; |
| 73 | struct io_sq_ring sq_ring; |
| 74 | struct io_uring_sqe *sqes; |
| 75 | struct io_cq_ring cq_ring; |
| 76 | int inflight; |
| 77 | int tid; |
| 78 | unsigned long reaps; |
| 79 | unsigned long done; |
| 80 | unsigned long calls; |
| 81 | volatile int finish; |
| 82 | |
| 83 | __s32 *fds; |
| 84 | |
| 85 | unsigned long *clock_batch; |
| 86 | int clock_index; |
| 87 | unsigned long *plat; |
| 88 | |
| 89 | struct file files[MAX_FDS]; |
| 90 | unsigned nr_files; |
| 91 | unsigned cur_file; |
| 92 | struct iovec iovecs[]; |
| 93 | }; |
| 94 | |
| 95 | static struct submitter *submitter; |
| 96 | static volatile int finish; |
| 97 | |
| 98 | static int depth = DEPTH; |
| 99 | static int batch_submit = BATCH_SUBMIT; |
| 100 | static int batch_complete = BATCH_COMPLETE; |
| 101 | static int bs = BS; |
| 102 | static int polled = 1; /* use IO polling */ |
| 103 | static int fixedbufs = 1; /* use fixed user buffers */ |
| 104 | static int register_files = 1; /* use fixed files */ |
| 105 | static int buffered = 0; /* use buffered IO, not O_DIRECT */ |
| 106 | static int sq_thread_poll = 0; /* use kernel submission/poller thread */ |
| 107 | static int sq_thread_cpu = -1; /* pin above thread to this CPU */ |
| 108 | static int do_nop = 0; /* no-op SQ ring commands */ |
| 109 | static int nthreads = 1; |
| 110 | static int stats = 0; /* generate IO stats */ |
| 111 | static unsigned long tsc_rate; |
| 112 | |
| 113 | #define TSC_RATE_FILE "tsc-rate" |
| 114 | |
| 115 | static int vectored = 1; |
| 116 | |
| 117 | static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, |
| 118 | 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 }; |
| 119 | static int plist_len = 17; |
| 120 | |
| 121 | static unsigned long cycles_to_nsec(unsigned long cycles) |
| 122 | { |
| 123 | uint64_t val; |
| 124 | |
| 125 | if (!tsc_rate) |
| 126 | return cycles; |
| 127 | |
| 128 | val = cycles * 1000000000ULL; |
| 129 | return val / tsc_rate; |
| 130 | } |
| 131 | |
| 132 | static unsigned long plat_idx_to_val(unsigned int idx) |
| 133 | { |
| 134 | unsigned int error_bits; |
| 135 | unsigned long k, base; |
| 136 | |
| 137 | assert(idx < PLAT_NR); |
| 138 | |
| 139 | /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use |
| 140 | * all bits of the sample as index */ |
| 141 | if (idx < (PLAT_VAL << 1)) |
| 142 | return cycles_to_nsec(idx); |
| 143 | |
| 144 | /* Find the group and compute the minimum value of that group */ |
| 145 | error_bits = (idx >> PLAT_BITS) - 1; |
| 146 | base = ((unsigned long) 1) << (error_bits + PLAT_BITS); |
| 147 | |
| 148 | /* Find its bucket number of the group */ |
| 149 | k = idx % PLAT_VAL; |
| 150 | |
| 151 | /* Return the mean of the range of the bucket */ |
| 152 | return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits))); |
| 153 | } |
| 154 | |
| 155 | unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr, |
| 156 | unsigned long **output, |
| 157 | unsigned long *maxv, unsigned long *minv) |
| 158 | { |
| 159 | unsigned long sum = 0; |
| 160 | unsigned int len = plist_len, i, j = 0; |
| 161 | unsigned long *ovals = NULL; |
| 162 | bool is_last; |
| 163 | |
| 164 | *minv = -1ULL; |
| 165 | *maxv = 0; |
| 166 | |
| 167 | ovals = malloc(len * sizeof(*ovals)); |
| 168 | if (!ovals) |
| 169 | return 0; |
| 170 | |
| 171 | /* |
| 172 | * Calculate bucket values, note down max and min values |
| 173 | */ |
| 174 | is_last = false; |
| 175 | for (i = 0; i < PLAT_NR && !is_last; i++) { |
| 176 | sum += io_u_plat[i]; |
| 177 | while (sum >= ((long double) plist[j] / 100.0 * nr)) { |
| 178 | assert(plist[j] <= 100.0); |
| 179 | |
| 180 | ovals[j] = plat_idx_to_val(i); |
| 181 | if (ovals[j] < *minv) |
| 182 | *minv = ovals[j]; |
| 183 | if (ovals[j] > *maxv) |
| 184 | *maxv = ovals[j]; |
| 185 | |
| 186 | is_last = (j == len - 1) != 0; |
| 187 | if (is_last) |
| 188 | break; |
| 189 | |
| 190 | j++; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (!is_last) |
| 195 | fprintf(stderr, "error calculating latency percentiles\n"); |
| 196 | |
| 197 | *output = ovals; |
| 198 | return len; |
| 199 | } |
| 200 | |
| 201 | static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr, |
| 202 | unsigned int precision) |
| 203 | { |
| 204 | unsigned int divisor, len, i, j = 0; |
| 205 | unsigned long minv, maxv; |
| 206 | unsigned long *ovals; |
| 207 | int per_line, scale_down, time_width; |
| 208 | bool is_last; |
| 209 | char fmt[32]; |
| 210 | |
| 211 | len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv); |
| 212 | if (!len || !ovals) |
| 213 | goto out; |
| 214 | |
| 215 | if (!tsc_rate) { |
| 216 | scale_down = 0; |
| 217 | divisor = 1; |
| 218 | printf(" percentiles (tsc ticks):\n |"); |
| 219 | } else if (minv > 2000 && maxv > 99999) { |
| 220 | scale_down = 1; |
| 221 | divisor = 1000; |
| 222 | printf(" percentiles (usec):\n |"); |
| 223 | } else { |
| 224 | scale_down = 0; |
| 225 | divisor = 1; |
| 226 | printf(" percentiles (nsec):\n |"); |
| 227 | } |
| 228 | |
| 229 | time_width = max(5, (int) (log10(maxv / divisor) + 1)); |
| 230 | snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3, |
| 231 | precision, time_width); |
| 232 | /* fmt will be something like " %5.2fth=[%4llu]%c" */ |
| 233 | per_line = (80 - 7) / (precision + 10 + time_width); |
| 234 | |
| 235 | for (j = 0; j < len; j++) { |
| 236 | /* for formatting */ |
| 237 | if (j != 0 && (j % per_line) == 0) |
| 238 | printf(" |"); |
| 239 | |
| 240 | /* end of the list */ |
| 241 | is_last = (j == len - 1) != 0; |
| 242 | |
| 243 | for (i = 0; i < scale_down; i++) |
| 244 | ovals[j] = (ovals[j] + 999) / 1000; |
| 245 | |
| 246 | printf(fmt, plist[j], ovals[j], is_last ? '\n' : ','); |
| 247 | |
| 248 | if (is_last) |
| 249 | break; |
| 250 | |
| 251 | if ((j % per_line) == per_line - 1) /* for formatting */ |
| 252 | printf("\n"); |
| 253 | } |
| 254 | |
| 255 | out: |
| 256 | free(ovals); |
| 257 | } |
| 258 | |
| 259 | static unsigned int plat_val_to_idx(unsigned long val) |
| 260 | { |
| 261 | unsigned int msb, error_bits, base, offset, idx; |
| 262 | |
| 263 | /* Find MSB starting from bit 0 */ |
| 264 | if (val == 0) |
| 265 | msb = 0; |
| 266 | else |
| 267 | msb = (sizeof(val)*8) - __builtin_clzll(val) - 1; |
| 268 | |
| 269 | /* |
| 270 | * MSB <= (PLAT_BITS-1), cannot be rounded off. Use |
| 271 | * all bits of the sample as index |
| 272 | */ |
| 273 | if (msb <= PLAT_BITS) |
| 274 | return val; |
| 275 | |
| 276 | /* Compute the number of error bits to discard*/ |
| 277 | error_bits = msb - PLAT_BITS; |
| 278 | |
| 279 | /* Compute the number of buckets before the group */ |
| 280 | base = (error_bits + 1) << PLAT_BITS; |
| 281 | |
| 282 | /* |
| 283 | * Discard the error bits and apply the mask to find the |
| 284 | * index for the buckets in the group |
| 285 | */ |
| 286 | offset = (PLAT_VAL - 1) & (val >> error_bits); |
| 287 | |
| 288 | /* Make sure the index does not exceed (array size - 1) */ |
| 289 | idx = (base + offset) < (PLAT_NR - 1) ? |
| 290 | (base + offset) : (PLAT_NR - 1); |
| 291 | |
| 292 | return idx; |
| 293 | } |
| 294 | |
| 295 | static void add_stat(struct submitter *s, int clock_index, int nr) |
| 296 | { |
| 297 | #ifdef ARCH_HAVE_CPU_CLOCK |
| 298 | unsigned long cycles; |
| 299 | unsigned int pidx; |
| 300 | |
| 301 | cycles = get_cpu_clock(); |
| 302 | cycles -= s->clock_batch[clock_index]; |
| 303 | pidx = plat_val_to_idx(cycles); |
| 304 | s->plat[pidx] += nr; |
| 305 | #endif |
| 306 | } |
| 307 | |
| 308 | static int io_uring_register_buffers(struct submitter *s) |
| 309 | { |
| 310 | if (do_nop) |
| 311 | return 0; |
| 312 | |
| 313 | return syscall(__NR_io_uring_register, s->ring_fd, |
| 314 | IORING_REGISTER_BUFFERS, s->iovecs, depth); |
| 315 | } |
| 316 | |
| 317 | static int io_uring_register_files(struct submitter *s) |
| 318 | { |
| 319 | int i; |
| 320 | |
| 321 | if (do_nop) |
| 322 | return 0; |
| 323 | |
| 324 | s->fds = calloc(s->nr_files, sizeof(__s32)); |
| 325 | for (i = 0; i < s->nr_files; i++) { |
| 326 | s->fds[i] = s->files[i].real_fd; |
| 327 | s->files[i].fixed_fd = i; |
| 328 | } |
| 329 | |
| 330 | return syscall(__NR_io_uring_register, s->ring_fd, |
| 331 | IORING_REGISTER_FILES, s->fds, s->nr_files); |
| 332 | } |
| 333 | |
| 334 | static int io_uring_setup(unsigned entries, struct io_uring_params *p) |
| 335 | { |
| 336 | return syscall(__NR_io_uring_setup, entries, p); |
| 337 | } |
| 338 | |
| 339 | static void io_uring_probe(int fd) |
| 340 | { |
| 341 | struct io_uring_probe *p; |
| 342 | int ret; |
| 343 | |
| 344 | p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op)); |
| 345 | if (!p) |
| 346 | return; |
| 347 | |
| 348 | memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op)); |
| 349 | ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256); |
| 350 | if (ret < 0) |
| 351 | goto out; |
| 352 | |
| 353 | if (IORING_OP_READ > p->ops_len) |
| 354 | goto out; |
| 355 | |
| 356 | if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED)) |
| 357 | vectored = 0; |
| 358 | out: |
| 359 | free(p); |
| 360 | } |
| 361 | |
| 362 | static int io_uring_enter(struct submitter *s, unsigned int to_submit, |
| 363 | unsigned int min_complete, unsigned int flags) |
| 364 | { |
| 365 | return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete, |
| 366 | flags, NULL, 0); |
| 367 | } |
| 368 | |
| 369 | #ifndef CONFIG_HAVE_GETTID |
| 370 | static int gettid(void) |
| 371 | { |
| 372 | return syscall(__NR_gettid); |
| 373 | } |
| 374 | #endif |
| 375 | |
| 376 | static unsigned file_depth(struct submitter *s) |
| 377 | { |
| 378 | return (depth + s->nr_files - 1) / s->nr_files; |
| 379 | } |
| 380 | |
| 381 | static void init_io(struct submitter *s, unsigned index) |
| 382 | { |
| 383 | struct io_uring_sqe *sqe = &s->sqes[index]; |
| 384 | unsigned long offset; |
| 385 | struct file *f; |
| 386 | long r; |
| 387 | |
| 388 | if (do_nop) { |
| 389 | sqe->opcode = IORING_OP_NOP; |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | if (s->nr_files == 1) { |
| 394 | f = &s->files[0]; |
| 395 | } else { |
| 396 | f = &s->files[s->cur_file]; |
| 397 | if (f->pending_ios >= file_depth(s)) { |
| 398 | s->cur_file++; |
| 399 | if (s->cur_file == s->nr_files) |
| 400 | s->cur_file = 0; |
| 401 | f = &s->files[s->cur_file]; |
| 402 | } |
| 403 | } |
| 404 | f->pending_ios++; |
| 405 | |
| 406 | r = lrand48(); |
| 407 | offset = (r % (f->max_blocks - 1)) * bs; |
| 408 | |
| 409 | if (register_files) { |
| 410 | sqe->flags = IOSQE_FIXED_FILE; |
| 411 | sqe->fd = f->fixed_fd; |
| 412 | } else { |
| 413 | sqe->flags = 0; |
| 414 | sqe->fd = f->real_fd; |
| 415 | } |
| 416 | if (fixedbufs) { |
| 417 | sqe->opcode = IORING_OP_READ_FIXED; |
| 418 | sqe->addr = (unsigned long) s->iovecs[index].iov_base; |
| 419 | sqe->len = bs; |
| 420 | sqe->buf_index = index; |
| 421 | } else if (!vectored) { |
| 422 | sqe->opcode = IORING_OP_READ; |
| 423 | sqe->addr = (unsigned long) s->iovecs[index].iov_base; |
| 424 | sqe->len = bs; |
| 425 | sqe->buf_index = 0; |
| 426 | } else { |
| 427 | sqe->opcode = IORING_OP_READV; |
| 428 | sqe->addr = (unsigned long) &s->iovecs[index]; |
| 429 | sqe->len = 1; |
| 430 | sqe->buf_index = 0; |
| 431 | } |
| 432 | sqe->ioprio = 0; |
| 433 | sqe->off = offset; |
| 434 | sqe->user_data = (unsigned long) f->fileno; |
| 435 | if (stats) |
| 436 | sqe->user_data |= ((unsigned long)s->clock_index << 32); |
| 437 | } |
| 438 | |
| 439 | static int prep_more_ios(struct submitter *s, int max_ios) |
| 440 | { |
| 441 | struct io_sq_ring *ring = &s->sq_ring; |
| 442 | unsigned index, tail, next_tail, prepped = 0; |
| 443 | |
| 444 | next_tail = tail = *ring->tail; |
| 445 | do { |
| 446 | next_tail++; |
| 447 | if (next_tail == atomic_load_acquire(ring->head)) |
| 448 | break; |
| 449 | |
| 450 | index = tail & sq_ring_mask; |
| 451 | init_io(s, index); |
| 452 | ring->array[index] = index; |
| 453 | prepped++; |
| 454 | tail = next_tail; |
| 455 | } while (prepped < max_ios); |
| 456 | |
| 457 | if (prepped) |
| 458 | atomic_store_release(ring->tail, tail); |
| 459 | return prepped; |
| 460 | } |
| 461 | |
| 462 | static int get_file_size(struct file *f) |
| 463 | { |
| 464 | struct stat st; |
| 465 | |
| 466 | if (fstat(f->real_fd, &st) < 0) |
| 467 | return -1; |
| 468 | if (S_ISBLK(st.st_mode)) { |
| 469 | unsigned long long bytes; |
| 470 | |
| 471 | if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0) |
| 472 | return -1; |
| 473 | |
| 474 | f->max_blocks = bytes / bs; |
| 475 | return 0; |
| 476 | } else if (S_ISREG(st.st_mode)) { |
| 477 | f->max_blocks = st.st_size / bs; |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | static int reap_events(struct submitter *s) |
| 485 | { |
| 486 | struct io_cq_ring *ring = &s->cq_ring; |
| 487 | struct io_uring_cqe *cqe; |
| 488 | unsigned head, reaped = 0; |
| 489 | int last_idx = -1, stat_nr = 0; |
| 490 | |
| 491 | head = *ring->head; |
| 492 | do { |
| 493 | struct file *f; |
| 494 | |
| 495 | read_barrier(); |
| 496 | if (head == atomic_load_acquire(ring->tail)) |
| 497 | break; |
| 498 | cqe = &ring->cqes[head & cq_ring_mask]; |
| 499 | if (!do_nop) { |
| 500 | int fileno = cqe->user_data & 0xffffffff; |
| 501 | |
| 502 | f = &s->files[fileno]; |
| 503 | f->pending_ios--; |
| 504 | if (cqe->res != bs) { |
| 505 | printf("io: unexpected ret=%d\n", cqe->res); |
| 506 | if (polled && cqe->res == -EOPNOTSUPP) |
| 507 | printf("Your filesystem/driver/kernel doesn't support polled IO\n"); |
| 508 | return -1; |
| 509 | } |
| 510 | } |
| 511 | if (stats) { |
| 512 | int clock_index = cqe->user_data >> 32; |
| 513 | |
| 514 | if (last_idx != clock_index) { |
| 515 | if (last_idx != -1) { |
| 516 | add_stat(s, last_idx, stat_nr); |
| 517 | stat_nr = 0; |
| 518 | } |
| 519 | last_idx = clock_index; |
| 520 | } |
| 521 | stat_nr++; |
| 522 | add_stat(s, clock_index, 1); |
| 523 | } |
| 524 | reaped++; |
| 525 | head++; |
| 526 | } while (1); |
| 527 | |
| 528 | if (stat_nr) |
| 529 | add_stat(s, last_idx, stat_nr); |
| 530 | |
| 531 | if (reaped) { |
| 532 | s->inflight -= reaped; |
| 533 | atomic_store_release(ring->head, head); |
| 534 | } |
| 535 | return reaped; |
| 536 | } |
| 537 | |
| 538 | static void *submitter_fn(void *data) |
| 539 | { |
| 540 | struct submitter *s = data; |
| 541 | struct io_sq_ring *ring = &s->sq_ring; |
| 542 | int i, ret, prepped, nr_batch; |
| 543 | |
| 544 | s->tid = gettid(); |
| 545 | printf("submitter=%d\n", s->tid); |
| 546 | |
| 547 | srand48(pthread_self()); |
| 548 | |
| 549 | for (i = 0; i < MAX_FDS; i++) |
| 550 | s->files[i].fileno = i; |
| 551 | |
| 552 | if (stats) { |
| 553 | nr_batch = roundup_pow2(depth / batch_submit); |
| 554 | s->clock_batch = calloc(nr_batch, sizeof(unsigned long)); |
| 555 | s->clock_index = 0; |
| 556 | |
| 557 | s->plat = calloc(PLAT_NR, sizeof(unsigned long)); |
| 558 | } else { |
| 559 | s->clock_batch = NULL; |
| 560 | s->plat = NULL; |
| 561 | nr_batch = 0; |
| 562 | } |
| 563 | |
| 564 | prepped = 0; |
| 565 | do { |
| 566 | int to_wait, to_submit, this_reap, to_prep; |
| 567 | unsigned ring_flags = 0; |
| 568 | |
| 569 | if (!prepped && s->inflight < depth) { |
| 570 | to_prep = min(depth - s->inflight, batch_submit); |
| 571 | prepped = prep_more_ios(s, to_prep); |
| 572 | #ifdef ARCH_HAVE_CPU_CLOCK |
| 573 | if (prepped && stats) { |
| 574 | s->clock_batch[s->clock_index] = get_cpu_clock(); |
| 575 | s->clock_index = (s->clock_index + 1) & (nr_batch - 1); |
| 576 | } |
| 577 | #endif |
| 578 | } |
| 579 | s->inflight += prepped; |
| 580 | submit_more: |
| 581 | to_submit = prepped; |
| 582 | submit: |
| 583 | if (to_submit && (s->inflight + to_submit <= depth)) |
| 584 | to_wait = 0; |
| 585 | else |
| 586 | to_wait = min(s->inflight + to_submit, batch_complete); |
| 587 | |
| 588 | /* |
| 589 | * Only need to call io_uring_enter if we're not using SQ thread |
| 590 | * poll, or if IORING_SQ_NEED_WAKEUP is set. |
| 591 | */ |
| 592 | if (sq_thread_poll) |
| 593 | ring_flags = atomic_load_acquire(ring->flags); |
| 594 | if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) { |
| 595 | unsigned flags = 0; |
| 596 | |
| 597 | if (to_wait) |
| 598 | flags = IORING_ENTER_GETEVENTS; |
| 599 | if (ring_flags & IORING_SQ_NEED_WAKEUP) |
| 600 | flags |= IORING_ENTER_SQ_WAKEUP; |
| 601 | ret = io_uring_enter(s, to_submit, to_wait, flags); |
| 602 | s->calls++; |
| 603 | } else { |
| 604 | /* for SQPOLL, we submitted it all effectively */ |
| 605 | ret = to_submit; |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | * For non SQ thread poll, we already got the events we needed |
| 610 | * through the io_uring_enter() above. For SQ thread poll, we |
| 611 | * need to loop here until we find enough events. |
| 612 | */ |
| 613 | this_reap = 0; |
| 614 | do { |
| 615 | int r; |
| 616 | r = reap_events(s); |
| 617 | if (r == -1) { |
| 618 | s->finish = 1; |
| 619 | break; |
| 620 | } else if (r > 0) |
| 621 | this_reap += r; |
| 622 | } while (sq_thread_poll && this_reap < to_wait); |
| 623 | s->reaps += this_reap; |
| 624 | |
| 625 | if (ret >= 0) { |
| 626 | if (!ret) { |
| 627 | to_submit = 0; |
| 628 | if (s->inflight) |
| 629 | goto submit; |
| 630 | continue; |
| 631 | } else if (ret < to_submit) { |
| 632 | int diff = to_submit - ret; |
| 633 | |
| 634 | s->done += ret; |
| 635 | prepped -= diff; |
| 636 | goto submit_more; |
| 637 | } |
| 638 | s->done += ret; |
| 639 | prepped = 0; |
| 640 | continue; |
| 641 | } else if (ret < 0) { |
| 642 | if (errno == EAGAIN) { |
| 643 | if (s->finish) |
| 644 | break; |
| 645 | if (this_reap) |
| 646 | goto submit; |
| 647 | to_submit = 0; |
| 648 | goto submit; |
| 649 | } |
| 650 | printf("io_submit: %s\n", strerror(errno)); |
| 651 | break; |
| 652 | } |
| 653 | } while (!s->finish); |
| 654 | |
| 655 | finish = 1; |
| 656 | return NULL; |
| 657 | } |
| 658 | |
| 659 | static struct submitter *get_submitter(int offset) |
| 660 | { |
| 661 | void *ret; |
| 662 | |
| 663 | ret = submitter; |
| 664 | if (offset) |
| 665 | ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec)); |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | static void sig_int(int sig) |
| 670 | { |
| 671 | int j; |
| 672 | |
| 673 | printf("Exiting on signal %d\n", sig); |
| 674 | for (j = 0; j < nthreads; j++) { |
| 675 | struct submitter *s = get_submitter(j); |
| 676 | s->finish = 1; |
| 677 | } |
| 678 | finish = 1; |
| 679 | } |
| 680 | |
| 681 | static void arm_sig_int(void) |
| 682 | { |
| 683 | struct sigaction act; |
| 684 | |
| 685 | memset(&act, 0, sizeof(act)); |
| 686 | act.sa_handler = sig_int; |
| 687 | act.sa_flags = SA_RESTART; |
| 688 | sigaction(SIGINT, &act, NULL); |
| 689 | |
| 690 | /* Windows uses SIGBREAK as a quit signal from other applications */ |
| 691 | #ifdef WIN32 |
| 692 | sigaction(SIGBREAK, &act, NULL); |
| 693 | #endif |
| 694 | } |
| 695 | |
| 696 | static int setup_ring(struct submitter *s) |
| 697 | { |
| 698 | struct io_sq_ring *sring = &s->sq_ring; |
| 699 | struct io_cq_ring *cring = &s->cq_ring; |
| 700 | struct io_uring_params p; |
| 701 | int ret, fd; |
| 702 | void *ptr; |
| 703 | |
| 704 | memset(&p, 0, sizeof(p)); |
| 705 | |
| 706 | if (polled && !do_nop) |
| 707 | p.flags |= IORING_SETUP_IOPOLL; |
| 708 | if (sq_thread_poll) { |
| 709 | p.flags |= IORING_SETUP_SQPOLL; |
| 710 | if (sq_thread_cpu != -1) { |
| 711 | p.flags |= IORING_SETUP_SQ_AFF; |
| 712 | p.sq_thread_cpu = sq_thread_cpu; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | fd = io_uring_setup(depth, &p); |
| 717 | if (fd < 0) { |
| 718 | perror("io_uring_setup"); |
| 719 | return 1; |
| 720 | } |
| 721 | s->ring_fd = fd; |
| 722 | |
| 723 | io_uring_probe(fd); |
| 724 | |
| 725 | if (fixedbufs) { |
| 726 | struct rlimit rlim; |
| 727 | |
| 728 | rlim.rlim_cur = RLIM_INFINITY; |
| 729 | rlim.rlim_max = RLIM_INFINITY; |
| 730 | /* ignore potential error, not needed on newer kernels */ |
| 731 | setrlimit(RLIMIT_MEMLOCK, &rlim); |
| 732 | |
| 733 | ret = io_uring_register_buffers(s); |
| 734 | if (ret < 0) { |
| 735 | perror("io_uring_register_buffers"); |
| 736 | return 1; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | if (register_files) { |
| 741 | ret = io_uring_register_files(s); |
| 742 | if (ret < 0) { |
| 743 | perror("io_uring_register_files"); |
| 744 | return 1; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32), |
| 749 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
| 750 | IORING_OFF_SQ_RING); |
| 751 | printf("sq_ring ptr = 0x%p\n", ptr); |
| 752 | sring->head = ptr + p.sq_off.head; |
| 753 | sring->tail = ptr + p.sq_off.tail; |
| 754 | sring->ring_mask = ptr + p.sq_off.ring_mask; |
| 755 | sring->ring_entries = ptr + p.sq_off.ring_entries; |
| 756 | sring->flags = ptr + p.sq_off.flags; |
| 757 | sring->array = ptr + p.sq_off.array; |
| 758 | sq_ring_mask = *sring->ring_mask; |
| 759 | |
| 760 | s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe), |
| 761 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
| 762 | IORING_OFF_SQES); |
| 763 | printf("sqes ptr = 0x%p\n", s->sqes); |
| 764 | |
| 765 | ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe), |
| 766 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
| 767 | IORING_OFF_CQ_RING); |
| 768 | printf("cq_ring ptr = 0x%p\n", ptr); |
| 769 | cring->head = ptr + p.cq_off.head; |
| 770 | cring->tail = ptr + p.cq_off.tail; |
| 771 | cring->ring_mask = ptr + p.cq_off.ring_mask; |
| 772 | cring->ring_entries = ptr + p.cq_off.ring_entries; |
| 773 | cring->cqes = ptr + p.cq_off.cqes; |
| 774 | cq_ring_mask = *cring->ring_mask; |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | static void file_depths(char *buf) |
| 779 | { |
| 780 | bool prev = false; |
| 781 | char *p; |
| 782 | int i, j; |
| 783 | |
| 784 | buf[0] = '\0'; |
| 785 | p = buf; |
| 786 | for (j = 0; j < nthreads; j++) { |
| 787 | struct submitter *s = get_submitter(j); |
| 788 | |
| 789 | for (i = 0; i < s->nr_files; i++) { |
| 790 | struct file *f = &s->files[i]; |
| 791 | |
| 792 | if (prev) |
| 793 | p += sprintf(p, " %d", f->pending_ios); |
| 794 | else |
| 795 | p += sprintf(p, "%d", f->pending_ios); |
| 796 | prev = true; |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | static void usage(char *argv, int status) |
| 802 | { |
| 803 | printf("%s [options] -- [filenames]\n" |
| 804 | " -d <int> : IO Depth, default %d\n" |
| 805 | " -s <int> : Batch submit, default %d\n" |
| 806 | " -c <int> : Batch complete, default %d\n" |
| 807 | " -b <int> : Block size, default %d\n" |
| 808 | " -p <bool> : Polled IO, default %d\n" |
| 809 | " -B <bool> : Fixed buffers, default %d\n" |
| 810 | " -F <bool> : Register files, default %d\n" |
| 811 | " -n <int> : Number of threads, default %d\n" |
| 812 | " -O <bool> : Use O_DIRECT, default %d\n" |
| 813 | " -N <bool> : Perform just no-op requests, default %d\n" |
| 814 | " -t <bool> : Track IO latencies, default %d\n" |
| 815 | " -T <int> : TSC rate in HZ\n", |
| 816 | argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled, |
| 817 | fixedbufs, register_files, nthreads, !buffered, do_nop, stats); |
| 818 | exit(status); |
| 819 | } |
| 820 | |
| 821 | static void read_tsc_rate(void) |
| 822 | { |
| 823 | char buffer[32]; |
| 824 | int fd, ret; |
| 825 | |
| 826 | if (tsc_rate) |
| 827 | return; |
| 828 | |
| 829 | fd = open(TSC_RATE_FILE, O_RDONLY); |
| 830 | if (fd < 0) |
| 831 | return; |
| 832 | |
| 833 | ret = read(fd, buffer, sizeof(buffer)); |
| 834 | if (ret < 0) { |
| 835 | close(fd); |
| 836 | return; |
| 837 | } |
| 838 | |
| 839 | tsc_rate = strtoul(buffer, NULL, 10); |
| 840 | printf("Using TSC rate %luHz\n", tsc_rate); |
| 841 | close(fd); |
| 842 | } |
| 843 | |
| 844 | static void write_tsc_rate(void) |
| 845 | { |
| 846 | char buffer[32]; |
| 847 | struct stat sb; |
| 848 | int fd, ret; |
| 849 | |
| 850 | if (!stat(TSC_RATE_FILE, &sb)) |
| 851 | return; |
| 852 | |
| 853 | fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644); |
| 854 | if (fd < 0) |
| 855 | return; |
| 856 | |
| 857 | memset(buffer, 0, sizeof(buffer)); |
| 858 | sprintf(buffer, "%lu", tsc_rate); |
| 859 | ret = write(fd, buffer, strlen(buffer)); |
| 860 | if (ret < 0) |
| 861 | perror("write"); |
| 862 | close(fd); |
| 863 | } |
| 864 | |
| 865 | int main(int argc, char *argv[]) |
| 866 | { |
| 867 | struct submitter *s; |
| 868 | unsigned long done, calls, reap; |
| 869 | int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles; |
| 870 | struct file f; |
| 871 | char *fdepths; |
| 872 | void *ret; |
| 873 | |
| 874 | if (!do_nop && argc < 2) |
| 875 | usage(argv[0], 1); |
| 876 | |
| 877 | while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:h?")) != -1) { |
| 878 | switch (opt) { |
| 879 | case 'd': |
| 880 | depth = atoi(optarg); |
| 881 | break; |
| 882 | case 's': |
| 883 | batch_submit = atoi(optarg); |
| 884 | if (!batch_submit) |
| 885 | batch_submit = 1; |
| 886 | break; |
| 887 | case 'c': |
| 888 | batch_complete = atoi(optarg); |
| 889 | if (!batch_complete) |
| 890 | batch_complete = 1; |
| 891 | break; |
| 892 | case 'b': |
| 893 | bs = atoi(optarg); |
| 894 | break; |
| 895 | case 'p': |
| 896 | polled = !!atoi(optarg); |
| 897 | break; |
| 898 | case 'B': |
| 899 | fixedbufs = !!atoi(optarg); |
| 900 | break; |
| 901 | case 'F': |
| 902 | register_files = !!atoi(optarg); |
| 903 | break; |
| 904 | case 'n': |
| 905 | nthreads = atoi(optarg); |
| 906 | if (!nthreads) { |
| 907 | printf("Threads must be non-zero\n"); |
| 908 | usage(argv[0], 1); |
| 909 | } |
| 910 | break; |
| 911 | case 'N': |
| 912 | do_nop = !!atoi(optarg); |
| 913 | break; |
| 914 | case 'O': |
| 915 | buffered = !atoi(optarg); |
| 916 | break; |
| 917 | case 't': |
| 918 | #ifndef ARCH_HAVE_CPU_CLOCK |
| 919 | fprintf(stderr, "Stats not supported on this CPU\n"); |
| 920 | return 1; |
| 921 | #endif |
| 922 | stats = !!atoi(optarg); |
| 923 | break; |
| 924 | case 'T': |
| 925 | #ifndef ARCH_HAVE_CPU_CLOCK |
| 926 | fprintf(stderr, "Stats not supported on this CPU\n"); |
| 927 | return 1; |
| 928 | #endif |
| 929 | tsc_rate = strtoul(optarg, NULL, 10); |
| 930 | write_tsc_rate(); |
| 931 | break; |
| 932 | case 'h': |
| 933 | case '?': |
| 934 | default: |
| 935 | usage(argv[0], 0); |
| 936 | break; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | if (stats) |
| 941 | read_tsc_rate(); |
| 942 | |
| 943 | if (batch_complete > depth) |
| 944 | batch_complete = depth; |
| 945 | if (batch_submit > depth) |
| 946 | batch_submit = depth; |
| 947 | |
| 948 | submitter = calloc(nthreads, sizeof(*submitter) + |
| 949 | depth * sizeof(struct iovec)); |
| 950 | for (j = 0; j < nthreads; j++) { |
| 951 | s = get_submitter(j); |
| 952 | s->index = j; |
| 953 | s->done = s->calls = s->reaps = 0; |
| 954 | } |
| 955 | |
| 956 | flags = O_RDONLY | O_NOATIME; |
| 957 | if (!buffered) |
| 958 | flags |= O_DIRECT; |
| 959 | |
| 960 | j = 0; |
| 961 | i = optind; |
| 962 | nfiles = argc - i; |
| 963 | if (!do_nop) { |
| 964 | if (!nfiles) { |
| 965 | printf("No files specified\n"); |
| 966 | usage(argv[0], 1); |
| 967 | } |
| 968 | threads_per_f = nthreads / nfiles; |
| 969 | /* make sure each thread gets assigned files */ |
| 970 | if (threads_per_f == 0) { |
| 971 | threads_per_f = 1; |
| 972 | } else { |
| 973 | threads_rem = nthreads - threads_per_f * nfiles; |
| 974 | } |
| 975 | } |
| 976 | while (!do_nop && i < argc) { |
| 977 | int k, limit; |
| 978 | |
| 979 | memset(&f, 0, sizeof(f)); |
| 980 | |
| 981 | fd = open(argv[i], flags); |
| 982 | if (fd < 0) { |
| 983 | perror("open"); |
| 984 | return 1; |
| 985 | } |
| 986 | f.real_fd = fd; |
| 987 | if (get_file_size(&f)) { |
| 988 | printf("failed getting size of device/file\n"); |
| 989 | return 1; |
| 990 | } |
| 991 | if (f.max_blocks <= 1) { |
| 992 | printf("Zero file/device size?\n"); |
| 993 | return 1; |
| 994 | } |
| 995 | f.max_blocks--; |
| 996 | |
| 997 | limit = threads_per_f; |
| 998 | limit += threads_rem > 0 ? 1 : 0; |
| 999 | for (k = 0; k < limit; k++) { |
| 1000 | s = get_submitter((j + k) % nthreads); |
| 1001 | |
| 1002 | if (s->nr_files == MAX_FDS) { |
| 1003 | printf("Max number of files (%d) reached\n", MAX_FDS); |
| 1004 | break; |
| 1005 | } |
| 1006 | |
| 1007 | memcpy(&s->files[s->nr_files], &f, sizeof(f)); |
| 1008 | |
| 1009 | printf("Added file %s (submitter %d)\n", argv[i], s->index); |
| 1010 | s->nr_files++; |
| 1011 | } |
| 1012 | threads_rem--; |
| 1013 | i++; |
| 1014 | j += limit; |
| 1015 | } |
| 1016 | |
| 1017 | arm_sig_int(); |
| 1018 | |
| 1019 | for (j = 0; j < nthreads; j++) { |
| 1020 | s = get_submitter(j); |
| 1021 | for (i = 0; i < depth; i++) { |
| 1022 | void *buf; |
| 1023 | |
| 1024 | if (posix_memalign(&buf, bs, bs)) { |
| 1025 | printf("failed alloc\n"); |
| 1026 | return 1; |
| 1027 | } |
| 1028 | s->iovecs[i].iov_base = buf; |
| 1029 | s->iovecs[i].iov_len = bs; |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | for (j = 0; j < nthreads; j++) { |
| 1034 | s = get_submitter(j); |
| 1035 | |
| 1036 | err = setup_ring(s); |
| 1037 | if (err) { |
| 1038 | printf("ring setup failed: %s, %d\n", strerror(errno), err); |
| 1039 | return 1; |
| 1040 | } |
| 1041 | } |
| 1042 | s = get_submitter(0); |
| 1043 | printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d", polled, fixedbufs, register_files, buffered); |
| 1044 | printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries); |
| 1045 | |
| 1046 | for (j = 0; j < nthreads; j++) { |
| 1047 | s = get_submitter(j); |
| 1048 | pthread_create(&s->thread, NULL, submitter_fn, s); |
| 1049 | } |
| 1050 | |
| 1051 | fdepths = malloc(8 * s->nr_files * nthreads); |
| 1052 | reap = calls = done = 0; |
| 1053 | do { |
| 1054 | unsigned long this_done = 0; |
| 1055 | unsigned long this_reap = 0; |
| 1056 | unsigned long this_call = 0; |
| 1057 | unsigned long rpc = 0, ipc = 0; |
| 1058 | unsigned long iops, bw; |
| 1059 | |
| 1060 | sleep(1); |
| 1061 | for (j = 0; j < nthreads; j++) { |
| 1062 | this_done += s->done; |
| 1063 | this_call += s->calls; |
| 1064 | this_reap += s->reaps; |
| 1065 | } |
| 1066 | if (this_call - calls) { |
| 1067 | rpc = (this_done - done) / (this_call - calls); |
| 1068 | ipc = (this_reap - reap) / (this_call - calls); |
| 1069 | } else |
| 1070 | rpc = ipc = -1; |
| 1071 | file_depths(fdepths); |
| 1072 | iops = this_done - done; |
| 1073 | if (bs > 1048576) |
| 1074 | bw = iops * (bs / 1048576); |
| 1075 | else |
| 1076 | bw = iops / (1048576 / bs); |
| 1077 | printf("IOPS=%lu, ", iops); |
| 1078 | if (!do_nop) |
| 1079 | printf("BW=%luMiB/s, ", bw); |
| 1080 | printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths); |
| 1081 | done = this_done; |
| 1082 | calls = this_call; |
| 1083 | reap = this_reap; |
| 1084 | } while (!finish); |
| 1085 | |
| 1086 | for (j = 0; j < nthreads; j++) { |
| 1087 | s = get_submitter(j); |
| 1088 | pthread_join(s->thread, &ret); |
| 1089 | close(s->ring_fd); |
| 1090 | |
| 1091 | if (stats) { |
| 1092 | unsigned long nr; |
| 1093 | |
| 1094 | printf("%d: Latency percentiles:\n", s->tid); |
| 1095 | for (i = 0, nr = 0; i < PLAT_NR; i++) |
| 1096 | nr += s->plat[i]; |
| 1097 | show_clat_percentiles(s->plat, nr, 4); |
| 1098 | free(s->clock_batch); |
| 1099 | free(s->plat); |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | free(fdepths); |
| 1104 | free(submitter); |
| 1105 | return 0; |
| 1106 | } |