| 1 | /* |
| 2 | * test_urcu_hash.c |
| 3 | * |
| 4 | * Userspace RCU library - test program |
| 5 | * |
| 6 | * Copyright February 2009 - 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 <stdio.h> |
| 25 | #include <pthread.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <unistd.h> |
| 31 | #include <stdio.h> |
| 32 | #include <assert.h> |
| 33 | #include <sched.h> |
| 34 | #include <errno.h> |
| 35 | |
| 36 | #ifdef __linux__ |
| 37 | #include <syscall.h> |
| 38 | #endif |
| 39 | |
| 40 | #define DEFAULT_HASH_SIZE 32 |
| 41 | #define DEFAULT_RAND_POOL 1000000 |
| 42 | |
| 43 | /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */ |
| 44 | #define CACHE_LINE_SIZE 4096 |
| 45 | |
| 46 | /* hardcoded number of CPUs */ |
| 47 | #define NR_CPUS 16384 |
| 48 | |
| 49 | #ifdef POISON_FREE |
| 50 | #define poison_free(ptr) \ |
| 51 | do { \ |
| 52 | memset(ptr, 0x42, sizeof(*(ptr))); \ |
| 53 | free(ptr); \ |
| 54 | } while (0) |
| 55 | #else |
| 56 | #define poison_free(ptr) free(ptr) |
| 57 | #endif |
| 58 | |
| 59 | |
| 60 | |
| 61 | #if defined(_syscall0) |
| 62 | _syscall0(pid_t, gettid) |
| 63 | #elif defined(__NR_gettid) |
| 64 | static inline pid_t gettid(void) |
| 65 | { |
| 66 | return syscall(__NR_gettid); |
| 67 | } |
| 68 | #else |
| 69 | #warning "use pid as tid" |
| 70 | static inline pid_t gettid(void) |
| 71 | { |
| 72 | return getpid(); |
| 73 | } |
| 74 | #endif |
| 75 | |
| 76 | #ifndef DYNAMIC_LINK_TEST |
| 77 | #define _LGPL_SOURCE |
| 78 | #else |
| 79 | #define debug_yield_read() |
| 80 | #endif |
| 81 | #include <urcu-qsbr.h> |
| 82 | #include <urcu/rculfhash.h> |
| 83 | #include <urcu-call-rcu.h> |
| 84 | |
| 85 | struct wr_count { |
| 86 | unsigned long update_ops; |
| 87 | unsigned long add; |
| 88 | unsigned long add_exist; |
| 89 | unsigned long remove; |
| 90 | }; |
| 91 | |
| 92 | static unsigned int __thread rand_lookup; |
| 93 | static unsigned long __thread nr_add; |
| 94 | static unsigned long __thread nr_addexist; |
| 95 | static unsigned long __thread nr_del; |
| 96 | static unsigned long __thread nr_delnoent; |
| 97 | static unsigned long __thread lookup_fail; |
| 98 | static unsigned long __thread lookup_ok; |
| 99 | |
| 100 | static struct cds_lfht *test_ht; |
| 101 | |
| 102 | struct test_data { |
| 103 | int a; |
| 104 | int b; |
| 105 | }; |
| 106 | |
| 107 | static volatile int test_go, test_stop; |
| 108 | |
| 109 | static unsigned long wdelay; |
| 110 | |
| 111 | static unsigned long duration; |
| 112 | |
| 113 | /* read-side C.S. duration, in loops */ |
| 114 | static unsigned long rduration; |
| 115 | |
| 116 | static unsigned long init_hash_size = DEFAULT_HASH_SIZE; |
| 117 | static unsigned long init_populate; |
| 118 | static unsigned long rand_pool = DEFAULT_RAND_POOL; |
| 119 | static int opt_auto_resize; |
| 120 | static int add_only, add_unique; |
| 121 | |
| 122 | static unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset; |
| 123 | static int validate_lookup; |
| 124 | |
| 125 | static inline void loop_sleep(unsigned long l) |
| 126 | { |
| 127 | while(l-- != 0) |
| 128 | caa_cpu_relax(); |
| 129 | } |
| 130 | |
| 131 | static int verbose_mode; |
| 132 | |
| 133 | #define printf_verbose(fmt, args...) \ |
| 134 | do { \ |
| 135 | if (verbose_mode) \ |
| 136 | printf(fmt, ## args); \ |
| 137 | } while (0) |
| 138 | |
| 139 | static unsigned int cpu_affinities[NR_CPUS]; |
| 140 | static unsigned int next_aff = 0; |
| 141 | static int use_affinity = 0; |
| 142 | |
| 143 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 144 | |
| 145 | static void set_affinity(void) |
| 146 | { |
| 147 | cpu_set_t mask; |
| 148 | int cpu; |
| 149 | int ret; |
| 150 | |
| 151 | if (!use_affinity) |
| 152 | return; |
| 153 | |
| 154 | ret = pthread_mutex_lock(&affinity_mutex); |
| 155 | if (ret) { |
| 156 | perror("Error in pthread mutex lock"); |
| 157 | exit(-1); |
| 158 | } |
| 159 | cpu = cpu_affinities[next_aff++]; |
| 160 | ret = pthread_mutex_unlock(&affinity_mutex); |
| 161 | if (ret) { |
| 162 | perror("Error in pthread mutex unlock"); |
| 163 | exit(-1); |
| 164 | } |
| 165 | CPU_ZERO(&mask); |
| 166 | CPU_SET(cpu, &mask); |
| 167 | sched_setaffinity(0, sizeof(mask), &mask); |
| 168 | } |
| 169 | |
| 170 | static enum { |
| 171 | AR_RANDOM = 0, |
| 172 | AR_ADD = 1, |
| 173 | AR_REMOVE = -1, |
| 174 | } addremove; /* 1: add, -1 remove, 0: random */ |
| 175 | |
| 176 | static |
| 177 | void sigusr1_handler(int signo) |
| 178 | { |
| 179 | switch (addremove) { |
| 180 | case AR_ADD: |
| 181 | printf("Add/Remove: random.\n"); |
| 182 | addremove = AR_RANDOM; |
| 183 | break; |
| 184 | case AR_RANDOM: |
| 185 | printf("Add/Remove: remove only.\n"); |
| 186 | addremove = AR_REMOVE; |
| 187 | break; |
| 188 | case AR_REMOVE: |
| 189 | printf("Add/Remove: add only.\n"); |
| 190 | addremove = AR_ADD; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * returns 0 if test should end. |
| 197 | */ |
| 198 | static int test_duration_write(void) |
| 199 | { |
| 200 | return !test_stop; |
| 201 | } |
| 202 | |
| 203 | static int test_duration_read(void) |
| 204 | { |
| 205 | return !test_stop; |
| 206 | } |
| 207 | |
| 208 | static unsigned long long __thread nr_writes; |
| 209 | static unsigned long long __thread nr_reads; |
| 210 | |
| 211 | static unsigned int nr_readers; |
| 212 | static unsigned int nr_writers; |
| 213 | |
| 214 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 215 | |
| 216 | void rcu_copy_mutex_lock(void) |
| 217 | { |
| 218 | int ret; |
| 219 | ret = pthread_mutex_lock(&rcu_copy_mutex); |
| 220 | if (ret) { |
| 221 | perror("Error in pthread mutex lock"); |
| 222 | exit(-1); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void rcu_copy_mutex_unlock(void) |
| 227 | { |
| 228 | int ret; |
| 229 | |
| 230 | ret = pthread_mutex_unlock(&rcu_copy_mutex); |
| 231 | if (ret) { |
| 232 | perror("Error in pthread mutex unlock"); |
| 233 | exit(-1); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Hash function |
| 239 | * Source: http://burtleburtle.net/bob/c/lookup3.c |
| 240 | * Originally Public Domain |
| 241 | */ |
| 242 | |
| 243 | #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) |
| 244 | |
| 245 | #define mix(a, b, c) \ |
| 246 | do { \ |
| 247 | a -= c; a ^= rot(c, 4); c += b; \ |
| 248 | b -= a; b ^= rot(a, 6); a += c; \ |
| 249 | c -= b; c ^= rot(b, 8); b += a; \ |
| 250 | a -= c; a ^= rot(c, 16); c += b; \ |
| 251 | b -= a; b ^= rot(a, 19); a += c; \ |
| 252 | c -= b; c ^= rot(b, 4); b += a; \ |
| 253 | } while (0) |
| 254 | |
| 255 | #define final(a, b, c) \ |
| 256 | { \ |
| 257 | c ^= b; c -= rot(b, 14); \ |
| 258 | a ^= c; a -= rot(c, 11); \ |
| 259 | b ^= a; b -= rot(a, 25); \ |
| 260 | c ^= b; c -= rot(b, 16); \ |
| 261 | a ^= c; a -= rot(c, 4);\ |
| 262 | b ^= a; b -= rot(a, 14); \ |
| 263 | c ^= b; c -= rot(b, 24); \ |
| 264 | } |
| 265 | |
| 266 | static __attribute__((unused)) |
| 267 | uint32_t hash_u32( |
| 268 | const uint32_t *k, /* the key, an array of uint32_t values */ |
| 269 | size_t length, /* the length of the key, in uint32_ts */ |
| 270 | uint32_t initval) /* the previous hash, or an arbitrary value */ |
| 271 | { |
| 272 | uint32_t a, b, c; |
| 273 | |
| 274 | /* Set up the internal state */ |
| 275 | a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval; |
| 276 | |
| 277 | /*----------------------------------------- handle most of the key */ |
| 278 | while (length > 3) { |
| 279 | a += k[0]; |
| 280 | b += k[1]; |
| 281 | c += k[2]; |
| 282 | mix(a, b, c); |
| 283 | length -= 3; |
| 284 | k += 3; |
| 285 | } |
| 286 | |
| 287 | /*----------------------------------- handle the last 3 uint32_t's */ |
| 288 | switch (length) { /* all the case statements fall through */ |
| 289 | case 3: c += k[2]; |
| 290 | case 2: b += k[1]; |
| 291 | case 1: a += k[0]; |
| 292 | final(a, b, c); |
| 293 | case 0: /* case 0: nothing left to add */ |
| 294 | break; |
| 295 | } |
| 296 | /*---------------------------------------------- report the result */ |
| 297 | return c; |
| 298 | } |
| 299 | |
| 300 | static |
| 301 | void hashword2( |
| 302 | const uint32_t *k, /* the key, an array of uint32_t values */ |
| 303 | size_t length, /* the length of the key, in uint32_ts */ |
| 304 | uint32_t *pc, /* IN: seed OUT: primary hash value */ |
| 305 | uint32_t *pb) /* IN: more seed OUT: secondary hash value */ |
| 306 | { |
| 307 | uint32_t a, b, c; |
| 308 | |
| 309 | /* Set up the internal state */ |
| 310 | a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc; |
| 311 | c += *pb; |
| 312 | |
| 313 | /*----------------------------------------- handle most of the key */ |
| 314 | while (length > 3) { |
| 315 | a += k[0]; |
| 316 | b += k[1]; |
| 317 | c += k[2]; |
| 318 | mix(a, b, c); |
| 319 | length -= 3; |
| 320 | k += 3; |
| 321 | } |
| 322 | |
| 323 | /*----------------------------------- handle the last 3 uint32_t's */ |
| 324 | switch (length) { /* all the case statements fall through */ |
| 325 | case 3: c += k[2]; |
| 326 | case 2: b += k[1]; |
| 327 | case 1: a += k[0]; |
| 328 | final(a, b, c); |
| 329 | case 0: /* case 0: nothing left to add */ |
| 330 | break; |
| 331 | } |
| 332 | /*---------------------------------------------- report the result */ |
| 333 | *pc = c; |
| 334 | *pb = b; |
| 335 | } |
| 336 | |
| 337 | #if (CAA_BITS_PER_LONG == 32) |
| 338 | static |
| 339 | unsigned long test_hash(void *_key, size_t length, unsigned long seed) |
| 340 | { |
| 341 | unsigned long key = (unsigned long) _key; |
| 342 | unsigned long v; |
| 343 | |
| 344 | assert(length == sizeof(unsigned long)); |
| 345 | return hash_u32(&v, 1, seed); |
| 346 | } |
| 347 | #else |
| 348 | static |
| 349 | unsigned long test_hash(void *_key, size_t length, unsigned long seed) |
| 350 | { |
| 351 | union { |
| 352 | uint64_t v64; |
| 353 | uint32_t v32[2]; |
| 354 | } v; |
| 355 | union { |
| 356 | uint64_t v64; |
| 357 | uint32_t v32[2]; |
| 358 | } key; |
| 359 | |
| 360 | assert(length == sizeof(unsigned long)); |
| 361 | v.v64 = (uint64_t) seed; |
| 362 | key.v64 = (uint64_t) _key; |
| 363 | hashword2(key.v32, 2, &v.v32[0], &v.v32[1]); |
| 364 | return v.v64; |
| 365 | } |
| 366 | #endif |
| 367 | |
| 368 | static |
| 369 | unsigned long test_compare(void *key1, size_t key1_len, |
| 370 | void *key2, size_t key2_len) |
| 371 | { |
| 372 | if (unlikely(key1_len != key2_len)) |
| 373 | return -1; |
| 374 | assert(key1_len == sizeof(unsigned long)); |
| 375 | if (key1 == key2) |
| 376 | return 0; |
| 377 | else |
| 378 | return 1; |
| 379 | } |
| 380 | |
| 381 | void *thr_reader(void *_count) |
| 382 | { |
| 383 | unsigned long long *count = _count; |
| 384 | struct cds_lfht_node *node; |
| 385 | |
| 386 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", |
| 387 | "reader", pthread_self(), (unsigned long)gettid()); |
| 388 | |
| 389 | set_affinity(); |
| 390 | |
| 391 | rcu_register_thread(); |
| 392 | |
| 393 | while (!test_go) |
| 394 | { |
| 395 | } |
| 396 | cmm_smp_mb(); |
| 397 | |
| 398 | for (;;) { |
| 399 | rcu_read_lock(); |
| 400 | node = cds_lfht_lookup(test_ht, |
| 401 | (void *)(unsigned long)((rand_r(&rand_lookup) % rand_pool) + lookup_pool_offset), |
| 402 | sizeof(void *)); |
| 403 | if (node == NULL) { |
| 404 | if (validate_lookup) { |
| 405 | printf("[ERROR] Lookup cannot find initial node.\n"); |
| 406 | } |
| 407 | lookup_fail++; |
| 408 | } else { |
| 409 | lookup_ok++; |
| 410 | } |
| 411 | debug_yield_read(); |
| 412 | if (unlikely(rduration)) |
| 413 | loop_sleep(rduration); |
| 414 | rcu_read_unlock(); |
| 415 | nr_reads++; |
| 416 | if (unlikely(!test_duration_read())) |
| 417 | break; |
| 418 | if (unlikely((nr_reads & ((1 << 10) - 1)) == 0)) |
| 419 | rcu_quiescent_state(); |
| 420 | } |
| 421 | |
| 422 | rcu_unregister_thread(); |
| 423 | |
| 424 | *count = nr_reads; |
| 425 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", |
| 426 | "reader", pthread_self(), (unsigned long)gettid()); |
| 427 | printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n", |
| 428 | pthread_self(), lookup_fail, lookup_ok); |
| 429 | return ((void*)1); |
| 430 | |
| 431 | } |
| 432 | |
| 433 | static |
| 434 | void free_node_cb(struct rcu_head *head) |
| 435 | { |
| 436 | struct cds_lfht_node *node = |
| 437 | caa_container_of(head, struct cds_lfht_node, head); |
| 438 | free(node); |
| 439 | } |
| 440 | |
| 441 | void *thr_writer(void *_count) |
| 442 | { |
| 443 | struct cds_lfht_node *node, *ret_node; |
| 444 | struct wr_count *count = _count; |
| 445 | int ret; |
| 446 | |
| 447 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", |
| 448 | "writer", pthread_self(), (unsigned long)gettid()); |
| 449 | |
| 450 | set_affinity(); |
| 451 | |
| 452 | rcu_register_thread(); |
| 453 | |
| 454 | while (!test_go) |
| 455 | { |
| 456 | } |
| 457 | cmm_smp_mb(); |
| 458 | |
| 459 | for (;;) { |
| 460 | if ((addremove == AR_ADD || add_only) |
| 461 | || (addremove == AR_RANDOM && rand_r(&rand_lookup) & 1)) { |
| 462 | node = malloc(sizeof(struct cds_lfht_node)); |
| 463 | rcu_read_lock(); |
| 464 | cds_lfht_node_init(node, |
| 465 | (void *)(unsigned long)((rand_r(&rand_lookup) % rand_pool) + write_pool_offset), |
| 466 | sizeof(void *)); |
| 467 | if (add_unique) |
| 468 | ret_node = cds_lfht_add_unique(test_ht, node); |
| 469 | else |
| 470 | cds_lfht_add(test_ht, node); |
| 471 | rcu_read_unlock(); |
| 472 | if (add_unique && ret_node != node) { |
| 473 | free(node); |
| 474 | nr_addexist++; |
| 475 | } else |
| 476 | nr_add++; |
| 477 | } else { |
| 478 | /* May delete */ |
| 479 | rcu_read_lock(); |
| 480 | node = cds_lfht_lookup(test_ht, |
| 481 | (void *)(unsigned long)((rand_r(&rand_lookup) % rand_pool) + write_pool_offset), |
| 482 | sizeof(void *)); |
| 483 | if (node) |
| 484 | ret = cds_lfht_remove(test_ht, node); |
| 485 | else |
| 486 | ret = -ENOENT; |
| 487 | rcu_read_unlock(); |
| 488 | if (ret == 0) { |
| 489 | call_rcu(&node->head, free_node_cb); |
| 490 | nr_del++; |
| 491 | } else |
| 492 | nr_delnoent++; |
| 493 | } |
| 494 | #if 0 |
| 495 | //if (nr_writes % 100000 == 0) { |
| 496 | if (nr_writes % 1000 == 0) { |
| 497 | rcu_read_lock(); |
| 498 | if (rand_r(&rand_lookup) & 1) { |
| 499 | ht_resize(test_ht, 1); |
| 500 | } else { |
| 501 | ht_resize(test_ht, -1); |
| 502 | } |
| 503 | rcu_read_unlock(); |
| 504 | } |
| 505 | #endif //0 |
| 506 | nr_writes++; |
| 507 | if (unlikely(!test_duration_write())) |
| 508 | break; |
| 509 | if (unlikely(wdelay)) |
| 510 | loop_sleep(wdelay); |
| 511 | if (unlikely((nr_writes & ((1 << 10) - 1)) == 0)) |
| 512 | rcu_quiescent_state(); |
| 513 | } |
| 514 | |
| 515 | rcu_unregister_thread(); |
| 516 | |
| 517 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", |
| 518 | "writer", pthread_self(), (unsigned long)gettid()); |
| 519 | printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, " |
| 520 | "nr_delnoent %lu\n", pthread_self(), nr_add, |
| 521 | nr_addexist, nr_del, nr_delnoent); |
| 522 | count->update_ops = nr_writes; |
| 523 | count->add = nr_add; |
| 524 | count->add_exist = nr_addexist; |
| 525 | count->remove = nr_del; |
| 526 | return ((void*)2); |
| 527 | } |
| 528 | |
| 529 | static int populate_hash(void) |
| 530 | { |
| 531 | struct cds_lfht_node *node, *ret_node; |
| 532 | |
| 533 | if (!init_populate) |
| 534 | return 0; |
| 535 | |
| 536 | if (add_unique && init_populate * 10 > rand_pool) { |
| 537 | printf("WARNING: required to populate %lu nodes (-k), but random " |
| 538 | "pool is quite small (%lu values) and we are in add_unique (-u) mode. Try with a " |
| 539 | "larger random pool (-p option). This may take a while...\n", init_populate, rand_pool); |
| 540 | } |
| 541 | |
| 542 | while (nr_add < init_populate) { |
| 543 | node = malloc(sizeof(struct cds_lfht_node)); |
| 544 | cds_lfht_node_init(node, |
| 545 | (void *)(unsigned long)((rand_r(&rand_lookup) % rand_pool) + init_pool_offset), |
| 546 | sizeof(void *)); |
| 547 | if (add_unique) |
| 548 | ret_node = cds_lfht_add_unique(test_ht, node); |
| 549 | else |
| 550 | cds_lfht_add(test_ht, node); |
| 551 | if (add_unique && ret_node != node) { |
| 552 | free(node); |
| 553 | nr_addexist++; |
| 554 | } else |
| 555 | nr_add++; |
| 556 | nr_writes++; |
| 557 | } |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | void show_usage(int argc, char **argv) |
| 562 | { |
| 563 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); |
| 564 | #ifdef DEBUG_YIELD |
| 565 | printf(" [-r] [-w] (yield reader and/or writer)"); |
| 566 | #endif |
| 567 | printf(" [-d delay] (writer period (us))"); |
| 568 | printf(" [-c duration] (reader C.S. duration (in loops))"); |
| 569 | printf(" [-v] (verbose output)"); |
| 570 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); |
| 571 | printf(" [-p size] (random key value pool size)"); |
| 572 | printf(" [-h size] (initial hash table size)"); |
| 573 | printf(" [-u] Uniquify add."); |
| 574 | printf(" [-i] Add only (no removal)."); |
| 575 | printf(" [-k nr_nodes] Number of nodes to insert initially."); |
| 576 | printf(" [-A] Automatically resize hash table."); |
| 577 | printf(" [-R offset] Lookup pool offset."); |
| 578 | printf(" [-S offset] Write pool offset."); |
| 579 | printf(" [-T offset] Init pool offset."); |
| 580 | printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range)."); |
| 581 | printf("\n"); |
| 582 | } |
| 583 | |
| 584 | int main(int argc, char **argv) |
| 585 | { |
| 586 | int err; |
| 587 | pthread_t *tid_reader, *tid_writer; |
| 588 | void *tret; |
| 589 | unsigned long long *count_reader; |
| 590 | struct wr_count *count_writer; |
| 591 | unsigned long long tot_reads = 0, tot_writes = 0, |
| 592 | tot_add = 0, tot_add_exist = 0, tot_remove = 0; |
| 593 | unsigned long count, removed; |
| 594 | int i, a, ret; |
| 595 | struct sigaction act; |
| 596 | unsigned int remain; |
| 597 | |
| 598 | if (argc < 4) { |
| 599 | show_usage(argc, argv); |
| 600 | return -1; |
| 601 | } |
| 602 | |
| 603 | err = sscanf(argv[1], "%u", &nr_readers); |
| 604 | if (err != 1) { |
| 605 | show_usage(argc, argv); |
| 606 | return -1; |
| 607 | } |
| 608 | |
| 609 | err = sscanf(argv[2], "%u", &nr_writers); |
| 610 | if (err != 1) { |
| 611 | show_usage(argc, argv); |
| 612 | return -1; |
| 613 | } |
| 614 | |
| 615 | err = sscanf(argv[3], "%lu", &duration); |
| 616 | if (err != 1) { |
| 617 | show_usage(argc, argv); |
| 618 | return -1; |
| 619 | } |
| 620 | |
| 621 | for (i = 4; i < argc; i++) { |
| 622 | if (argv[i][0] != '-') |
| 623 | continue; |
| 624 | switch (argv[i][1]) { |
| 625 | #ifdef DEBUG_YIELD |
| 626 | case 'r': |
| 627 | yield_active |= YIELD_READ; |
| 628 | break; |
| 629 | case 'w': |
| 630 | yield_active |= YIELD_WRITE; |
| 631 | break; |
| 632 | #endif |
| 633 | case 'a': |
| 634 | if (argc < i + 2) { |
| 635 | show_usage(argc, argv); |
| 636 | return -1; |
| 637 | } |
| 638 | a = atoi(argv[++i]); |
| 639 | cpu_affinities[next_aff++] = a; |
| 640 | use_affinity = 1; |
| 641 | printf_verbose("Adding CPU %d affinity\n", a); |
| 642 | break; |
| 643 | case 'c': |
| 644 | if (argc < i + 2) { |
| 645 | show_usage(argc, argv); |
| 646 | return -1; |
| 647 | } |
| 648 | rduration = atol(argv[++i]); |
| 649 | break; |
| 650 | case 'd': |
| 651 | if (argc < i + 2) { |
| 652 | show_usage(argc, argv); |
| 653 | return -1; |
| 654 | } |
| 655 | wdelay = atol(argv[++i]); |
| 656 | break; |
| 657 | case 'v': |
| 658 | verbose_mode = 1; |
| 659 | break; |
| 660 | case 'p': |
| 661 | if (argc < i + 2) { |
| 662 | show_usage(argc, argv); |
| 663 | return -1; |
| 664 | } |
| 665 | rand_pool = atol(argv[++i]); |
| 666 | break; |
| 667 | case 'h': |
| 668 | if (argc < i + 2) { |
| 669 | show_usage(argc, argv); |
| 670 | return -1; |
| 671 | } |
| 672 | init_hash_size = atol(argv[++i]); |
| 673 | break; |
| 674 | case 'u': |
| 675 | add_unique = 1; |
| 676 | break; |
| 677 | case 'i': |
| 678 | add_only = 1; |
| 679 | break; |
| 680 | case 'k': |
| 681 | init_populate = atol(argv[++i]); |
| 682 | break; |
| 683 | case 'A': |
| 684 | opt_auto_resize = 1; |
| 685 | break; |
| 686 | case 'R': |
| 687 | lookup_pool_offset = atol(argv[++i]); |
| 688 | break; |
| 689 | case 'S': |
| 690 | write_pool_offset = atol(argv[++i]); |
| 691 | break; |
| 692 | case 'T': |
| 693 | init_pool_offset = atol(argv[++i]); |
| 694 | break; |
| 695 | case 'V': |
| 696 | validate_lookup = 1; |
| 697 | break; |
| 698 | |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /* Check if hash size is power of 2 */ |
| 703 | if (init_hash_size && init_hash_size & (init_hash_size - 1)) { |
| 704 | printf("Error: Hash table size %lu is not a power of 2.\n", |
| 705 | init_hash_size); |
| 706 | return -1; |
| 707 | } |
| 708 | |
| 709 | memset(&act, 0, sizeof(act)); |
| 710 | ret = sigemptyset(&act.sa_mask); |
| 711 | if (ret == -1) { |
| 712 | perror("sigemptyset"); |
| 713 | return -1; |
| 714 | } |
| 715 | act.sa_handler = sigusr1_handler; |
| 716 | act.sa_flags = SA_RESTART; |
| 717 | ret = sigaction(SIGUSR1, &act, NULL); |
| 718 | if (ret == -1) { |
| 719 | perror("sigaction"); |
| 720 | return -1; |
| 721 | } |
| 722 | |
| 723 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", |
| 724 | duration, nr_readers, nr_writers); |
| 725 | printf_verbose("Writer delay : %lu loops.\n", wdelay); |
| 726 | printf_verbose("Reader duration : %lu loops.\n", rduration); |
| 727 | printf_verbose("Random pool size : %lu.\n", rand_pool); |
| 728 | printf_verbose("Mode:%s%s.\n", |
| 729 | add_only ? " add only" : " add/remove", |
| 730 | add_unique ? " uniquify" : ""); |
| 731 | printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size); |
| 732 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", |
| 733 | "main", pthread_self(), (unsigned long)gettid()); |
| 734 | |
| 735 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); |
| 736 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); |
| 737 | count_reader = malloc(sizeof(*count_reader) * nr_readers); |
| 738 | count_writer = malloc(sizeof(*count_writer) * nr_writers); |
| 739 | test_ht = cds_lfht_new(test_hash, test_compare, 0x42UL, |
| 740 | init_hash_size, |
| 741 | opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0, |
| 742 | call_rcu, synchronize_rcu, rcu_read_lock, |
| 743 | rcu_read_unlock, rcu_thread_offline, |
| 744 | rcu_thread_online); |
| 745 | ret = populate_hash(); |
| 746 | assert(!ret); |
| 747 | err = create_all_cpu_call_rcu_data(0); |
| 748 | assert(!err); |
| 749 | |
| 750 | next_aff = 0; |
| 751 | |
| 752 | for (i = 0; i < nr_readers; i++) { |
| 753 | err = pthread_create(&tid_reader[i], NULL, thr_reader, |
| 754 | &count_reader[i]); |
| 755 | if (err != 0) |
| 756 | exit(1); |
| 757 | } |
| 758 | for (i = 0; i < nr_writers; i++) { |
| 759 | err = pthread_create(&tid_writer[i], NULL, thr_writer, |
| 760 | &count_writer[i]); |
| 761 | if (err != 0) |
| 762 | exit(1); |
| 763 | } |
| 764 | |
| 765 | cmm_smp_mb(); |
| 766 | |
| 767 | test_go = 1; |
| 768 | |
| 769 | remain = duration; |
| 770 | do { |
| 771 | remain = sleep(remain); |
| 772 | } while (remain > 0); |
| 773 | |
| 774 | test_stop = 1; |
| 775 | |
| 776 | for (i = 0; i < nr_readers; i++) { |
| 777 | err = pthread_join(tid_reader[i], &tret); |
| 778 | if (err != 0) |
| 779 | exit(1); |
| 780 | tot_reads += count_reader[i]; |
| 781 | } |
| 782 | for (i = 0; i < nr_writers; i++) { |
| 783 | err = pthread_join(tid_writer[i], &tret); |
| 784 | if (err != 0) |
| 785 | exit(1); |
| 786 | tot_writes += count_writer[i].update_ops; |
| 787 | tot_add += count_writer[i].add; |
| 788 | tot_add_exist += count_writer[i].add_exist; |
| 789 | tot_remove += count_writer[i].remove; |
| 790 | } |
| 791 | printf("Counting nodes... "); |
| 792 | fflush(stdout); |
| 793 | cds_lfht_count_nodes(test_ht, &count, &removed); |
| 794 | printf("done.\n"); |
| 795 | if (count || removed) |
| 796 | printf("WARNING: nodes left in the hash table upon destroy: " |
| 797 | "%lu nodes + %lu logically removed.\n", count, removed); |
| 798 | ret = cds_lfht_destroy(test_ht); |
| 799 | |
| 800 | if (ret) |
| 801 | printf_verbose("final delete aborted\n"); |
| 802 | else |
| 803 | printf_verbose("final delete success\n"); |
| 804 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, |
| 805 | tot_writes); |
| 806 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu " |
| 807 | "nr_writers %3u " |
| 808 | "wdelay %6lu rand_pool %12llu nr_reads %12llu nr_writes %12llu nr_ops %12llu " |
| 809 | "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n", |
| 810 | argv[0], duration, nr_readers, rduration, |
| 811 | nr_writers, wdelay, rand_pool, tot_reads, tot_writes, |
| 812 | tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove, |
| 813 | (long long) tot_add + init_populate - tot_remove - count); |
| 814 | free_all_cpu_call_rcu_data(); |
| 815 | free(tid_reader); |
| 816 | free(tid_writer); |
| 817 | free(count_reader); |
| 818 | free(count_writer); |
| 819 | return 0; |
| 820 | } |