|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +#include "liburing.h" |
| 7 | +#include "helpers.h" |
| 8 | + |
| 9 | +static int get_iowait(int cpu) |
| 10 | +{ |
| 11 | + char cpu_buf[32], this_cpu[32]; |
| 12 | + int user, nice, system, idle, iowait, ret; |
| 13 | + FILE *file; |
| 14 | + |
| 15 | + file = fopen("/proc/stat", "r"); |
| 16 | + if (!file) { |
| 17 | + perror("fopen stat"); |
| 18 | + return 0; |
| 19 | + } |
| 20 | + |
| 21 | + sprintf(this_cpu, "cpu%d", cpu); |
| 22 | + ret = 0; |
| 23 | + do { |
| 24 | + int r; |
| 25 | + r = fscanf(file, "%s %d %d %d %d %d", cpu_buf, &user, &nice, |
| 26 | + &system, &idle, &iowait); |
| 27 | + if (r != 6) |
| 28 | + continue; |
| 29 | + if (strncmp(cpu_buf, this_cpu, strlen(this_cpu))) |
| 30 | + continue; |
| 31 | + ret = iowait; |
| 32 | + break; |
| 33 | + } while (1); |
| 34 | + |
| 35 | + fclose(file); |
| 36 | + return ret; |
| 37 | +} |
| 38 | + |
| 39 | +static int test(struct io_uring *ring, int with_iowait, int cpu) |
| 40 | +{ |
| 41 | + struct io_uring_sqe *sqe; |
| 42 | + struct io_uring_cqe *cqe; |
| 43 | + int iowait_pre, iowait_post; |
| 44 | + struct __kernel_timespec ts; |
| 45 | + int ret, fds[2], diff; |
| 46 | + char buf[32]; |
| 47 | + |
| 48 | + if (!(ring->features & IORING_FEAT_NO_IOWAIT)) |
| 49 | + return T_EXIT_SKIP; |
| 50 | + |
| 51 | + if (pipe(fds) < 0) { |
| 52 | + perror("pipe"); |
| 53 | + return T_EXIT_FAIL; |
| 54 | + } |
| 55 | + |
| 56 | + ret = io_uring_set_iowait(ring, with_iowait); |
| 57 | + if (ret) { |
| 58 | + fprintf(stderr, "set_iowait=%d\n", ret); |
| 59 | + return T_EXIT_FAIL; |
| 60 | + } |
| 61 | + |
| 62 | + sqe = io_uring_get_sqe(ring); |
| 63 | + io_uring_prep_read(sqe, fds[0], buf, sizeof(buf), 0); |
| 64 | + io_uring_submit(ring); |
| 65 | + |
| 66 | + ts.tv_sec = 1; |
| 67 | + ts.tv_nsec = 0; |
| 68 | + iowait_pre = get_iowait(cpu); |
| 69 | + ret = io_uring_wait_cqe_timeout(ring, &cqe, &ts); |
| 70 | + if (ret != -ETIME) { |
| 71 | + fprintf(stderr, "Unexpected wait ret: %d\n", ret); |
| 72 | + return T_EXIT_FAIL; |
| 73 | + } |
| 74 | + iowait_post = get_iowait(cpu); |
| 75 | + diff = iowait_post - iowait_pre; |
| 76 | + |
| 77 | + close(fds[0]); |
| 78 | + close(fds[1]); |
| 79 | + |
| 80 | + ret = io_uring_wait_cqe(ring, &cqe); |
| 81 | + if (ret) { |
| 82 | + fprintf(stderr, "Unexpected wait ret: %d\n", ret); |
| 83 | + return T_EXIT_FAIL; |
| 84 | + } |
| 85 | + io_uring_cqe_seen(ring, cqe); |
| 86 | + |
| 87 | + if (with_iowait) { |
| 88 | + if (diff < 90) { |
| 89 | + fprintf(stderr, "iowait diff too small: %d\n", diff); |
| 90 | + return T_EXIT_FAIL; |
| 91 | + } |
| 92 | + } else { |
| 93 | + if (diff > 10) { |
| 94 | + fprintf(stderr, "iowait diff too large: %d\n", diff); |
| 95 | + return T_EXIT_FAIL; |
| 96 | + } |
| 97 | + } |
| 98 | + return T_EXIT_PASS; |
| 99 | +} |
| 100 | + |
| 101 | +int main(int argc, char *argv[]) |
| 102 | +{ |
| 103 | + struct io_uring ring; |
| 104 | + cpu_set_t cpuset; |
| 105 | + int ret; |
| 106 | + |
| 107 | + CPU_ZERO(&cpuset); |
| 108 | + CPU_SET(0, &cpuset); |
| 109 | + if (sched_setaffinity(0, sizeof(cpuset), &cpuset)) |
| 110 | + return T_EXIT_SKIP; |
| 111 | + |
| 112 | + ret = t_create_ring(64, &ring, 0); |
| 113 | + if (ret == T_SETUP_SKIP) |
| 114 | + return T_EXIT_SKIP; |
| 115 | + else if (ret != T_SETUP_OK) |
| 116 | + return T_EXIT_FAIL; |
| 117 | + |
| 118 | + ret = test(&ring, 0, 0); |
| 119 | + if (ret == T_EXIT_SKIP) { |
| 120 | + return T_EXIT_SKIP; |
| 121 | + } else if (ret == T_EXIT_FAIL) { |
| 122 | + fprintf(stderr, "test 0 failed\n"); |
| 123 | + return T_EXIT_FAIL; |
| 124 | + } |
| 125 | + |
| 126 | + ret = test(&ring, 1, 0); |
| 127 | + if (ret == T_EXIT_SKIP) { |
| 128 | + return T_EXIT_SKIP; |
| 129 | + } else if (ret == T_EXIT_FAIL) { |
| 130 | + fprintf(stderr, "test 1 failed\n"); |
| 131 | + return T_EXIT_FAIL; |
| 132 | + } |
| 133 | + |
| 134 | + io_uring_queue_exit(&ring); |
| 135 | + return 0; |
| 136 | +} |
0 commit comments