4 * Userspace RCU library - test program
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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.
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.
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.
24 #include "../config.h"
29 #include <sys/types.h>
42 #define DEFAULT_HASH_SIZE 32
43 #define DEFAULT_MIN_ALLOC_SIZE 1
44 #define DEFAULT_RAND_POOL 1000000
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.
52 #define TEST_HASH_SEED 0x42UL
54 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
55 #define CACHE_LINE_SIZE 4096
57 /* hardcoded number of CPUs */
61 #define poison_free(ptr) \
63 memset(ptr, 0x42, sizeof(*(ptr))); \
67 #define poison_free(ptr) free(ptr)
72 #if defined(_syscall0)
73 _syscall0(pid_t
, gettid
)
74 #elif defined(__NR_gettid)
75 static inline pid_t
gettid(void)
77 return syscall(__NR_gettid
);
80 #warning "use pid as tid"
81 static inline pid_t
gettid(void)
87 #ifndef DYNAMIC_LINK_TEST
90 #define debug_yield_read()
92 #include <urcu-qsbr.h>
93 #include <urcu/rculfhash.h>
94 #include <urcu-call-rcu.h>
97 unsigned long update_ops
;
99 unsigned long add_exist
;
100 unsigned long remove
;
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
;
111 static struct cds_lfht
*test_ht
;
118 struct lfht_test_node
{
119 struct cds_lfht_node node
;
121 unsigned int key_len
;
122 /* cache-cold for iteration */
123 struct rcu_head head
;
126 static inline struct lfht_test_node
*
127 to_test_node(struct cds_lfht_node
*node
)
129 return caa_container_of(node
, struct lfht_test_node
, node
);
133 void lfht_test_node_init(struct lfht_test_node
*node
, void *key
,
136 cds_lfht_node_init(&node
->node
);
138 node
->key_len
= key_len
;
141 static inline struct lfht_test_node
*
142 cds_lfht_iter_get_test_node(struct cds_lfht_iter
*iter
)
144 return to_test_node(cds_lfht_iter_get_node(iter
));
147 static volatile int test_go
, test_stop
;
149 static unsigned long wdelay
;
151 static unsigned long duration
;
153 /* read-side C.S. duration, in loops */
154 static unsigned long rduration
;
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
;
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
;
170 static int count_pipe
[2];
172 static inline void loop_sleep(unsigned long l
)
178 static int verbose_mode
;
180 #define printf_verbose(fmt, args...) \
183 printf(fmt, ## args); \
186 static unsigned int cpu_affinities
[NR_CPUS
];
187 static unsigned int next_aff
= 0;
188 static int use_affinity
= 0;
190 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
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)
198 static void set_affinity(void)
207 #if HAVE_SCHED_SETAFFINITY
208 ret
= pthread_mutex_lock(&affinity_mutex
);
210 perror("Error in pthread mutex lock");
213 cpu
= cpu_affinities
[next_aff
++];
214 ret
= pthread_mutex_unlock(&affinity_mutex
);
216 perror("Error in pthread mutex unlock");
221 #if SCHED_SETAFFINITY_ARGS == 2
222 sched_setaffinity(0, &mask
);
224 sched_setaffinity(0, sizeof(mask
), &mask
);
226 #endif /* HAVE_SCHED_SETAFFINITY */
233 } addremove
; /* 1: add, -1 remove, 0: random */
236 void sigusr1_handler(int signo
)
240 printf("Add/Remove: random.\n");
241 addremove
= AR_RANDOM
;
244 printf("Add/Remove: remove only.\n");
245 addremove
= AR_REMOVE
;
248 printf("Add/Remove: add only.\n");
255 void sigusr2_handler(int signo
)
257 char msg
[1] = { 0x42 };
261 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
262 } while (ret
== -1L && errno
== EINTR
);
266 * returns 0 if test should end.
268 static int test_duration_write(void)
273 static int test_duration_read(void)
278 static unsigned long long __thread nr_writes
;
279 static unsigned long long __thread nr_reads
;
281 static unsigned int nr_readers
;
282 static unsigned int nr_writers
;
284 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
286 void rcu_copy_mutex_lock(void)
289 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
291 perror("Error in pthread mutex lock");
296 void rcu_copy_mutex_unlock(void)
300 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
302 perror("Error in pthread mutex unlock");
309 * Source: http://burtleburtle.net/bob/c/lookup3.c
310 * Originally Public Domain
313 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
315 #define mix(a, b, c) \
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; \
325 #define final(a, b, c) \
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); \
336 static __attribute__((unused
))
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 */
344 /* Set up the internal state */
345 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
347 /*----------------------------------------- handle most of the key */
357 /*----------------------------------- handle the last 3 uint32_t's */
358 switch (length
) { /* all the case statements fall through */
363 case 0: /* case 0: nothing left to add */
366 /*---------------------------------------------- report the result */
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 */
379 /* Set up the internal state */
380 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
383 /*----------------------------------------- handle most of the key */
393 /*----------------------------------- handle the last 3 uint32_t's */
394 switch (length
) { /* all the case statements fall through */
399 case 0: /* case 0: nothing left to add */
402 /*---------------------------------------------- report the result */
407 #if (CAA_BITS_PER_LONG == 32)
409 unsigned long test_hash(const void *_key
, size_t length
, unsigned long seed
)
411 unsigned int key
= (unsigned int) _key
;
413 assert(length
== sizeof(unsigned int));
414 return hash_u32(&key
, 1, seed
);
418 unsigned long test_hash(const void *_key
, size_t length
, unsigned long seed
)
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]);
438 unsigned long test_compare(const void *key1
, size_t key1_len
,
439 const void *key2
, size_t key2_len
)
441 if (caa_unlikely(key1_len
!= key2_len
))
443 assert(key1_len
== sizeof(unsigned long));
451 int test_match(struct cds_lfht_node
*node
, const void *key
)
453 struct lfht_test_node
*test_node
= to_test_node(node
);
455 return !test_compare(test_node
->key
, test_node
->key_len
,
456 key
, sizeof(unsigned long));
460 void cds_lfht_test_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
,
461 struct cds_lfht_iter
*iter
)
463 assert(key_len
== sizeof(unsigned long));
465 cds_lfht_lookup(ht
, test_hash(key
, key_len
, TEST_HASH_SEED
),
466 test_match
, key
, iter
);
469 void *thr_count(void *arg
)
471 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
472 "counter", pthread_self(), (unsigned long)gettid());
474 rcu_register_thread();
478 long approx_before
, approx_after
;
482 rcu_thread_offline();
483 len
= read(count_pipe
[0], buf
, 1);
485 if (caa_unlikely(!test_duration_read()))
490 printf("Counting nodes... ");
493 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
,
497 printf("Approximation before node accounting: %ld nodes.\n",
499 printf("Accounting of nodes in the hash table: "
502 printf("Approximation after node accounting: %ld nodes.\n",
505 rcu_unregister_thread();
509 void *thr_reader(void *_count
)
511 unsigned long long *count
= _count
;
512 struct lfht_test_node
*node
;
513 struct cds_lfht_iter iter
;
515 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
516 "reader", pthread_self(), (unsigned long)gettid());
520 rcu_register_thread();
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
);
534 if (validate_lookup
) {
535 printf("[ERROR] Lookup cannot find initial node.\n");
543 if (caa_unlikely(rduration
))
544 loop_sleep(rduration
);
547 if (caa_unlikely(!test_duration_read()))
549 if (caa_unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
550 rcu_quiescent_state();
553 rcu_unregister_thread();
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
);
565 void free_node_cb(struct rcu_head
*head
)
567 struct lfht_test_node
*node
=
568 caa_container_of(head
, struct lfht_test_node
, head
);
572 void *thr_writer(void *_count
)
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
;
580 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
581 "writer", pthread_self(), (unsigned long)gettid());
585 rcu_register_thread();
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
),
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
);
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
);
610 cds_lfht_add(test_ht
,
611 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
615 if (add_unique
&& ret_node
!= &node
->node
) {
619 if (add_replace
&& ret_node
) {
620 call_rcu(&to_test_node(ret_node
)->head
,
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
));
636 node
= cds_lfht_iter_get_test_node(&iter
);
637 call_rcu(&node
->head
, free_node_cb
);
643 //if (nr_writes % 100000 == 0) {
644 if (nr_writes
% 1000 == 0) {
646 if (rand_r(&rand_lookup
) & 1) {
647 ht_resize(test_ht
, 1);
649 ht_resize(test_ht
, -1);
655 if (caa_unlikely(!test_duration_write()))
657 if (caa_unlikely(wdelay
))
659 if (caa_unlikely((nr_writes
& ((1 << 10) - 1)) == 0))
660 rcu_quiescent_state();
663 rcu_unregister_thread();
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
;
672 count
->add_exist
= nr_addexist
;
673 count
->remove
= nr_del
;
677 static int populate_hash(void)
679 struct lfht_test_node
*node
;
680 struct cds_lfht_node
*ret_node
;
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
);
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
),
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
);
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
);
707 cds_lfht_add(test_ht
,
708 test_hash(node
->key
, node
->key_len
, TEST_HASH_SEED
),
712 if (add_unique
&& ret_node
!= &node
->node
) {
716 if (add_replace
&& ret_node
) {
717 call_rcu(&to_test_node(ret_node
)->head
, free_node_cb
);
729 void test_delete_all_nodes(struct cds_lfht
*ht
)
731 struct cds_lfht_iter iter
;
732 struct lfht_test_node
*node
;
733 unsigned long count
= 0;
735 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
738 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
740 call_rcu(&node
->head
, free_node_cb
);
743 printf("deleted %lu nodes.\n", count
);
746 void show_usage(int argc
, char **argv
)
748 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
750 printf(" [-r] [-w] (yield reader and/or writer)\n");
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");
776 int main(int argc
, char **argv
)
779 pthread_t
*tid_reader
, *tid_writer
;
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;
787 long approx_before
, approx_after
;
789 struct sigaction act
;
793 show_usage(argc
, argv
);
797 err
= sscanf(argv
[1], "%u", &nr_readers
);
799 show_usage(argc
, argv
);
803 err
= sscanf(argv
[2], "%u", &nr_writers
);
805 show_usage(argc
, argv
);
809 err
= sscanf(argv
[3], "%lu", &duration
);
811 show_usage(argc
, argv
);
815 for (i
= 4; i
< argc
; i
++) {
816 if (argv
[i
][0] != '-')
818 switch (argv
[i
][1]) {
821 yield_active
|= YIELD_READ
;
824 yield_active
|= YIELD_WRITE
;
829 show_usage(argc
, argv
);
833 cpu_affinities
[next_aff
++] = a
;
835 printf_verbose("Adding CPU %d affinity\n", a
);
839 show_usage(argc
, argv
);
842 rduration
= atol(argv
[++i
]);
846 show_usage(argc
, argv
);
849 wdelay
= atol(argv
[++i
]);
856 show_usage(argc
, argv
);
859 init_hash_size
= atol(argv
[++i
]);
863 show_usage(argc
, argv
);
866 min_hash_alloc_size
= atol(argv
[++i
]);
870 show_usage(argc
, argv
);
873 max_hash_buckets_size
= atol(argv
[++i
]);
877 printf("Please specify at most one of -s or -u.\n");
884 printf("Please specify at most one of -s or -u.\n");
893 init_populate
= atol(argv
[++i
]);
900 show_usage(argc
, argv
);
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
;
911 printf("Please specify memory backend with order|chunk|mmap.\n");
916 lookup_pool_offset
= atol(argv
[++i
]);
919 write_pool_offset
= atol(argv
[++i
]);
922 init_pool_offset
= atol(argv
[++i
]);
925 lookup_pool_size
= atol(argv
[++i
]);
928 write_pool_size
= atol(argv
[++i
]);
931 init_pool_size
= atol(argv
[++i
]);
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",
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
);
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
);
959 memset(&act
, 0, sizeof(act
));
960 ret
= sigemptyset(&act
.sa_mask
);
962 perror("sigemptyset");
965 act
.sa_handler
= sigusr1_handler
;
966 act
.sa_flags
= SA_RESTART
;
967 ret
= sigaction(SIGUSR1
, &act
, NULL
);
973 ret
= pipe(count_pipe
);
979 /* spawn counter thread */
980 err
= pthread_create(&tid_count
, NULL
, thr_count
,
985 act
.sa_handler
= sigusr2_handler
;
986 act
.sa_flags
= SA_RESTART
;
987 ret
= sigaction(SIGUSR2
, &act
, NULL
);
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());
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
);
1017 err
= create_all_cpu_call_rcu_data(0);
1020 if (memory_backend
) {
1021 test_ht
= _cds_lfht_new(init_hash_size
, min_hash_alloc_size
,
1022 max_hash_buckets_size
,
1023 (opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0) |
1024 CDS_LFHT_ACCOUNTING
, memory_backend
,
1027 test_ht
= cds_lfht_new(init_hash_size
, min_hash_alloc_size
,
1028 max_hash_buckets_size
,
1029 (opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0) |
1030 CDS_LFHT_ACCOUNTING
, NULL
);
1034 * Hash Population needs to be seen as a RCU reader
1035 * thread from the point of view of resize.
1037 rcu_register_thread();
1038 ret
= populate_hash();
1041 rcu_thread_offline();
1045 for (i
= 0; i
< nr_readers
; i
++) {
1046 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
1051 for (i
= 0; i
< nr_writers
; i
++) {
1052 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
1064 remain
= sleep(remain
);
1065 } while (remain
> 0);
1069 for (i
= 0; i
< nr_readers
; i
++) {
1070 err
= pthread_join(tid_reader
[i
], &tret
);
1073 tot_reads
+= count_reader
[i
];
1075 for (i
= 0; i
< nr_writers
; i
++) {
1076 err
= pthread_join(tid_writer
[i
], &tret
);
1079 tot_writes
+= count_writer
[i
].update_ops
;
1080 tot_add
+= count_writer
[i
].add
;
1081 tot_add_exist
+= count_writer
[i
].add_exist
;
1082 tot_remove
+= count_writer
[i
].remove
;
1085 /* teardown counter thread */
1086 act
.sa_handler
= SIG_IGN
;
1087 act
.sa_flags
= SA_RESTART
;
1088 ret
= sigaction(SIGUSR2
, &act
, NULL
);
1090 perror("sigaction");
1094 char msg
[1] = { 0x42 };
1098 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
1099 } while (ret
== -1L && errno
== EINTR
);
1101 err
= pthread_join(tid_count
, &tret
);
1106 rcu_thread_online();
1108 printf("Counting nodes... ");
1109 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &approx_after
);
1111 test_delete_all_nodes(test_ht
);
1113 rcu_thread_offline();
1115 printf("Approximation before node accounting: %ld nodes.\n",
1117 printf("Nodes deleted from hash table before destroy: "
1120 printf("Approximation after node accounting: %ld nodes.\n",
1123 ret
= cds_lfht_destroy(test_ht
, NULL
);
1125 printf_verbose("final delete aborted\n");
1127 printf_verbose("final delete success\n");
1128 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
1130 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
1132 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
1133 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
1134 argv
[0], duration
, nr_readers
, rduration
,
1135 nr_writers
, wdelay
, tot_reads
, tot_writes
,
1136 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
1137 (long long) tot_add
+ init_populate
- tot_remove
- count
);
1138 rcu_unregister_thread();
1139 free_all_cpu_call_rcu_data();