diff options
Diffstat (limited to 'tests/t_fdstate.c')
| -rw-r--r-- | tests/t_fdstate.c | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/tests/t_fdstate.c b/tests/t_fdstate.c new file mode 100644 index 0000000..bf8724a --- /dev/null +++ b/tests/t_fdstate.c | |||
| @@ -0,0 +1,224 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | /* System */ | ||
| 7 | #include <pthread.h> | ||
| 8 | #include <stdatomic.h> | ||
| 9 | #include <stdint.h> | ||
| 10 | #include <stdlib.h> | ||
| 11 | #include <string.h> | ||
| 12 | |||
| 13 | /* Opentracker */ | ||
| 14 | #include "ot_fdstate.h" | ||
| 15 | #include "test.h" | ||
| 16 | |||
| 17 | /* ------------------------------------------------------- one thread first */ | ||
| 18 | |||
| 19 | static void case_open_claim_release(void) { | ||
| 20 | char cookie[4]; | ||
| 21 | uint32_t generation; | ||
| 22 | |||
| 23 | test_case("a descriptor taken into the table can be claimed and gives back its cookie"); | ||
| 24 | generation = fdstate_open(7, cookie, 0); | ||
| 25 | CHECK_INT(fdstate_claim(7, generation), 1); | ||
| 26 | CHECK_PTR(fdstate_cookie(7), cookie); | ||
| 27 | fdstate_release(7); | ||
| 28 | |||
| 29 | test_case("and can be claimed again once released"); | ||
| 30 | CHECK_INT(fdstate_claim(7, generation), 1); | ||
| 31 | fdstate_release(7); | ||
| 32 | fdstate_close(7); | ||
| 33 | } | ||
| 34 | |||
| 35 | static void case_one_holder_at_a_time(void) { | ||
| 36 | uint32_t generation = fdstate_open(8, NULL, 0); | ||
| 37 | |||
| 38 | test_case("a descriptor somebody holds cannot be claimed again"); | ||
| 39 | CHECK_INT(fdstate_claim(8, generation), 1); | ||
| 40 | CHECK_INT(fdstate_claim(8, generation), 0); | ||
| 41 | fdstate_release(8); | ||
| 42 | CHECK_INT(fdstate_claim(8, generation), 1); | ||
| 43 | fdstate_release(8); | ||
| 44 | fdstate_close(8); | ||
| 45 | } | ||
| 46 | |||
| 47 | static void case_stale_generation_is_refused(void) { | ||
| 48 | char first[4], second[4]; | ||
| 49 | uint32_t old, new; | ||
| 50 | |||
| 51 | test_case("a caller quoting the generation of a closed descriptor is turned away"); | ||
| 52 | old = fdstate_open(9, first, 0); | ||
| 53 | fdstate_close(9); | ||
| 54 | new = fdstate_open(9, second, 0); | ||
| 55 | CHECK(old != new); | ||
| 56 | CHECK_INT(fdstate_claim(9, old), 0); | ||
| 57 | |||
| 58 | test_case("while the one quoting the new generation gets the new cookie"); | ||
| 59 | CHECK_INT(fdstate_claim(9, new), 1); | ||
| 60 | CHECK_PTR(fdstate_cookie(9), second); | ||
| 61 | fdstate_release(9); | ||
| 62 | fdstate_close(9); | ||
| 63 | } | ||
| 64 | |||
| 65 | static void case_expiry(void) { | ||
| 66 | uint32_t generation = fdstate_open(10, NULL, 100); | ||
| 67 | |||
| 68 | test_case("a descriptor past its expiry is collected"); | ||
| 69 | CHECK_INT(fdstate_claim_expired(10, 99), 0); | ||
| 70 | CHECK_INT(fdstate_claim_expired(10, 100), 1); | ||
| 71 | fdstate_close(10); | ||
| 72 | |||
| 73 | test_case("one that never expires is never collected"); | ||
| 74 | generation = fdstate_open(10, NULL, 0); | ||
| 75 | CHECK_INT(fdstate_claim_expired(10, 1 << 30), 0); | ||
| 76 | |||
| 77 | test_case("and one somebody is working on is left alone"); | ||
| 78 | fdstate_set_expiry(10, 1); | ||
| 79 | CHECK_INT(fdstate_claim(10, generation), 1); | ||
| 80 | CHECK_INT(fdstate_claim_expired(10, 1 << 30), 0); | ||
| 81 | fdstate_release(10); | ||
| 82 | CHECK_INT(fdstate_claim_expired(10, 1 << 30), 1); | ||
| 83 | fdstate_close(10); | ||
| 84 | } | ||
| 85 | |||
| 86 | static void case_out_of_range(void) { | ||
| 87 | test_case("a descriptor outside the table is refused rather than trusted"); | ||
| 88 | CHECK_INT(fdstate_claim(-1, 0), 0); | ||
| 89 | CHECK_INT(fdstate_claim((int)fdstate_slots(), 0), 0); | ||
| 90 | CHECK_INT(fdstate_claim_expired(-1, 0), 0); | ||
| 91 | CHECK_PTR(fdstate_cookie(-1), NULL); | ||
| 92 | } | ||
| 93 | |||
| 94 | /* --------------------------------------------------------- then in anger */ | ||
| 95 | |||
| 96 | /* The race this is about only exists while a descriptor is being recycled, | ||
| 97 | so recycling is driven by work done rather than by a clock: every slot is | ||
| 98 | given up after RECYCLE_AFTER claims, whatever the machine's speed. */ | ||
| 99 | |||
| 100 | #define STRESS_SLOTS 64 | ||
| 101 | #define RECYCLE_AFTER 32 | ||
| 102 | #define STRESS_CLAIMS 200000 | ||
| 103 | #define STRESS_GIVEUP (STRESS_CLAIMS * 100) | ||
| 104 | |||
| 105 | static _Atomic long stress_claims, stress_recycles, stress_violations, stress_attempts; | ||
| 106 | static _Atomic int stress_lives[STRESS_SLOTS]; | ||
| 107 | static _Atomic int stress_live[STRESS_SLOTS]; | ||
| 108 | |||
| 109 | /* Generation and cookie are published in one word so a worker can never read | ||
| 110 | a cookie that belongs to a different generation than the one it claimed | ||
| 111 | with. Each connection gets a cookie nobody else ever gets. */ | ||
| 112 | static _Atomic uint64_t stress_conn[STRESS_SLOTS]; | ||
| 113 | static _Atomic uint64_t stress_next_cookie; | ||
| 114 | |||
| 115 | #define STRESS_CONN(cookie, gen) (((uint64_t)(cookie) << 32) | (uint32_t)(gen)) | ||
| 116 | #define STRESS_CONN_COOKIE(w) ((void *)(uintptr_t)((w) >> 32)) | ||
| 117 | #define STRESS_CONN_GEN(w) ((uint32_t)(w)) | ||
| 118 | |||
| 119 | static int stress_running(void) { | ||
| 120 | return atomic_load(&stress_claims) < STRESS_CLAIMS && atomic_load(&stress_attempts) < STRESS_GIVEUP; | ||
| 121 | } | ||
| 122 | |||
| 123 | static void *stress_worker(void *arg) { | ||
| 124 | unsigned seed = (unsigned)(uintptr_t)arg; | ||
| 125 | |||
| 126 | while (stress_running()) { | ||
| 127 | int fd = (int)(rand_r(&seed) % STRESS_SLOTS); | ||
| 128 | uint64_t conn = atomic_load(&stress_conn[fd]); | ||
| 129 | |||
| 130 | atomic_fetch_add(&stress_attempts, 1); | ||
| 131 | if (!fdstate_claim(fd, STRESS_CONN_GEN(conn))) | ||
| 132 | continue; | ||
| 133 | |||
| 134 | if (fdstate_cookie(fd) != STRESS_CONN_COOKIE(conn)) | ||
| 135 | atomic_fetch_add(&stress_violations, 1); | ||
| 136 | if (atomic_fetch_sub(&stress_lives[fd], 1) <= 1) | ||
| 137 | fdstate_set_expiry(fd, 1); /* retired by its holder, as a handler would */ | ||
| 138 | if (fdstate_cookie(fd) != STRESS_CONN_COOKIE(conn)) | ||
| 139 | atomic_fetch_add(&stress_violations, 1); | ||
| 140 | |||
| 141 | fdstate_release(fd); | ||
| 142 | atomic_fetch_add(&stress_claims, 1); | ||
| 143 | } | ||
| 144 | return NULL; | ||
| 145 | } | ||
| 146 | |||
| 147 | static void *stress_keeper(void *arg) { | ||
| 148 | (void)arg; | ||
| 149 | while (stress_running()) { | ||
| 150 | int fd; | ||
| 151 | |||
| 152 | for (fd = 0; fd < STRESS_SLOTS; ++fd) { | ||
| 153 | if (atomic_load(&stress_live[fd])) { | ||
| 154 | if (!fdstate_claim_expired(fd, 1)) | ||
| 155 | continue; /* a worker has it, come back */ | ||
| 156 | fdstate_close(fd); | ||
| 157 | atomic_store(&stress_live[fd], 0); | ||
| 158 | atomic_fetch_add(&stress_recycles, 1); | ||
| 159 | } | ||
| 160 | { | ||
| 161 | void *cookie = (void *)(uintptr_t)(atomic_fetch_add(&stress_next_cookie, 1) + 1); | ||
| 162 | uint32_t gen = fdstate_open(fd, cookie, 0); | ||
| 163 | |||
| 164 | atomic_store(&stress_lives[fd], RECYCLE_AFTER); | ||
| 165 | atomic_store(&stress_conn[fd], STRESS_CONN((uintptr_t)cookie, gen)); | ||
| 166 | atomic_store(&stress_live[fd], 1); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | } | ||
| 170 | return NULL; | ||
| 171 | } | ||
| 172 | |||
| 173 | static void case_recycling_under_load(void) { | ||
| 174 | pthread_t worker[4], keeper; | ||
| 175 | size_t i; | ||
| 176 | |||
| 177 | test_case("no worker is ever handed a connection from another generation"); | ||
| 178 | atomic_store(&stress_claims, 0); | ||
| 179 | atomic_store(&stress_recycles, 0); | ||
| 180 | atomic_store(&stress_violations, 0); | ||
| 181 | atomic_store(&stress_attempts, 0); | ||
| 182 | atomic_store(&stress_next_cookie, 0); | ||
| 183 | /* Every slot is opened and published before any worker runs: a worker only | ||
| 184 | ever learns of a descriptor from whoever registered it, and never | ||
| 185 | invents a generation of its own. */ | ||
| 186 | for (i = 0; i < STRESS_SLOTS; ++i) { | ||
| 187 | void *cookie = (void *)(uintptr_t)(atomic_fetch_add(&stress_next_cookie, 1) + 1); | ||
| 188 | uint32_t gen = fdstate_open((int)i, cookie, 0); | ||
| 189 | |||
| 190 | atomic_store(&stress_lives[i], RECYCLE_AFTER); | ||
| 191 | atomic_store(&stress_conn[i], STRESS_CONN((uintptr_t)cookie, gen)); | ||
| 192 | atomic_store(&stress_live[i], 1); | ||
| 193 | } | ||
| 194 | |||
| 195 | pthread_create(&keeper, NULL, stress_keeper, NULL); | ||
| 196 | for (i = 0; i < sizeof(worker) / sizeof(worker[0]); ++i) | ||
| 197 | pthread_create(&worker[i], NULL, stress_worker, (void *)(uintptr_t)(i + 1)); | ||
| 198 | for (i = 0; i < sizeof(worker) / sizeof(worker[0]); ++i) | ||
| 199 | pthread_join(worker[i], NULL); | ||
| 200 | pthread_join(keeper, NULL); | ||
| 201 | |||
| 202 | CHECK_INT(atomic_load(&stress_violations), 0); | ||
| 203 | |||
| 204 | test_case("and the run actually recycled descriptors, or it proved nothing"); | ||
| 205 | CHECK(atomic_load(&stress_claims) >= STRESS_CLAIMS); | ||
| 206 | CHECK(atomic_load(&stress_recycles) > STRESS_CLAIMS / (RECYCLE_AFTER * 4)); | ||
| 207 | |||
| 208 | for (i = 0; i < STRESS_SLOTS; ++i) | ||
| 209 | fdstate_close((int)i); | ||
| 210 | } | ||
| 211 | |||
| 212 | void test_fdstate(void) { | ||
| 213 | CHECK_INT(fdstate_init(), 0); | ||
| 214 | CHECK(fdstate_slots() > STRESS_SLOTS); | ||
| 215 | |||
| 216 | case_open_claim_release(); | ||
| 217 | case_one_holder_at_a_time(); | ||
| 218 | case_stale_generation_is_refused(); | ||
| 219 | case_expiry(); | ||
| 220 | case_out_of_range(); | ||
| 221 | case_recycling_under_load(); | ||
| 222 | |||
| 223 | fdstate_deinit(); | ||
| 224 | } | ||
