Merge branch 'master' into urcu/ht-shrink
[urcu.git] / tests / test_urcu_hash.c
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_MIN_ALLOC_SIZE 1
42 #define DEFAULT_RAND_POOL 1000000
43
44 /*
45 * Note: the hash seed should be a random value for hash tables
46 * targeting production environments to provide protection against
47 * denial of service attacks. We keep it a static value within this test
48 * program to compare identical benchmark runs.
49 */
50 #define TEST_HASH_SEED 0x42UL
51
52 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
53 #define CACHE_LINE_SIZE 4096
54
55 /* hardcoded number of CPUs */
56 #define NR_CPUS 16384
57
58 #ifdef POISON_FREE
59 #define poison_free(ptr) \
60 do { \
61 memset(ptr, 0x42, sizeof(*(ptr))); \
62 free(ptr); \
63 } while (0)
64 #else
65 #define poison_free(ptr) free(ptr)
66 #endif
67
68
69
70 #if defined(_syscall0)
71 _syscall0(pid_t, gettid)
72 #elif defined(__NR_gettid)
73 static inline pid_t gettid(void)
74 {
75 return syscall(__NR_gettid);
76 }
77 #else
78 #warning "use pid as tid"
79 static inline pid_t gettid(void)
80 {
81 return getpid();
82 }
83 #endif
84
85 #ifndef DYNAMIC_LINK_TEST
86 #define _LGPL_SOURCE
87 #else
88 #define debug_yield_read()
89 #endif
90 #include <urcu-qsbr.h>
91 #include <urcu/rculfhash.h>
92 #include <urcu-call-rcu.h>
93
94 struct wr_count {
95 unsigned long update_ops;
96 unsigned long add;
97 unsigned long add_exist;
98 unsigned long remove;
99 };
100
101 static unsigned int __thread rand_lookup;
102 static unsigned long __thread nr_add;
103 static unsigned long __thread nr_addexist;
104 static unsigned long __thread nr_del;
105 static unsigned long __thread nr_delnoent;
106 static unsigned long __thread lookup_fail;
107 static unsigned long __thread lookup_ok;
108
109 static struct cds_lfht *test_ht;
110
111 struct test_data {
112 int a;
113 int b;
114 };
115
116 struct lfht_test_node {
117 struct cds_lfht_node node;
118 void *key;
119 unsigned int key_len;
120 /* cache-cold for iteration */
121 struct rcu_head head;
122 };
123
124 static inline struct lfht_test_node *
125 to_test_node(struct cds_lfht_node *node)
126 {
127 return caa_container_of(node, struct lfht_test_node, node);
128 }
129
130 static inline
131 void lfht_test_node_init(struct lfht_test_node *node, void *key,
132 size_t key_len)
133 {
134 cds_lfht_node_init(&node->node);
135 node->key = key;
136 node->key_len = key_len;
137 }
138
139 static inline struct lfht_test_node *
140 cds_lfht_iter_get_test_node(struct cds_lfht_iter *iter)
141 {
142 return to_test_node(cds_lfht_iter_get_node(iter));
143 }
144
145 static volatile int test_go, test_stop;
146
147 static unsigned long wdelay;
148
149 static unsigned long duration;
150
151 /* read-side C.S. duration, in loops */
152 static unsigned long rduration;
153
154 static unsigned long init_hash_size = DEFAULT_HASH_SIZE;
155 static unsigned long min_hash_alloc_size = DEFAULT_MIN_ALLOC_SIZE;
156 static unsigned long max_hash_buckets_size = (1UL << 20);
157 static unsigned long init_populate;
158 static int opt_auto_resize;
159 static int add_only, add_unique, add_replace;
160 static const struct cds_lfht_mm_type *memory_backend;
161
162 static unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
163 static unsigned long init_pool_size = DEFAULT_RAND_POOL,
164 lookup_pool_size = DEFAULT_RAND_POOL,
165 write_pool_size = DEFAULT_RAND_POOL;
166 static int validate_lookup;
167
168 static int count_pipe[2];
169
170 static inline void loop_sleep(unsigned long l)
171 {
172 while(l-- != 0)
173 caa_cpu_relax();
174 }
175
176 static int verbose_mode;
177
178 #define printf_verbose(fmt, args...) \
179 do { \
180 if (verbose_mode) \
181 printf(fmt, ## args); \
182 } while (0)
183
184 static unsigned int cpu_affinities[NR_CPUS];
185 static unsigned int next_aff = 0;
186 static int use_affinity = 0;
187
188 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
189
190 static void set_affinity(void)
191 {
192 cpu_set_t mask;
193 int cpu;
194 int ret;
195
196 if (!use_affinity)
197 return;
198
199 ret = pthread_mutex_lock(&affinity_mutex);
200 if (ret) {
201 perror("Error in pthread mutex lock");
202 exit(-1);
203 }
204 cpu = cpu_affinities[next_aff++];
205 ret = pthread_mutex_unlock(&affinity_mutex);
206 if (ret) {
207 perror("Error in pthread mutex unlock");
208 exit(-1);
209 }
210 CPU_ZERO(&mask);
211 CPU_SET(cpu, &mask);
212 sched_setaffinity(0, sizeof(mask), &mask);
213 }
214
215 static enum {
216 AR_RANDOM = 0,
217 AR_ADD = 1,
218 AR_REMOVE = -1,
219 } addremove; /* 1: add, -1 remove, 0: random */
220
221 static
222 void sigusr1_handler(int signo)
223 {
224 switch (addremove) {
225 case AR_ADD:
226 printf("Add/Remove: random.\n");
227 addremove = AR_RANDOM;
228 break;
229 case AR_RANDOM:
230 printf("Add/Remove: remove only.\n");
231 addremove = AR_REMOVE;
232 break;
233 case AR_REMOVE:
234 printf("Add/Remove: add only.\n");
235 addremove = AR_ADD;
236 break;
237 }
238 }
239
240 static
241 void sigusr2_handler(int signo)
242 {
243 char msg[1] = { 0x42 };
244 ssize_t ret;
245
246 do {
247 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
248 } while (ret == -1L && errno == EINTR);
249 }
250
251 /*
252 * returns 0 if test should end.
253 */
254 static int test_duration_write(void)
255 {
256 return !test_stop;
257 }
258
259 static int test_duration_read(void)
260 {
261 return !test_stop;
262 }
263
264 static unsigned long long __thread nr_writes;
265 static unsigned long long __thread nr_reads;
266
267 static unsigned int nr_readers;
268 static unsigned int nr_writers;
269
270 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
271
272 void rcu_copy_mutex_lock(void)
273 {
274 int ret;
275 ret = pthread_mutex_lock(&rcu_copy_mutex);
276 if (ret) {
277 perror("Error in pthread mutex lock");
278 exit(-1);
279 }
280 }
281
282 void rcu_copy_mutex_unlock(void)
283 {
284 int ret;
285
286 ret = pthread_mutex_unlock(&rcu_copy_mutex);
287 if (ret) {
288 perror("Error in pthread mutex unlock");
289 exit(-1);
290 }
291 }
292
293 /*
294 * Hash function
295 * Source: http://burtleburtle.net/bob/c/lookup3.c
296 * Originally Public Domain
297 */
298
299 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
300
301 #define mix(a, b, c) \
302 do { \
303 a -= c; a ^= rot(c, 4); c += b; \
304 b -= a; b ^= rot(a, 6); a += c; \
305 c -= b; c ^= rot(b, 8); b += a; \
306 a -= c; a ^= rot(c, 16); c += b; \
307 b -= a; b ^= rot(a, 19); a += c; \
308 c -= b; c ^= rot(b, 4); b += a; \
309 } while (0)
310
311 #define final(a, b, c) \
312 { \
313 c ^= b; c -= rot(b, 14); \
314 a ^= c; a -= rot(c, 11); \
315 b ^= a; b -= rot(a, 25); \
316 c ^= b; c -= rot(b, 16); \
317 a ^= c; a -= rot(c, 4);\
318 b ^= a; b -= rot(a, 14); \
319 c ^= b; c -= rot(b, 24); \
320 }
321
322 static __attribute__((unused))
323 uint32_t hash_u32(
324 const uint32_t *k, /* the key, an array of uint32_t values */
325 size_t length, /* the length of the key, in uint32_ts */
326 uint32_t initval) /* the previous hash, or an arbitrary value */
327 {
328 uint32_t a, b, c;
329
330 /* Set up the internal state */
331 a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
332
333 /*----------------------------------------- handle most of the key */
334 while (length > 3) {
335 a += k[0];
336 b += k[1];
337 c += k[2];
338 mix(a, b, c);
339 length -= 3;
340 k += 3;
341 }
342
343 /*----------------------------------- handle the last 3 uint32_t's */
344 switch (length) { /* all the case statements fall through */
345 case 3: c += k[2];
346 case 2: b += k[1];
347 case 1: a += k[0];
348 final(a, b, c);
349 case 0: /* case 0: nothing left to add */
350 break;
351 }
352 /*---------------------------------------------- report the result */
353 return c;
354 }
355
356 static
357 void hashword2(
358 const uint32_t *k, /* the key, an array of uint32_t values */
359 size_t length, /* the length of the key, in uint32_ts */
360 uint32_t *pc, /* IN: seed OUT: primary hash value */
361 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
362 {
363 uint32_t a, b, c;
364
365 /* Set up the internal state */
366 a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
367 c += *pb;
368
369 /*----------------------------------------- handle most of the key */
370 while (length > 3) {
371 a += k[0];
372 b += k[1];
373 c += k[2];
374 mix(a, b, c);
375 length -= 3;
376 k += 3;
377 }
378
379 /*----------------------------------- handle the last 3 uint32_t's */
380 switch (length) { /* all the case statements fall through */
381 case 3: c += k[2];
382 case 2: b += k[1];
383 case 1: a += k[0];
384 final(a, b, c);
385 case 0: /* case 0: nothing left to add */
386 break;
387 }
388 /*---------------------------------------------- report the result */
389 *pc = c;
390 *pb = b;
391 }
392
393 #if (CAA_BITS_PER_LONG == 32)
394 static
395 unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
396 {
397 unsigned int key = (unsigned int) _key;
398
399 assert(length == sizeof(unsigned int));
400 return hash_u32(&key, 1, seed);
401 }
402 #else
403 static
404 unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
405 {
406 union {
407 uint64_t v64;
408 uint32_t v32[2];
409 } v;
410 union {
411 uint64_t v64;
412 uint32_t v32[2];
413 } key;
414
415 assert(length == sizeof(unsigned long));
416 v.v64 = (uint64_t) seed;
417 key.v64 = (uint64_t) _key;
418 hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
419 return v.v64;
420 }
421 #endif
422
423 static
424 unsigned long test_compare(const void *key1, size_t key1_len,
425 const void *key2, size_t key2_len)
426 {
427 if (caa_unlikely(key1_len != key2_len))
428 return -1;
429 assert(key1_len == sizeof(unsigned long));
430 if (key1 == key2)
431 return 0;
432 else
433 return 1;
434 }
435
436 static
437 int test_match(struct cds_lfht_node *node, const void *key)
438 {
439 struct lfht_test_node *test_node = to_test_node(node);
440
441 return !test_compare(test_node->key, test_node->key_len,
442 key, sizeof(unsigned long));
443 }
444
445 static inline
446 void cds_lfht_test_lookup(struct cds_lfht *ht, void *key, size_t key_len,
447 struct cds_lfht_iter *iter)
448 {
449 assert(key_len == sizeof(unsigned long));
450
451 cds_lfht_lookup(ht, test_hash(key, key_len, TEST_HASH_SEED),
452 test_match, key, iter);
453 }
454
455 void *thr_count(void *arg)
456 {
457 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
458 "counter", pthread_self(), (unsigned long)gettid());
459
460 rcu_register_thread();
461
462 for (;;) {
463 unsigned long count;
464 long approx_before, approx_after;
465 ssize_t len;
466 char buf[1];
467
468 rcu_thread_offline();
469 len = read(count_pipe[0], buf, 1);
470 rcu_thread_online();
471 if (caa_unlikely(!test_duration_read()))
472 break;
473 if (len != 1)
474 continue;
475 /* Accounting */
476 printf("Counting nodes... ");
477 fflush(stdout);
478 rcu_read_lock();
479 cds_lfht_count_nodes(test_ht, &approx_before, &count,
480 &approx_after);
481 rcu_read_unlock();
482 printf("done.\n");
483 printf("Approximation before node accounting: %ld nodes.\n",
484 approx_before);
485 printf("Accounting of nodes in the hash table: "
486 "%lu nodes.\n",
487 count);
488 printf("Approximation after node accounting: %ld nodes.\n",
489 approx_after);
490 }
491 rcu_unregister_thread();
492 return NULL;
493 }
494
495 void *thr_reader(void *_count)
496 {
497 unsigned long long *count = _count;
498 struct lfht_test_node *node;
499 struct cds_lfht_iter iter;
500
501 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
502 "reader", pthread_self(), (unsigned long)gettid());
503
504 set_affinity();
505
506 rcu_register_thread();
507
508 while (!test_go)
509 {
510 }
511 cmm_smp_mb();
512
513 for (;;) {
514 rcu_read_lock();
515 cds_lfht_test_lookup(test_ht,
516 (void *)(((unsigned long) rand_r(&rand_lookup) % lookup_pool_size) + lookup_pool_offset),
517 sizeof(void *), &iter);
518 node = cds_lfht_iter_get_test_node(&iter);
519 if (node == NULL) {
520 if (validate_lookup) {
521 printf("[ERROR] Lookup cannot find initial node.\n");
522 exit(-1);
523 }
524 lookup_fail++;
525 } else {
526 lookup_ok++;
527 }
528 debug_yield_read();
529 if (caa_unlikely(rduration))
530 loop_sleep(rduration);
531 rcu_read_unlock();
532 nr_reads++;
533 if (caa_unlikely(!test_duration_read()))
534 break;
535 if (caa_unlikely((nr_reads & ((1 << 10) - 1)) == 0))
536 rcu_quiescent_state();
537 }
538
539 rcu_unregister_thread();
540
541 *count = nr_reads;
542 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
543 "reader", pthread_self(), (unsigned long)gettid());
544 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
545 pthread_self(), lookup_fail, lookup_ok);
546 return ((void*)1);
547
548 }
549
550 static
551 void free_node_cb(struct rcu_head *head)
552 {
553 struct lfht_test_node *node =
554 caa_container_of(head, struct lfht_test_node, head);
555 free(node);
556 }
557
558 void *thr_writer(void *_count)
559 {
560 struct lfht_test_node *node;
561 struct cds_lfht_node *ret_node;
562 struct cds_lfht_iter iter;
563 struct wr_count *count = _count;
564 int ret;
565
566 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
567 "writer", pthread_self(), (unsigned long)gettid());
568
569 set_affinity();
570
571 rcu_register_thread();
572
573 while (!test_go)
574 {
575 }
576 cmm_smp_mb();
577
578 for (;;) {
579 if ((addremove == AR_ADD || add_only)
580 || (addremove == AR_RANDOM && rand_r(&rand_lookup) & 1)) {
581 node = malloc(sizeof(struct lfht_test_node));
582 lfht_test_node_init(node,
583 (void *)(((unsigned long) rand_r(&rand_lookup) % write_pool_size) + write_pool_offset),
584 sizeof(void *));
585 rcu_read_lock();
586 if (add_unique) {
587 ret_node = cds_lfht_add_unique(test_ht,
588 test_hash(node->key, node->key_len, TEST_HASH_SEED),
589 test_match, node->key, &node->node);
590 } else {
591 if (add_replace)
592 ret_node = cds_lfht_add_replace(test_ht,
593 test_hash(node->key, node->key_len, TEST_HASH_SEED),
594 test_match, node->key, &node->node);
595 else
596 cds_lfht_add(test_ht,
597 test_hash(node->key, node->key_len, TEST_HASH_SEED),
598 &node->node);
599 }
600 rcu_read_unlock();
601 if (add_unique && ret_node != &node->node) {
602 free(node);
603 nr_addexist++;
604 } else {
605 if (add_replace && ret_node) {
606 call_rcu(&to_test_node(ret_node)->head,
607 free_node_cb);
608 nr_addexist++;
609 } else {
610 nr_add++;
611 }
612 }
613 } else {
614 /* May delete */
615 rcu_read_lock();
616 cds_lfht_test_lookup(test_ht,
617 (void *)(((unsigned long) rand_r(&rand_lookup) % write_pool_size) + write_pool_offset),
618 sizeof(void *), &iter);
619 ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
620 rcu_read_unlock();
621 if (ret == 0) {
622 node = cds_lfht_iter_get_test_node(&iter);
623 call_rcu(&node->head, free_node_cb);
624 nr_del++;
625 } else
626 nr_delnoent++;
627 }
628 #if 0
629 //if (nr_writes % 100000 == 0) {
630 if (nr_writes % 1000 == 0) {
631 rcu_read_lock();
632 if (rand_r(&rand_lookup) & 1) {
633 ht_resize(test_ht, 1);
634 } else {
635 ht_resize(test_ht, -1);
636 }
637 rcu_read_unlock();
638 }
639 #endif //0
640 nr_writes++;
641 if (caa_unlikely(!test_duration_write()))
642 break;
643 if (caa_unlikely(wdelay))
644 loop_sleep(wdelay);
645 if (caa_unlikely((nr_writes & ((1 << 10) - 1)) == 0))
646 rcu_quiescent_state();
647 }
648
649 rcu_unregister_thread();
650
651 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
652 "writer", pthread_self(), (unsigned long)gettid());
653 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
654 "nr_delnoent %lu\n", pthread_self(), nr_add,
655 nr_addexist, nr_del, nr_delnoent);
656 count->update_ops = nr_writes;
657 count->add = nr_add;
658 count->add_exist = nr_addexist;
659 count->remove = nr_del;
660 return ((void*)2);
661 }
662
663 static int populate_hash(void)
664 {
665 struct lfht_test_node *node;
666 struct cds_lfht_node *ret_node;
667
668 if (!init_populate)
669 return 0;
670
671 if ((add_unique || add_replace) && init_populate * 10 > init_pool_size) {
672 printf("WARNING: required to populate %lu nodes (-k), but random "
673 "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
674 "larger random pool (-p option). This may take a while...\n", init_populate, init_pool_size);
675 }
676
677 while (nr_add < init_populate) {
678 node = malloc(sizeof(struct lfht_test_node));
679 lfht_test_node_init(node,
680 (void *)(((unsigned long) rand_r(&rand_lookup) % init_pool_size) + init_pool_offset),
681 sizeof(void *));
682 rcu_read_lock();
683 if (add_unique) {
684 ret_node = cds_lfht_add_unique(test_ht,
685 test_hash(node->key, node->key_len, TEST_HASH_SEED),
686 test_match, node->key, &node->node);
687 } else {
688 if (add_replace)
689 ret_node = cds_lfht_add_replace(test_ht,
690 test_hash(node->key, node->key_len, TEST_HASH_SEED),
691 test_match, node->key, &node->node);
692 else
693 cds_lfht_add(test_ht,
694 test_hash(node->key, node->key_len, TEST_HASH_SEED),
695 &node->node);
696 }
697 rcu_read_unlock();
698 if (add_unique && ret_node != &node->node) {
699 free(node);
700 nr_addexist++;
701 } else {
702 if (add_replace && ret_node) {
703 call_rcu(&to_test_node(ret_node)->head, free_node_cb);
704 nr_addexist++;
705 } else {
706 nr_add++;
707 }
708 }
709 nr_writes++;
710 }
711 return 0;
712 }
713
714 static
715 void test_delete_all_nodes(struct cds_lfht *ht)
716 {
717 struct cds_lfht_iter iter;
718 struct lfht_test_node *node;
719 unsigned long count = 0;
720
721 cds_lfht_for_each_entry(ht, &iter, node, node) {
722 int ret;
723
724 ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
725 assert(!ret);
726 call_rcu(&node->head, free_node_cb);
727 count++;
728 }
729 printf("deleted %lu nodes.\n", count);
730 }
731
732 void show_usage(int argc, char **argv)
733 {
734 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]);
735 #ifdef DEBUG_YIELD
736 printf(" [-r] [-w] (yield reader and/or writer)\n");
737 #endif
738 printf(" [-d delay] (writer period (us))\n");
739 printf(" [-c duration] (reader C.S. duration (in loops))\n");
740 printf(" [-v] (verbose output)\n");
741 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
742 printf(" [-h size] (initial number of buckets)\n");
743 printf(" [-m size] (minimum number of allocated buckets)\n");
744 printf(" [-n size] (maximum number of buckets)\n");
745 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
746 printf(" [-u] Uniquify add (no redundant keys).\n");
747 printf(" [-s] Replace (swap) entries.\n");
748 printf(" [-i] Add only (no removal).\n");
749 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
750 printf(" [-A] Automatically resize hash table.\n");
751 printf(" [-B order|chunk|mmap] Specify the memory backend.\n");
752 printf(" [-R offset] Lookup pool offset.\n");
753 printf(" [-S offset] Write pool offset.\n");
754 printf(" [-T offset] Init pool offset.\n");
755 printf(" [-M size] Lookup pool size.\n");
756 printf(" [-N size] Write pool size.\n");
757 printf(" [-O size] Init pool size.\n");
758 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
759 printf("\n\n");
760 }
761
762 int main(int argc, char **argv)
763 {
764 int err;
765 pthread_t *tid_reader, *tid_writer;
766 pthread_t tid_count;
767 void *tret;
768 unsigned long long *count_reader;
769 struct wr_count *count_writer;
770 unsigned long long tot_reads = 0, tot_writes = 0,
771 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
772 unsigned long count;
773 long approx_before, approx_after;
774 int i, a, ret;
775 struct sigaction act;
776 unsigned int remain;
777
778 if (argc < 4) {
779 show_usage(argc, argv);
780 return -1;
781 }
782
783 err = sscanf(argv[1], "%u", &nr_readers);
784 if (err != 1) {
785 show_usage(argc, argv);
786 return -1;
787 }
788
789 err = sscanf(argv[2], "%u", &nr_writers);
790 if (err != 1) {
791 show_usage(argc, argv);
792 return -1;
793 }
794
795 err = sscanf(argv[3], "%lu", &duration);
796 if (err != 1) {
797 show_usage(argc, argv);
798 return -1;
799 }
800
801 for (i = 4; i < argc; i++) {
802 if (argv[i][0] != '-')
803 continue;
804 switch (argv[i][1]) {
805 #ifdef DEBUG_YIELD
806 case 'r':
807 yield_active |= YIELD_READ;
808 break;
809 case 'w':
810 yield_active |= YIELD_WRITE;
811 break;
812 #endif
813 case 'a':
814 if (argc < i + 2) {
815 show_usage(argc, argv);
816 return -1;
817 }
818 a = atoi(argv[++i]);
819 cpu_affinities[next_aff++] = a;
820 use_affinity = 1;
821 printf_verbose("Adding CPU %d affinity\n", a);
822 break;
823 case 'c':
824 if (argc < i + 2) {
825 show_usage(argc, argv);
826 return -1;
827 }
828 rduration = atol(argv[++i]);
829 break;
830 case 'd':
831 if (argc < i + 2) {
832 show_usage(argc, argv);
833 return -1;
834 }
835 wdelay = atol(argv[++i]);
836 break;
837 case 'v':
838 verbose_mode = 1;
839 break;
840 case 'h':
841 if (argc < i + 2) {
842 show_usage(argc, argv);
843 return -1;
844 }
845 init_hash_size = atol(argv[++i]);
846 break;
847 case 'm':
848 if (argc < i + 2) {
849 show_usage(argc, argv);
850 return -1;
851 }
852 min_hash_alloc_size = atol(argv[++i]);
853 break;
854 case 'n':
855 if (argc < i + 2) {
856 show_usage(argc, argv);
857 return -1;
858 }
859 max_hash_buckets_size = atol(argv[++i]);
860 break;
861 case 'u':
862 if (add_replace) {
863 printf("Please specify at most one of -s or -u.\n");
864 exit(-1);
865 }
866 add_unique = 1;
867 break;
868 case 's':
869 if (add_unique) {
870 printf("Please specify at most one of -s or -u.\n");
871 exit(-1);
872 }
873 add_replace = 1;
874 break;
875 case 'i':
876 add_only = 1;
877 break;
878 case 'k':
879 init_populate = atol(argv[++i]);
880 break;
881 case 'A':
882 opt_auto_resize = 1;
883 break;
884 case 'B':
885 if (argc < i + 2) {
886 show_usage(argc, argv);
887 return -1;
888 }
889 i++;
890 if (!strcmp("order", argv[i]))
891 memory_backend = &cds_lfht_mm_order;
892 else if (!strcmp("chunk", argv[i]))
893 memory_backend = &cds_lfht_mm_chunk;
894 else if (!strcmp("mmap", argv[i]))
895 memory_backend = &cds_lfht_mm_mmap;
896 else {
897 printf("Please specify memory backend with order|chunk|mmap.\n");
898 exit(-1);
899 }
900 break;
901 case 'R':
902 lookup_pool_offset = atol(argv[++i]);
903 break;
904 case 'S':
905 write_pool_offset = atol(argv[++i]);
906 break;
907 case 'T':
908 init_pool_offset = atol(argv[++i]);
909 break;
910 case 'M':
911 lookup_pool_size = atol(argv[++i]);
912 break;
913 case 'N':
914 write_pool_size = atol(argv[++i]);
915 break;
916 case 'O':
917 init_pool_size = atol(argv[++i]);
918 break;
919 case 'V':
920 validate_lookup = 1;
921 break;
922
923 }
924 }
925
926 /* Check if hash size is power of 2 */
927 if (init_hash_size && init_hash_size & (init_hash_size - 1)) {
928 printf("Error: Initial number of buckets (%lu) is not a power of 2.\n",
929 init_hash_size);
930 return -1;
931 }
932
933 if (min_hash_alloc_size && min_hash_alloc_size & (min_hash_alloc_size - 1)) {
934 printf("Error: Minimum number of allocated buckets (%lu) is not a power of 2.\n",
935 min_hash_alloc_size);
936 return -1;
937 }
938
939 if (max_hash_buckets_size && max_hash_buckets_size & (max_hash_buckets_size - 1)) {
940 printf("Error: Maximum number of buckets (%lu) is not a power of 2.\n",
941 max_hash_buckets_size);
942 return -1;
943 }
944
945 memset(&act, 0, sizeof(act));
946 ret = sigemptyset(&act.sa_mask);
947 if (ret == -1) {
948 perror("sigemptyset");
949 return -1;
950 }
951 act.sa_handler = sigusr1_handler;
952 act.sa_flags = SA_RESTART;
953 ret = sigaction(SIGUSR1, &act, NULL);
954 if (ret == -1) {
955 perror("sigaction");
956 return -1;
957 }
958
959 ret = pipe(count_pipe);
960 if (ret == -1) {
961 perror("pipe");
962 return -1;
963 }
964
965 /* spawn counter thread */
966 err = pthread_create(&tid_count, NULL, thr_count,
967 NULL);
968 if (err != 0)
969 exit(1);
970
971 act.sa_handler = sigusr2_handler;
972 act.sa_flags = SA_RESTART;
973 ret = sigaction(SIGUSR2, &act, NULL);
974 if (ret == -1) {
975 perror("sigaction");
976 return -1;
977 }
978
979 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
980 duration, nr_readers, nr_writers);
981 printf_verbose("Writer delay : %lu loops.\n", wdelay);
982 printf_verbose("Reader duration : %lu loops.\n", rduration);
983 printf_verbose("Mode:%s%s.\n",
984 add_only ? " add only" : " add/remove",
985 add_unique ? " uniquify" : ( add_replace ? " replace" : " insert"));
986 printf_verbose("Initial number of buckets: %lu buckets.\n", init_hash_size);
987 printf_verbose("Minimum number of allocated buckets: %lu buckets.\n", min_hash_alloc_size);
988 printf_verbose("Maximum number of buckets: %lu buckets.\n", max_hash_buckets_size);
989 printf_verbose("Init pool size offset %lu size %lu.\n",
990 init_pool_offset, init_pool_size);
991 printf_verbose("Lookup pool size offset %lu size %lu.\n",
992 lookup_pool_offset, lookup_pool_size);
993 printf_verbose("Update pool size offset %lu size %lu.\n",
994 write_pool_offset, write_pool_size);
995 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
996 "main", pthread_self(), (unsigned long)gettid());
997
998 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
999 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
1000 count_reader = malloc(sizeof(*count_reader) * nr_readers);
1001 count_writer = malloc(sizeof(*count_writer) * nr_writers);
1002
1003 err = create_all_cpu_call_rcu_data(0);
1004 assert(!err);
1005
1006 if (memory_backend) {
1007 test_ht = _cds_lfht_new(init_hash_size, min_hash_alloc_size,
1008 max_hash_buckets_size,
1009 (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) |
1010 CDS_LFHT_ACCOUNTING, memory_backend,
1011 &rcu_flavor, NULL);
1012 } else {
1013 test_ht = cds_lfht_new(init_hash_size, min_hash_alloc_size,
1014 max_hash_buckets_size,
1015 (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) |
1016 CDS_LFHT_ACCOUNTING, NULL);
1017 }
1018
1019 /*
1020 * Hash Population needs to be seen as a RCU reader
1021 * thread from the point of view of resize.
1022 */
1023 rcu_register_thread();
1024 ret = populate_hash();
1025 assert(!ret);
1026
1027 rcu_thread_offline();
1028
1029 next_aff = 0;
1030
1031 for (i = 0; i < nr_readers; i++) {
1032 err = pthread_create(&tid_reader[i], NULL, thr_reader,
1033 &count_reader[i]);
1034 if (err != 0)
1035 exit(1);
1036 }
1037 for (i = 0; i < nr_writers; i++) {
1038 err = pthread_create(&tid_writer[i], NULL, thr_writer,
1039 &count_writer[i]);
1040 if (err != 0)
1041 exit(1);
1042 }
1043
1044 cmm_smp_mb();
1045
1046 test_go = 1;
1047
1048 remain = duration;
1049 do {
1050 remain = sleep(remain);
1051 } while (remain > 0);
1052
1053 test_stop = 1;
1054
1055 for (i = 0; i < nr_readers; i++) {
1056 err = pthread_join(tid_reader[i], &tret);
1057 if (err != 0)
1058 exit(1);
1059 tot_reads += count_reader[i];
1060 }
1061 for (i = 0; i < nr_writers; i++) {
1062 err = pthread_join(tid_writer[i], &tret);
1063 if (err != 0)
1064 exit(1);
1065 tot_writes += count_writer[i].update_ops;
1066 tot_add += count_writer[i].add;
1067 tot_add_exist += count_writer[i].add_exist;
1068 tot_remove += count_writer[i].remove;
1069 }
1070
1071 /* teardown counter thread */
1072 act.sa_handler = SIG_IGN;
1073 act.sa_flags = SA_RESTART;
1074 ret = sigaction(SIGUSR2, &act, NULL);
1075 if (ret == -1) {
1076 perror("sigaction");
1077 return -1;
1078 }
1079 {
1080 char msg[1] = { 0x42 };
1081 ssize_t ret;
1082
1083 do {
1084 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
1085 } while (ret == -1L && errno == EINTR);
1086 }
1087 err = pthread_join(tid_count, &tret);
1088 if (err != 0)
1089 exit(1);
1090
1091 fflush(stdout);
1092 rcu_thread_online();
1093 rcu_read_lock();
1094 printf("Counting nodes... ");
1095 cds_lfht_count_nodes(test_ht, &approx_before, &count, &approx_after);
1096 printf("done.\n");
1097 test_delete_all_nodes(test_ht);
1098 rcu_read_unlock();
1099 rcu_thread_offline();
1100 if (count) {
1101 printf("Approximation before node accounting: %ld nodes.\n",
1102 approx_before);
1103 printf("Nodes deleted from hash table before destroy: "
1104 "%lu nodes.\n",
1105 count);
1106 printf("Approximation after node accounting: %ld nodes.\n",
1107 approx_after);
1108 }
1109 ret = cds_lfht_destroy(test_ht, NULL);
1110 if (ret)
1111 printf_verbose("final delete aborted\n");
1112 else
1113 printf_verbose("final delete success\n");
1114 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
1115 tot_writes);
1116 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
1117 "nr_writers %3u "
1118 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
1119 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
1120 argv[0], duration, nr_readers, rduration,
1121 nr_writers, wdelay, tot_reads, tot_writes,
1122 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
1123 (long long) tot_add + init_populate - tot_remove - count);
1124 rcu_unregister_thread();
1125 free_all_cpu_call_rcu_data();
1126 free(tid_reader);
1127 free(tid_writer);
1128 free(count_reader);
1129 free(count_writer);
1130 return 0;
1131 }
This page took 0.065375 seconds and 5 git commands to generate.