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
;
56 unsigned int key_bits
= 32;
62 unsigned int cpu_affinities
[NR_CPUS
];
63 unsigned int next_aff
= 0;
66 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
68 DEFINE_URCU_TLS(unsigned long long, nr_writes
);
69 DEFINE_URCU_TLS(unsigned long long, nr_reads
);
71 unsigned int nr_readers
;
72 unsigned int nr_writers
;
74 static unsigned int add_ratio
= 50;
75 static uint64_t key_mul
= 1ULL;
77 static int add_unique
, add_replace
;
79 static pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
81 static int leak_detection
;
82 static unsigned long test_nodes_allocated
, test_nodes_freed
;
84 void set_affinity(void)
93 #if HAVE_SCHED_SETAFFINITY
94 ret
= pthread_mutex_lock(&affinity_mutex
);
96 perror("Error in pthread mutex lock");
99 cpu
= cpu_affinities
[next_aff
++];
100 ret
= pthread_mutex_unlock(&affinity_mutex
);
102 perror("Error in pthread mutex unlock");
107 #if SCHED_SETAFFINITY_ARGS == 2
108 sched_setaffinity(0, &mask
);
110 sched_setaffinity(0, sizeof(mask
), &mask
);
112 #endif /* HAVE_SCHED_SETAFFINITY */
115 void rcu_copy_mutex_lock(void)
118 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
120 perror("Error in pthread mutex lock");
125 void rcu_copy_mutex_unlock(void)
129 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
131 perror("Error in pthread mutex unlock");
137 struct ja_test_node
*node_alloc(void)
139 struct ja_test_node
*node
;
141 node
= calloc(sizeof(*node
), 1);
142 if (leak_detection
&& node
)
143 uatomic_inc(&test_nodes_allocated
);
148 void free_test_node(struct ja_test_node
*node
)
152 uatomic_inc(&test_nodes_freed
);
156 void free_test_node_cb(struct rcu_head
*head
)
158 struct ja_test_node
*node
=
159 caa_container_of(head
, struct ja_test_node
, head
);
160 free_test_node(node
);
164 void rcu_free_test_node(struct ja_test_node
*test_node
)
166 call_rcu(&test_node
->head
, free_test_node_cb
);
170 void free_node(struct cds_ja_node
*node
)
172 struct ja_test_node
*test_node
= to_test_node(node
);
174 free_test_node(test_node
);
179 void test_delete_all_nodes(struct cds_lfht
*ht
)
181 struct cds_lfht_iter iter
;
182 struct lfht_test_node
*node
;
183 unsigned long count
= 0;
185 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
188 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
190 call_rcu(&node
->head
, free_node_cb
);
193 printf("deleted %lu nodes.\n", count
);
197 void show_usage(int argc
, char **argv
)
199 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
201 printf(" [-r] [-w] (yield reader and/or writer)\n");
203 printf(" [-d delay] (writer period (us))\n");
204 printf(" [-c duration] (reader C.S. duration (in loops))\n");
205 printf(" [-v] (verbose output)\n");
206 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
207 printf(" [-u] Add unique keys.\n");
208 printf(" [-s] Replace existing keys.\n");
209 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
210 printf(" [-r ratio] Add ratio (in %% of add+removal).\n");
211 printf(" [-k] Populate init nodes.\n");
212 printf(" [-R offset] Lookup pool offset.\n");
213 printf(" [-S offset] Write pool offset.\n");
214 printf(" [-T offset] Init pool offset.\n");
215 printf(" [-M size] Lookup pool size.\n");
216 printf(" [-N size] Write pool size.\n");
217 printf(" [-O size] Init pool size.\n");
218 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
219 printf(" [-t] Do sanity test.\n");
220 printf(" [-B] Key bits for multithread test (default: 32).\n");
221 printf(" [-m factor] Key multiplication factor.\n");
222 printf(" [-l] Memory leak detection.\n");
227 int test_free_all_nodes(struct cds_ja
*ja
)
230 struct cds_ja_node
*ja_node
;
234 cds_ja_for_each_key_rcu(test_ja
, key
, ja_node
) {
235 struct cds_ja_node
*tmp_node
;
237 cds_ja_for_each_duplicate_safe_rcu(ja_node
, tmp_node
) {
238 ret
= cds_ja_del(test_ja
, key
, ja_node
);
240 fprintf(stderr
, "Error (%d) removing node %" PRIu64
"\n", ret
, key
);
243 /* Alone using Judy array, OK to free now */
253 int test_8bit_key(void)
257 uint64_t ka
[] = { 5, 17, 100, 222 };
258 uint64_t ka_test_offset
= 5;
259 struct cds_ja_node
*ja_node
;
261 /* Test with 8-bit key */
262 test_ja
= cds_ja_new(8);
264 printf("Error allocating judy array.\n");
269 printf("Test #1: add keys (8-bit).\n");
270 for (key
= 0; key
< 200; key
++) {
271 struct ja_test_node
*node
= node_alloc();
273 ja_test_node_init(node
, key
);
275 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
278 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
285 printf("Test #2: successful key lookup (8-bit).\n");
286 for (key
= 0; key
< 200; key
++) {
288 ja_node
= cds_ja_lookup(test_ja
, key
);
290 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
296 printf("Test #3: unsuccessful key lookup (8-bit).\n");
297 for (key
= 200; key
< 240; key
++) {
299 ja_node
= cds_ja_lookup(test_ja
, key
);
302 "Error unexpected lookup node %" PRIu64
"\n",
309 printf("Test #4: remove keys (8-bit).\n");
310 for (key
= 0; key
< 200; key
++) {
311 struct ja_test_node
*node
;
314 ja_node
= cds_ja_lookup(test_ja
, key
);
316 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
319 node
= caa_container_of(ja_node
, struct ja_test_node
, node
);
320 ret
= cds_ja_del(test_ja
, key
, &node
->node
);
322 fprintf(stderr
, "Error (%d) removing node %" PRIu64
"\n", ret
, key
);
325 rcu_free_test_node(node
);
326 ja_node
= cds_ja_lookup(test_ja
, key
);
328 fprintf(stderr
, "Error lookup %" PRIu64
": %p (after delete) failed. Node is not expected.\n", key
, ja_node
);
335 printf("Test #5: lookup below/above equal (8-bit).\n");
337 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
338 struct ja_test_node
*node
= node_alloc();
341 ja_test_node_init(node
, key
);
343 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
346 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
352 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
353 struct ja_test_node
*node
;
356 key
= ka
[i
] + ka_test_offset
;
358 ja_node
= cds_ja_lookup_below_equal(test_ja
, key
, &result_key
);
360 fprintf(stderr
, "Error lookup below equal. Cannot find expected key %" PRIu64
" below or equal to %" PRIu64
".\n",
364 node
= caa_container_of(ja_node
, struct ja_test_node
, node
);
365 if (node
->key
!= ka
[i
] || result_key
!= ka
[i
]) {
366 fprintf(stderr
, "Error lookup below equal. Expecting key %" PRIu64
" below or equal to %" PRIu64
", but found %" PRIu64
"/%" PRIu64
" instead.\n",
367 ka
[i
], key
, node
->key
, result_key
);
373 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
374 struct ja_test_node
*node
;
377 key
= ka
[i
] - ka_test_offset
;
379 ja_node
= cds_ja_lookup_above_equal(test_ja
, key
, &result_key
);
381 fprintf(stderr
, "Error lookup above equal. Cannot find expected key %" PRIu64
" below or equal to %" PRIu64
".\n",
385 node
= caa_container_of(ja_node
, struct ja_test_node
, node
);
386 if (node
->key
!= ka
[i
] || result_key
!= ka
[i
]) {
387 fprintf(stderr
, "Error lookup above equal. Expecting key %" PRIu64
" below or equal to %" PRIu64
", but found %" PRIu64
"/%" PRIu64
" instead.\n",
388 ka
[i
], key
, node
->key
, result_key
);
394 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
395 struct ja_test_node
*node
;
398 key
= ka
[i
]; /* without offset */
400 ja_node
= cds_ja_lookup_below_equal(test_ja
, key
, &result_key
);
402 fprintf(stderr
, "Error lookup below equal. Cannot find expected key %" PRIu64
" below or equal to %" PRIu64
".\n",
406 node
= caa_container_of(ja_node
, struct ja_test_node
, node
);
407 if (node
->key
!= ka
[i
] || result_key
!= ka
[i
]) {
408 fprintf(stderr
, "Error lookup below equal. Expecting key %" PRIu64
" below or equal to %" PRIu64
", but found %" PRIu64
"/%" PRIu64
" instead.\n",
409 ka
[i
], key
, node
->key
, result_key
);
413 ja_node
= cds_ja_lookup_above_equal(test_ja
, key
, &result_key
);
415 fprintf(stderr
, "Error lookup above equal. Cannot find expected key %" PRIu64
" below or equal to %" PRIu64
".\n",
419 node
= caa_container_of(ja_node
, struct ja_test_node
, node
);
420 if (node
->key
!= ka
[i
] || result_key
!= ka
[i
]) {
421 fprintf(stderr
, "Error lookup above equal. Expecting key %" PRIu64
" below or equal to %" PRIu64
", but found %" PRIu64
"/%" PRIu64
" instead.\n",
422 ka
[i
], key
, node
->key
, result_key
);
430 ret
= test_free_all_nodes(test_ja
);
432 fprintf(stderr
, "Error freeing all nodes\n");
436 ret
= cds_ja_destroy(test_ja
, free_node
);
438 fprintf(stderr
, "Error destroying judy array\n");
445 int test_16bit_key(void)
449 uint64_t ka
[] = { 105, 206, 4000, 4111, 59990, 65435 };
450 uint64_t ka_test_offset
= 100;
451 struct cds_ja_node
*ja_node
;
453 /* Test with 16-bit key */
454 test_ja
= cds_ja_new(16);
456 printf("Error allocating judy array.\n");
461 printf("Test #1: add keys (16-bit).\n");
462 for (key
= 0; key
< 10000; key
++) {
463 //for (key = 0; key < 65536; key+=256) {
464 struct ja_test_node
*node
= node_alloc();
466 ja_test_node_init(node
, key
);
468 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
471 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
478 printf("Test #2: successful key lookup (16-bit).\n");
479 for (key
= 0; key
< 10000; key
++) {
480 //for (key = 0; key < 65536; key+=256) {
481 struct cds_ja_node
*ja_node
;
484 ja_node
= cds_ja_lookup(test_ja
, key
);
486 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
492 printf("Test #3: unsuccessful key lookup (16-bit).\n");
493 for (key
= 11000; key
<= 11002; key
++) {
494 struct cds_ja_node
*ja_node
;
497 ja_node
= cds_ja_lookup(test_ja
, key
);
500 "Error unexpected lookup node %" PRIu64
"\n",
507 printf("Test #4: remove keys (16-bit).\n");
508 for (key
= 0; key
< 10000; key
++) {
509 //for (key = 0; key < 65536; key+=256) {
510 struct ja_test_node
*node
;
513 ja_node
= cds_ja_lookup(test_ja
, key
);
515 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
518 node
= caa_container_of(ja_node
, struct ja_test_node
, node
);
519 ret
= cds_ja_del(test_ja
, key
, &node
->node
);
521 fprintf(stderr
, "Error (%d) removing node %" PRIu64
"\n", ret
, key
);
524 rcu_free_test_node(node
);
525 ja_node
= cds_ja_lookup(test_ja
, key
);
527 fprintf(stderr
, "Error lookup %" PRIu64
": %p (after delete) failed. Node is not expected.\n", key
, ja_node
);
534 printf("Test #5: lookup below/above equal (16-bit).\n");
536 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
537 struct ja_test_node
*node
= node_alloc();
540 ja_test_node_init(node
, key
);
542 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
545 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
551 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
552 struct ja_test_node
*node
;
555 key
= ka
[i
] + ka_test_offset
;
557 ja_node
= cds_ja_lookup_below_equal(test_ja
, key
, &result_key
);
559 fprintf(stderr
, "Error lookup below equal. Cannot find expected key %" PRIu64
" below or equal to %" PRIu64
".\n",
563 node
= caa_container_of(ja_node
, struct ja_test_node
, node
);
564 if (node
->key
!= ka
[i
] || result_key
!= ka
[i
]) {
565 fprintf(stderr
, "Error lookup below equal. Expecting key %" PRIu64
" below or equal to %" PRIu64
", but found %" PRIu64
"/%" PRIu64
" instead.\n",
566 ka
[i
], key
, node
->key
, result_key
);
572 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
573 struct ja_test_node
*node
;
576 key
= ka
[i
] - ka_test_offset
;
578 ja_node
= cds_ja_lookup_above_equal(test_ja
, key
, &result_key
);
580 fprintf(stderr
, "Error lookup above equal. Cannot find expected key %" PRIu64
" above or equal to %" PRIu64
".\n",
584 node
= caa_container_of(ja_node
, struct ja_test_node
, node
);
585 if (node
->key
!= ka
[i
] || result_key
!= ka
[i
]) {
586 fprintf(stderr
, "Error lookup above equal. Expecting key %" PRIu64
" above or equal to %" PRIu64
", but found %" PRIu64
"/%" PRIu64
" instead.\n",
587 ka
[i
], key
, node
->key
, result_key
);
593 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
594 struct ja_test_node
*node
;
597 key
= ka
[i
]; /* without offset */
599 ja_node
= cds_ja_lookup_below_equal(test_ja
, key
, &result_key
);
601 fprintf(stderr
, "Error lookup below equal. Cannot find expected key %" PRIu64
" below or equal to %" PRIu64
".\n",
605 node
= caa_container_of(ja_node
, struct ja_test_node
, node
);
606 if (node
->key
!= ka
[i
] || result_key
!= ka
[i
]) {
607 fprintf(stderr
, "Error lookup below equal. Expecting key %" PRIu64
" below or equal to %" PRIu64
", but found %" PRIu64
"/%" PRIu64
" instead.\n",
608 ka
[i
], key
, node
->key
, result_key
);
612 ja_node
= cds_ja_lookup_above_equal(test_ja
, key
, &result_key
);
614 fprintf(stderr
, "Error lookup above equal. Cannot find expected key %" PRIu64
" above or equal to %" PRIu64
".\n",
618 node
= caa_container_of(ja_node
, struct ja_test_node
, node
);
619 if (node
->key
!= ka
[i
] || result_key
!= ka
[i
]) {
620 fprintf(stderr
, "Error lookup above equal. Expecting key %" PRIu64
" above or equal to %" PRIu64
", but found %" PRIu64
"/%" PRIu64
" instead.\n",
621 ka
[i
], key
, node
->key
, result_key
);
629 ret
= test_free_all_nodes(test_ja
);
631 fprintf(stderr
, "Error freeing all nodes\n");
635 ret
= cds_ja_destroy(test_ja
, free_node
);
637 fprintf(stderr
, "Error destroying judy array\n");
644 * nr_dup is number of nodes per key.
647 int test_sparse_key(unsigned int bits
, int nr_dup
)
649 uint64_t key
, max_key
;
650 int zerocount
, i
, ret
;
651 struct cds_ja_node
*ja_node
;
654 max_key
= UINT64_MAX
;
656 max_key
= (1ULL << bits
) - 1;
658 printf("Sparse key test begins for %u-bit keys\n", bits
);
659 /* Test with 16-bit key */
660 test_ja
= cds_ja_new(bits
);
662 printf("Error allocating judy array.\n");
667 printf("Test #1: add keys (%u-bit).\n", bits
);
668 for (i
= 0; i
< nr_dup
; i
++) {
670 for (key
= 0; key
<= max_key
&& (key
!= 0 || zerocount
< 1); key
+= 1ULL << (bits
- 8)) {
671 struct ja_test_node
*node
= node_alloc();
673 ja_test_node_init(node
, key
);
675 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
678 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
688 printf("Test #2: successful key lookup (%u-bit).\n", bits
);
690 for (key
= 0; key
<= max_key
&& (key
!= 0 || zerocount
< 1); key
+= 1ULL << (bits
- 8)) {
691 struct ja_test_node
*node
;
695 ja_node
= cds_ja_lookup(test_ja
, key
);
697 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
700 cds_ja_for_each_duplicate_rcu(ja_node
) {
703 if (count
!= nr_dup
) {
704 fprintf(stderr
, "Unexpected number of match for key %" PRIu64
", expected %d, got %d.\n", key
, nr_dup
, count
);
712 printf("Test #3: unsuccessful key lookup (%u-bit).\n", bits
);
714 for (key
= 0; key
<= max_key
&& (key
!= 0 || zerocount
< 1); key
+= 1ULL << (bits
- 8)) {
716 ja_node
= cds_ja_lookup(test_ja
, key
+ 42);
719 "Error unexpected lookup node %" PRIu64
"\n",
729 printf("Test #4: remove keys (%u-bit).\n", bits
);
731 for (key
= 0; key
<= max_key
&& (key
!= 0 || zerocount
< 1); key
+= 1ULL << (bits
- 8)) {
735 ja_node
= cds_ja_lookup(test_ja
, key
);
737 cds_ja_for_each_duplicate_rcu(ja_node
) {
738 struct cds_ja_node
*test_ja_node
;
739 struct ja_test_node
*node
;
742 node
= caa_container_of(ja_node
,
743 struct ja_test_node
, node
);
744 ret
= cds_ja_del(test_ja
, key
, &node
->node
);
746 fprintf(stderr
, "Error (%d) removing node %" PRIu64
"\n", ret
, key
);
749 rcu_free_test_node(node
);
750 test_ja_node
= cds_ja_lookup(test_ja
, key
);
751 if (count
< nr_dup
&& !test_ja_node
) {
752 fprintf(stderr
, "Error: no node found after deletion of some nodes of a key\n");
756 ja_node
= cds_ja_lookup(test_ja
, key
);
758 fprintf(stderr
, "Error lookup %" PRIu64
": %p (after delete) failed. Node is not expected.\n", key
, ja_node
);
767 ret
= test_free_all_nodes(test_ja
);
769 fprintf(stderr
, "Error freeing all nodes\n");
773 ret
= cds_ja_destroy(test_ja
, free_node
);
775 fprintf(stderr
, "Error destroying judy array\n");
778 printf("Test ends\n");
784 int do_sanity_test(void)
788 printf("Sanity test start.\n");
790 for (i
= 0; i
< 3; i
++) {
791 ret
= test_8bit_key();
795 rcu_quiescent_state();
797 ret
= test_16bit_key();
801 rcu_quiescent_state();
804 for (i
= 8; i
<= 64; i
*= 2) {
805 /* nr of nodes per key */
806 for (j
= 1; j
< 4; j
++) {
807 ret
= test_sparse_key(i
, j
);
811 rcu_quiescent_state();
814 printf("Sanity test end.\n");
819 enum urcu_ja_addremove
{
823 }; /* 1: add, -1 remove, 0: random */
825 static enum urcu_ja_addremove addremove
; /* 1: add, -1 remove, 0: random */
828 void test_ja_rw_sigusr1_handler(int signo
)
832 printf("Add/Remove: random.\n");
833 addremove
= AR_RANDOM
;
836 printf("Add/Remove: remove only.\n");
837 addremove
= AR_REMOVE
;
840 printf("Add/Remove: add only.\n");
847 void *test_ja_rw_thr_reader(void *_count
)
849 unsigned long long *count
= _count
;
850 struct cds_ja_node
*ja_node
;
853 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
854 "reader", pthread_self(), (unsigned long) gettid());
858 rcu_register_thread();
868 /* note: only looking up ulong keys */
869 key
= ((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % lookup_pool_size
) + lookup_pool_offset
;
871 ja_node
= cds_ja_lookup(test_ja
, key
);
873 if (validate_lookup
) {
874 printf("[ERROR] Lookup cannot find initial node.\n");
877 URCU_TLS(lookup_fail
)++;
879 URCU_TLS(lookup_ok
)++;
881 rcu_debug_yield_read();
882 if (caa_unlikely(rduration
))
883 loop_sleep(rduration
);
885 URCU_TLS(nr_reads
)++;
886 if (caa_unlikely(!test_duration_read()))
888 if (caa_unlikely((URCU_TLS(nr_reads
) & ((1 << 10) - 1)) == 0))
889 rcu_quiescent_state();
892 rcu_unregister_thread();
894 *count
= URCU_TLS(nr_reads
);
895 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
896 "reader", pthread_self(), (unsigned long) gettid());
897 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
898 pthread_self(), URCU_TLS(lookup_fail
),
899 URCU_TLS(lookup_ok
));
906 return ((unsigned int) rand_r(&URCU_TLS(rand_lookup
)) % 100) < add_ratio
;
910 void *test_ja_rw_thr_writer(void *_count
)
912 struct wr_count
*count
= _count
;
916 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
917 "writer", pthread_self(), (unsigned long) gettid());
921 rcu_register_thread();
929 if ((addremove
== AR_ADD
)
930 || (addremove
== AR_RANDOM
&& is_add())) {
931 struct ja_test_node
*node
= node_alloc();
932 struct cds_ja_node
*ret_node
;
934 /* note: only inserting ulong keys */
935 key
= ((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % write_pool_size
) + write_pool_offset
;
937 ja_test_node_init(node
, key
);
940 ret_node
= cds_ja_add_unique(test_ja
, key
, &node
->node
);
941 if (ret_node
!= &node
->node
) {
942 free_test_node(node
);
943 URCU_TLS(nr_addexist
)++;
947 } else if (add_replace
) {
948 assert(0); /* not implemented yet. */
950 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
952 fprintf(stderr
, "Error in cds_ja_add: %d\n", ret
);
953 free_test_node(node
);
960 struct cds_ja_node
*ja_node
;
961 struct ja_test_node
*node
;
964 /* note: only deleting ulong keys */
965 key
= ((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % write_pool_size
) + write_pool_offset
;
970 ja_node
= cds_ja_lookup(test_ja
, key
);
971 /* Remove first entry */
973 node
= caa_container_of(ja_node
,
974 struct ja_test_node
, node
);
975 ret
= cds_ja_del(test_ja
, key
, &node
->node
);
977 rcu_free_test_node(node
);
980 URCU_TLS(nr_delnoent
)++;
983 URCU_TLS(nr_delnoent
)++;
988 URCU_TLS(nr_writes
)++;
989 if (caa_unlikely(!test_duration_write()))
991 if (caa_unlikely(wdelay
))
993 if (caa_unlikely((URCU_TLS(nr_writes
) & ((1 << 10) - 1)) == 0))
994 rcu_quiescent_state();
997 rcu_unregister_thread();
999 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
1000 "writer", pthread_self(), (unsigned long) gettid());
1001 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
1002 "nr_delnoent %lu\n", pthread_self(), URCU_TLS(nr_add
),
1003 URCU_TLS(nr_addexist
), URCU_TLS(nr_del
),
1004 URCU_TLS(nr_delnoent
));
1005 count
->update_ops
= URCU_TLS(nr_writes
);
1006 count
->add
= URCU_TLS(nr_add
);
1007 count
->add_exist
= URCU_TLS(nr_addexist
);
1008 count
->remove
= URCU_TLS(nr_del
);
1013 int do_mt_populate_ja(void)
1021 printf("Starting rw test\n");
1023 for (iter
= init_pool_offset
; iter
< init_pool_offset
+ init_pool_size
; iter
++) {
1024 struct ja_test_node
*node
= node_alloc();
1027 /* note: only inserting ulong keys */
1028 key
= (unsigned long) iter
;
1030 ja_test_node_init(node
, key
);
1032 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
1034 URCU_TLS(nr_writes
)++;
1037 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
1046 int do_mt_test(void)
1048 pthread_t
*tid_reader
, *tid_writer
;
1051 unsigned long long *count_reader
;
1052 struct wr_count
*count_writer
;
1053 unsigned long long tot_reads
= 0, tot_writes
= 0,
1054 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
1055 unsigned int remain
;
1057 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
1058 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
1059 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
1060 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
1062 printf("Allocating Judy Array for %u-bit keys\n", key_bits
);
1063 test_ja
= cds_ja_new(key_bits
);
1065 printf("Error allocating judy array.\n");
1070 do_mt_populate_ja();
1074 for (i
= 0; i
< nr_readers
; i
++) {
1075 err
= pthread_create(&tid_reader
[i
],
1076 NULL
, test_ja_rw_thr_reader
,
1081 for (i
= 0; i
< nr_writers
; i
++) {
1082 err
= pthread_create(&tid_writer
[i
],
1083 NULL
, test_ja_rw_thr_writer
,
1093 rcu_thread_offline_qsbr();
1097 remain
= sleep(remain
);
1098 } while (remain
> 0);
1102 for (i
= 0; i
< nr_readers
; i
++) {
1103 err
= pthread_join(tid_reader
[i
], &tret
);
1106 tot_reads
+= count_reader
[i
];
1108 for (i
= 0; i
< nr_writers
; i
++) {
1109 err
= pthread_join(tid_writer
[i
], &tret
);
1112 tot_writes
+= count_writer
[i
].update_ops
;
1113 tot_add
+= count_writer
[i
].add
;
1114 tot_add_exist
+= count_writer
[i
].add_exist
;
1115 tot_remove
+= count_writer
[i
].remove
;
1117 rcu_thread_online_qsbr();
1119 ret
= test_free_all_nodes(test_ja
);
1121 fprintf(stderr
, "Error freeing all nodes\n");
1125 ret
= cds_ja_destroy(test_ja
, free_node
);
1127 fprintf(stderr
, "Error destroying judy array\n");
1141 int check_memory_leaks(void)
1143 unsigned long na
, nf
;
1145 na
= uatomic_read(&test_nodes_allocated
);
1146 nf
= uatomic_read(&test_nodes_freed
);
1148 fprintf(stderr
, "Memory leak of %ld test nodes detected. Allocated: %lu, freed: %lu\n",
1155 int main(int argc
, char **argv
)
1157 int i
, j
, a
, ret
, err
;
1159 struct sigaction act
;
1162 show_usage(argc
, argv
);
1166 err
= sscanf(argv
[1], "%u", &nr_readers
);
1168 show_usage(argc
, argv
);
1172 err
= sscanf(argv
[2], "%u", &nr_writers
);
1174 show_usage(argc
, argv
);
1178 err
= sscanf(argv
[3], "%lu", &duration
);
1180 show_usage(argc
, argv
);
1184 for (i
= 4; i
< argc
; i
++) {
1185 if (argv
[i
][0] != '-')
1187 switch (argv
[i
][1]) {
1190 yield_active
|= YIELD_READ
;
1193 yield_active
|= YIELD_WRITE
;
1198 show_usage(argc
, argv
);
1201 a
= atoi(argv
[++i
]);
1202 cpu_affinities
[next_aff
++] = a
;
1204 printf_verbose("Adding CPU %d affinity\n", a
);
1208 show_usage(argc
, argv
);
1211 rduration
= atol(argv
[++i
]);
1215 show_usage(argc
, argv
);
1218 wdelay
= atol(argv
[++i
]);
1224 add_ratio
= atoi(argv
[++i
]);
1230 lookup_pool_offset
= atol(argv
[++i
]);
1233 write_pool_offset
= atol(argv
[++i
]);
1236 init_pool_offset
= atol(argv
[++i
]);
1239 lookup_pool_size
= atol(argv
[++i
]);
1242 write_pool_size
= atol(argv
[++i
]);
1245 init_pool_size
= atol(argv
[++i
]);
1248 validate_lookup
= 1;
1254 key_bits
= atol(argv
[++i
]);
1257 key_mul
= atoll(argv
[++i
]);
1271 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
1272 duration
, nr_readers
, nr_writers
);
1273 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
1274 printf_verbose("Reader duration : %lu loops.\n", rduration
);
1275 printf_verbose("Add ratio: %u%%.\n", add_ratio
);
1276 printf_verbose("Mode:%s%s.\n",
1278 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
1279 printf_verbose("Key multiplication factor: %" PRIu64
".\n", key_mul
);
1280 printf_verbose("Init pool size offset %lu size %lu.\n",
1281 init_pool_offset
, init_pool_size
);
1282 printf_verbose("Lookup pool size offset %lu size %lu.\n",
1283 lookup_pool_offset
, lookup_pool_size
);
1284 printf_verbose("Update pool size offset %lu size %lu.\n",
1285 write_pool_offset
, write_pool_size
);
1286 if (validate_lookup
)
1287 printf_verbose("Validating lookups.\n");
1289 printf_verbose("Memory leak dection activated.\n");
1290 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
1291 "main", pthread_self(), (unsigned long)gettid());
1293 memset(&act
, 0, sizeof(act
));
1294 ret
= sigemptyset(&act
.sa_mask
);
1296 perror("sigemptyset");
1299 act
.sa_handler
= test_ja_rw_sigusr1_handler
;
1300 act
.sa_flags
= SA_RESTART
;
1301 ret
= sigaction(SIGUSR1
, &act
, NULL
);
1303 perror("sigaction");
1307 err
= create_all_cpu_call_rcu_data(0);
1309 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
1312 rcu_register_thread();
1315 ret
= do_sanity_test();
1320 /* Wait for in-flight call_rcu free to complete for leak detection */
1323 ret
|= check_memory_leaks();
1325 rcu_unregister_thread();
1326 free_all_cpu_call_rcu_data();
1329 printf("Test ended with error: %d\n", ret
);