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
;
87 unsigned long duration
;
89 /* read-side C.S. duration, in loops */
90 unsigned long rduration
;
92 unsigned long init_hash_size
= DEFAULT_HASH_SIZE
;
93 unsigned long min_hash_alloc_size
= DEFAULT_MIN_ALLOC_SIZE
;
94 unsigned long max_hash_buckets_size
= (1UL << 20);
95 unsigned long init_populate
;
97 int add_only
, add_unique
, add_replace
;
98 const struct cds_lfht_mm_type
*memory_backend
;
100 unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
101 unsigned long init_pool_size
= DEFAULT_RAND_POOL
,
102 lookup_pool_size
= DEFAULT_RAND_POOL
,
103 write_pool_size
= DEFAULT_RAND_POOL
;
105 unsigned long nr_hash_chains
; /* 0: normal table, other: number of hash chains */
111 unsigned int cpu_affinities
[NR_CPUS
];
112 unsigned int next_aff
= 0;
113 int use_affinity
= 0;
115 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
117 DEFINE_URCU_TLS(unsigned long long, nr_writes
);
118 DEFINE_URCU_TLS(unsigned long long, nr_reads
);
120 unsigned int nr_readers
;
121 unsigned int nr_writers
;
123 static pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
125 void set_affinity(void)
127 #ifdef HAVE_SCHED_SETAFFINITY
130 #endif /* HAVE_SCHED_SETAFFINITY */
135 #ifdef HAVE_SCHED_SETAFFINITY
136 ret
= pthread_mutex_lock(&affinity_mutex
);
138 perror("Error in pthread mutex lock");
141 cpu
= cpu_affinities
[next_aff
++];
142 ret
= pthread_mutex_unlock(&affinity_mutex
);
144 perror("Error in pthread mutex unlock");
149 sched_setaffinity(0, sizeof(mask
), &mask
);
150 #endif /* HAVE_SCHED_SETAFFINITY */
153 void rcu_copy_mutex_lock(void)
156 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
158 perror("Error in pthread mutex lock");
163 void rcu_copy_mutex_unlock(void)
167 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
169 perror("Error in pthread mutex unlock");
174 unsigned long test_compare(const void *key1
, size_t key1_len
,
175 const void *key2
, size_t key2_len
)
177 if (caa_unlikely(key1_len
!= key2_len
))
179 urcu_posix_assert(key1_len
== sizeof(unsigned long));
187 void *thr_count(void *arg
__attribute__((unused
)))
189 printf_verbose("thread_begin %s, tid %lu\n",
190 "counter", urcu_get_thread_id());
192 rcu_register_thread();
196 long approx_before
, approx_after
;
200 rcu_thread_offline();
201 len
= read(count_pipe
[0], buf
, 1);
203 if (caa_unlikely(!test_duration_read()))
208 printf("Counting nodes... ");
211 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
,
215 printf("Approximation before node accounting: %ld nodes.\n",
217 printf("Accounting of nodes in the hash table: "
220 printf("Approximation after node accounting: %ld nodes.\n",
223 rcu_unregister_thread();
227 void free_node_cb(struct rcu_head
*head
)
229 struct lfht_test_node
*node
=
230 caa_container_of(head
, struct lfht_test_node
, head
);
235 void test_delete_all_nodes(struct cds_lfht
*ht
)
237 struct cds_lfht_iter iter
;
238 struct lfht_test_node
*node
;
239 unsigned long count
= 0;
241 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
244 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
245 urcu_posix_assert(!ret
);
246 call_rcu(&node
->head
, free_node_cb
);
249 printf("deleted %lu nodes.\n", count
);
253 void show_usage(char **argv
)
255 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
257 printf("OPTIONS:\n");
258 printf(" [-r] [-w] (yield reader and/or writer)\n");
259 printf(" [-d delay] (writer period (us))\n");
260 printf(" [-c duration] (reader C.S. duration (in loops))\n");
261 printf(" [-v] (verbose output)\n");
262 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
263 printf(" [-h size] (initial number of buckets)\n");
264 printf(" [-m size] (minimum number of allocated buckets)\n");
265 printf(" [-n size] (maximum number of buckets)\n");
266 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
267 printf(" [-u] Uniquify add (no redundant keys).\n");
268 printf(" [-s] Replace (swap) entries.\n");
269 printf(" [-i] Add only (no removal).\n");
270 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
271 printf(" [-A] Automatically resize hash table.\n");
272 printf(" [-B order|chunk|mmap] Specify the memory backend.\n");
273 printf(" [-R offset] Lookup pool offset.\n");
274 printf(" [-S offset] Write pool offset.\n");
275 printf(" [-T offset] Init pool offset.\n");
276 printf(" [-M size] Lookup pool size.\n");
277 printf(" [-N size] Write pool size.\n");
278 printf(" [-O size] Init pool size.\n");
279 printf(" [-V] Validate lookups of init values.\n");
280 printf(" (use with filled init pool, same lookup range,\n");
281 printf(" with different write range)\n");
282 printf(" [-U] Uniqueness test.\n");
283 printf(" [-C] Number of hash chains.\n");
287 int main(int argc
, char **argv
)
289 pthread_t
*tid_reader
, *tid_writer
;
292 unsigned long long *count_reader
;
293 struct wr_count
*count_writer
;
294 unsigned long long tot_reads
= 0, tot_writes
= 0,
295 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
297 long approx_before
, approx_after
;
298 int i
, a
, ret
, err
, mainret
= 0;
300 struct sigaction act
;
302 unsigned int nr_readers_created
= 0, nr_writers_created
= 0;
311 err
= sscanf(argv
[1], "%u", &nr_readers
);
318 err
= sscanf(argv
[2], "%u", &nr_writers
);
325 err
= sscanf(argv
[3], "%lu", &duration
);
332 for (i
= 4; i
< argc
; i
++) {
333 if (argv
[i
][0] != '-')
335 switch (argv
[i
][1]) {
337 rcu_debug_yield_enable(RCU_YIELD_READ
);
340 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
349 cpu_affinities
[next_aff
++] = a
;
351 printf_verbose("Adding CPU %d affinity\n", a
);
359 rduration
= atol(argv
[++i
]);
367 wdelay
= atol(argv
[++i
]);
378 init_hash_size
= atol(argv
[++i
]);
386 min_hash_alloc_size
= atol(argv
[++i
]);
394 max_hash_buckets_size
= atol(argv
[++i
]);
398 printf("Please specify at most one of -s or -u.\n");
405 printf("Please specify at most one of -s or -u.\n");
414 init_populate
= atol(argv
[++i
]);
426 if (!strcmp("order", argv
[i
]))
427 memory_backend
= &cds_lfht_mm_order
;
428 else if (!strcmp("chunk", argv
[i
]))
429 memory_backend
= &cds_lfht_mm_chunk
;
430 else if (!strcmp("mmap", argv
[i
]))
431 memory_backend
= &cds_lfht_mm_mmap
;
433 printf("Please specify memory backend with order|chunk|mmap.\n");
439 lookup_pool_offset
= atol(argv
[++i
]);
442 write_pool_offset
= atol(argv
[++i
]);
445 init_pool_offset
= atol(argv
[++i
]);
448 lookup_pool_size
= atol(argv
[++i
]);
451 write_pool_size
= atol(argv
[++i
]);
454 init_pool_size
= atol(argv
[++i
]);
460 test_choice
= TEST_HASH_UNIQUE
;
463 nr_hash_chains
= atol(argv
[++i
]);
468 /* Check if hash size is power of 2 */
469 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
470 printf("Error: Initial number of buckets (%lu) is not a power of 2.\n",
476 if (min_hash_alloc_size
&& min_hash_alloc_size
& (min_hash_alloc_size
- 1)) {
477 printf("Error: Minimum number of allocated buckets (%lu) is not a power of 2.\n",
478 min_hash_alloc_size
);
483 if (max_hash_buckets_size
&& max_hash_buckets_size
& (max_hash_buckets_size
- 1)) {
484 printf("Error: Maximum number of buckets (%lu) is not a power of 2.\n",
485 max_hash_buckets_size
);
490 memset(&act
, 0, sizeof(act
));
491 ret
= sigemptyset(&act
.sa_mask
);
493 perror("sigemptyset");
497 act
.sa_handler
= get_sigusr1_cb();
498 act
.sa_flags
= SA_RESTART
;
499 ret
= sigaction(SIGUSR1
, &act
, NULL
);
506 act
.sa_handler
= get_sigusr2_cb();
507 act
.sa_flags
= SA_RESTART
;
508 ret
= sigaction(SIGUSR2
, &act
, NULL
);
515 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
516 duration
, nr_readers
, nr_writers
);
517 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
518 printf_verbose("Reader duration : %lu loops.\n", rduration
);
519 printf_verbose("Mode:%s%s.\n",
520 add_only
? " add only" : " add/remove",
521 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
522 printf_verbose("Initial number of buckets: %lu buckets.\n", init_hash_size
);
523 printf_verbose("Minimum number of allocated buckets: %lu buckets.\n", min_hash_alloc_size
);
524 printf_verbose("Maximum number of buckets: %lu buckets.\n", max_hash_buckets_size
);
525 printf_verbose("Init pool size offset %lu size %lu.\n",
526 init_pool_offset
, init_pool_size
);
527 printf_verbose("Lookup pool size offset %lu size %lu.\n",
528 lookup_pool_offset
, lookup_pool_size
);
529 printf_verbose("Update pool size offset %lu size %lu.\n",
530 write_pool_offset
, write_pool_size
);
531 printf_verbose("Number of hash chains: %lu.\n",
533 printf_verbose("thread %-6s, tid %lu\n",
534 "main", urcu_get_thread_id());
536 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
541 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
544 goto end_free_tid_reader
;
546 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
549 goto end_free_tid_writer
;
551 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
554 goto end_free_count_reader
;
557 err
= create_all_cpu_call_rcu_data(0);
559 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
562 if (memory_backend
) {
563 test_ht
= _cds_lfht_new(init_hash_size
, min_hash_alloc_size
,
564 max_hash_buckets_size
,
565 (opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0) |
566 CDS_LFHT_ACCOUNTING
, memory_backend
,
569 test_ht
= cds_lfht_new(init_hash_size
, min_hash_alloc_size
,
570 max_hash_buckets_size
,
571 (opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0) |
572 CDS_LFHT_ACCOUNTING
, NULL
);
575 printf("Error allocating hash table.\n");
577 goto end_free_call_rcu_data
;
581 * Hash Population needs to be seen as a RCU reader
582 * thread from the point of view of resize.
584 rcu_register_thread();
585 ret
= (get_populate_hash_cb())();
586 urcu_posix_assert(!ret
);
588 rcu_thread_offline();
592 ret
= pipe(count_pipe
);
599 /* spawn counter thread */
600 err
= pthread_create(&tid_count
, NULL
, thr_count
,
605 perror("pthread_create");
609 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
610 err
= pthread_create(&tid_reader
[i_thr
],
611 NULL
, get_thr_reader_cb(),
612 &count_reader
[i_thr
]);
616 perror("pthread_create");
617 goto end_pthread_join
;
619 nr_readers_created
++;
621 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
622 err
= pthread_create(&tid_writer
[i_thr
],
623 NULL
, get_thr_writer_cb(),
624 &count_writer
[i_thr
]);
628 perror("pthread_create");
629 goto end_pthread_join
;
631 nr_writers_created
++;
640 remain
= sleep(remain
);
641 } while (remain
> 0);
646 for (i_thr
= 0; i_thr
< nr_readers_created
; i_thr
++) {
647 err
= pthread_join(tid_reader
[i_thr
], &tret
);
651 perror("pthread_join");
653 tot_reads
+= count_reader
[i_thr
];
655 for (i_thr
= 0; i_thr
< nr_writers_created
; i_thr
++) {
656 err
= pthread_join(tid_writer
[i_thr
], &tret
);
660 perror("pthread_join");
662 tot_writes
+= count_writer
[i_thr
].update_ops
;
663 tot_add
+= count_writer
[i_thr
].add
;
664 tot_add_exist
+= count_writer
[i_thr
].add_exist
;
665 tot_remove
+= count_writer
[i_thr
].remove
;
668 /* teardown counter thread */
669 act
.sa_handler
= SIG_IGN
;
670 act
.sa_flags
= SA_RESTART
;
671 ret
= sigaction(SIGUSR2
, &act
, NULL
);
677 char msg
[1] = { 0x42 };
681 sret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
682 } while (sret
== -1L && errno
== EINTR
);
684 err
= pthread_join(tid_count
, &tret
);
688 perror("pthread_join");
692 for (i
= 0; i
< 2; i
++) {
693 err
= close(count_pipe
[i
]);
696 perror("close pipe");
703 printf("Counting nodes... ");
704 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &approx_after
);
706 test_delete_all_nodes(test_ht
);
708 rcu_thread_offline();
710 printf("Approximation before node accounting: %ld nodes.\n",
712 printf("Nodes deleted from hash table before destroy: "
715 printf("Approximation after node accounting: %ld nodes.\n",
719 ret
= cds_lfht_destroy(test_ht
, NULL
);
721 printf_verbose("final delete aborted\n");
724 printf_verbose("final delete success\n");
726 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
728 nr_leaked
= (long long) tot_add
+ init_populate
- tot_remove
- count
;
729 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
731 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
732 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
733 argv
[0], duration
, nr_readers
, rduration
,
734 nr_writers
, wdelay
, tot_reads
, tot_writes
,
735 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
737 if (nr_leaked
!= 0) {
739 printf("WARNING: %lld nodes were leaked!\n", nr_leaked
);
742 rcu_unregister_thread();
743 end_free_call_rcu_data
:
744 free_all_cpu_call_rcu_data();
746 end_free_count_reader
: