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.
24 #include "test_urcu_hash.h"
32 void (*sigusr1
)(int signo
);
33 void (*sigusr2
)(int signo
);
34 void *(*thr_reader
)(void *_count
);
35 void *(*thr_writer
)(void *_count
);
36 int (*populate_hash
)(void);
40 struct test_hash_cb test_hash_cb
[] = {
42 test_hash_rw_sigusr1_handler
,
43 test_hash_rw_sigusr2_handler
,
44 test_hash_rw_thr_reader
,
45 test_hash_rw_thr_writer
,
46 test_hash_rw_populate_hash
,
48 [TEST_HASH_UNIQUE
] = {
49 test_hash_unique_sigusr1_handler
,
50 test_hash_unique_sigusr2_handler
,
51 test_hash_unique_thr_reader
,
52 test_hash_unique_thr_writer
,
53 test_hash_unique_populate_hash
,
58 static enum test_hash test_choice
= TEST_HASH_RW
;
60 void (*get_sigusr1_cb(void))(int)
62 return test_hash_cb
[test_choice
].sigusr1
;
65 void (*get_sigusr2_cb(void))(int)
67 return test_hash_cb
[test_choice
].sigusr2
;
70 void *(*get_thr_reader_cb(void))(void *)
72 return test_hash_cb
[test_choice
].thr_reader
;
75 void *(*get_thr_writer_cb(void))(void *)
77 return test_hash_cb
[test_choice
].thr_writer
;
80 int (*get_populate_hash_cb(void))(void)
82 return test_hash_cb
[test_choice
].populate_hash
;
85 DEFINE_URCU_TLS(unsigned int, rand_lookup
);
86 DEFINE_URCU_TLS(unsigned long, nr_add
);
87 DEFINE_URCU_TLS(unsigned long, nr_addexist
);
88 DEFINE_URCU_TLS(unsigned long, nr_del
);
89 DEFINE_URCU_TLS(unsigned long, nr_delnoent
);
90 DEFINE_URCU_TLS(unsigned long, lookup_fail
);
91 DEFINE_URCU_TLS(unsigned long, lookup_ok
);
93 struct cds_lfht
*test_ht
;
95 volatile int test_go
, test_stop
;
99 unsigned long duration
;
101 /* read-side C.S. duration, in loops */
102 unsigned long rduration
;
104 unsigned long init_hash_size
= DEFAULT_HASH_SIZE
;
105 unsigned long min_hash_alloc_size
= DEFAULT_MIN_ALLOC_SIZE
;
106 unsigned long max_hash_buckets_size
= (1UL << 20);
107 unsigned long init_populate
;
109 int add_only
, add_unique
, add_replace
;
110 const struct cds_lfht_mm_type
*memory_backend
;
112 unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
113 unsigned long init_pool_size
= DEFAULT_RAND_POOL
,
114 lookup_pool_size
= DEFAULT_RAND_POOL
,
115 write_pool_size
= DEFAULT_RAND_POOL
;
117 unsigned long nr_hash_chains
; /* 0: normal table, other: number of hash chains */
123 unsigned int cpu_affinities
[NR_CPUS
];
124 unsigned int next_aff
= 0;
125 int use_affinity
= 0;
127 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
129 DEFINE_URCU_TLS(unsigned long long, nr_writes
);
130 DEFINE_URCU_TLS(unsigned long long, nr_reads
);
132 unsigned int nr_readers
;
133 unsigned int nr_writers
;
135 static pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
137 void set_affinity(void)
139 #if HAVE_SCHED_SETAFFINITY
142 #endif /* HAVE_SCHED_SETAFFINITY */
147 #if HAVE_SCHED_SETAFFINITY
148 ret
= pthread_mutex_lock(&affinity_mutex
);
150 perror("Error in pthread mutex lock");
153 cpu
= cpu_affinities
[next_aff
++];
154 ret
= pthread_mutex_unlock(&affinity_mutex
);
156 perror("Error in pthread mutex unlock");
161 #if SCHED_SETAFFINITY_ARGS == 2
162 sched_setaffinity(0, &mask
);
164 sched_setaffinity(0, sizeof(mask
), &mask
);
166 #endif /* HAVE_SCHED_SETAFFINITY */
169 void rcu_copy_mutex_lock(void)
172 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
174 perror("Error in pthread mutex lock");
179 void rcu_copy_mutex_unlock(void)
183 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
185 perror("Error in pthread mutex unlock");
190 unsigned long test_compare(const void *key1
, size_t key1_len
,
191 const void *key2
, size_t key2_len
)
193 if (caa_unlikely(key1_len
!= key2_len
))
195 assert(key1_len
== sizeof(unsigned long));
202 void *thr_count(void *arg
)
204 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
205 "counter", (unsigned long) pthread_self(),
206 (unsigned long) gettid());
208 rcu_register_thread();
212 long approx_before
, approx_after
;
216 rcu_thread_offline();
217 len
= read(count_pipe
[0], buf
, 1);
219 if (caa_unlikely(!test_duration_read()))
224 printf("Counting nodes... ");
227 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
,
231 printf("Approximation before node accounting: %ld nodes.\n",
233 printf("Accounting of nodes in the hash table: "
236 printf("Approximation after node accounting: %ld nodes.\n",
239 rcu_unregister_thread();
243 void free_node_cb(struct rcu_head
*head
)
245 struct lfht_test_node
*node
=
246 caa_container_of(head
, struct lfht_test_node
, head
);
251 void test_delete_all_nodes(struct cds_lfht
*ht
)
253 struct cds_lfht_iter iter
;
254 struct lfht_test_node
*node
;
255 unsigned long count
= 0;
257 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
260 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
262 call_rcu(&node
->head
, free_node_cb
);
265 printf("deleted %lu nodes.\n", count
);
268 void show_usage(int argc
, char **argv
)
270 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
272 printf(" [-r] [-w] (yield reader and/or writer)\n");
274 printf(" [-d delay] (writer period (us))\n");
275 printf(" [-c duration] (reader C.S. duration (in loops))\n");
276 printf(" [-v] (verbose output)\n");
277 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
278 printf(" [-h size] (initial number of buckets)\n");
279 printf(" [-m size] (minimum number of allocated buckets)\n");
280 printf(" [-n size] (maximum number of buckets)\n");
281 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
282 printf(" [-u] Uniquify add (no redundant keys).\n");
283 printf(" [-s] Replace (swap) entries.\n");
284 printf(" [-i] Add only (no removal).\n");
285 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
286 printf(" [-A] Automatically resize hash table.\n");
287 printf(" [-B order|chunk|mmap] Specify the memory backend.\n");
288 printf(" [-R offset] Lookup pool offset.\n");
289 printf(" [-S offset] Write pool offset.\n");
290 printf(" [-T offset] Init pool offset.\n");
291 printf(" [-M size] Lookup pool size.\n");
292 printf(" [-N size] Write pool size.\n");
293 printf(" [-O size] Init pool size.\n");
294 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
295 printf(" [-U] Uniqueness test.\n");
296 printf(" [-C] Number of hash chains.\n");
300 int main(int argc
, char **argv
)
302 pthread_t
*tid_reader
, *tid_writer
;
305 unsigned long long *count_reader
;
306 struct wr_count
*count_writer
;
307 unsigned long long tot_reads
= 0, tot_writes
= 0,
308 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
310 long approx_before
, approx_after
;
311 int i
, a
, ret
, err
, mainret
= 0;
312 struct sigaction act
;
314 unsigned int nr_readers_created
= 0, nr_writers_created
= 0;
318 show_usage(argc
, argv
);
323 err
= sscanf(argv
[1], "%u", &nr_readers
);
325 show_usage(argc
, argv
);
330 err
= sscanf(argv
[2], "%u", &nr_writers
);
332 show_usage(argc
, argv
);
337 err
= sscanf(argv
[3], "%lu", &duration
);
339 show_usage(argc
, argv
);
344 for (i
= 4; i
< argc
; i
++) {
345 if (argv
[i
][0] != '-')
347 switch (argv
[i
][1]) {
350 yield_active
|= YIELD_READ
;
353 yield_active
|= YIELD_WRITE
;
358 show_usage(argc
, argv
);
363 cpu_affinities
[next_aff
++] = a
;
365 printf_verbose("Adding CPU %d affinity\n", a
);
369 show_usage(argc
, argv
);
373 rduration
= atol(argv
[++i
]);
377 show_usage(argc
, argv
);
381 wdelay
= atol(argv
[++i
]);
388 show_usage(argc
, argv
);
392 init_hash_size
= atol(argv
[++i
]);
396 show_usage(argc
, argv
);
400 min_hash_alloc_size
= atol(argv
[++i
]);
404 show_usage(argc
, argv
);
408 max_hash_buckets_size
= atol(argv
[++i
]);
412 printf("Please specify at most one of -s or -u.\n");
419 printf("Please specify at most one of -s or -u.\n");
428 init_populate
= atol(argv
[++i
]);
435 show_usage(argc
, argv
);
440 if (!strcmp("order", argv
[i
]))
441 memory_backend
= &cds_lfht_mm_order
;
442 else if (!strcmp("chunk", argv
[i
]))
443 memory_backend
= &cds_lfht_mm_chunk
;
444 else if (!strcmp("mmap", argv
[i
]))
445 memory_backend
= &cds_lfht_mm_mmap
;
447 printf("Please specify memory backend with order|chunk|mmap.\n");
453 lookup_pool_offset
= atol(argv
[++i
]);
456 write_pool_offset
= atol(argv
[++i
]);
459 init_pool_offset
= atol(argv
[++i
]);
462 lookup_pool_size
= atol(argv
[++i
]);
465 write_pool_size
= atol(argv
[++i
]);
468 init_pool_size
= atol(argv
[++i
]);
474 test_choice
= TEST_HASH_UNIQUE
;
477 nr_hash_chains
= atol(argv
[++i
]);
482 /* Check if hash size is power of 2 */
483 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
484 printf("Error: Initial number of buckets (%lu) is not a power of 2.\n",
490 if (min_hash_alloc_size
&& min_hash_alloc_size
& (min_hash_alloc_size
- 1)) {
491 printf("Error: Minimum number of allocated buckets (%lu) is not a power of 2.\n",
492 min_hash_alloc_size
);
497 if (max_hash_buckets_size
&& max_hash_buckets_size
& (max_hash_buckets_size
- 1)) {
498 printf("Error: Maximum number of buckets (%lu) is not a power of 2.\n",
499 max_hash_buckets_size
);
504 memset(&act
, 0, sizeof(act
));
505 ret
= sigemptyset(&act
.sa_mask
);
507 perror("sigemptyset");
511 act
.sa_handler
= get_sigusr1_cb();
512 act
.sa_flags
= SA_RESTART
;
513 ret
= sigaction(SIGUSR1
, &act
, NULL
);
520 act
.sa_handler
= get_sigusr2_cb();
521 act
.sa_flags
= SA_RESTART
;
522 ret
= sigaction(SIGUSR2
, &act
, NULL
);
529 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
530 duration
, nr_readers
, nr_writers
);
531 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
532 printf_verbose("Reader duration : %lu loops.\n", rduration
);
533 printf_verbose("Mode:%s%s.\n",
534 add_only
? " add only" : " add/remove",
535 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
536 printf_verbose("Initial number of buckets: %lu buckets.\n", init_hash_size
);
537 printf_verbose("Minimum number of allocated buckets: %lu buckets.\n", min_hash_alloc_size
);
538 printf_verbose("Maximum number of buckets: %lu buckets.\n", max_hash_buckets_size
);
539 printf_verbose("Init pool size offset %lu size %lu.\n",
540 init_pool_offset
, init_pool_size
);
541 printf_verbose("Lookup pool size offset %lu size %lu.\n",
542 lookup_pool_offset
, lookup_pool_size
);
543 printf_verbose("Update pool size offset %lu size %lu.\n",
544 write_pool_offset
, write_pool_size
);
545 printf_verbose("Number of hash chains: %lu.\n",
547 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
548 "main", (unsigned long) pthread_self(),
549 (unsigned long) gettid());
551 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
556 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
559 goto end_free_tid_reader
;
561 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
564 goto end_free_tid_writer
;
566 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
569 goto end_free_count_reader
;
572 err
= create_all_cpu_call_rcu_data(0);
574 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
577 if (memory_backend
) {
578 test_ht
= _cds_lfht_new(init_hash_size
, min_hash_alloc_size
,
579 max_hash_buckets_size
,
580 (opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0) |
581 CDS_LFHT_ACCOUNTING
, memory_backend
,
584 test_ht
= cds_lfht_new(init_hash_size
, min_hash_alloc_size
,
585 max_hash_buckets_size
,
586 (opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0) |
587 CDS_LFHT_ACCOUNTING
, NULL
);
590 printf("Error allocating hash table.\n");
592 goto end_free_call_rcu_data
;
596 * Hash Population needs to be seen as a RCU reader
597 * thread from the point of view of resize.
599 rcu_register_thread();
600 ret
= (get_populate_hash_cb())();
603 rcu_thread_offline();
607 ret
= pipe(count_pipe
);
614 /* spawn counter thread */
615 err
= pthread_create(&tid_count
, NULL
, thr_count
,
620 perror("pthread_create");
624 for (i
= 0; i
< nr_readers
; i
++) {
625 err
= pthread_create(&tid_reader
[i
],
626 NULL
, get_thr_reader_cb(),
631 perror("pthread_create");
632 goto end_pthread_join
;
634 nr_readers_created
++;
636 for (i
= 0; i
< nr_writers
; i
++) {
637 err
= pthread_create(&tid_writer
[i
],
638 NULL
, get_thr_writer_cb(),
643 perror("pthread_create");
644 goto end_pthread_join
;
646 nr_writers_created
++;
655 remain
= sleep(remain
);
656 } while (remain
> 0);
661 for (i
= 0; i
< nr_readers_created
; i
++) {
662 err
= pthread_join(tid_reader
[i
], &tret
);
666 perror("pthread_join");
668 tot_reads
+= count_reader
[i
];
670 for (i
= 0; i
< nr_writers_created
; i
++) {
671 err
= pthread_join(tid_writer
[i
], &tret
);
675 perror("pthread_join");
677 tot_writes
+= count_writer
[i
].update_ops
;
678 tot_add
+= count_writer
[i
].add
;
679 tot_add_exist
+= count_writer
[i
].add_exist
;
680 tot_remove
+= count_writer
[i
].remove
;
683 /* teardown counter thread */
684 act
.sa_handler
= SIG_IGN
;
685 act
.sa_flags
= SA_RESTART
;
686 ret
= sigaction(SIGUSR2
, &act
, NULL
);
692 char msg
[1] = { 0x42 };
696 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
697 } while (ret
== -1L && errno
== EINTR
);
699 err
= pthread_join(tid_count
, &tret
);
703 perror("pthread_join");
707 for (i
= 0; i
< 2; i
++) {
708 err
= close(count_pipe
[i
]);
711 perror("close pipe");
718 printf("Counting nodes... ");
719 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &approx_after
);
721 test_delete_all_nodes(test_ht
);
723 rcu_thread_offline();
725 printf("Approximation before node accounting: %ld nodes.\n",
727 printf("Nodes deleted from hash table before destroy: "
730 printf("Approximation after node accounting: %ld nodes.\n",
734 ret
= cds_lfht_destroy(test_ht
, NULL
);
736 printf_verbose("final delete aborted\n");
739 printf_verbose("final delete success\n");
741 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
743 nr_leaked
= (long long) tot_add
+ init_populate
- tot_remove
- count
;
744 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
746 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
747 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
748 argv
[0], duration
, nr_readers
, rduration
,
749 nr_writers
, wdelay
, tot_reads
, tot_writes
,
750 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
752 if (nr_leaked
!= 0) {
754 printf("WARNING: %lld nodes were leaked!\n", nr_leaked
);
757 rcu_unregister_thread();
758 end_free_call_rcu_data
:
759 free_all_cpu_call_rcu_data();
761 end_free_count_reader
: