1 // SPDX-FileCopyrightText: 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Userspace RCU library - test program
9 #include "test_urcu_hash.h"
17 void (*sigusr1
)(int signo
);
18 void (*sigusr2
)(int signo
);
19 void *(*thr_reader
)(void *_count
);
20 void *(*thr_writer
)(void *_count
);
21 int (*populate_hash
)(void);
25 struct test_hash_cb test_hash_cb
[] = {
27 test_hash_rw_sigusr1_handler
,
28 test_hash_rw_sigusr2_handler
,
29 test_hash_rw_thr_reader
,
30 test_hash_rw_thr_writer
,
31 test_hash_rw_populate_hash
,
33 [TEST_HASH_UNIQUE
] = {
34 test_hash_unique_sigusr1_handler
,
35 test_hash_unique_sigusr2_handler
,
36 test_hash_unique_thr_reader
,
37 test_hash_unique_thr_writer
,
38 test_hash_unique_populate_hash
,
43 static enum test_hash test_choice
= TEST_HASH_RW
;
46 void (*get_sigusr1_cb(void))(int)
48 return test_hash_cb
[test_choice
].sigusr1
;
52 void (*get_sigusr2_cb(void))(int)
54 return test_hash_cb
[test_choice
].sigusr2
;
58 void *(*get_thr_reader_cb(void))(void *)
60 return test_hash_cb
[test_choice
].thr_reader
;
64 void *(*get_thr_writer_cb(void))(void *)
66 return test_hash_cb
[test_choice
].thr_writer
;
70 int (*get_populate_hash_cb(void))(void)
72 return test_hash_cb
[test_choice
].populate_hash
;
75 DEFINE_URCU_TLS(unsigned int, rand_lookup
);
76 DEFINE_URCU_TLS(unsigned long, nr_add
);
77 DEFINE_URCU_TLS(unsigned long, nr_addexist
);
78 DEFINE_URCU_TLS(unsigned long, nr_del
);
79 DEFINE_URCU_TLS(unsigned long, nr_delnoent
);
80 DEFINE_URCU_TLS(unsigned long, lookup_fail
);
81 DEFINE_URCU_TLS(unsigned long, lookup_ok
);
83 struct cds_lfht
*test_ht
;
85 volatile int test_go
, test_stop
;
89 unsigned long duration
;
91 /* read-side C.S. duration, in loops */
92 unsigned long rduration
;
94 unsigned long init_hash_size
= DEFAULT_HASH_SIZE
;
95 unsigned long min_hash_alloc_size
= DEFAULT_MIN_ALLOC_SIZE
;
96 unsigned long max_hash_buckets_size
= (1UL << 20);
97 unsigned long init_populate
;
99 int add_only
, add_unique
, add_replace
;
100 const struct cds_lfht_mm_type
*memory_backend
;
102 unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
103 unsigned long init_pool_size
= DEFAULT_RAND_POOL
,
104 lookup_pool_size
= DEFAULT_RAND_POOL
,
105 write_pool_size
= DEFAULT_RAND_POOL
;
107 unsigned long nr_hash_chains
; /* 0: normal table, other: number of hash chains */
113 unsigned int cpu_affinities
[NR_CPUS
];
114 unsigned int next_aff
= 0;
115 int use_affinity
= 0;
117 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
119 DEFINE_URCU_TLS(unsigned long long, nr_writes
);
120 DEFINE_URCU_TLS(unsigned long long, nr_reads
);
122 unsigned int nr_readers
;
123 unsigned int nr_writers
;
125 static pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
127 void set_affinity(void)
129 #ifdef HAVE_SCHED_SETAFFINITY
132 #endif /* HAVE_SCHED_SETAFFINITY */
137 #ifdef HAVE_SCHED_SETAFFINITY
138 ret
= pthread_mutex_lock(&affinity_mutex
);
140 perror("Error in pthread mutex lock");
143 cpu
= cpu_affinities
[next_aff
++];
144 ret
= pthread_mutex_unlock(&affinity_mutex
);
146 perror("Error in pthread mutex unlock");
151 sched_setaffinity(0, sizeof(mask
), &mask
);
152 #endif /* HAVE_SCHED_SETAFFINITY */
155 void rcu_copy_mutex_lock(void)
158 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
160 perror("Error in pthread mutex lock");
165 void rcu_copy_mutex_unlock(void)
169 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
171 perror("Error in pthread mutex unlock");
176 unsigned long test_compare(const void *key1
, size_t key1_len
,
177 const void *key2
, size_t key2_len
)
179 if (caa_unlikely(key1_len
!= key2_len
))
181 urcu_posix_assert(key1_len
== sizeof(unsigned long));
189 void *thr_count(void *arg
__attribute__((unused
)))
191 printf_verbose("thread_begin %s, tid %lu\n",
192 "counter", urcu_get_thread_id());
194 rcu_register_thread();
198 long approx_before
, approx_after
;
202 rcu_thread_offline();
203 len
= read(count_pipe
[0], buf
, 1);
205 if (caa_unlikely(!test_duration_read()))
210 printf("Counting nodes... ");
213 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
,
217 printf("Approximation before node accounting: %ld nodes.\n",
219 printf("Accounting of nodes in the hash table: "
222 printf("Approximation after node accounting: %ld nodes.\n",
225 rcu_unregister_thread();
229 void free_node_cb(struct rcu_head
*head
)
231 struct lfht_test_node
*node
=
232 caa_container_of(head
, struct lfht_test_node
, head
);
237 void test_delete_all_nodes(struct cds_lfht
*ht
)
239 struct cds_lfht_iter iter
;
240 struct lfht_test_node
*node
;
241 unsigned long count
= 0;
243 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
246 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
247 urcu_posix_assert(!ret
);
248 call_rcu(&node
->head
, free_node_cb
);
251 printf("deleted %lu nodes.\n", count
);
255 void show_usage(char **argv
)
257 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
259 printf("OPTIONS:\n");
260 printf(" [-r] [-w] (yield reader and/or writer)\n");
261 printf(" [-d delay] (writer period (us))\n");
262 printf(" [-c duration] (reader C.S. duration (in loops))\n");
263 printf(" [-v] (verbose output)\n");
264 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
265 printf(" [-h size] (initial number of buckets)\n");
266 printf(" [-m size] (minimum number of allocated buckets)\n");
267 printf(" [-n size] (maximum number of buckets)\n");
268 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
269 printf(" [-u] Uniquify add (no redundant keys).\n");
270 printf(" [-s] Replace (swap) entries.\n");
271 printf(" [-i] Add only (no removal).\n");
272 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
273 printf(" [-A] Automatically resize hash table.\n");
274 printf(" [-B order|chunk|mmap] Specify the memory backend.\n");
275 printf(" [-R offset] Lookup pool offset.\n");
276 printf(" [-S offset] Write pool offset.\n");
277 printf(" [-T offset] Init pool offset.\n");
278 printf(" [-M size] Lookup pool size.\n");
279 printf(" [-N size] Write pool size.\n");
280 printf(" [-O size] Init pool size.\n");
281 printf(" [-V] Validate lookups of init values.\n");
282 printf(" (use with filled init pool, same lookup range,\n");
283 printf(" with different write range)\n");
284 printf(" [-U] Uniqueness test.\n");
285 printf(" [-C] Number of hash chains.\n");
289 int main(int argc
, char **argv
)
291 pthread_t
*tid_reader
, *tid_writer
;
294 unsigned long long *count_reader
;
295 struct wr_count
*count_writer
;
296 unsigned long long tot_reads
= 0, tot_writes
= 0,
297 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
299 long approx_before
, approx_after
;
300 int i
, a
, ret
, err
, mainret
= 0;
302 struct sigaction act
;
304 unsigned int nr_readers_created
= 0, nr_writers_created
= 0;
313 err
= sscanf(argv
[1], "%u", &nr_readers
);
320 err
= sscanf(argv
[2], "%u", &nr_writers
);
327 err
= sscanf(argv
[3], "%lu", &duration
);
334 for (i
= 4; i
< argc
; i
++) {
335 if (argv
[i
][0] != '-')
337 switch (argv
[i
][1]) {
339 rcu_debug_yield_enable(RCU_YIELD_READ
);
342 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
351 cpu_affinities
[next_aff
++] = a
;
353 printf_verbose("Adding CPU %d affinity\n", a
);
361 rduration
= atol(argv
[++i
]);
369 wdelay
= atol(argv
[++i
]);
380 init_hash_size
= atol(argv
[++i
]);
388 min_hash_alloc_size
= atol(argv
[++i
]);
396 max_hash_buckets_size
= atol(argv
[++i
]);
400 printf("Please specify at most one of -s or -u.\n");
407 printf("Please specify at most one of -s or -u.\n");
416 init_populate
= atol(argv
[++i
]);
428 if (!strcmp("order", argv
[i
]))
429 memory_backend
= &cds_lfht_mm_order
;
430 else if (!strcmp("chunk", argv
[i
]))
431 memory_backend
= &cds_lfht_mm_chunk
;
432 else if (!strcmp("mmap", argv
[i
]))
433 memory_backend
= &cds_lfht_mm_mmap
;
435 printf("Please specify memory backend with order|chunk|mmap.\n");
441 lookup_pool_offset
= atol(argv
[++i
]);
444 write_pool_offset
= atol(argv
[++i
]);
447 init_pool_offset
= atol(argv
[++i
]);
450 lookup_pool_size
= atol(argv
[++i
]);
453 write_pool_size
= atol(argv
[++i
]);
456 init_pool_size
= atol(argv
[++i
]);
462 test_choice
= TEST_HASH_UNIQUE
;
465 nr_hash_chains
= atol(argv
[++i
]);
470 /* Check if hash size is power of 2 */
471 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
472 printf("Error: Initial number of buckets (%lu) is not a power of 2.\n",
478 if (min_hash_alloc_size
&& min_hash_alloc_size
& (min_hash_alloc_size
- 1)) {
479 printf("Error: Minimum number of allocated buckets (%lu) is not a power of 2.\n",
480 min_hash_alloc_size
);
485 if (max_hash_buckets_size
&& max_hash_buckets_size
& (max_hash_buckets_size
- 1)) {
486 printf("Error: Maximum number of buckets (%lu) is not a power of 2.\n",
487 max_hash_buckets_size
);
492 memset(&act
, 0, sizeof(act
));
493 ret
= sigemptyset(&act
.sa_mask
);
495 perror("sigemptyset");
499 act
.sa_handler
= get_sigusr1_cb();
500 act
.sa_flags
= SA_RESTART
;
501 ret
= sigaction(SIGUSR1
, &act
, NULL
);
508 act
.sa_handler
= get_sigusr2_cb();
509 act
.sa_flags
= SA_RESTART
;
510 ret
= sigaction(SIGUSR2
, &act
, NULL
);
517 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
518 duration
, nr_readers
, nr_writers
);
519 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
520 printf_verbose("Reader duration : %lu loops.\n", rduration
);
521 printf_verbose("Mode:%s%s.\n",
522 add_only
? " add only" : " add/remove",
523 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
524 printf_verbose("Initial number of buckets: %lu buckets.\n", init_hash_size
);
525 printf_verbose("Minimum number of allocated buckets: %lu buckets.\n", min_hash_alloc_size
);
526 printf_verbose("Maximum number of buckets: %lu buckets.\n", max_hash_buckets_size
);
527 printf_verbose("Init pool size offset %lu size %lu.\n",
528 init_pool_offset
, init_pool_size
);
529 printf_verbose("Lookup pool size offset %lu size %lu.\n",
530 lookup_pool_offset
, lookup_pool_size
);
531 printf_verbose("Update pool size offset %lu size %lu.\n",
532 write_pool_offset
, write_pool_size
);
533 printf_verbose("Number of hash chains: %lu.\n",
535 printf_verbose("thread %-6s, tid %lu\n",
536 "main", urcu_get_thread_id());
538 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
543 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
546 goto end_free_tid_reader
;
548 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
551 goto end_free_tid_writer
;
553 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
556 goto end_free_count_reader
;
559 err
= create_all_cpu_call_rcu_data(0);
561 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
564 if (memory_backend
) {
565 test_ht
= _cds_lfht_new(init_hash_size
, min_hash_alloc_size
,
566 max_hash_buckets_size
,
567 (opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0) |
568 CDS_LFHT_ACCOUNTING
, memory_backend
,
571 test_ht
= cds_lfht_new(init_hash_size
, min_hash_alloc_size
,
572 max_hash_buckets_size
,
573 (opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0) |
574 CDS_LFHT_ACCOUNTING
, NULL
);
577 printf("Error allocating hash table.\n");
579 goto end_free_call_rcu_data
;
583 * Hash Population needs to be seen as a RCU reader
584 * thread from the point of view of resize.
586 rcu_register_thread();
587 ret
= (get_populate_hash_cb())();
588 urcu_posix_assert(!ret
);
590 rcu_thread_offline();
594 ret
= pipe(count_pipe
);
601 /* spawn counter thread */
602 err
= pthread_create(&tid_count
, NULL
, thr_count
,
607 perror("pthread_create");
611 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
612 err
= pthread_create(&tid_reader
[i_thr
],
613 NULL
, get_thr_reader_cb(),
614 &count_reader
[i_thr
]);
618 perror("pthread_create");
619 goto end_pthread_join
;
621 nr_readers_created
++;
623 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
624 err
= pthread_create(&tid_writer
[i_thr
],
625 NULL
, get_thr_writer_cb(),
626 &count_writer
[i_thr
]);
630 perror("pthread_create");
631 goto end_pthread_join
;
633 nr_writers_created
++;
642 remain
= sleep(remain
);
643 } while (remain
> 0);
648 for (i_thr
= 0; i_thr
< nr_readers_created
; i_thr
++) {
649 err
= pthread_join(tid_reader
[i_thr
], &tret
);
653 perror("pthread_join");
655 tot_reads
+= count_reader
[i_thr
];
657 for (i_thr
= 0; i_thr
< nr_writers_created
; i_thr
++) {
658 err
= pthread_join(tid_writer
[i_thr
], &tret
);
662 perror("pthread_join");
664 tot_writes
+= count_writer
[i_thr
].update_ops
;
665 tot_add
+= count_writer
[i_thr
].add
;
666 tot_add_exist
+= count_writer
[i_thr
].add_exist
;
667 tot_remove
+= count_writer
[i_thr
].remove
;
670 /* teardown counter thread */
671 act
.sa_handler
= SIG_IGN
;
672 act
.sa_flags
= SA_RESTART
;
673 ret
= sigaction(SIGUSR2
, &act
, NULL
);
679 char msg
[1] = { 0x42 };
683 sret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
684 } while (sret
== -1L && errno
== EINTR
);
686 err
= pthread_join(tid_count
, &tret
);
690 perror("pthread_join");
694 for (i
= 0; i
< 2; i
++) {
695 err
= close(count_pipe
[i
]);
698 perror("close pipe");
705 printf("Counting nodes... ");
706 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &approx_after
);
708 test_delete_all_nodes(test_ht
);
710 rcu_thread_offline();
712 printf("Approximation before node accounting: %ld nodes.\n",
714 printf("Nodes deleted from hash table before destroy: "
717 printf("Approximation after node accounting: %ld nodes.\n",
721 ret
= cds_lfht_destroy(test_ht
, NULL
);
723 printf_verbose("final delete aborted\n");
726 printf_verbose("final delete success\n");
728 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
730 nr_leaked
= (long long) tot_add
+ init_populate
- tot_remove
- count
;
731 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
733 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
734 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
735 argv
[0], duration
, nr_readers
, rduration
,
736 nr_writers
, wdelay
, tot_reads
, tot_writes
,
737 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
739 if (nr_leaked
!= 0) {
741 printf("WARNING: %lld nodes were leaked!\n", nr_leaked
);
744 rcu_unregister_thread();
745 end_free_call_rcu_data
:
746 free_all_cpu_call_rcu_data();
748 end_free_count_reader
: