4 * Userspace RCU library - test program
6 * Copyright 2009-2012 - 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.
23 #include "test_urcu_hash.h"
31 void (*sigusr1
)(int signo
);
32 void (*sigusr2
)(int signo
);
33 void *(*thr_reader
)(void *_count
);
34 void *(*thr_writer
)(void *_count
);
35 int (*populate_hash
)(void);
39 struct test_hash_cb test_hash_cb
[] = {
41 test_hash_rw_sigusr1_handler
,
42 test_hash_rw_sigusr2_handler
,
43 test_hash_rw_thr_reader
,
44 test_hash_rw_thr_writer
,
45 test_hash_rw_populate_hash
,
47 [TEST_HASH_UNIQUE
] = {
48 test_hash_unique_sigusr1_handler
,
49 test_hash_unique_sigusr2_handler
,
50 test_hash_unique_thr_reader
,
51 test_hash_unique_thr_writer
,
52 test_hash_unique_populate_hash
,
57 static enum test_hash test_choice
= TEST_HASH_RW
;
60 void (*get_sigusr1_cb(void))(int)
62 return test_hash_cb
[test_choice
].sigusr1
;
66 void (*get_sigusr2_cb(void))(int)
68 return test_hash_cb
[test_choice
].sigusr2
;
72 void *(*get_thr_reader_cb(void))(void *)
74 return test_hash_cb
[test_choice
].thr_reader
;
78 void *(*get_thr_writer_cb(void))(void *)
80 return test_hash_cb
[test_choice
].thr_writer
;
84 int (*get_populate_hash_cb(void))(void)
86 return test_hash_cb
[test_choice
].populate_hash
;
89 DEFINE_URCU_TLS(unsigned int, rand_lookup
);
90 DEFINE_URCU_TLS(unsigned long, nr_add
);
91 DEFINE_URCU_TLS(unsigned long, nr_addexist
);
92 DEFINE_URCU_TLS(unsigned long, nr_del
);
93 DEFINE_URCU_TLS(unsigned long, nr_delnoent
);
94 DEFINE_URCU_TLS(unsigned long, lookup_fail
);
95 DEFINE_URCU_TLS(unsigned long, lookup_ok
);
97 struct cds_lfht
*test_ht
;
99 volatile int test_go
, test_stop
;
101 unsigned long wdelay
;
103 unsigned long duration
;
105 /* read-side C.S. duration, in loops */
106 unsigned long rduration
;
108 unsigned long init_hash_size
= DEFAULT_HASH_SIZE
;
109 unsigned long min_hash_alloc_size
= DEFAULT_MIN_ALLOC_SIZE
;
110 unsigned long max_hash_buckets_size
= (1UL << 20);
111 unsigned long init_populate
;
113 int add_only
, add_unique
, add_replace
;
114 const struct cds_lfht_mm_type
*memory_backend
;
116 unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
117 unsigned long init_pool_size
= DEFAULT_RAND_POOL
,
118 lookup_pool_size
= DEFAULT_RAND_POOL
,
119 write_pool_size
= DEFAULT_RAND_POOL
;
121 unsigned long nr_hash_chains
; /* 0: normal table, other: number of hash chains */
127 unsigned int cpu_affinities
[NR_CPUS
];
128 unsigned int next_aff
= 0;
129 int use_affinity
= 0;
131 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
133 DEFINE_URCU_TLS(unsigned long long, nr_writes
);
134 DEFINE_URCU_TLS(unsigned long long, nr_reads
);
136 unsigned int nr_readers
;
137 unsigned int nr_writers
;
139 static pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
141 void set_affinity(void)
143 #if HAVE_SCHED_SETAFFINITY
146 #endif /* HAVE_SCHED_SETAFFINITY */
151 #if HAVE_SCHED_SETAFFINITY
152 ret
= pthread_mutex_lock(&affinity_mutex
);
154 perror("Error in pthread mutex lock");
157 cpu
= cpu_affinities
[next_aff
++];
158 ret
= pthread_mutex_unlock(&affinity_mutex
);
160 perror("Error in pthread mutex unlock");
165 #if SCHED_SETAFFINITY_ARGS == 2
166 sched_setaffinity(0, &mask
);
168 sched_setaffinity(0, sizeof(mask
), &mask
);
170 #endif /* HAVE_SCHED_SETAFFINITY */
173 void rcu_copy_mutex_lock(void)
176 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
178 perror("Error in pthread mutex lock");
183 void rcu_copy_mutex_unlock(void)
187 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
189 perror("Error in pthread mutex unlock");
194 unsigned long test_compare(const void *key1
, size_t key1_len
,
195 const void *key2
, size_t key2_len
)
197 if (caa_unlikely(key1_len
!= key2_len
))
199 assert(key1_len
== sizeof(unsigned long));
207 void *thr_count(void *arg
__attribute__((unused
)))
209 printf_verbose("thread_begin %s, tid %lu\n",
210 "counter", urcu_get_thread_id());
212 rcu_register_thread();
216 long approx_before
, approx_after
;
220 rcu_thread_offline();
221 len
= read(count_pipe
[0], buf
, 1);
223 if (caa_unlikely(!test_duration_read()))
228 printf("Counting nodes... ");
231 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
,
235 printf("Approximation before node accounting: %ld nodes.\n",
237 printf("Accounting of nodes in the hash table: "
240 printf("Approximation after node accounting: %ld nodes.\n",
243 rcu_unregister_thread();
247 void free_node_cb(struct rcu_head
*head
)
249 struct lfht_test_node
*node
=
250 caa_container_of(head
, struct lfht_test_node
, head
);
255 void test_delete_all_nodes(struct cds_lfht
*ht
)
257 struct cds_lfht_iter iter
;
258 struct lfht_test_node
*node
;
259 unsigned long count
= 0;
261 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
264 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
266 call_rcu(&node
->head
, free_node_cb
);
269 printf("deleted %lu nodes.\n", count
);
273 void show_usage(char **argv
)
275 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
277 printf("OPTIONS:\n");
278 printf(" [-r] [-w] (yield reader and/or writer)\n");
279 printf(" [-d delay] (writer period (us))\n");
280 printf(" [-c duration] (reader C.S. duration (in loops))\n");
281 printf(" [-v] (verbose output)\n");
282 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
283 printf(" [-h size] (initial number of buckets)\n");
284 printf(" [-m size] (minimum number of allocated buckets)\n");
285 printf(" [-n size] (maximum number of buckets)\n");
286 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
287 printf(" [-u] Uniquify add (no redundant keys).\n");
288 printf(" [-s] Replace (swap) entries.\n");
289 printf(" [-i] Add only (no removal).\n");
290 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
291 printf(" [-A] Automatically resize hash table.\n");
292 printf(" [-B order|chunk|mmap] Specify the memory backend.\n");
293 printf(" [-R offset] Lookup pool offset.\n");
294 printf(" [-S offset] Write pool offset.\n");
295 printf(" [-T offset] Init pool offset.\n");
296 printf(" [-M size] Lookup pool size.\n");
297 printf(" [-N size] Write pool size.\n");
298 printf(" [-O size] Init pool size.\n");
299 printf(" [-V] Validate lookups of init values.\n");
300 printf(" (use with filled init pool, same lookup range,\n");
301 printf(" with different write range)\n");
302 printf(" [-U] Uniqueness test.\n");
303 printf(" [-C] Number of hash chains.\n");
307 int main(int argc
, char **argv
)
309 pthread_t
*tid_reader
, *tid_writer
;
312 unsigned long long *count_reader
;
313 struct wr_count
*count_writer
;
314 unsigned long long tot_reads
= 0, tot_writes
= 0,
315 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
317 long approx_before
, approx_after
;
318 int i
, a
, ret
, err
, mainret
= 0;
320 struct sigaction act
;
322 unsigned int nr_readers_created
= 0, nr_writers_created
= 0;
331 err
= sscanf(argv
[1], "%u", &nr_readers
);
338 err
= sscanf(argv
[2], "%u", &nr_writers
);
345 err
= sscanf(argv
[3], "%lu", &duration
);
352 for (i
= 4; i
< argc
; i
++) {
353 if (argv
[i
][0] != '-')
355 switch (argv
[i
][1]) {
357 rcu_debug_yield_enable(RCU_YIELD_READ
);
360 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
369 cpu_affinities
[next_aff
++] = a
;
371 printf_verbose("Adding CPU %d affinity\n", a
);
379 rduration
= atol(argv
[++i
]);
387 wdelay
= atol(argv
[++i
]);
398 init_hash_size
= atol(argv
[++i
]);
406 min_hash_alloc_size
= atol(argv
[++i
]);
414 max_hash_buckets_size
= atol(argv
[++i
]);
418 printf("Please specify at most one of -s or -u.\n");
425 printf("Please specify at most one of -s or -u.\n");
434 init_populate
= atol(argv
[++i
]);
446 if (!strcmp("order", argv
[i
]))
447 memory_backend
= &cds_lfht_mm_order
;
448 else if (!strcmp("chunk", argv
[i
]))
449 memory_backend
= &cds_lfht_mm_chunk
;
450 else if (!strcmp("mmap", argv
[i
]))
451 memory_backend
= &cds_lfht_mm_mmap
;
453 printf("Please specify memory backend with order|chunk|mmap.\n");
459 lookup_pool_offset
= atol(argv
[++i
]);
462 write_pool_offset
= atol(argv
[++i
]);
465 init_pool_offset
= atol(argv
[++i
]);
468 lookup_pool_size
= atol(argv
[++i
]);
471 write_pool_size
= atol(argv
[++i
]);
474 init_pool_size
= atol(argv
[++i
]);
480 test_choice
= TEST_HASH_UNIQUE
;
483 nr_hash_chains
= atol(argv
[++i
]);
488 /* Check if hash size is power of 2 */
489 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
490 printf("Error: Initial number of buckets (%lu) is not a power of 2.\n",
496 if (min_hash_alloc_size
&& min_hash_alloc_size
& (min_hash_alloc_size
- 1)) {
497 printf("Error: Minimum number of allocated buckets (%lu) is not a power of 2.\n",
498 min_hash_alloc_size
);
503 if (max_hash_buckets_size
&& max_hash_buckets_size
& (max_hash_buckets_size
- 1)) {
504 printf("Error: Maximum number of buckets (%lu) is not a power of 2.\n",
505 max_hash_buckets_size
);
510 memset(&act
, 0, sizeof(act
));
511 ret
= sigemptyset(&act
.sa_mask
);
513 perror("sigemptyset");
517 act
.sa_handler
= get_sigusr1_cb();
518 act
.sa_flags
= SA_RESTART
;
519 ret
= sigaction(SIGUSR1
, &act
, NULL
);
526 act
.sa_handler
= get_sigusr2_cb();
527 act
.sa_flags
= SA_RESTART
;
528 ret
= sigaction(SIGUSR2
, &act
, NULL
);
535 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
536 duration
, nr_readers
, nr_writers
);
537 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
538 printf_verbose("Reader duration : %lu loops.\n", rduration
);
539 printf_verbose("Mode:%s%s.\n",
540 add_only
? " add only" : " add/remove",
541 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
542 printf_verbose("Initial number of buckets: %lu buckets.\n", init_hash_size
);
543 printf_verbose("Minimum number of allocated buckets: %lu buckets.\n", min_hash_alloc_size
);
544 printf_verbose("Maximum number of buckets: %lu buckets.\n", max_hash_buckets_size
);
545 printf_verbose("Init pool size offset %lu size %lu.\n",
546 init_pool_offset
, init_pool_size
);
547 printf_verbose("Lookup pool size offset %lu size %lu.\n",
548 lookup_pool_offset
, lookup_pool_size
);
549 printf_verbose("Update pool size offset %lu size %lu.\n",
550 write_pool_offset
, write_pool_size
);
551 printf_verbose("Number of hash chains: %lu.\n",
553 printf_verbose("thread %-6s, tid %lu\n",
554 "main", urcu_get_thread_id());
556 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
561 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
564 goto end_free_tid_reader
;
566 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
569 goto end_free_tid_writer
;
571 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
574 goto end_free_count_reader
;
577 err
= create_all_cpu_call_rcu_data(0);
579 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
582 if (memory_backend
) {
583 test_ht
= _cds_lfht_new(init_hash_size
, min_hash_alloc_size
,
584 max_hash_buckets_size
,
585 (opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0) |
586 CDS_LFHT_ACCOUNTING
, memory_backend
,
589 test_ht
= cds_lfht_new(init_hash_size
, min_hash_alloc_size
,
590 max_hash_buckets_size
,
591 (opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0) |
592 CDS_LFHT_ACCOUNTING
, NULL
);
595 printf("Error allocating hash table.\n");
597 goto end_free_call_rcu_data
;
601 * Hash Population needs to be seen as a RCU reader
602 * thread from the point of view of resize.
604 rcu_register_thread();
605 ret
= (get_populate_hash_cb())();
608 rcu_thread_offline();
612 ret
= pipe(count_pipe
);
619 /* spawn counter thread */
620 err
= pthread_create(&tid_count
, NULL
, thr_count
,
625 perror("pthread_create");
629 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
630 err
= pthread_create(&tid_reader
[i_thr
],
631 NULL
, get_thr_reader_cb(),
632 &count_reader
[i_thr
]);
636 perror("pthread_create");
637 goto end_pthread_join
;
639 nr_readers_created
++;
641 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
642 err
= pthread_create(&tid_writer
[i_thr
],
643 NULL
, get_thr_writer_cb(),
644 &count_writer
[i_thr
]);
648 perror("pthread_create");
649 goto end_pthread_join
;
651 nr_writers_created
++;
660 remain
= sleep(remain
);
661 } while (remain
> 0);
666 for (i_thr
= 0; i_thr
< nr_readers_created
; i_thr
++) {
667 err
= pthread_join(tid_reader
[i_thr
], &tret
);
671 perror("pthread_join");
673 tot_reads
+= count_reader
[i_thr
];
675 for (i_thr
= 0; i_thr
< nr_writers_created
; i_thr
++) {
676 err
= pthread_join(tid_writer
[i_thr
], &tret
);
680 perror("pthread_join");
682 tot_writes
+= count_writer
[i_thr
].update_ops
;
683 tot_add
+= count_writer
[i_thr
].add
;
684 tot_add_exist
+= count_writer
[i_thr
].add_exist
;
685 tot_remove
+= count_writer
[i_thr
].remove
;
688 /* teardown counter thread */
689 act
.sa_handler
= SIG_IGN
;
690 act
.sa_flags
= SA_RESTART
;
691 ret
= sigaction(SIGUSR2
, &act
, NULL
);
697 char msg
[1] = { 0x42 };
701 sret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
702 } while (sret
== -1L && errno
== EINTR
);
704 err
= pthread_join(tid_count
, &tret
);
708 perror("pthread_join");
712 for (i
= 0; i
< 2; i
++) {
713 err
= close(count_pipe
[i
]);
716 perror("close pipe");
723 printf("Counting nodes... ");
724 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &approx_after
);
726 test_delete_all_nodes(test_ht
);
728 rcu_thread_offline();
730 printf("Approximation before node accounting: %ld nodes.\n",
732 printf("Nodes deleted from hash table before destroy: "
735 printf("Approximation after node accounting: %ld nodes.\n",
739 ret
= cds_lfht_destroy(test_ht
, NULL
);
741 printf_verbose("final delete aborted\n");
744 printf_verbose("final delete success\n");
746 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
748 nr_leaked
= (long long) tot_add
+ init_populate
- tot_remove
- count
;
749 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
751 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
752 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
753 argv
[0], duration
, nr_readers
, rduration
,
754 nr_writers
, wdelay
, tot_reads
, tot_writes
,
755 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
757 if (nr_leaked
!= 0) {
759 printf("WARNING: %lld nodes were leaked!\n", nr_leaked
);
762 rcu_unregister_thread();
763 end_free_call_rcu_data
:
764 free_all_cpu_call_rcu_data();
766 end_free_count_reader
: