| 1 | /* |
| 2 | * test_urcu_hash.c |
| 3 | * |
| 4 | * Userspace RCU library - test program |
| 5 | * |
| 6 | * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include "test_urcu_hash.h" |
| 24 | |
| 25 | enum test_hash { |
| 26 | TEST_HASH_RW, |
| 27 | TEST_HASH_UNIQUE, |
| 28 | }; |
| 29 | |
| 30 | struct test_hash_cb { |
| 31 | void (*sigusr1)(int signo); |
| 32 | void (*sigusr2)(int signo); |
| 33 | void *(*thr_reader)(void *_count); |
| 34 | void *(*thr_writer)(void *_count); |
| 35 | int (*populate_hash)(void); |
| 36 | }; |
| 37 | |
| 38 | static |
| 39 | struct test_hash_cb test_hash_cb[] = { |
| 40 | [TEST_HASH_RW] = { |
| 41 | test_hash_rw_sigusr1_handler, |
| 42 | test_hash_rw_sigusr2_handler, |
| 43 | test_hash_rw_thr_reader, |
| 44 | test_hash_rw_thr_writer, |
| 45 | test_hash_rw_populate_hash, |
| 46 | }, |
| 47 | [TEST_HASH_UNIQUE] = { |
| 48 | test_hash_unique_sigusr1_handler, |
| 49 | test_hash_unique_sigusr2_handler, |
| 50 | test_hash_unique_thr_reader, |
| 51 | test_hash_unique_thr_writer, |
| 52 | test_hash_unique_populate_hash, |
| 53 | }, |
| 54 | |
| 55 | }; |
| 56 | |
| 57 | static enum test_hash test_choice = TEST_HASH_RW; |
| 58 | |
| 59 | void (*get_sigusr1_cb(void))(int) |
| 60 | { |
| 61 | return test_hash_cb[test_choice].sigusr1; |
| 62 | } |
| 63 | |
| 64 | void (*get_sigusr2_cb(void))(int) |
| 65 | { |
| 66 | return test_hash_cb[test_choice].sigusr2; |
| 67 | } |
| 68 | |
| 69 | void *(*get_thr_reader_cb(void))(void *) |
| 70 | { |
| 71 | return test_hash_cb[test_choice].thr_reader; |
| 72 | } |
| 73 | |
| 74 | void *(*get_thr_writer_cb(void))(void *) |
| 75 | { |
| 76 | return test_hash_cb[test_choice].thr_writer; |
| 77 | } |
| 78 | |
| 79 | int (*get_populate_hash_cb(void))(void) |
| 80 | { |
| 81 | return test_hash_cb[test_choice].populate_hash; |
| 82 | } |
| 83 | |
| 84 | DEFINE_URCU_TLS(unsigned int, rand_lookup); |
| 85 | DEFINE_URCU_TLS(unsigned long, nr_add); |
| 86 | DEFINE_URCU_TLS(unsigned long, nr_addexist); |
| 87 | DEFINE_URCU_TLS(unsigned long, nr_del); |
| 88 | DEFINE_URCU_TLS(unsigned long, nr_delnoent); |
| 89 | DEFINE_URCU_TLS(unsigned long, lookup_fail); |
| 90 | DEFINE_URCU_TLS(unsigned long, lookup_ok); |
| 91 | |
| 92 | struct cds_lfht *test_ht; |
| 93 | |
| 94 | volatile int test_go, test_stop; |
| 95 | |
| 96 | unsigned long wdelay; |
| 97 | |
| 98 | unsigned long duration; |
| 99 | |
| 100 | /* read-side C.S. duration, in loops */ |
| 101 | unsigned long rduration; |
| 102 | |
| 103 | unsigned long init_hash_size = DEFAULT_HASH_SIZE; |
| 104 | unsigned long min_hash_alloc_size = DEFAULT_MIN_ALLOC_SIZE; |
| 105 | unsigned long max_hash_buckets_size = (1UL << 20); |
| 106 | unsigned long init_populate; |
| 107 | int opt_auto_resize; |
| 108 | int add_only, add_unique, add_replace; |
| 109 | const struct cds_lfht_mm_type *memory_backend; |
| 110 | |
| 111 | unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset; |
| 112 | unsigned long init_pool_size = DEFAULT_RAND_POOL, |
| 113 | lookup_pool_size = DEFAULT_RAND_POOL, |
| 114 | write_pool_size = DEFAULT_RAND_POOL; |
| 115 | int validate_lookup; |
| 116 | unsigned long nr_hash_chains; /* 0: normal table, other: number of hash chains */ |
| 117 | |
| 118 | int count_pipe[2]; |
| 119 | |
| 120 | int verbose_mode; |
| 121 | |
| 122 | unsigned int cpu_affinities[NR_CPUS]; |
| 123 | unsigned int next_aff = 0; |
| 124 | int use_affinity = 0; |
| 125 | |
| 126 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 127 | |
| 128 | DEFINE_URCU_TLS(unsigned long long, nr_writes); |
| 129 | DEFINE_URCU_TLS(unsigned long long, nr_reads); |
| 130 | |
| 131 | unsigned int nr_readers; |
| 132 | unsigned int nr_writers; |
| 133 | |
| 134 | static pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 135 | |
| 136 | void set_affinity(void) |
| 137 | { |
| 138 | #if HAVE_SCHED_SETAFFINITY |
| 139 | cpu_set_t mask; |
| 140 | int cpu, ret; |
| 141 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 142 | |
| 143 | if (!use_affinity) |
| 144 | return; |
| 145 | |
| 146 | #if HAVE_SCHED_SETAFFINITY |
| 147 | ret = pthread_mutex_lock(&affinity_mutex); |
| 148 | if (ret) { |
| 149 | perror("Error in pthread mutex lock"); |
| 150 | exit(-1); |
| 151 | } |
| 152 | cpu = cpu_affinities[next_aff++]; |
| 153 | ret = pthread_mutex_unlock(&affinity_mutex); |
| 154 | if (ret) { |
| 155 | perror("Error in pthread mutex unlock"); |
| 156 | exit(-1); |
| 157 | } |
| 158 | CPU_ZERO(&mask); |
| 159 | CPU_SET(cpu, &mask); |
| 160 | #if SCHED_SETAFFINITY_ARGS == 2 |
| 161 | sched_setaffinity(0, &mask); |
| 162 | #else |
| 163 | sched_setaffinity(0, sizeof(mask), &mask); |
| 164 | #endif |
| 165 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 166 | } |
| 167 | |
| 168 | void rcu_copy_mutex_lock(void) |
| 169 | { |
| 170 | int ret; |
| 171 | ret = pthread_mutex_lock(&rcu_copy_mutex); |
| 172 | if (ret) { |
| 173 | perror("Error in pthread mutex lock"); |
| 174 | exit(-1); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void rcu_copy_mutex_unlock(void) |
| 179 | { |
| 180 | int ret; |
| 181 | |
| 182 | ret = pthread_mutex_unlock(&rcu_copy_mutex); |
| 183 | if (ret) { |
| 184 | perror("Error in pthread mutex unlock"); |
| 185 | exit(-1); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | unsigned long test_compare(const void *key1, size_t key1_len, |
| 190 | const void *key2, size_t key2_len) |
| 191 | { |
| 192 | if (caa_unlikely(key1_len != key2_len)) |
| 193 | return -1; |
| 194 | assert(key1_len == sizeof(unsigned long)); |
| 195 | if (key1 == key2) |
| 196 | return 0; |
| 197 | else |
| 198 | return 1; |
| 199 | } |
| 200 | |
| 201 | void *thr_count(void *arg) |
| 202 | { |
| 203 | printf_verbose("thread_begin %s, tid %lu\n", |
| 204 | "counter", urcu_get_thread_id()); |
| 205 | |
| 206 | rcu_register_thread(); |
| 207 | |
| 208 | for (;;) { |
| 209 | unsigned long count; |
| 210 | long approx_before, approx_after; |
| 211 | ssize_t len; |
| 212 | char buf[1]; |
| 213 | |
| 214 | rcu_thread_offline(); |
| 215 | len = read(count_pipe[0], buf, 1); |
| 216 | rcu_thread_online(); |
| 217 | if (caa_unlikely(!test_duration_read())) |
| 218 | break; |
| 219 | if (len != 1) |
| 220 | continue; |
| 221 | /* Accounting */ |
| 222 | printf("Counting nodes... "); |
| 223 | fflush(stdout); |
| 224 | rcu_read_lock(); |
| 225 | cds_lfht_count_nodes(test_ht, &approx_before, &count, |
| 226 | &approx_after); |
| 227 | rcu_read_unlock(); |
| 228 | printf("done.\n"); |
| 229 | printf("Approximation before node accounting: %ld nodes.\n", |
| 230 | approx_before); |
| 231 | printf("Accounting of nodes in the hash table: " |
| 232 | "%lu nodes.\n", |
| 233 | count); |
| 234 | printf("Approximation after node accounting: %ld nodes.\n", |
| 235 | approx_after); |
| 236 | } |
| 237 | rcu_unregister_thread(); |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | void free_node_cb(struct rcu_head *head) |
| 242 | { |
| 243 | struct lfht_test_node *node = |
| 244 | caa_container_of(head, struct lfht_test_node, head); |
| 245 | free(node); |
| 246 | } |
| 247 | |
| 248 | static |
| 249 | void test_delete_all_nodes(struct cds_lfht *ht) |
| 250 | { |
| 251 | struct cds_lfht_iter iter; |
| 252 | struct lfht_test_node *node; |
| 253 | unsigned long count = 0; |
| 254 | |
| 255 | cds_lfht_for_each_entry(ht, &iter, node, node) { |
| 256 | int ret; |
| 257 | |
| 258 | ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter)); |
| 259 | assert(!ret); |
| 260 | call_rcu(&node->head, free_node_cb); |
| 261 | count++; |
| 262 | } |
| 263 | printf("deleted %lu nodes.\n", count); |
| 264 | } |
| 265 | |
| 266 | void show_usage(int argc, char **argv) |
| 267 | { |
| 268 | printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n", |
| 269 | argv[0]); |
| 270 | printf("OPTIONS:\n"); |
| 271 | printf(" [-r] [-w] (yield reader and/or writer)\n"); |
| 272 | printf(" [-d delay] (writer period (us))\n"); |
| 273 | printf(" [-c duration] (reader C.S. duration (in loops))\n"); |
| 274 | printf(" [-v] (verbose output)\n"); |
| 275 | printf(" [-a cpu#] [-a cpu#]... (affinity)\n"); |
| 276 | printf(" [-h size] (initial number of buckets)\n"); |
| 277 | printf(" [-m size] (minimum number of allocated buckets)\n"); |
| 278 | printf(" [-n size] (maximum number of buckets)\n"); |
| 279 | printf(" [not -u nor -s] Add entries (supports redundant keys).\n"); |
| 280 | printf(" [-u] Uniquify add (no redundant keys).\n"); |
| 281 | printf(" [-s] Replace (swap) entries.\n"); |
| 282 | printf(" [-i] Add only (no removal).\n"); |
| 283 | printf(" [-k nr_nodes] Number of nodes to insert initially.\n"); |
| 284 | printf(" [-A] Automatically resize hash table.\n"); |
| 285 | printf(" [-B order|chunk|mmap] Specify the memory backend.\n"); |
| 286 | printf(" [-R offset] Lookup pool offset.\n"); |
| 287 | printf(" [-S offset] Write pool offset.\n"); |
| 288 | printf(" [-T offset] Init pool offset.\n"); |
| 289 | printf(" [-M size] Lookup pool size.\n"); |
| 290 | printf(" [-N size] Write pool size.\n"); |
| 291 | printf(" [-O size] Init pool size.\n"); |
| 292 | printf(" [-V] Validate lookups of init values.\n"); |
| 293 | printf(" (use with filled init pool, same lookup range,\n"); |
| 294 | printf(" with different write range)\n"); |
| 295 | printf(" [-U] Uniqueness test.\n"); |
| 296 | printf(" [-C] Number of hash chains.\n"); |
| 297 | printf("\n"); |
| 298 | } |
| 299 | |
| 300 | int main(int argc, char **argv) |
| 301 | { |
| 302 | pthread_t *tid_reader, *tid_writer; |
| 303 | pthread_t tid_count; |
| 304 | void *tret; |
| 305 | unsigned long long *count_reader; |
| 306 | struct wr_count *count_writer; |
| 307 | unsigned long long tot_reads = 0, tot_writes = 0, |
| 308 | tot_add = 0, tot_add_exist = 0, tot_remove = 0; |
| 309 | unsigned long count; |
| 310 | long approx_before, approx_after; |
| 311 | int i, a, ret, err, mainret = 0; |
| 312 | struct sigaction act; |
| 313 | unsigned int remain; |
| 314 | unsigned int nr_readers_created = 0, nr_writers_created = 0; |
| 315 | long long nr_leaked; |
| 316 | |
| 317 | if (argc < 4) { |
| 318 | show_usage(argc, argv); |
| 319 | mainret = 1; |
| 320 | goto end; |
| 321 | } |
| 322 | |
| 323 | err = sscanf(argv[1], "%u", &nr_readers); |
| 324 | if (err != 1) { |
| 325 | show_usage(argc, argv); |
| 326 | mainret = 1; |
| 327 | goto end; |
| 328 | } |
| 329 | |
| 330 | err = sscanf(argv[2], "%u", &nr_writers); |
| 331 | if (err != 1) { |
| 332 | show_usage(argc, argv); |
| 333 | mainret = 1; |
| 334 | goto end; |
| 335 | } |
| 336 | |
| 337 | err = sscanf(argv[3], "%lu", &duration); |
| 338 | if (err != 1) { |
| 339 | show_usage(argc, argv); |
| 340 | mainret = 1; |
| 341 | goto end; |
| 342 | } |
| 343 | |
| 344 | for (i = 4; i < argc; i++) { |
| 345 | if (argv[i][0] != '-') |
| 346 | continue; |
| 347 | switch (argv[i][1]) { |
| 348 | case 'r': |
| 349 | rcu_debug_yield_enable(RCU_YIELD_READ); |
| 350 | break; |
| 351 | case 'w': |
| 352 | rcu_debug_yield_enable(RCU_YIELD_WRITE); |
| 353 | break; |
| 354 | case 'a': |
| 355 | if (argc < i + 2) { |
| 356 | show_usage(argc, argv); |
| 357 | mainret = 1; |
| 358 | goto end; |
| 359 | } |
| 360 | a = atoi(argv[++i]); |
| 361 | cpu_affinities[next_aff++] = a; |
| 362 | use_affinity = 1; |
| 363 | printf_verbose("Adding CPU %d affinity\n", a); |
| 364 | break; |
| 365 | case 'c': |
| 366 | if (argc < i + 2) { |
| 367 | show_usage(argc, argv); |
| 368 | mainret = 1; |
| 369 | goto end; |
| 370 | } |
| 371 | rduration = atol(argv[++i]); |
| 372 | break; |
| 373 | case 'd': |
| 374 | if (argc < i + 2) { |
| 375 | show_usage(argc, argv); |
| 376 | mainret = 1; |
| 377 | goto end; |
| 378 | } |
| 379 | wdelay = atol(argv[++i]); |
| 380 | break; |
| 381 | case 'v': |
| 382 | verbose_mode = 1; |
| 383 | break; |
| 384 | case 'h': |
| 385 | if (argc < i + 2) { |
| 386 | show_usage(argc, argv); |
| 387 | mainret = 1; |
| 388 | goto end; |
| 389 | } |
| 390 | init_hash_size = atol(argv[++i]); |
| 391 | break; |
| 392 | case 'm': |
| 393 | if (argc < i + 2) { |
| 394 | show_usage(argc, argv); |
| 395 | mainret = 1; |
| 396 | goto end; |
| 397 | } |
| 398 | min_hash_alloc_size = atol(argv[++i]); |
| 399 | break; |
| 400 | case 'n': |
| 401 | if (argc < i + 2) { |
| 402 | show_usage(argc, argv); |
| 403 | mainret = 1; |
| 404 | goto end; |
| 405 | } |
| 406 | max_hash_buckets_size = atol(argv[++i]); |
| 407 | break; |
| 408 | case 'u': |
| 409 | if (add_replace) { |
| 410 | printf("Please specify at most one of -s or -u.\n"); |
| 411 | exit(-1); |
| 412 | } |
| 413 | add_unique = 1; |
| 414 | break; |
| 415 | case 's': |
| 416 | if (add_unique) { |
| 417 | printf("Please specify at most one of -s or -u.\n"); |
| 418 | exit(-1); |
| 419 | } |
| 420 | add_replace = 1; |
| 421 | break; |
| 422 | case 'i': |
| 423 | add_only = 1; |
| 424 | break; |
| 425 | case 'k': |
| 426 | init_populate = atol(argv[++i]); |
| 427 | break; |
| 428 | case 'A': |
| 429 | opt_auto_resize = 1; |
| 430 | break; |
| 431 | case 'B': |
| 432 | if (argc < i + 2) { |
| 433 | show_usage(argc, argv); |
| 434 | mainret = 1; |
| 435 | goto end; |
| 436 | } |
| 437 | i++; |
| 438 | if (!strcmp("order", argv[i])) |
| 439 | memory_backend = &cds_lfht_mm_order; |
| 440 | else if (!strcmp("chunk", argv[i])) |
| 441 | memory_backend = &cds_lfht_mm_chunk; |
| 442 | else if (!strcmp("mmap", argv[i])) |
| 443 | memory_backend = &cds_lfht_mm_mmap; |
| 444 | else { |
| 445 | printf("Please specify memory backend with order|chunk|mmap.\n"); |
| 446 | mainret = 1; |
| 447 | goto end; |
| 448 | } |
| 449 | break; |
| 450 | case 'R': |
| 451 | lookup_pool_offset = atol(argv[++i]); |
| 452 | break; |
| 453 | case 'S': |
| 454 | write_pool_offset = atol(argv[++i]); |
| 455 | break; |
| 456 | case 'T': |
| 457 | init_pool_offset = atol(argv[++i]); |
| 458 | break; |
| 459 | case 'M': |
| 460 | lookup_pool_size = atol(argv[++i]); |
| 461 | break; |
| 462 | case 'N': |
| 463 | write_pool_size = atol(argv[++i]); |
| 464 | break; |
| 465 | case 'O': |
| 466 | init_pool_size = atol(argv[++i]); |
| 467 | break; |
| 468 | case 'V': |
| 469 | validate_lookup = 1; |
| 470 | break; |
| 471 | case 'U': |
| 472 | test_choice = TEST_HASH_UNIQUE; |
| 473 | break; |
| 474 | case 'C': |
| 475 | nr_hash_chains = atol(argv[++i]); |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /* Check if hash size is power of 2 */ |
| 481 | if (init_hash_size && init_hash_size & (init_hash_size - 1)) { |
| 482 | printf("Error: Initial number of buckets (%lu) is not a power of 2.\n", |
| 483 | init_hash_size); |
| 484 | mainret = 1; |
| 485 | goto end; |
| 486 | } |
| 487 | |
| 488 | if (min_hash_alloc_size && min_hash_alloc_size & (min_hash_alloc_size - 1)) { |
| 489 | printf("Error: Minimum number of allocated buckets (%lu) is not a power of 2.\n", |
| 490 | min_hash_alloc_size); |
| 491 | mainret = 1; |
| 492 | goto end; |
| 493 | } |
| 494 | |
| 495 | if (max_hash_buckets_size && max_hash_buckets_size & (max_hash_buckets_size - 1)) { |
| 496 | printf("Error: Maximum number of buckets (%lu) is not a power of 2.\n", |
| 497 | max_hash_buckets_size); |
| 498 | mainret = 1; |
| 499 | goto end; |
| 500 | } |
| 501 | |
| 502 | memset(&act, 0, sizeof(act)); |
| 503 | ret = sigemptyset(&act.sa_mask); |
| 504 | if (ret == -1) { |
| 505 | perror("sigemptyset"); |
| 506 | mainret = 1; |
| 507 | goto end; |
| 508 | } |
| 509 | act.sa_handler = get_sigusr1_cb(); |
| 510 | act.sa_flags = SA_RESTART; |
| 511 | ret = sigaction(SIGUSR1, &act, NULL); |
| 512 | if (ret == -1) { |
| 513 | perror("sigaction"); |
| 514 | mainret = 1; |
| 515 | goto end; |
| 516 | } |
| 517 | |
| 518 | act.sa_handler = get_sigusr2_cb(); |
| 519 | act.sa_flags = SA_RESTART; |
| 520 | ret = sigaction(SIGUSR2, &act, NULL); |
| 521 | if (ret == -1) { |
| 522 | perror("sigaction"); |
| 523 | mainret = 1; |
| 524 | goto end; |
| 525 | } |
| 526 | |
| 527 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", |
| 528 | duration, nr_readers, nr_writers); |
| 529 | printf_verbose("Writer delay : %lu loops.\n", wdelay); |
| 530 | printf_verbose("Reader duration : %lu loops.\n", rduration); |
| 531 | printf_verbose("Mode:%s%s.\n", |
| 532 | add_only ? " add only" : " add/remove", |
| 533 | add_unique ? " uniquify" : ( add_replace ? " replace" : " insert")); |
| 534 | printf_verbose("Initial number of buckets: %lu buckets.\n", init_hash_size); |
| 535 | printf_verbose("Minimum number of allocated buckets: %lu buckets.\n", min_hash_alloc_size); |
| 536 | printf_verbose("Maximum number of buckets: %lu buckets.\n", max_hash_buckets_size); |
| 537 | printf_verbose("Init pool size offset %lu size %lu.\n", |
| 538 | init_pool_offset, init_pool_size); |
| 539 | printf_verbose("Lookup pool size offset %lu size %lu.\n", |
| 540 | lookup_pool_offset, lookup_pool_size); |
| 541 | printf_verbose("Update pool size offset %lu size %lu.\n", |
| 542 | write_pool_offset, write_pool_size); |
| 543 | printf_verbose("Number of hash chains: %lu.\n", |
| 544 | nr_hash_chains); |
| 545 | printf_verbose("thread %-6s, tid %lu\n", |
| 546 | "main", urcu_get_thread_id()); |
| 547 | |
| 548 | tid_reader = calloc(nr_readers, sizeof(*tid_reader)); |
| 549 | if (!tid_reader) { |
| 550 | mainret = 1; |
| 551 | goto end; |
| 552 | } |
| 553 | tid_writer = calloc(nr_writers, sizeof(*tid_writer)); |
| 554 | if (!tid_writer) { |
| 555 | mainret = 1; |
| 556 | goto end_free_tid_reader; |
| 557 | } |
| 558 | count_reader = calloc(nr_readers, sizeof(*count_reader)); |
| 559 | if (!count_reader) { |
| 560 | mainret = 1; |
| 561 | goto end_free_tid_writer; |
| 562 | } |
| 563 | count_writer = calloc(nr_writers, sizeof(*count_writer)); |
| 564 | if (!count_writer) { |
| 565 | mainret = 1; |
| 566 | goto end_free_count_reader; |
| 567 | } |
| 568 | |
| 569 | err = create_all_cpu_call_rcu_data(0); |
| 570 | if (err) { |
| 571 | printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n"); |
| 572 | } |
| 573 | |
| 574 | if (memory_backend) { |
| 575 | test_ht = _cds_lfht_new(init_hash_size, min_hash_alloc_size, |
| 576 | max_hash_buckets_size, |
| 577 | (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) | |
| 578 | CDS_LFHT_ACCOUNTING, memory_backend, |
| 579 | &rcu_flavor, NULL); |
| 580 | } else { |
| 581 | test_ht = cds_lfht_new(init_hash_size, min_hash_alloc_size, |
| 582 | max_hash_buckets_size, |
| 583 | (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) | |
| 584 | CDS_LFHT_ACCOUNTING, NULL); |
| 585 | } |
| 586 | if (!test_ht) { |
| 587 | printf("Error allocating hash table.\n"); |
| 588 | mainret = 1; |
| 589 | goto end_free_call_rcu_data; |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | * Hash Population needs to be seen as a RCU reader |
| 594 | * thread from the point of view of resize. |
| 595 | */ |
| 596 | rcu_register_thread(); |
| 597 | ret = (get_populate_hash_cb())(); |
| 598 | assert(!ret); |
| 599 | |
| 600 | rcu_thread_offline(); |
| 601 | |
| 602 | next_aff = 0; |
| 603 | |
| 604 | ret = pipe(count_pipe); |
| 605 | if (ret == -1) { |
| 606 | perror("pipe"); |
| 607 | mainret = 1; |
| 608 | goto end_online; |
| 609 | } |
| 610 | |
| 611 | /* spawn counter thread */ |
| 612 | err = pthread_create(&tid_count, NULL, thr_count, |
| 613 | NULL); |
| 614 | if (err != 0) { |
| 615 | errno = err; |
| 616 | mainret = 1; |
| 617 | perror("pthread_create"); |
| 618 | goto end_close_pipe; |
| 619 | } |
| 620 | |
| 621 | for (i = 0; i < nr_readers; i++) { |
| 622 | err = pthread_create(&tid_reader[i], |
| 623 | NULL, get_thr_reader_cb(), |
| 624 | &count_reader[i]); |
| 625 | if (err != 0) { |
| 626 | errno = err; |
| 627 | mainret = 1; |
| 628 | perror("pthread_create"); |
| 629 | goto end_pthread_join; |
| 630 | } |
| 631 | nr_readers_created++; |
| 632 | } |
| 633 | for (i = 0; i < nr_writers; i++) { |
| 634 | err = pthread_create(&tid_writer[i], |
| 635 | NULL, get_thr_writer_cb(), |
| 636 | &count_writer[i]); |
| 637 | if (err != 0) { |
| 638 | errno = err; |
| 639 | mainret = 1; |
| 640 | perror("pthread_create"); |
| 641 | goto end_pthread_join; |
| 642 | } |
| 643 | nr_writers_created++; |
| 644 | } |
| 645 | |
| 646 | cmm_smp_mb(); |
| 647 | |
| 648 | test_go = 1; |
| 649 | |
| 650 | remain = duration; |
| 651 | do { |
| 652 | remain = sleep(remain); |
| 653 | } while (remain > 0); |
| 654 | |
| 655 | test_stop = 1; |
| 656 | |
| 657 | end_pthread_join: |
| 658 | for (i = 0; i < nr_readers_created; i++) { |
| 659 | err = pthread_join(tid_reader[i], &tret); |
| 660 | if (err != 0) { |
| 661 | errno = err; |
| 662 | mainret = 1; |
| 663 | perror("pthread_join"); |
| 664 | } |
| 665 | tot_reads += count_reader[i]; |
| 666 | } |
| 667 | for (i = 0; i < nr_writers_created; i++) { |
| 668 | err = pthread_join(tid_writer[i], &tret); |
| 669 | if (err != 0) { |
| 670 | errno = err; |
| 671 | mainret = 1; |
| 672 | perror("pthread_join"); |
| 673 | } |
| 674 | tot_writes += count_writer[i].update_ops; |
| 675 | tot_add += count_writer[i].add; |
| 676 | tot_add_exist += count_writer[i].add_exist; |
| 677 | tot_remove += count_writer[i].remove; |
| 678 | } |
| 679 | |
| 680 | /* teardown counter thread */ |
| 681 | act.sa_handler = SIG_IGN; |
| 682 | act.sa_flags = SA_RESTART; |
| 683 | ret = sigaction(SIGUSR2, &act, NULL); |
| 684 | if (ret == -1) { |
| 685 | mainret = 1; |
| 686 | perror("sigaction"); |
| 687 | } |
| 688 | { |
| 689 | char msg[1] = { 0x42 }; |
| 690 | ssize_t ret; |
| 691 | |
| 692 | do { |
| 693 | ret = write(count_pipe[1], msg, 1); /* wakeup thread */ |
| 694 | } while (ret == -1L && errno == EINTR); |
| 695 | } |
| 696 | err = pthread_join(tid_count, &tret); |
| 697 | if (err != 0) { |
| 698 | errno = err; |
| 699 | mainret = 1; |
| 700 | perror("pthread_join"); |
| 701 | } |
| 702 | |
| 703 | end_close_pipe: |
| 704 | for (i = 0; i < 2; i++) { |
| 705 | err = close(count_pipe[i]); |
| 706 | if (err) { |
| 707 | mainret = 1; |
| 708 | perror("close pipe"); |
| 709 | } |
| 710 | } |
| 711 | fflush(stdout); |
| 712 | end_online: |
| 713 | rcu_thread_online(); |
| 714 | rcu_read_lock(); |
| 715 | printf("Counting nodes... "); |
| 716 | cds_lfht_count_nodes(test_ht, &approx_before, &count, &approx_after); |
| 717 | printf("done.\n"); |
| 718 | test_delete_all_nodes(test_ht); |
| 719 | rcu_read_unlock(); |
| 720 | rcu_thread_offline(); |
| 721 | if (count) { |
| 722 | printf("Approximation before node accounting: %ld nodes.\n", |
| 723 | approx_before); |
| 724 | printf("Nodes deleted from hash table before destroy: " |
| 725 | "%lu nodes.\n", |
| 726 | count); |
| 727 | printf("Approximation after node accounting: %ld nodes.\n", |
| 728 | approx_after); |
| 729 | } |
| 730 | |
| 731 | ret = cds_lfht_destroy(test_ht, NULL); |
| 732 | if (ret) { |
| 733 | printf_verbose("final delete aborted\n"); |
| 734 | mainret = 1; |
| 735 | } else { |
| 736 | printf_verbose("final delete success\n"); |
| 737 | } |
| 738 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, |
| 739 | tot_writes); |
| 740 | nr_leaked = (long long) tot_add + init_populate - tot_remove - count; |
| 741 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu " |
| 742 | "nr_writers %3u " |
| 743 | "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu " |
| 744 | "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n", |
| 745 | argv[0], duration, nr_readers, rduration, |
| 746 | nr_writers, wdelay, tot_reads, tot_writes, |
| 747 | tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove, |
| 748 | nr_leaked); |
| 749 | if (nr_leaked != 0) { |
| 750 | mainret = 1; |
| 751 | printf("WARNING: %lld nodes were leaked!\n", nr_leaked); |
| 752 | } |
| 753 | |
| 754 | rcu_unregister_thread(); |
| 755 | end_free_call_rcu_data: |
| 756 | free_all_cpu_call_rcu_data(); |
| 757 | free(count_writer); |
| 758 | end_free_count_reader: |
| 759 | free(count_reader); |
| 760 | end_free_tid_writer: |
| 761 | free(tid_writer); |
| 762 | end_free_tid_reader: |
| 763 | free(tid_reader); |
| 764 | end: |
| 765 | if (!mainret) |
| 766 | exit(EXIT_SUCCESS); |
| 767 | else |
| 768 | exit(EXIT_FAILURE); |
| 769 | } |