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