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_ja.h"
28 DEFINE_URCU_TLS(unsigned int, rand_lookup
);
29 DEFINE_URCU_TLS(unsigned long, nr_add
);
30 DEFINE_URCU_TLS(unsigned long, nr_addexist
);
31 DEFINE_URCU_TLS(unsigned long, nr_del
);
32 DEFINE_URCU_TLS(unsigned long, nr_delnoent
);
33 DEFINE_URCU_TLS(unsigned long, lookup_fail
);
34 DEFINE_URCU_TLS(unsigned long, lookup_ok
);
36 struct cds_ja
*test_ja
;
38 volatile int test_go
, test_stop
;
42 unsigned long duration
;
44 /* read-side C.S. duration, in loops */
45 unsigned long rduration
;
47 unsigned long init_populate
;
50 unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
51 unsigned long init_pool_size
= DEFAULT_RAND_POOL
,
52 lookup_pool_size
= DEFAULT_RAND_POOL
,
53 write_pool_size
= DEFAULT_RAND_POOL
;
60 unsigned int cpu_affinities
[NR_CPUS
];
61 unsigned int next_aff
= 0;
64 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
66 DEFINE_URCU_TLS(unsigned long long, nr_writes
);
67 DEFINE_URCU_TLS(unsigned long long, nr_reads
);
69 unsigned int nr_readers
;
70 unsigned int nr_writers
;
72 static pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
74 void set_affinity(void)
83 #if HAVE_SCHED_SETAFFINITY
84 ret
= pthread_mutex_lock(&affinity_mutex
);
86 perror("Error in pthread mutex lock");
89 cpu
= cpu_affinities
[next_aff
++];
90 ret
= pthread_mutex_unlock(&affinity_mutex
);
92 perror("Error in pthread mutex unlock");
97 #if SCHED_SETAFFINITY_ARGS == 2
98 sched_setaffinity(0, &mask
);
100 sched_setaffinity(0, sizeof(mask
), &mask
);
102 #endif /* HAVE_SCHED_SETAFFINITY */
105 void rcu_copy_mutex_lock(void)
108 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
110 perror("Error in pthread mutex lock");
115 void rcu_copy_mutex_unlock(void)
119 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
121 perror("Error in pthread mutex unlock");
126 void free_node_cb(struct rcu_head
*head
)
128 struct ja_test_node
*node
=
129 caa_container_of(head
, struct ja_test_node
, node
.head
);
135 void test_delete_all_nodes(struct cds_lfht
*ht
)
137 struct cds_lfht_iter iter
;
138 struct lfht_test_node
*node
;
139 unsigned long count
= 0;
141 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
144 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
146 call_rcu(&node
->head
, free_node_cb
);
149 printf("deleted %lu nodes.\n", count
);
153 void show_usage(int argc
, char **argv
)
155 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
157 printf(" [-r] [-w] (yield reader and/or writer)\n");
159 printf(" [-d delay] (writer period (us))\n");
160 printf(" [-c duration] (reader C.S. duration (in loops))\n");
161 printf(" [-v] (verbose output)\n");
162 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
163 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
164 printf(" [-i] Add only (no removal).\n");
165 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
166 printf(" [-R offset] Lookup pool offset.\n");
167 printf(" [-S offset] Write pool offset.\n");
168 printf(" [-T offset] Init pool offset.\n");
169 printf(" [-M size] Lookup pool size.\n");
170 printf(" [-N size] Write pool size.\n");
171 printf(" [-O size] Init pool size.\n");
172 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
178 int test_8bit_key(void)
183 /* Test with 8-bit key */
184 test_ja
= cds_ja_new(8);
186 printf("Error allocating judy array.\n");
191 printf("Test #1: add keys (8-bit).\n");
192 for (key
= 0; key
< 200; key
++) {
193 struct ja_test_node
*node
=
194 calloc(sizeof(*node
), 1);
196 ja_test_node_init(node
, key
);
198 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
201 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
208 printf("Test #2: successful key lookup (8-bit).\n");
209 for (key
= 0; key
< 200; key
++) {
210 struct cds_hlist_head head
;
213 head
= cds_ja_lookup(test_ja
, key
);
214 if (cds_hlist_empty(&head
)) {
215 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
221 printf("Test #3: unsuccessful key lookup (8-bit).\n");
222 for (key
= 200; key
< 240; key
++) {
223 struct cds_hlist_head head
;
226 head
= cds_ja_lookup(test_ja
, key
);
227 if (!cds_hlist_empty(&head
)) {
229 "Error unexpected lookup node %" PRIu64
"\n",
236 printf("Test #4: remove keys (8-bit).\n");
237 for (key
= 0; key
< 200; key
++) {
238 struct cds_hlist_head head
;
239 struct ja_test_node
*node
;
242 head
= cds_ja_lookup(test_ja
, key
);
243 node
= cds_hlist_first_entry_rcu(&head
, struct ja_test_node
, node
.list
);
245 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
248 ret
= cds_ja_del(test_ja
, key
, &node
->node
);
250 fprintf(stderr
, "Error (%d) removing node %" PRIu64
"\n", ret
, key
);
253 call_rcu(&node
->node
.head
, free_node_cb
);
258 ret
= cds_ja_destroy(test_ja
, free_node_cb
);
260 fprintf(stderr
, "Error destroying judy array\n");
267 int test_16bit_key(void)
272 /* Test with 16-bit key */
273 test_ja
= cds_ja_new(16);
275 printf("Error allocating judy array.\n");
280 printf("Test #1: add keys (16-bit).\n");
281 //for (key = 0; key < 10000; key++) {
282 for (key
= 0; key
< 65536; key
+=256) {
283 struct ja_test_node
*node
=
284 calloc(sizeof(*node
), 1);
286 ja_test_node_init(node
, key
);
288 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
291 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
298 printf("Test #2: successful key lookup (16-bit).\n");
299 //for (key = 0; key < 10000; key++) {
300 for (key
= 0; key
< 65536; key
+=256) {
301 struct cds_hlist_head head
;
304 head
= cds_ja_lookup(test_ja
, key
);
305 if (cds_hlist_empty(&head
)) {
306 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
312 printf("Test #3: unsuccessful key lookup (16-bit).\n");
313 for (key
= 11000; key
<= 11002; key
++) {
314 struct cds_hlist_head head
;
317 head
= cds_ja_lookup(test_ja
, key
);
318 if (!cds_hlist_empty(&head
)) {
320 "Error unexpected lookup node %" PRIu64
"\n",
328 ret
= cds_ja_destroy(test_ja
, free_node_cb
);
330 fprintf(stderr
, "Error destroying judy array\n");
337 int test_sparse_key(unsigned int bits
)
340 uint64_t key
, max_key
;
344 max_key
= UINT64_MAX
;
346 max_key
= (1ULL << bits
) - 1;
348 printf("Sparse key test begins for %u-bit keys\n", bits
);
349 /* Test with 16-bit key */
350 test_ja
= cds_ja_new(bits
);
352 printf("Error allocating judy array.\n");
357 printf("Test #1: add keys (%u-bit).\n", bits
);
359 for (key
= 0; key
<= max_key
&& (key
!= 0 || zerocount
< 1); key
+= 1ULL << (bits
- 8)) {
360 struct ja_test_node
*node
=
361 calloc(sizeof(*node
), 1);
363 ja_test_node_init(node
, key
);
365 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
368 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
377 printf("Test #2: successful key lookup (%u-bit).\n", bits
);
379 for (key
= 0; key
<= max_key
&& (key
!= 0 || zerocount
< 1); key
+= 1ULL << (bits
- 8)) {
380 struct cds_hlist_head head
;
383 head
= cds_ja_lookup(test_ja
, key
);
384 if (cds_hlist_empty(&head
)) {
385 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
394 printf("Test #3: unsuccessful key lookup (%u-bit).\n", bits
);
396 for (key
= 0; key
<= max_key
&& (key
!= 0 || zerocount
< 1); key
+= 1ULL << (bits
- 8)) {
397 struct cds_hlist_head head
;
400 head
= cds_ja_lookup(test_ja
, key
+ 42);
401 if (!cds_hlist_empty(&head
)) {
403 "Error unexpected lookup node %" PRIu64
"\n",
414 ret
= cds_ja_destroy(test_ja
, free_node_cb
);
416 fprintf(stderr
, "Error destroying judy array\n");
419 printf("Test ends\n");
425 int main(int argc
, char **argv
)
428 pthread_t
*tid_reader
, *tid_writer
;
430 unsigned long long *count_reader
;
431 struct wr_count
*count_writer
;
432 unsigned long long tot_reads
= 0, tot_writes
= 0,
433 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
439 show_usage(argc
, argv
);
443 err
= sscanf(argv
[1], "%u", &nr_readers
);
445 show_usage(argc
, argv
);
449 err
= sscanf(argv
[2], "%u", &nr_writers
);
451 show_usage(argc
, argv
);
455 err
= sscanf(argv
[3], "%lu", &duration
);
457 show_usage(argc
, argv
);
461 for (i
= 4; i
< argc
; i
++) {
462 if (argv
[i
][0] != '-')
464 switch (argv
[i
][1]) {
467 yield_active
|= YIELD_READ
;
470 yield_active
|= YIELD_WRITE
;
475 show_usage(argc
, argv
);
479 cpu_affinities
[next_aff
++] = a
;
481 printf_verbose("Adding CPU %d affinity\n", a
);
485 show_usage(argc
, argv
);
488 rduration
= atol(argv
[++i
]);
492 show_usage(argc
, argv
);
495 wdelay
= atol(argv
[++i
]);
504 init_populate
= atol(argv
[++i
]);
507 lookup_pool_offset
= atol(argv
[++i
]);
510 write_pool_offset
= atol(argv
[++i
]);
513 init_pool_offset
= atol(argv
[++i
]);
516 lookup_pool_size
= atol(argv
[++i
]);
519 write_pool_size
= atol(argv
[++i
]);
522 init_pool_size
= atol(argv
[++i
]);
530 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
531 duration
, nr_readers
, nr_writers
);
532 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
533 printf_verbose("Reader duration : %lu loops.\n", rduration
);
534 printf_verbose("Mode:%s.\n",
535 add_only
? " add only" : " add/delete");
536 printf_verbose("Init pool size offset %lu size %lu.\n",
537 init_pool_offset
, init_pool_size
);
538 printf_verbose("Lookup pool size offset %lu size %lu.\n",
539 lookup_pool_offset
, lookup_pool_size
);
540 printf_verbose("Update pool size offset %lu size %lu.\n",
541 write_pool_offset
, write_pool_size
);
542 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
543 "main", pthread_self(), (unsigned long)gettid());
545 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
546 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
547 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
548 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
550 err
= create_all_cpu_call_rcu_data(0);
552 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
555 rcu_register_thread();
557 printf("Test start.\n");
559 for (i
= 0; i
< 3; i
++) {
560 ret
= test_8bit_key();
564 rcu_quiescent_state();
566 ret
= test_16bit_key();
570 rcu_quiescent_state();
572 ret
= test_sparse_key(8);
576 rcu_quiescent_state();
578 ret
= test_sparse_key(16);
582 rcu_quiescent_state();
584 ret
= test_sparse_key(32);
588 rcu_quiescent_state();
590 ret
= test_sparse_key(64);
594 rcu_quiescent_state();
596 printf("Test end.\n");
597 rcu_unregister_thread();
602 * Hash Population needs to be seen as a RCU reader
603 * thread from the point of view of resize.
605 rcu_register_thread();
606 ret
= (get_populate_hash_cb())();
609 rcu_thread_offline();
613 for (i
= 0; i
< nr_readers
; i
++) {
614 err
= pthread_create(&tid_reader
[i
],
615 NULL
, get_thr_reader_cb(),
620 for (i
= 0; i
< nr_writers
; i
++) {
621 err
= pthread_create(&tid_writer
[i
],
622 NULL
, get_thr_writer_cb(),
634 remain
= sleep(remain
);
635 } while (remain
> 0);
639 for (i
= 0; i
< nr_readers
; i
++) {
640 err
= pthread_join(tid_reader
[i
], &tret
);
643 tot_reads
+= count_reader
[i
];
645 for (i
= 0; i
< nr_writers
; i
++) {
646 err
= pthread_join(tid_writer
[i
], &tret
);
649 tot_writes
+= count_writer
[i
].update_ops
;
650 tot_add
+= count_writer
[i
].add
;
651 tot_add_exist
+= count_writer
[i
].add_exist
;
652 tot_remove
+= count_writer
[i
].remove
;
655 /* teardown counter thread */
656 act
.sa_handler
= SIG_IGN
;
657 act
.sa_flags
= SA_RESTART
;
658 ret
= sigaction(SIGUSR2
, &act
, NULL
);
664 char msg
[1] = { 0x42 };
668 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
669 } while (ret
== -1L && errno
== EINTR
);
675 printf("Counting nodes... ");
676 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &approx_after
);
678 test_delete_all_nodes(test_ht
);
680 rcu_thread_offline();
682 printf("Approximation before node accounting: %ld nodes.\n",
684 printf("Nodes deleted from hash table before destroy: "
687 printf("Approximation after node accounting: %ld nodes.\n",
690 ret
= cds_lfht_destroy(test_ht
, NULL
);
692 printf_verbose("final delete aborted\n");
694 printf_verbose("final delete success\n");
695 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
697 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
699 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
700 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
701 argv
[0], duration
, nr_readers
, rduration
,
702 nr_writers
, wdelay
, tot_reads
, tot_writes
,
703 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
704 (long long) tot_add
+ init_populate
- tot_remove
- count
);
705 rcu_unregister_thread();
706 free_all_cpu_call_rcu_data();