[94b39ba] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Adam Hraska
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup test
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file rcutest.c
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[498ced1] | 37 | #include <atomic.h>
|
---|
[94b39ba] | 38 | #include <stdio.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
| 40 | #include <stdint.h>
|
---|
[c1694b6b] | 41 | #include <str_error.h>
|
---|
[94b39ba] | 42 | #include <mem.h>
|
---|
| 43 | #include <errno.h>
|
---|
| 44 | #include <assert.h>
|
---|
[29b8138] | 45 | #include <async.h>
|
---|
[94b39ba] | 46 | #include <fibril.h>
|
---|
[5d230a30] | 47 | #include <fibril_synch.h>
|
---|
[29b8138] | 48 | #include <compiler/barrier.h>
|
---|
[1d6dd2a] | 49 | #include <str.h>
|
---|
[94b39ba] | 50 |
|
---|
| 51 | #include <rcu.h>
|
---|
| 52 |
|
---|
[d04e46e] | 53 |
|
---|
[94b39ba] | 54 |
|
---|
[29b8138] | 55 | #define USECS_PER_SEC (1000 * 1000)
|
---|
| 56 | #define USECS_PER_MS 1000
|
---|
| 57 |
|
---|
[d04e46e] | 58 | /* fwd decl. */
|
---|
| 59 | struct test_info;
|
---|
[29b8138] | 60 |
|
---|
| 61 | typedef struct test_desc {
|
---|
| 62 | /* Aggregate test that runs other tests already in the table test_desc. */
|
---|
| 63 | bool aggregate;
|
---|
| 64 | enum {
|
---|
| 65 | T_OTHER,
|
---|
| 66 | T_SANITY,
|
---|
| 67 | T_STRESS
|
---|
| 68 | } type;
|
---|
[1433ecda] | 69 | bool (*func)(struct test_info *);
|
---|
[29b8138] | 70 | const char *name;
|
---|
| 71 | const char *desc;
|
---|
| 72 | } test_desc_t;
|
---|
| 73 |
|
---|
| 74 |
|
---|
[d04e46e] | 75 | typedef struct test_info {
|
---|
| 76 | size_t thread_cnt;
|
---|
| 77 | test_desc_t *desc;
|
---|
| 78 | } test_info_t;
|
---|
| 79 |
|
---|
| 80 |
|
---|
[29b8138] | 81 |
|
---|
[1433ecda] | 82 | static bool run_all_tests(struct test_info *);
|
---|
| 83 | static bool run_sanity_tests(struct test_info *);
|
---|
| 84 | static bool run_stress_tests(struct test_info *);
|
---|
[d04e46e] | 85 |
|
---|
[1433ecda] | 86 | static bool wait_for_one_reader(struct test_info *);
|
---|
| 87 | static bool basic_sanity_check(struct test_info *);
|
---|
| 88 | static bool dont_wait_for_new_reader(struct test_info *);
|
---|
| 89 | static bool wait_for_exiting_reader(struct test_info *);
|
---|
| 90 | static bool seq_test(struct test_info *);
|
---|
[5d230a30] | 91 |
|
---|
[29b8138] | 92 |
|
---|
| 93 | static test_desc_t test_desc[] = {
|
---|
| 94 | {
|
---|
| 95 | .aggregate = true,
|
---|
| 96 | .type = T_OTHER,
|
---|
| 97 | .func = run_all_tests,
|
---|
| 98 | .name = "*",
|
---|
| 99 | .desc = "Runs all tests.",
|
---|
| 100 | },
|
---|
| 101 | {
|
---|
| 102 | .aggregate = true,
|
---|
| 103 | .type = T_SANITY,
|
---|
| 104 | .func = run_sanity_tests,
|
---|
| 105 | .name = "sanity-tests",
|
---|
| 106 | .desc = "Runs all RCU sanity tests.",
|
---|
| 107 | },
|
---|
| 108 | {
|
---|
| 109 | .aggregate = true,
|
---|
| 110 | .type = T_STRESS,
|
---|
| 111 | .func = run_stress_tests,
|
---|
| 112 | .name = "stress-tests",
|
---|
| 113 | .desc = "Runs all RCU stress tests.",
|
---|
| 114 | },
|
---|
| 115 |
|
---|
| 116 | {
|
---|
| 117 | .aggregate = false,
|
---|
| 118 | .type = T_SANITY,
|
---|
| 119 | .func = basic_sanity_check,
|
---|
| 120 | .name = "basic-sanity",
|
---|
[a879d73] | 121 | .desc = "Locks/unlocks and syncs in 1 fibril, no contention.",
|
---|
[29b8138] | 122 | },
|
---|
| 123 | {
|
---|
| 124 | .aggregate = false,
|
---|
| 125 | .type = T_SANITY,
|
---|
| 126 | .func = wait_for_one_reader,
|
---|
| 127 | .name = "wait-for-one",
|
---|
| 128 | .desc = "Syncs with one 2 secs sleeping reader.",
|
---|
| 129 | },
|
---|
| 130 | {
|
---|
| 131 | .aggregate = false,
|
---|
| 132 | .type = T_SANITY,
|
---|
| 133 | .func = dont_wait_for_new_reader,
|
---|
| 134 | .name = "ignore-new-r",
|
---|
[a879d73] | 135 | .desc = "Syncs with preexisting reader; ignores new reader.",
|
---|
| 136 | },
|
---|
| 137 | {
|
---|
| 138 | .aggregate = false,
|
---|
| 139 | .type = T_SANITY,
|
---|
| 140 | .func = wait_for_exiting_reader,
|
---|
| 141 | .name = "dereg-unlocks",
|
---|
| 142 | .desc = "Lets deregister_fibril unlock the reader section.",
|
---|
[29b8138] | 143 | },
|
---|
[5d230a30] | 144 | {
|
---|
| 145 | .aggregate = false,
|
---|
| 146 | .type = T_STRESS,
|
---|
| 147 | .func = seq_test,
|
---|
| 148 | .name = "seq",
|
---|
| 149 | .desc = "Checks lock/unlock/sync w/ global time sequence.",
|
---|
| 150 | },
|
---|
[29b8138] | 151 | {
|
---|
| 152 | .aggregate = false,
|
---|
| 153 | .type = T_OTHER,
|
---|
| 154 | .func = NULL,
|
---|
| 155 | .name = "(null)",
|
---|
| 156 | .desc = "",
|
---|
| 157 | },
|
---|
| 158 | };
|
---|
| 159 |
|
---|
| 160 | static const size_t test_desc_cnt = sizeof(test_desc) / sizeof(test_desc[0]);
|
---|
| 161 |
|
---|
| 162 | /*--------------------------------------------------------------------*/
|
---|
| 163 |
|
---|
[5d230a30] | 164 | static size_t next_rand(size_t seed)
|
---|
| 165 | {
|
---|
| 166 | return (seed * 1103515245 + 12345) & ((1U << 31) - 1);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 |
|
---|
[b7fd2a0] | 170 | typedef errno_t (*fibril_func_t)(void *);
|
---|
[29b8138] | 171 |
|
---|
[1433ecda] | 172 | static bool create_fibril(errno_t (*func)(void *), void *arg)
|
---|
[29b8138] | 173 | {
|
---|
| 174 | fid_t fid = fibril_create(func, arg);
|
---|
[a35b458] | 175 |
|
---|
[29b8138] | 176 | if (0 == fid) {
|
---|
| 177 | printf("Failed to create a fibril!\n");
|
---|
| 178 | return false;
|
---|
| 179 | }
|
---|
[a35b458] | 180 |
|
---|
[29b8138] | 181 | fibril_add_ready(fid);
|
---|
| 182 | return true;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /*--------------------------------------------------------------------*/
|
---|
| 186 |
|
---|
[1b20da0] | 187 | static bool run_tests(test_info_t *info, bool (*include_filter)(test_desc_t *))
|
---|
[29b8138] | 188 | {
|
---|
| 189 | size_t failed_cnt = 0;
|
---|
| 190 | size_t ok_cnt = 0;
|
---|
[a35b458] | 191 |
|
---|
[29b8138] | 192 | for (size_t i = 0; i < test_desc_cnt; ++i) {
|
---|
| 193 | test_desc_t *t = &test_desc[i];
|
---|
[a35b458] | 194 |
|
---|
[29b8138] | 195 | if (t->func && !t->aggregate && include_filter(t)) {
|
---|
| 196 | printf("Running \'%s\'...\n", t->name);
|
---|
[d04e46e] | 197 | bool ok = test_desc[i].func(info);
|
---|
[a35b458] | 198 |
|
---|
[29b8138] | 199 | if (ok) {
|
---|
| 200 | ++ok_cnt;
|
---|
| 201 | printf("Passed: \'%s\'\n", t->name);
|
---|
| 202 | } else {
|
---|
| 203 | ++failed_cnt;
|
---|
| 204 | printf("FAILED: \'%s\'\n", t->name);
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
[a35b458] | 208 |
|
---|
[29b8138] | 209 | printf("\n");
|
---|
| 210 |
|
---|
| 211 | printf("%zu tests passed\n", ok_cnt);
|
---|
| 212 |
|
---|
| 213 | if (failed_cnt) {
|
---|
| 214 | printf("%zu tests failed\n", failed_cnt);
|
---|
[1b20da0] | 215 | }
|
---|
[a35b458] | 216 |
|
---|
[29b8138] | 217 | return 0 == failed_cnt;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /*--------------------------------------------------------------------*/
|
---|
| 221 |
|
---|
| 222 | static bool all_tests_include_filter(test_desc_t *desc)
|
---|
| 223 | {
|
---|
| 224 | return true;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /* Runs all available tests tests one-by-one. */
|
---|
[d04e46e] | 228 | static bool run_all_tests(test_info_t *test_info)
|
---|
[29b8138] | 229 | {
|
---|
| 230 | printf("Running all tests...\n");
|
---|
[d04e46e] | 231 | return run_tests(test_info, all_tests_include_filter);
|
---|
[29b8138] | 232 | }
|
---|
| 233 |
|
---|
| 234 | /*--------------------------------------------------------------------*/
|
---|
| 235 |
|
---|
| 236 | static bool stress_tests_include_filter(test_desc_t *desc)
|
---|
| 237 | {
|
---|
| 238 | return desc->type == T_STRESS;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /* Runs all available stress tests one-by-one. */
|
---|
[d04e46e] | 242 | static bool run_stress_tests(test_info_t *test_info)
|
---|
[29b8138] | 243 | {
|
---|
| 244 | printf("Running stress tests...\n");
|
---|
[d04e46e] | 245 | return run_tests(test_info, stress_tests_include_filter);
|
---|
[29b8138] | 246 | }
|
---|
| 247 |
|
---|
| 248 | /*--------------------------------------------------------------------*/
|
---|
| 249 |
|
---|
| 250 | static bool sanity_tests_include_filter(test_desc_t *desc)
|
---|
| 251 | {
|
---|
| 252 | return desc->type == T_SANITY;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | /* Runs all available sanity tests one-by-one. */
|
---|
[d04e46e] | 256 | static bool run_sanity_tests(test_info_t *test_info)
|
---|
[29b8138] | 257 | {
|
---|
| 258 | printf("Running sanity tests...\n");
|
---|
[d04e46e] | 259 | return run_tests(test_info, sanity_tests_include_filter);
|
---|
[29b8138] | 260 | }
|
---|
| 261 |
|
---|
| 262 | /*--------------------------------------------------------------------*/
|
---|
| 263 |
|
---|
| 264 | /* Locks/unlocks rcu and synchronizes without contention in a single fibril. */
|
---|
[d04e46e] | 265 | static bool basic_sanity_check(test_info_t *test_info)
|
---|
[94b39ba] | 266 | {
|
---|
| 267 | rcu_read_lock();
|
---|
[29b8138] | 268 | /* nop */
|
---|
| 269 | rcu_read_unlock();
|
---|
| 270 |
|
---|
[94b39ba] | 271 | rcu_read_lock();
|
---|
| 272 | /* nop */
|
---|
| 273 | rcu_read_unlock();
|
---|
[a35b458] | 274 |
|
---|
[29b8138] | 275 | rcu_synchronize();
|
---|
| 276 |
|
---|
| 277 | /* Nested lock with yield(). */
|
---|
| 278 | rcu_read_lock();
|
---|
| 279 | fibril_yield();
|
---|
| 280 | rcu_read_lock();
|
---|
| 281 | fibril_yield();
|
---|
| 282 | rcu_read_unlock();
|
---|
| 283 | fibril_yield();
|
---|
[94b39ba] | 284 | rcu_read_unlock();
|
---|
[a35b458] | 285 |
|
---|
[29b8138] | 286 | fibril_yield();
|
---|
| 287 | rcu_synchronize();
|
---|
[94b39ba] | 288 | rcu_synchronize();
|
---|
[a35b458] | 289 |
|
---|
[29b8138] | 290 | rcu_read_lock();
|
---|
| 291 | /* nop */
|
---|
| 292 | if (!rcu_read_locked())
|
---|
| 293 | return false;
|
---|
| 294 |
|
---|
| 295 | rcu_read_unlock();
|
---|
[a35b458] | 296 |
|
---|
[29b8138] | 297 | return !rcu_read_locked();
|
---|
[94b39ba] | 298 | }
|
---|
| 299 |
|
---|
[29b8138] | 300 | typedef struct one_reader_info {
|
---|
| 301 | bool entered_cs;
|
---|
| 302 | bool exited_cs;
|
---|
| 303 | size_t done_sleeps_cnt;
|
---|
| 304 | bool synching;
|
---|
| 305 | bool synched;
|
---|
[a879d73] | 306 | size_t failed;
|
---|
[29b8138] | 307 | } one_reader_info_t;
|
---|
| 308 |
|
---|
| 309 |
|
---|
[b7fd2a0] | 310 | static errno_t sleeping_reader(one_reader_info_t *arg)
|
---|
[94b39ba] | 311 | {
|
---|
[29b8138] | 312 | rcu_register_fibril();
|
---|
[a35b458] | 313 |
|
---|
[29b8138] | 314 | printf("lock{");
|
---|
| 315 | rcu_read_lock();
|
---|
[a879d73] | 316 | rcu_read_lock();
|
---|
[29b8138] | 317 | arg->entered_cs = true;
|
---|
[a879d73] | 318 | rcu_read_unlock();
|
---|
[94b39ba] | 319 |
|
---|
[29b8138] | 320 | printf("r-sleep{");
|
---|
| 321 | /* 2 sec */
|
---|
[5f97ef44] | 322 | fibril_usleep(2 * USECS_PER_SEC);
|
---|
[29b8138] | 323 | ++arg->done_sleeps_cnt;
|
---|
| 324 | printf("}");
|
---|
[a35b458] | 325 |
|
---|
[a879d73] | 326 | if (arg->synched) {
|
---|
| 327 | arg->failed = 1;
|
---|
| 328 | printf("Error: rcu_sync exited prematurely.\n");
|
---|
| 329 | }
|
---|
[a35b458] | 330 |
|
---|
[29b8138] | 331 | arg->exited_cs = true;
|
---|
| 332 | rcu_read_unlock();
|
---|
| 333 | printf("}");
|
---|
[a35b458] | 334 |
|
---|
[29b8138] | 335 | rcu_deregister_fibril();
|
---|
| 336 | return 0;
|
---|
| 337 | }
|
---|
[94b39ba] | 338 |
|
---|
[d04e46e] | 339 | static bool wait_for_one_reader(test_info_t *test_info)
|
---|
[94b39ba] | 340 | {
|
---|
[29b8138] | 341 | one_reader_info_t info = { 0 };
|
---|
[a35b458] | 342 |
|
---|
[29b8138] | 343 | if (!create_fibril((fibril_func_t) sleeping_reader, &info))
|
---|
| 344 | return false;
|
---|
[a35b458] | 345 |
|
---|
[29b8138] | 346 | /* 1 sec, waits for the reader to enter its critical section and sleep. */
|
---|
[5f97ef44] | 347 | fibril_usleep(1 * USECS_PER_SEC);
|
---|
[a35b458] | 348 |
|
---|
[29b8138] | 349 | if (!info.entered_cs || info.exited_cs) {
|
---|
| 350 | printf("Error: reader is unexpectedly outside of critical section.\n");
|
---|
| 351 | return false;
|
---|
[94b39ba] | 352 | }
|
---|
[a35b458] | 353 |
|
---|
[29b8138] | 354 | info.synching = true;
|
---|
| 355 | printf("sync[");
|
---|
| 356 | rcu_synchronize();
|
---|
| 357 | printf("]\n");
|
---|
| 358 | info.synched = true;
|
---|
| 359 |
|
---|
| 360 | /* Load info.exited_cs */
|
---|
| 361 | memory_barrier();
|
---|
[a35b458] | 362 |
|
---|
[a879d73] | 363 | if (!info.exited_cs || info.failed) {
|
---|
[29b8138] | 364 | printf("Error: rcu_sync() returned before the reader exited its CS.\n");
|
---|
[1b20da0] | 365 | /*
|
---|
| 366 | * Sleep some more so we don't free info on stack while the reader
|
---|
[29b8138] | 367 | * is using it.
|
---|
| 368 | */
|
---|
| 369 | /* 1.5 sec */
|
---|
[5f97ef44] | 370 | fibril_usleep(1500 * 1000);
|
---|
[29b8138] | 371 | return false;
|
---|
| 372 | } else {
|
---|
| 373 | return true;
|
---|
| 374 | }
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | /*--------------------------------------------------------------------*/
|
---|
| 378 |
|
---|
[a879d73] | 379 | #define WAIT_STEP_US 500 * USECS_PER_MS
|
---|
[29b8138] | 380 |
|
---|
| 381 | typedef struct two_reader_info {
|
---|
| 382 | bool new_entered_cs;
|
---|
| 383 | bool new_exited_cs;
|
---|
| 384 | bool old_entered_cs;
|
---|
| 385 | bool old_exited_cs;
|
---|
| 386 | bool synching;
|
---|
| 387 | bool synched;
|
---|
| 388 | size_t failed;
|
---|
| 389 | } two_reader_info_t;
|
---|
| 390 |
|
---|
| 391 |
|
---|
[b7fd2a0] | 392 | static errno_t preexisting_reader(two_reader_info_t *arg)
|
---|
[29b8138] | 393 | {
|
---|
[94b39ba] | 394 | rcu_register_fibril();
|
---|
[a35b458] | 395 |
|
---|
[29b8138] | 396 | printf("old-lock{");
|
---|
| 397 | rcu_read_lock();
|
---|
| 398 | arg->old_entered_cs = true;
|
---|
[a35b458] | 399 |
|
---|
[29b8138] | 400 | printf("wait-for-sync{");
|
---|
| 401 | /* Wait for rcu_sync() to start waiting for us. */
|
---|
| 402 | while (!arg->synching) {
|
---|
[5f97ef44] | 403 | fibril_usleep(WAIT_STEP_US);
|
---|
[29b8138] | 404 | }
|
---|
| 405 | printf(" }");
|
---|
[a35b458] | 406 |
|
---|
[29b8138] | 407 | /* A new reader starts while rcu_sync() is in progress. */
|
---|
[a35b458] | 408 |
|
---|
[29b8138] | 409 | printf("wait-for-new-R{");
|
---|
| 410 | /* Wait for the new reader to enter its reader section. */
|
---|
| 411 | while (!arg->new_entered_cs) {
|
---|
[5f97ef44] | 412 | fibril_usleep(WAIT_STEP_US);
|
---|
[29b8138] | 413 | }
|
---|
| 414 | printf(" }");
|
---|
[a35b458] | 415 |
|
---|
[29b8138] | 416 | arg->old_exited_cs = true;
|
---|
[a35b458] | 417 |
|
---|
[29b8138] | 418 | assert(!arg->new_exited_cs);
|
---|
[a35b458] | 419 |
|
---|
[29b8138] | 420 | if (arg->synched) {
|
---|
| 421 | arg->failed = 1;
|
---|
| 422 | printf("Error: rcu_sync() did not wait for preexisting reader.\n");
|
---|
| 423 | }
|
---|
[a35b458] | 424 |
|
---|
[29b8138] | 425 | rcu_read_unlock();
|
---|
| 426 | printf(" }");
|
---|
[a35b458] | 427 |
|
---|
[94b39ba] | 428 | rcu_deregister_fibril();
|
---|
[29b8138] | 429 | return 0;
|
---|
| 430 | }
|
---|
| 431 |
|
---|
[b7fd2a0] | 432 | static errno_t new_reader(two_reader_info_t *arg)
|
---|
[29b8138] | 433 | {
|
---|
| 434 | rcu_register_fibril();
|
---|
[a35b458] | 435 |
|
---|
[29b8138] | 436 | /* Wait until rcu_sync() starts. */
|
---|
| 437 | while (!arg->synching) {
|
---|
[5f97ef44] | 438 | fibril_usleep(WAIT_STEP_US);
|
---|
[29b8138] | 439 | }
|
---|
[a35b458] | 440 |
|
---|
[1b20da0] | 441 | /*
|
---|
[29b8138] | 442 | * synching is set when rcu_sync() is about to be entered so wait
|
---|
| 443 | * some more to make sure it really does start executing.
|
---|
| 444 | */
|
---|
[5f97ef44] | 445 | fibril_usleep(WAIT_STEP_US);
|
---|
[a35b458] | 446 |
|
---|
[29b8138] | 447 | printf("new-lock(");
|
---|
| 448 | rcu_read_lock();
|
---|
| 449 | arg->new_entered_cs = true;
|
---|
| 450 |
|
---|
| 451 | /* Wait for rcu_sync() exit, ie stop waiting for the preexisting reader. */
|
---|
| 452 | while (!arg->synched) {
|
---|
[5f97ef44] | 453 | fibril_usleep(WAIT_STEP_US);
|
---|
[29b8138] | 454 | }
|
---|
[a35b458] | 455 |
|
---|
[29b8138] | 456 | arg->new_exited_cs = true;
|
---|
| 457 | /* Write new_exited_cs before exiting reader section. */
|
---|
| 458 | memory_barrier();
|
---|
[a35b458] | 459 |
|
---|
[1b20da0] | 460 | /*
|
---|
| 461 | * Preexisting reader should have exited by now, so rcu_synchronize()
|
---|
[29b8138] | 462 | * must have returned.
|
---|
| 463 | */
|
---|
| 464 | if (!arg->old_exited_cs) {
|
---|
| 465 | arg->failed = 1;
|
---|
| 466 | printf("Error: preexisting reader should have exited by now!\n");
|
---|
| 467 | }
|
---|
[a35b458] | 468 |
|
---|
[29b8138] | 469 | rcu_read_unlock();
|
---|
| 470 | printf(")");
|
---|
| 471 |
|
---|
| 472 | rcu_deregister_fibril();
|
---|
[94b39ba] | 473 | return 0;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
[d04e46e] | 476 | static bool dont_wait_for_new_reader(test_info_t *test_info)
|
---|
[29b8138] | 477 | {
|
---|
| 478 | two_reader_info_t info = { 0 };
|
---|
[a35b458] | 479 |
|
---|
[29b8138] | 480 | if (!create_fibril((fibril_func_t) preexisting_reader, &info))
|
---|
| 481 | return false;
|
---|
| 482 |
|
---|
| 483 | if (!create_fibril((fibril_func_t) new_reader, &info))
|
---|
| 484 | return false;
|
---|
[a35b458] | 485 |
|
---|
[29b8138] | 486 | /* Waits for the preexisting_reader to enter its CS.*/
|
---|
| 487 | while (!info.old_entered_cs) {
|
---|
[5f97ef44] | 488 | fibril_usleep(WAIT_STEP_US);
|
---|
[29b8138] | 489 | }
|
---|
[a35b458] | 490 |
|
---|
[29b8138] | 491 | assert(!info.old_exited_cs);
|
---|
| 492 | assert(!info.new_entered_cs);
|
---|
| 493 | assert(!info.new_exited_cs);
|
---|
[a35b458] | 494 |
|
---|
[29b8138] | 495 | printf("sync[");
|
---|
| 496 | info.synching = true;
|
---|
| 497 | rcu_synchronize();
|
---|
| 498 | printf(" ]");
|
---|
[a35b458] | 499 |
|
---|
[29b8138] | 500 | /* Load info.exited_cs */
|
---|
| 501 | memory_barrier();
|
---|
[a35b458] | 502 |
|
---|
[29b8138] | 503 | if (!info.old_exited_cs) {
|
---|
| 504 | printf("Error: rcu_sync() returned before preexisting reader exited.\n");
|
---|
| 505 | info.failed = 1;
|
---|
| 506 | }
|
---|
[a35b458] | 507 |
|
---|
[29b8138] | 508 | bool new_outside_cs = !info.new_entered_cs || info.new_exited_cs;
|
---|
[a35b458] | 509 |
|
---|
[29b8138] | 510 | /* Test if new reader is waiting in CS before setting synched. */
|
---|
| 511 | compiler_barrier();
|
---|
| 512 | info.synched = true;
|
---|
[a35b458] | 513 |
|
---|
[29b8138] | 514 | if (new_outside_cs) {
|
---|
| 515 | printf("Error: new reader CS held up rcu_sync(). (4)\n");
|
---|
| 516 | info.failed = 1;
|
---|
| 517 | } else {
|
---|
| 518 | /* Wait for the new reader. */
|
---|
| 519 | rcu_synchronize();
|
---|
[a35b458] | 520 |
|
---|
[29b8138] | 521 | if (!info.new_exited_cs) {
|
---|
| 522 | printf("Error: 2nd rcu_sync() returned before new reader exited.\n");
|
---|
| 523 | info.failed = 1;
|
---|
| 524 | }
|
---|
[a35b458] | 525 |
|
---|
[29b8138] | 526 | printf("\n");
|
---|
| 527 | }
|
---|
[a35b458] | 528 |
|
---|
[29b8138] | 529 | if (info.failed) {
|
---|
[1b20da0] | 530 | /*
|
---|
| 531 | * Sleep some more so we don't free info on stack while readers
|
---|
[29b8138] | 532 | * are using it.
|
---|
| 533 | */
|
---|
[5f97ef44] | 534 | fibril_usleep(WAIT_STEP_US);
|
---|
[29b8138] | 535 | }
|
---|
[a35b458] | 536 |
|
---|
[29b8138] | 537 | return 0 == info.failed;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | #undef WAIT_STEP_US
|
---|
[a879d73] | 541 |
|
---|
| 542 | /*--------------------------------------------------------------------*/
|
---|
| 543 | #define WAIT_STEP_US 500 * USECS_PER_MS
|
---|
| 544 |
|
---|
| 545 | typedef struct exit_reader_info {
|
---|
| 546 | bool entered_cs;
|
---|
| 547 | bool exited_cs;
|
---|
| 548 | bool synching;
|
---|
| 549 | bool synched;
|
---|
| 550 | } exit_reader_info_t;
|
---|
| 551 |
|
---|
| 552 |
|
---|
[b7fd2a0] | 553 | static errno_t exiting_locked_reader(exit_reader_info_t *arg)
|
---|
[a879d73] | 554 | {
|
---|
| 555 | rcu_register_fibril();
|
---|
[a35b458] | 556 |
|
---|
[a879d73] | 557 | printf("old-lock{");
|
---|
| 558 | rcu_read_lock();
|
---|
| 559 | rcu_read_lock();
|
---|
| 560 | rcu_read_lock();
|
---|
| 561 | arg->entered_cs = true;
|
---|
[a35b458] | 562 |
|
---|
[a879d73] | 563 | printf("wait-for-sync{");
|
---|
| 564 | /* Wait for rcu_sync() to start waiting for us. */
|
---|
| 565 | while (!arg->synching) {
|
---|
[5f97ef44] | 566 | fibril_usleep(WAIT_STEP_US);
|
---|
[a879d73] | 567 | }
|
---|
| 568 | printf(" }");
|
---|
[a35b458] | 569 |
|
---|
[a879d73] | 570 | rcu_read_unlock();
|
---|
| 571 | printf(" }");
|
---|
| 572 |
|
---|
| 573 | arg->exited_cs = true;
|
---|
| 574 | /* Store exited_cs before unlocking reader section in deregister. */
|
---|
| 575 | memory_barrier();
|
---|
[a35b458] | 576 |
|
---|
[a879d73] | 577 | /* Deregister forcefully unlocks the reader section. */
|
---|
| 578 | rcu_deregister_fibril();
|
---|
| 579 | return 0;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 |
|
---|
[d04e46e] | 583 | static bool wait_for_exiting_reader(test_info_t *test_info)
|
---|
[a879d73] | 584 | {
|
---|
| 585 | exit_reader_info_t info = { 0 };
|
---|
[a35b458] | 586 |
|
---|
[a879d73] | 587 | if (!create_fibril((fibril_func_t) exiting_locked_reader, &info))
|
---|
| 588 | return false;
|
---|
[a35b458] | 589 |
|
---|
[a879d73] | 590 | /* Waits for the preexisting_reader to enter its CS.*/
|
---|
| 591 | while (!info.entered_cs) {
|
---|
[5f97ef44] | 592 | fibril_usleep(WAIT_STEP_US);
|
---|
[a879d73] | 593 | }
|
---|
[a35b458] | 594 |
|
---|
[a879d73] | 595 | assert(!info.exited_cs);
|
---|
[a35b458] | 596 |
|
---|
[a879d73] | 597 | printf("sync[");
|
---|
| 598 | info.synching = true;
|
---|
| 599 | rcu_synchronize();
|
---|
| 600 | info.synched = true;
|
---|
| 601 | printf(" ]\n");
|
---|
[a35b458] | 602 |
|
---|
[a879d73] | 603 | /* Load info.exited_cs */
|
---|
| 604 | memory_barrier();
|
---|
[a35b458] | 605 |
|
---|
[a879d73] | 606 | if (!info.exited_cs) {
|
---|
| 607 | printf("Error: rcu_deregister_fibril did not unlock the CS.\n");
|
---|
| 608 | return false;
|
---|
[1b20da0] | 609 | }
|
---|
[a35b458] | 610 |
|
---|
[a879d73] | 611 | return true;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 | #undef WAIT_STEP_US
|
---|
| 615 |
|
---|
| 616 |
|
---|
[5d230a30] | 617 | /*--------------------------------------------------------------------*/
|
---|
| 618 |
|
---|
| 619 | typedef struct {
|
---|
| 620 | atomic_t time;
|
---|
| 621 | atomic_t max_start_time_of_done_sync;
|
---|
[a35b458] | 622 |
|
---|
[5d230a30] | 623 | size_t total_workers;
|
---|
| 624 | size_t done_reader_cnt;
|
---|
| 625 | size_t done_updater_cnt;
|
---|
| 626 | fibril_mutex_t done_cnt_mtx;
|
---|
| 627 | fibril_condvar_t done_cnt_changed;
|
---|
| 628 |
|
---|
| 629 | size_t read_iters;
|
---|
| 630 | size_t upd_iters;
|
---|
[a35b458] | 631 |
|
---|
[5d230a30] | 632 | atomic_t seed;
|
---|
| 633 | int failed;
|
---|
| 634 | } seq_test_info_t;
|
---|
| 635 |
|
---|
| 636 |
|
---|
| 637 | static void signal_seq_fibril_done(seq_test_info_t *arg, size_t *cnt)
|
---|
| 638 | {
|
---|
| 639 | fibril_mutex_lock(&arg->done_cnt_mtx);
|
---|
| 640 | ++*cnt;
|
---|
[a35b458] | 641 |
|
---|
[5d230a30] | 642 | if (arg->total_workers == arg->done_reader_cnt + arg->done_updater_cnt) {
|
---|
| 643 | fibril_condvar_signal(&arg->done_cnt_changed);
|
---|
| 644 | }
|
---|
[a35b458] | 645 |
|
---|
[5d230a30] | 646 | fibril_mutex_unlock(&arg->done_cnt_mtx);
|
---|
| 647 | }
|
---|
| 648 |
|
---|
[b7fd2a0] | 649 | static errno_t seq_reader(seq_test_info_t *arg)
|
---|
[5d230a30] | 650 | {
|
---|
| 651 | rcu_register_fibril();
|
---|
[a35b458] | 652 |
|
---|
[5d230a30] | 653 | size_t seed = (size_t) atomic_preinc(&arg->seed);
|
---|
| 654 | bool first = (seed == 1);
|
---|
[a35b458] | 655 |
|
---|
[5d230a30] | 656 | for (size_t k = 0; k < arg->read_iters; ++k) {
|
---|
| 657 | /* Print progress if the first reader fibril. */
|
---|
[1433ecda] | 658 | if (first && 0 == k % (arg->read_iters / 100 + 1)) {
|
---|
[5d230a30] | 659 | printf(".");
|
---|
| 660 | }
|
---|
[a35b458] | 661 |
|
---|
[5d230a30] | 662 | rcu_read_lock();
|
---|
| 663 | atomic_count_t start_time = atomic_preinc(&arg->time);
|
---|
[a35b458] | 664 |
|
---|
[5d230a30] | 665 | /* Do some work. */
|
---|
| 666 | seed = next_rand(seed);
|
---|
| 667 | size_t idle_iters = seed % 8;
|
---|
[a35b458] | 668 |
|
---|
[5d230a30] | 669 | for (size_t i = 0; i < idle_iters; ++i) {
|
---|
| 670 | fibril_yield();
|
---|
| 671 | }
|
---|
[a35b458] | 672 |
|
---|
[1b20da0] | 673 | /*
|
---|
[5d230a30] | 674 | * Check if the most recently started rcu_sync of the already
|
---|
| 675 | * finished rcu_syncs did not happen to start after this reader
|
---|
| 676 | * and, therefore, should have waited for this reader to exit
|
---|
| 677 | * (but did not - since it already announced it completed).
|
---|
| 678 | */
|
---|
| 679 | if (start_time <= atomic_get(&arg->max_start_time_of_done_sync)) {
|
---|
| 680 | arg->failed = 1;
|
---|
| 681 | }
|
---|
[a35b458] | 682 |
|
---|
[5d230a30] | 683 | rcu_read_unlock();
|
---|
| 684 | }
|
---|
[a35b458] | 685 |
|
---|
[5d230a30] | 686 | rcu_deregister_fibril();
|
---|
| 687 |
|
---|
| 688 | signal_seq_fibril_done(arg, &arg->done_reader_cnt);
|
---|
| 689 | return 0;
|
---|
| 690 | }
|
---|
| 691 |
|
---|
[b7fd2a0] | 692 | static errno_t seq_updater(seq_test_info_t *arg)
|
---|
[5d230a30] | 693 | {
|
---|
| 694 | rcu_register_fibril();
|
---|
[a35b458] | 695 |
|
---|
[5d230a30] | 696 | for (size_t k = 0; k < arg->upd_iters; ++k) {
|
---|
| 697 | atomic_count_t start_time = atomic_get(&arg->time);
|
---|
| 698 | rcu_synchronize();
|
---|
[a35b458] | 699 |
|
---|
[5d230a30] | 700 | /* This is prone to a race but if it happens it errs to the safe side.*/
|
---|
| 701 | if (atomic_get(&arg->max_start_time_of_done_sync) < start_time) {
|
---|
| 702 | atomic_set(&arg->max_start_time_of_done_sync, start_time);
|
---|
| 703 | }
|
---|
| 704 | }
|
---|
[a35b458] | 705 |
|
---|
[5d230a30] | 706 | rcu_deregister_fibril();
|
---|
[a35b458] | 707 |
|
---|
[5d230a30] | 708 | signal_seq_fibril_done(arg, &arg->done_updater_cnt);
|
---|
| 709 | return 0;
|
---|
| 710 | }
|
---|
| 711 |
|
---|
| 712 | static bool seq_test(test_info_t *test_info)
|
---|
| 713 | {
|
---|
[1b20da0] | 714 | size_t reader_cnt = test_info->thread_cnt;
|
---|
| 715 | size_t updater_cnt = test_info->thread_cnt;
|
---|
[a35b458] | 716 |
|
---|
[5d230a30] | 717 | seq_test_info_t info = {
|
---|
[1433ecda] | 718 | .time = { 0 },
|
---|
| 719 | .max_start_time_of_done_sync = { 0 },
|
---|
[5d230a30] | 720 | .read_iters = 10 * 1000,
|
---|
| 721 | .upd_iters = 5 * 1000,
|
---|
| 722 | .total_workers = updater_cnt + reader_cnt,
|
---|
| 723 | .done_reader_cnt = 0,
|
---|
| 724 | .done_updater_cnt = 0,
|
---|
| 725 | .done_cnt_mtx = FIBRIL_MUTEX_INITIALIZER(info.done_cnt_mtx),
|
---|
| 726 | .done_cnt_changed = FIBRIL_CONDVAR_INITIALIZER(info.done_cnt_changed),
|
---|
[1433ecda] | 727 | .seed = { 0 },
|
---|
[5d230a30] | 728 | .failed = 0,
|
---|
| 729 | };
|
---|
[a35b458] | 730 |
|
---|
[5d230a30] | 731 | /* Create and start worker fibrils. */
|
---|
| 732 | for (size_t k = 0; k + k < reader_cnt + updater_cnt; ++k) {
|
---|
| 733 | bool ok = create_fibril((fibril_func_t) seq_reader, &info);
|
---|
| 734 | ok = ok && create_fibril((fibril_func_t) seq_updater, &info);
|
---|
[a35b458] | 735 |
|
---|
[5d230a30] | 736 | if (!ok) {
|
---|
| 737 | /* Let the already created fibrils corrupt the stack. */
|
---|
| 738 | return false;
|
---|
| 739 | }
|
---|
| 740 | }
|
---|
[a35b458] | 741 |
|
---|
[5d230a30] | 742 | /* Wait for all worker fibrils to complete their work. */
|
---|
| 743 | fibril_mutex_lock(&info.done_cnt_mtx);
|
---|
[a35b458] | 744 |
|
---|
[5d230a30] | 745 | while (info.total_workers != info.done_reader_cnt + info.done_updater_cnt) {
|
---|
| 746 | fibril_condvar_wait(&info.done_cnt_changed, &info.done_cnt_mtx);
|
---|
| 747 | }
|
---|
[a35b458] | 748 |
|
---|
[5d230a30] | 749 | fibril_mutex_unlock(&info.done_cnt_mtx);
|
---|
[a35b458] | 750 |
|
---|
[5d230a30] | 751 | if (info.failed) {
|
---|
| 752 | printf("Error: rcu_sync() did not wait for a preexisting reader.");
|
---|
| 753 | }
|
---|
[a35b458] | 754 |
|
---|
[5d230a30] | 755 | return 0 == info.failed;
|
---|
| 756 | }
|
---|
| 757 |
|
---|
[29b8138] | 758 | /*--------------------------------------------------------------------*/
|
---|
[d04e46e] | 759 |
|
---|
| 760 | static bool create_threads(size_t cnt)
|
---|
| 761 | {
|
---|
| 762 | /* Sanity check. */
|
---|
| 763 | assert(cnt < 1024);
|
---|
[38d8849] | 764 | fibril_test_spawn_runners(cnt);
|
---|
[d04e46e] | 765 | return true;
|
---|
| 766 | }
|
---|
| 767 |
|
---|
[29b8138] | 768 | /*--------------------------------------------------------------------*/
|
---|
| 769 | static test_desc_t *find_test(const char *name)
|
---|
| 770 | {
|
---|
| 771 | /* First match for test name. */
|
---|
| 772 | for (size_t k = 0; k < test_desc_cnt; ++k) {
|
---|
| 773 | test_desc_t *t = &test_desc[k];
|
---|
[a35b458] | 774 |
|
---|
[29b8138] | 775 | if (t->func && 0 == str_cmp(t->name, name))
|
---|
| 776 | return t;
|
---|
| 777 | }
|
---|
[a35b458] | 778 |
|
---|
[29b8138] | 779 | /* Try to match the test number. */
|
---|
| 780 | uint32_t test_num = 0;
|
---|
[a35b458] | 781 |
|
---|
[29b8138] | 782 | if (EOK == str_uint32_t(name, NULL, 0, true, &test_num)) {
|
---|
| 783 | if (test_num < test_desc_cnt && test_desc[test_num].func) {
|
---|
| 784 | return &test_desc[test_num];
|
---|
| 785 | }
|
---|
| 786 | }
|
---|
[a35b458] | 787 |
|
---|
[29b8138] | 788 | return NULL;
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | static void list_tests(void)
|
---|
| 792 | {
|
---|
| 793 | printf("Available tests: \n");
|
---|
[a35b458] | 794 |
|
---|
[29b8138] | 795 | for (size_t i = 0; i < test_desc_cnt; ++i) {
|
---|
| 796 | test_desc_t *t = &test_desc[i];
|
---|
[a35b458] | 797 |
|
---|
[1b20da0] | 798 | if (!t->func)
|
---|
[29b8138] | 799 | continue;
|
---|
[a35b458] | 800 |
|
---|
[29b8138] | 801 | const char *type = "";
|
---|
[a35b458] | 802 |
|
---|
[29b8138] | 803 | if (t->type == T_SANITY)
|
---|
| 804 | type = " (sanity)";
|
---|
| 805 | if (t->type == T_STRESS)
|
---|
| 806 | type = " (stress)";
|
---|
| 807 |
|
---|
| 808 | printf("%zu: %s ..%s %s\n", i, t->name, type, t->desc);
|
---|
| 809 | }
|
---|
| 810 | }
|
---|
| 811 |
|
---|
| 812 |
|
---|
| 813 | static void print_usage(void)
|
---|
| 814 | {
|
---|
[d04e46e] | 815 | printf("Usage: rcutest [test_name|test_number] {number_of_threads}\n");
|
---|
[29b8138] | 816 | list_tests();
|
---|
[a35b458] | 817 |
|
---|
[29b8138] | 818 | printf("\nExample usage:\n");
|
---|
| 819 | printf("\trcutest *\n");
|
---|
| 820 | printf("\trcutest sanity-tests\n");
|
---|
| 821 | }
|
---|
| 822 |
|
---|
| 823 |
|
---|
[d04e46e] | 824 | static bool parse_cmd_line(int argc, char **argv, test_info_t *info)
|
---|
[29b8138] | 825 | {
|
---|
[d04e46e] | 826 | if (argc != 2 && argc != 3) {
|
---|
[29b8138] | 827 | print_usage();
|
---|
[d04e46e] | 828 | return false;
|
---|
| 829 | }
|
---|
[a35b458] | 830 |
|
---|
[d04e46e] | 831 | info->desc = find_test(argv[1]);
|
---|
| 832 |
|
---|
| 833 | if (!info->desc) {
|
---|
| 834 | printf("Non-existent test '%s'.\n", argv[1]);
|
---|
| 835 | list_tests();
|
---|
| 836 | return false;
|
---|
| 837 | }
|
---|
[a35b458] | 838 |
|
---|
[d04e46e] | 839 | if (argc == 3) {
|
---|
| 840 | uint32_t thread_cnt = 0;
|
---|
[b7fd2a0] | 841 | errno_t ret = str_uint32_t(argv[2], NULL, 0, true, &thread_cnt);
|
---|
[a35b458] | 842 |
|
---|
[d04e46e] | 843 | if (ret == EOK && 1 <= thread_cnt && thread_cnt <= 64) {
|
---|
| 844 | info->thread_cnt = thread_cnt;
|
---|
| 845 | } else {
|
---|
| 846 | info->thread_cnt = 1;
|
---|
| 847 | printf("Err: Invalid number of threads '%s'; using 1.\n", argv[2]);
|
---|
[1b20da0] | 848 | }
|
---|
[5d230a30] | 849 | } else {
|
---|
| 850 | info->thread_cnt = 1;
|
---|
[29b8138] | 851 | }
|
---|
[a35b458] | 852 |
|
---|
[d04e46e] | 853 | return true;
|
---|
| 854 | }
|
---|
| 855 |
|
---|
| 856 | int main(int argc, char **argv)
|
---|
| 857 | {
|
---|
| 858 | rcu_register_fibril();
|
---|
[a35b458] | 859 |
|
---|
[d04e46e] | 860 | test_info_t info;
|
---|
[a35b458] | 861 |
|
---|
[d04e46e] | 862 | bool ok = parse_cmd_line(argc, argv, &info);
|
---|
| 863 | ok = ok && create_threads(info.thread_cnt - 1);
|
---|
[a35b458] | 864 |
|
---|
[d04e46e] | 865 | if (ok) {
|
---|
| 866 | assert(1 <= info.thread_cnt);
|
---|
| 867 | test_desc_t *t = info.desc;
|
---|
[a35b458] | 868 |
|
---|
[d04e46e] | 869 | printf("Running '%s' (in %zu threads)...\n", t->name, info.thread_cnt);
|
---|
| 870 | ok = t->func(&info);
|
---|
| 871 |
|
---|
[29b8138] | 872 | printf("%s: '%s'\n", ok ? "Passed" : "FAILED", t->name);
|
---|
[d04e46e] | 873 |
|
---|
[29b8138] | 874 | rcu_deregister_fibril();
|
---|
[a35b458] | 875 |
|
---|
[d04e46e] | 876 | /* Let the kernel clean up the created background threads. */
|
---|
[29b8138] | 877 | return ok ? 0 : 1;
|
---|
| 878 | } else {
|
---|
| 879 | rcu_deregister_fibril();
|
---|
[d04e46e] | 880 | return 2;
|
---|
[29b8138] | 881 | }
|
---|
| 882 | }
|
---|
| 883 |
|
---|
[94b39ba] | 884 |
|
---|
| 885 | /**
|
---|
| 886 | * @}
|
---|
| 887 | */
|
---|