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_node(struct ja_test_node
*node
)
152 uatomic_inc(&test_nodes_freed
);
156 void free_node_cb(struct rcu_head
*head
)
158 struct ja_test_node
*node
=
159 caa_container_of(head
, struct ja_test_node
, node
.head
);
165 void test_delete_all_nodes(struct cds_lfht
*ht
)
167 struct cds_lfht_iter iter
;
168 struct lfht_test_node
*node
;
169 unsigned long count
= 0;
171 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
174 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
176 call_rcu(&node
->head
, free_node_cb
);
179 printf("deleted %lu nodes.\n", count
);
183 void show_usage(int argc
, char **argv
)
185 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
187 printf(" [-r] [-w] (yield reader and/or writer)\n");
189 printf(" [-d delay] (writer period (us))\n");
190 printf(" [-c duration] (reader C.S. duration (in loops))\n");
191 printf(" [-v] (verbose output)\n");
192 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
193 printf(" [-u] Add unique keys.\n");
194 printf(" [-s] Replace existing keys.\n");
195 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
196 printf(" [-r ratio] Add ratio (in %% of add+removal).\n");
197 printf(" [-k] Populate init nodes.\n");
198 printf(" [-R offset] Lookup pool offset.\n");
199 printf(" [-S offset] Write pool offset.\n");
200 printf(" [-T offset] Init pool offset.\n");
201 printf(" [-M size] Lookup pool size.\n");
202 printf(" [-N size] Write pool size.\n");
203 printf(" [-O size] Init pool size.\n");
204 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
205 printf(" [-t] Do sanity test.\n");
206 printf(" [-B] Key bits for multithread test (default: 32).\n");
207 printf(" [-m factor] Key multiplication factor.\n");
208 printf(" [-l] Memory leak detection.\n");
213 int test_8bit_key(void)
217 uint64_t ka
[] = { 4, 17, 100, 222 };
218 uint64_t ka_test_offset
= 5;
220 /* Test with 8-bit key */
221 test_ja
= cds_ja_new(8);
223 printf("Error allocating judy array.\n");
228 printf("Test #1: add keys (8-bit).\n");
229 for (key
= 0; key
< 200; key
++) {
230 struct ja_test_node
*node
= node_alloc();
232 ja_test_node_init(node
, key
);
234 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
237 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
244 printf("Test #2: successful key lookup (8-bit).\n");
245 for (key
= 0; key
< 200; key
++) {
246 struct cds_hlist_head head
;
249 head
= cds_ja_lookup(test_ja
, key
);
250 if (cds_hlist_empty(&head
)) {
251 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
257 printf("Test #3: unsuccessful key lookup (8-bit).\n");
258 for (key
= 200; key
< 240; key
++) {
259 struct cds_hlist_head head
;
262 head
= cds_ja_lookup(test_ja
, key
);
263 if (!cds_hlist_empty(&head
)) {
265 "Error unexpected lookup node %" PRIu64
"\n",
272 printf("Test #4: remove keys (8-bit).\n");
273 for (key
= 0; key
< 200; key
++) {
274 struct cds_hlist_head head
;
275 struct ja_test_node
*node
;
278 head
= cds_ja_lookup(test_ja
, key
);
279 node
= cds_hlist_first_entry_rcu(&head
, struct ja_test_node
, node
.list
);
281 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
284 ret
= cds_ja_del(test_ja
, key
, &node
->node
);
286 fprintf(stderr
, "Error (%d) removing node %" PRIu64
"\n", ret
, key
);
289 call_rcu(&node
->node
.head
, free_node_cb
);
290 head
= cds_ja_lookup(test_ja
, key
);
291 if (!cds_hlist_empty(&head
)) {
292 fprintf(stderr
, "Error lookup %" PRIu64
": %p (after delete) failed. Node is not expected.\n", key
, head
.next
);
299 printf("Test #5: lookup lower equal (8-bit).\n");
301 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
302 struct ja_test_node
*node
= node_alloc();
305 ja_test_node_init(node
, key
);
307 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
310 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
316 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
317 struct cds_hlist_head head
;
318 struct ja_test_node
*node
;
320 key
= ka
[i
] + ka_test_offset
;
322 head
= cds_ja_lookup_lower_equal(test_ja
, key
);
323 if (cds_hlist_empty(&head
)) {
324 fprintf(stderr
, "Error lookup lower equal. Cannot find expected key %" PRIu64
" below or equal to %" PRIu64
".\n",
328 node
= cds_hlist_first_entry_rcu(&head
, struct ja_test_node
, node
.list
);
329 if (node
->key
!= ka
[i
]) {
330 fprintf(stderr
, "Error lookup lower equal. Expecting key %" PRIu64
" below or equal to %" PRIu64
", but found %" PRIu64
" instead.\n",
331 ka
[i
], key
, node
->key
);
337 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
338 struct cds_hlist_head head
;
339 struct ja_test_node
*node
;
341 key
= ka
[i
]; /* without offset */
343 head
= cds_ja_lookup_lower_equal(test_ja
, key
);
344 if (cds_hlist_empty(&head
)) {
345 fprintf(stderr
, "Error lookup lower equal. Cannot find expected key %" PRIu64
" below or equal to %" PRIu64
".\n",
349 node
= cds_hlist_first_entry_rcu(&head
, struct ja_test_node
, node
.list
);
350 if (node
->key
!= ka
[i
]) {
351 fprintf(stderr
, "Error lookup lower equal. Expecting key %" PRIu64
" below or equal to %" PRIu64
", but found %" PRIu64
" instead.\n",
352 ka
[i
], key
, node
->key
);
360 ret
= cds_ja_destroy(test_ja
, free_node_cb
);
362 fprintf(stderr
, "Error destroying judy array\n");
369 int test_16bit_key(void)
373 uint64_t ka
[] = { 4, 105, 222, 4000, 4111, 59990, 65435 };
374 uint64_t ka_test_offset
= 100;
376 /* Test with 16-bit key */
377 test_ja
= cds_ja_new(16);
379 printf("Error allocating judy array.\n");
384 printf("Test #1: add keys (16-bit).\n");
385 for (key
= 0; key
< 10000; key
++) {
386 //for (key = 0; key < 65536; key+=256) {
387 struct ja_test_node
*node
= node_alloc();
389 ja_test_node_init(node
, key
);
391 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
394 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
401 printf("Test #2: successful key lookup (16-bit).\n");
402 for (key
= 0; key
< 10000; key
++) {
403 //for (key = 0; key < 65536; key+=256) {
404 struct cds_hlist_head head
;
407 head
= cds_ja_lookup(test_ja
, key
);
408 if (cds_hlist_empty(&head
)) {
409 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
415 printf("Test #3: unsuccessful key lookup (16-bit).\n");
416 for (key
= 11000; key
<= 11002; key
++) {
417 struct cds_hlist_head head
;
420 head
= cds_ja_lookup(test_ja
, key
);
421 if (!cds_hlist_empty(&head
)) {
423 "Error unexpected lookup node %" PRIu64
"\n",
430 printf("Test #4: remove keys (16-bit).\n");
431 for (key
= 0; key
< 10000; key
++) {
432 //for (key = 0; key < 65536; key+=256) {
433 struct cds_hlist_head head
;
434 struct ja_test_node
*node
;
437 head
= cds_ja_lookup(test_ja
, key
);
438 node
= cds_hlist_first_entry_rcu(&head
, struct ja_test_node
, node
.list
);
440 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
443 ret
= cds_ja_del(test_ja
, key
, &node
->node
);
445 fprintf(stderr
, "Error (%d) removing node %" PRIu64
"\n", ret
, key
);
448 call_rcu(&node
->node
.head
, free_node_cb
);
449 head
= cds_ja_lookup(test_ja
, key
);
450 if (!cds_hlist_empty(&head
)) {
451 fprintf(stderr
, "Error lookup %" PRIu64
": %p (after delete) failed. Node is not expected.\n", key
, head
.next
);
458 printf("Test #5: lookup lower equal (16-bit).\n");
460 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
461 struct ja_test_node
*node
= node_alloc();
464 ja_test_node_init(node
, key
);
466 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
469 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
475 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
476 struct cds_hlist_head head
;
477 struct ja_test_node
*node
;
479 key
= ka
[i
] + ka_test_offset
;
481 head
= cds_ja_lookup_lower_equal(test_ja
, key
);
482 if (cds_hlist_empty(&head
)) {
483 fprintf(stderr
, "Error lookup lower equal. Cannot find expected key %" PRIu64
" below or equal to %" PRIu64
".\n",
487 node
= cds_hlist_first_entry_rcu(&head
, struct ja_test_node
, node
.list
);
488 if (node
->key
!= ka
[i
]) {
489 fprintf(stderr
, "Error lookup lower equal. Expecting key %" PRIu64
" below or equal to %" PRIu64
", but found %" PRIu64
" instead.\n",
490 ka
[i
], key
, node
->key
);
496 for (i
= 0; i
< CAA_ARRAY_SIZE(ka
); i
++) {
497 struct cds_hlist_head head
;
498 struct ja_test_node
*node
;
500 key
= ka
[i
]; /* without offset */
502 head
= cds_ja_lookup_lower_equal(test_ja
, key
);
503 if (cds_hlist_empty(&head
)) {
504 fprintf(stderr
, "Error lookup lower equal. Cannot find expected key %" PRIu64
" below or equal to %" PRIu64
".\n",
508 node
= cds_hlist_first_entry_rcu(&head
, struct ja_test_node
, node
.list
);
509 if (node
->key
!= ka
[i
]) {
510 fprintf(stderr
, "Error lookup lower equal. Expecting key %" PRIu64
" below or equal to %" PRIu64
", but found %" PRIu64
" instead.\n",
511 ka
[i
], key
, node
->key
);
519 ret
= cds_ja_destroy(test_ja
, free_node_cb
);
521 fprintf(stderr
, "Error destroying judy array\n");
528 * nr_dup is number of nodes per key.
531 int test_sparse_key(unsigned int bits
, int nr_dup
)
533 uint64_t key
, max_key
;
534 int zerocount
, i
, ret
;
537 max_key
= UINT64_MAX
;
539 max_key
= (1ULL << bits
) - 1;
541 printf("Sparse key test begins for %u-bit keys\n", bits
);
542 /* Test with 16-bit key */
543 test_ja
= cds_ja_new(bits
);
545 printf("Error allocating judy array.\n");
550 printf("Test #1: add keys (%u-bit).\n", bits
);
551 for (i
= 0; i
< nr_dup
; i
++) {
553 for (key
= 0; key
<= max_key
&& (key
!= 0 || zerocount
< 1); key
+= 1ULL << (bits
- 8)) {
554 struct ja_test_node
*node
= node_alloc();
556 ja_test_node_init(node
, key
);
558 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
561 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
571 printf("Test #2: successful key lookup (%u-bit).\n", bits
);
573 for (key
= 0; key
<= max_key
&& (key
!= 0 || zerocount
< 1); key
+= 1ULL << (bits
- 8)) {
574 struct cds_hlist_head head
;
575 struct ja_test_node
*node
;
576 struct cds_hlist_node
*pos
;
580 head
= cds_ja_lookup(test_ja
, key
);
581 if (cds_hlist_empty(&head
)) {
582 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
585 cds_hlist_for_each_entry_rcu(node
, pos
, &head
, node
.list
) {
588 if (count
!= nr_dup
) {
589 fprintf(stderr
, "Unexpected number of match for key %" PRIu64
", expected %d, got %d.\n", key
, nr_dup
, count
);
597 printf("Test #3: unsuccessful key lookup (%u-bit).\n", bits
);
599 for (key
= 0; key
<= max_key
&& (key
!= 0 || zerocount
< 1); key
+= 1ULL << (bits
- 8)) {
600 struct cds_hlist_head head
;
603 head
= cds_ja_lookup(test_ja
, key
+ 42);
604 if (!cds_hlist_empty(&head
)) {
606 "Error unexpected lookup node %" PRIu64
"\n",
616 printf("Test #4: remove keys (%u-bit).\n", bits
);
618 for (key
= 0; key
<= max_key
&& (key
!= 0 || zerocount
< 1); key
+= 1ULL << (bits
- 8)) {
619 struct cds_hlist_head head
;
620 struct ja_test_node
*node
;
621 struct cds_hlist_node
*pos
;
625 head
= cds_ja_lookup(test_ja
, key
);
628 cds_hlist_for_each_entry_rcu(node
, pos
, &head
, node
.list
) {
629 struct cds_hlist_head testhead
;
633 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
636 ret
= cds_ja_del(test_ja
, key
, &node
->node
);
638 fprintf(stderr
, "Error (%d) removing node %" PRIu64
"\n", ret
, key
);
641 call_rcu(&node
->node
.head
, free_node_cb
);
642 testhead
= cds_ja_lookup(test_ja
, key
);
643 if (count
< nr_dup
&& cds_hlist_empty(&testhead
)) {
644 fprintf(stderr
, "Error: no node found after deletion of some nodes of a key\n");
648 head
= cds_ja_lookup(test_ja
, key
);
649 if (!cds_hlist_empty(&head
)) {
650 fprintf(stderr
, "Error lookup %" PRIu64
": %p (after delete) failed. Node is not expected.\n", key
, head
.next
);
659 ret
= cds_ja_destroy(test_ja
, free_node_cb
);
661 fprintf(stderr
, "Error destroying judy array\n");
664 printf("Test ends\n");
670 int do_sanity_test(void)
674 printf("Sanity test start.\n");
676 for (i
= 0; i
< 3; i
++) {
677 ret
= test_8bit_key();
681 rcu_quiescent_state();
683 ret
= test_16bit_key();
687 rcu_quiescent_state();
690 for (i
= 8; i
<= 64; i
*= 2) {
691 /* nr of nodes per key */
692 for (j
= 1; j
< 4; j
++) {
693 ret
= test_sparse_key(i
, j
);
697 rcu_quiescent_state();
700 printf("Sanity test end.\n");
705 enum urcu_ja_addremove
{
709 }; /* 1: add, -1 remove, 0: random */
711 static enum urcu_ja_addremove addremove
; /* 1: add, -1 remove, 0: random */
714 void test_ja_rw_sigusr1_handler(int signo
)
718 printf("Add/Remove: random.\n");
719 addremove
= AR_RANDOM
;
722 printf("Add/Remove: remove only.\n");
723 addremove
= AR_REMOVE
;
726 printf("Add/Remove: add only.\n");
733 void *test_ja_rw_thr_reader(void *_count
)
735 unsigned long long *count
= _count
;
736 struct cds_hlist_head head
;
739 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
740 "reader", pthread_self(), (unsigned long) gettid());
744 rcu_register_thread();
754 /* note: only looking up ulong keys */
755 key
= ((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % lookup_pool_size
) + lookup_pool_offset
;
757 head
= cds_ja_lookup(test_ja
, key
);
758 if (cds_hlist_empty(&head
)) {
759 if (validate_lookup
) {
760 printf("[ERROR] Lookup cannot find initial node.\n");
763 URCU_TLS(lookup_fail
)++;
765 URCU_TLS(lookup_ok
)++;
767 rcu_debug_yield_read();
768 if (caa_unlikely(rduration
))
769 loop_sleep(rduration
);
771 URCU_TLS(nr_reads
)++;
772 if (caa_unlikely(!test_duration_read()))
774 if (caa_unlikely((URCU_TLS(nr_reads
) & ((1 << 10) - 1)) == 0))
775 rcu_quiescent_state();
778 rcu_unregister_thread();
780 *count
= URCU_TLS(nr_reads
);
781 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
782 "reader", pthread_self(), (unsigned long) gettid());
783 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
784 pthread_self(), URCU_TLS(lookup_fail
),
785 URCU_TLS(lookup_ok
));
792 return ((unsigned int) rand_r(&URCU_TLS(rand_lookup
)) % 100) < add_ratio
;
796 void *test_ja_rw_thr_writer(void *_count
)
798 struct wr_count
*count
= _count
;
799 struct cds_hlist_head head
;
803 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
804 "writer", pthread_self(), (unsigned long) gettid());
808 rcu_register_thread();
816 if ((addremove
== AR_ADD
)
817 || (addremove
== AR_RANDOM
&& is_add())) {
818 struct ja_test_node
*node
= node_alloc();
819 struct cds_ja_node
*ret_node
;
821 /* note: only inserting ulong keys */
822 key
= ((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % write_pool_size
) + write_pool_offset
;
824 ja_test_node_init(node
, key
);
827 ret_node
= cds_ja_add_unique(test_ja
, key
, &node
->node
);
828 if (ret_node
!= &node
->node
) {
830 URCU_TLS(nr_addexist
)++;
834 } else if (add_replace
) {
835 assert(0); /* not implemented yet. */
837 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
839 fprintf(stderr
, "Error in cds_ja_add: %d\n", ret
);
847 struct ja_test_node
*node
;
850 /* note: only deleting ulong keys */
851 key
= ((unsigned long) rand_r(&URCU_TLS(rand_lookup
)) % write_pool_size
) + write_pool_offset
;
856 head
= cds_ja_lookup(test_ja
, key
);
857 node
= cds_hlist_first_entry_rcu(&head
, struct ja_test_node
, node
.list
);
859 ret
= cds_ja_del(test_ja
, key
, &node
->node
);
861 call_rcu(&node
->node
.head
, free_node_cb
);
864 URCU_TLS(nr_delnoent
)++;
867 URCU_TLS(nr_delnoent
)++;
872 URCU_TLS(nr_writes
)++;
873 if (caa_unlikely(!test_duration_write()))
875 if (caa_unlikely(wdelay
))
877 if (caa_unlikely((URCU_TLS(nr_writes
) & ((1 << 10) - 1)) == 0))
878 rcu_quiescent_state();
881 rcu_unregister_thread();
883 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
884 "writer", pthread_self(), (unsigned long) gettid());
885 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
886 "nr_delnoent %lu\n", pthread_self(), URCU_TLS(nr_add
),
887 URCU_TLS(nr_addexist
), URCU_TLS(nr_del
),
888 URCU_TLS(nr_delnoent
));
889 count
->update_ops
= URCU_TLS(nr_writes
);
890 count
->add
= URCU_TLS(nr_add
);
891 count
->add_exist
= URCU_TLS(nr_addexist
);
892 count
->remove
= URCU_TLS(nr_del
);
897 int do_mt_populate_ja(void)
899 struct cds_hlist_head head
;
906 printf("Starting rw test\n");
908 for (iter
= init_pool_offset
; iter
< init_pool_offset
+ init_pool_size
; iter
++) {
909 struct ja_test_node
*node
= node_alloc();
912 /* note: only inserting ulong keys */
913 key
= (unsigned long) iter
;
915 ja_test_node_init(node
, key
);
917 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
919 URCU_TLS(nr_writes
)++;
922 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
933 pthread_t
*tid_reader
, *tid_writer
;
936 unsigned long long *count_reader
;
937 struct wr_count
*count_writer
;
938 unsigned long long tot_reads
= 0, tot_writes
= 0,
939 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
942 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
943 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
944 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
945 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
947 printf("Allocating Judy Array for %u-bit keys\n", key_bits
);
948 test_ja
= cds_ja_new(key_bits
);
950 printf("Error allocating judy array.\n");
959 for (i
= 0; i
< nr_readers
; i
++) {
960 err
= pthread_create(&tid_reader
[i
],
961 NULL
, test_ja_rw_thr_reader
,
966 for (i
= 0; i
< nr_writers
; i
++) {
967 err
= pthread_create(&tid_writer
[i
],
968 NULL
, test_ja_rw_thr_writer
,
978 rcu_thread_offline_qsbr();
982 remain
= sleep(remain
);
983 } while (remain
> 0);
987 for (i
= 0; i
< nr_readers
; i
++) {
988 err
= pthread_join(tid_reader
[i
], &tret
);
991 tot_reads
+= count_reader
[i
];
993 for (i
= 0; i
< nr_writers
; i
++) {
994 err
= pthread_join(tid_writer
[i
], &tret
);
997 tot_writes
+= count_writer
[i
].update_ops
;
998 tot_add
+= count_writer
[i
].add
;
999 tot_add_exist
+= count_writer
[i
].add_exist
;
1000 tot_remove
+= count_writer
[i
].remove
;
1002 rcu_thread_online_qsbr();
1004 ret
= cds_ja_destroy(test_ja
, free_node_cb
);
1006 fprintf(stderr
, "Error destroying judy array\n");
1020 int check_memory_leaks(void)
1022 unsigned long na
, nf
;
1024 na
= uatomic_read(&test_nodes_allocated
);
1025 nf
= uatomic_read(&test_nodes_freed
);
1027 fprintf(stderr
, "Memory leak of %ld test nodes detected. Allocated: %lu, freed: %lu\n",
1034 int main(int argc
, char **argv
)
1036 int i
, j
, a
, ret
, err
;
1038 struct sigaction act
;
1041 show_usage(argc
, argv
);
1045 err
= sscanf(argv
[1], "%u", &nr_readers
);
1047 show_usage(argc
, argv
);
1051 err
= sscanf(argv
[2], "%u", &nr_writers
);
1053 show_usage(argc
, argv
);
1057 err
= sscanf(argv
[3], "%lu", &duration
);
1059 show_usage(argc
, argv
);
1063 for (i
= 4; i
< argc
; i
++) {
1064 if (argv
[i
][0] != '-')
1066 switch (argv
[i
][1]) {
1069 yield_active
|= YIELD_READ
;
1072 yield_active
|= YIELD_WRITE
;
1077 show_usage(argc
, argv
);
1080 a
= atoi(argv
[++i
]);
1081 cpu_affinities
[next_aff
++] = a
;
1083 printf_verbose("Adding CPU %d affinity\n", a
);
1087 show_usage(argc
, argv
);
1090 rduration
= atol(argv
[++i
]);
1094 show_usage(argc
, argv
);
1097 wdelay
= atol(argv
[++i
]);
1103 add_ratio
= atoi(argv
[++i
]);
1109 lookup_pool_offset
= atol(argv
[++i
]);
1112 write_pool_offset
= atol(argv
[++i
]);
1115 init_pool_offset
= atol(argv
[++i
]);
1118 lookup_pool_size
= atol(argv
[++i
]);
1121 write_pool_size
= atol(argv
[++i
]);
1124 init_pool_size
= atol(argv
[++i
]);
1127 validate_lookup
= 1;
1133 key_bits
= atol(argv
[++i
]);
1136 key_mul
= atoll(argv
[++i
]);
1150 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
1151 duration
, nr_readers
, nr_writers
);
1152 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
1153 printf_verbose("Reader duration : %lu loops.\n", rduration
);
1154 printf_verbose("Add ratio: %u%%.\n", add_ratio
);
1155 printf_verbose("Mode:%s%s.\n",
1157 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
1158 printf_verbose("Key multiplication factor: %" PRIu64
".\n", key_mul
);
1159 printf_verbose("Init pool size offset %lu size %lu.\n",
1160 init_pool_offset
, init_pool_size
);
1161 printf_verbose("Lookup pool size offset %lu size %lu.\n",
1162 lookup_pool_offset
, lookup_pool_size
);
1163 printf_verbose("Update pool size offset %lu size %lu.\n",
1164 write_pool_offset
, write_pool_size
);
1165 if (validate_lookup
)
1166 printf_verbose("Validating lookups.\n");
1168 printf_verbose("Memory leak dection activated.\n");
1169 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
1170 "main", pthread_self(), (unsigned long)gettid());
1172 memset(&act
, 0, sizeof(act
));
1173 ret
= sigemptyset(&act
.sa_mask
);
1175 perror("sigemptyset");
1178 act
.sa_handler
= test_ja_rw_sigusr1_handler
;
1179 act
.sa_flags
= SA_RESTART
;
1180 ret
= sigaction(SIGUSR1
, &act
, NULL
);
1182 perror("sigaction");
1186 err
= create_all_cpu_call_rcu_data(0);
1188 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
1191 rcu_register_thread();
1194 ret
= do_sanity_test();
1199 /* Wait for in-flight call_rcu free to complete for leak detection */
1202 ret
|= check_memory_leaks();
1204 rcu_unregister_thread();
1205 free_all_cpu_call_rcu_data();
1208 printf("Test ended with error: %d\n", ret
);