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