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