rcuja test: destroy should be done online
[userspace-rcu.git] / tests / test_urcu_ja.c
CommitLineData
44151f89
MD
1/*
2 * test_urcu_ja.c
3 *
4 * Userspace RCU library - test program
5 *
6 * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
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.
12 *
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.
17 *
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.
21 */
22
23#define _GNU_SOURCE
24#include "test_urcu_ja.h"
a2a7ff59 25#include <inttypes.h>
582a6ade 26#include <stdint.h>
44151f89
MD
27
28DEFINE_URCU_TLS(unsigned int, rand_lookup);
29DEFINE_URCU_TLS(unsigned long, nr_add);
30DEFINE_URCU_TLS(unsigned long, nr_addexist);
31DEFINE_URCU_TLS(unsigned long, nr_del);
32DEFINE_URCU_TLS(unsigned long, nr_delnoent);
33DEFINE_URCU_TLS(unsigned long, lookup_fail);
34DEFINE_URCU_TLS(unsigned long, lookup_ok);
35
36struct cds_ja *test_ja;
37
38volatile int test_go, test_stop;
39
40unsigned long wdelay;
41
42unsigned long duration;
43
44/* read-side C.S. duration, in loops */
45unsigned long rduration;
46
47unsigned long init_populate;
48int add_only;
49
50unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
51unsigned long init_pool_size = DEFAULT_RAND_POOL,
52 lookup_pool_size = DEFAULT_RAND_POOL,
53 write_pool_size = DEFAULT_RAND_POOL;
54int validate_lookup;
1a0c0717
MD
55int sanity_test;
56unsigned int key_bits = 32;
44151f89
MD
57
58int count_pipe[2];
59
60int verbose_mode;
61
62unsigned int cpu_affinities[NR_CPUS];
63unsigned int next_aff = 0;
64int use_affinity = 0;
65
66pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
67
68DEFINE_URCU_TLS(unsigned long long, nr_writes);
69DEFINE_URCU_TLS(unsigned long long, nr_reads);
70
71unsigned int nr_readers;
72unsigned int nr_writers;
73
34ffee3e 74static unsigned int add_ratio = 50;
4df4328c 75static uint64_t key_mul = 1ULL;
34ffee3e 76
44151f89
MD
77static pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
78
79void set_affinity(void)
80{
81 cpu_set_t mask;
82 int cpu;
83 int ret;
84
85 if (!use_affinity)
86 return;
87
88#if HAVE_SCHED_SETAFFINITY
89 ret = pthread_mutex_lock(&affinity_mutex);
90 if (ret) {
91 perror("Error in pthread mutex lock");
92 exit(-1);
93 }
94 cpu = cpu_affinities[next_aff++];
95 ret = pthread_mutex_unlock(&affinity_mutex);
96 if (ret) {
97 perror("Error in pthread mutex unlock");
98 exit(-1);
99 }
100 CPU_ZERO(&mask);
101 CPU_SET(cpu, &mask);
102#if SCHED_SETAFFINITY_ARGS == 2
103 sched_setaffinity(0, &mask);
104#else
105 sched_setaffinity(0, sizeof(mask), &mask);
106#endif
107#endif /* HAVE_SCHED_SETAFFINITY */
108}
109
110void rcu_copy_mutex_lock(void)
111{
112 int ret;
113 ret = pthread_mutex_lock(&rcu_copy_mutex);
114 if (ret) {
115 perror("Error in pthread mutex lock");
116 exit(-1);
117 }
118}
119
120void rcu_copy_mutex_unlock(void)
121{
122 int ret;
123
124 ret = pthread_mutex_unlock(&rcu_copy_mutex);
125 if (ret) {
126 perror("Error in pthread mutex unlock");
127 exit(-1);
128 }
129}
130
44151f89
MD
131void free_node_cb(struct rcu_head *head)
132{
133 struct ja_test_node *node =
3d8fe307 134 caa_container_of(head, struct ja_test_node, node.head);
a7542b7f 135 poison_free(node);
44151f89
MD
136}
137
3d8fe307 138#if 0
44151f89
MD
139static
140void test_delete_all_nodes(struct cds_lfht *ht)
141{
142 struct cds_lfht_iter iter;
143 struct lfht_test_node *node;
144 unsigned long count = 0;
145
146 cds_lfht_for_each_entry(ht, &iter, node, node) {
147 int ret;
148
149 ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
150 assert(!ret);
151 call_rcu(&node->head, free_node_cb);
152 count++;
153 }
154 printf("deleted %lu nodes.\n", count);
155}
156#endif
157
158void show_usage(int argc, char **argv)
159{
160 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]);
161#ifdef DEBUG_YIELD
162 printf(" [-r] [-w] (yield reader and/or writer)\n");
163#endif
164 printf(" [-d delay] (writer period (us))\n");
165 printf(" [-c duration] (reader C.S. duration (in loops))\n");
166 printf(" [-v] (verbose output)\n");
167 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
168printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
4df4328c 169 printf(" [-r ratio] Add ratio (in %% of add+removal).\n");
46f373a6 170 printf(" [-k] Populate init nodes.\n");
44151f89
MD
171 printf(" [-R offset] Lookup pool offset.\n");
172 printf(" [-S offset] Write pool offset.\n");
173 printf(" [-T offset] Init pool offset.\n");
174 printf(" [-M size] Lookup pool size.\n");
175 printf(" [-N size] Write pool size.\n");
176 printf(" [-O size] Init pool size.\n");
177 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
1a0c0717
MD
178 printf(" [-s] Do sanity test.\n");
179 printf(" [-B] Key bits for multithread test (default: 32).\n");
4df4328c 180 printf(" [-m factor] Key multiplication factor.\n");
44151f89
MD
181 printf("\n\n");
182}
183
582a6ade
MD
184
185static
186int test_8bit_key(void)
187{
188 int ret;
189 uint64_t key;
190
191 /* Test with 8-bit key */
192 test_ja = cds_ja_new(8);
193 if (!test_ja) {
194 printf("Error allocating judy array.\n");
195 return -1;
196 }
197
198 /* Add keys */
199 printf("Test #1: add keys (8-bit).\n");
200 for (key = 0; key < 200; key++) {
201 struct ja_test_node *node =
202 calloc(sizeof(*node), 1);
203
204 ja_test_node_init(node, key);
205 rcu_read_lock();
206 ret = cds_ja_add(test_ja, key, &node->node);
207 rcu_read_unlock();
208 if (ret) {
209 fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
210 ret, key);
211 assert(0);
212 }
213 }
214 printf("OK\n");
215
216 printf("Test #2: successful key lookup (8-bit).\n");
217 for (key = 0; key < 200; key++) {
af3cbd45 218 struct cds_hlist_head head;
582a6ade
MD
219
220 rcu_read_lock();
221 head = cds_ja_lookup(test_ja, key);
af3cbd45 222 if (cds_hlist_empty(&head)) {
582a6ade
MD
223 fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
224 assert(0);
225 }
226 rcu_read_unlock();
227 }
228 printf("OK\n");
229 printf("Test #3: unsuccessful key lookup (8-bit).\n");
230 for (key = 200; key < 240; key++) {
af3cbd45 231 struct cds_hlist_head head;
582a6ade
MD
232
233 rcu_read_lock();
234 head = cds_ja_lookup(test_ja, key);
af3cbd45 235 if (!cds_hlist_empty(&head)) {
582a6ade
MD
236 fprintf(stderr,
237 "Error unexpected lookup node %" PRIu64 "\n",
238 key);
239 assert(0);
240 }
241 rcu_read_unlock();
242 }
243 printf("OK\n");
3d8fe307
MD
244 printf("Test #4: remove keys (8-bit).\n");
245 for (key = 0; key < 200; key++) {
246 struct cds_hlist_head head;
247 struct ja_test_node *node;
248
249 rcu_read_lock();
250 head = cds_ja_lookup(test_ja, key);
251 node = cds_hlist_first_entry_rcu(&head, struct ja_test_node, node.list);
252 if (!node) {
253 fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
254 assert(0);
255 }
256 ret = cds_ja_del(test_ja, key, &node->node);
257 if (ret) {
258 fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key);
259 assert(0);
260 }
261 call_rcu(&node->node.head, free_node_cb);
4d6ef45e
MD
262 head = cds_ja_lookup(test_ja, key);
263 if (!cds_hlist_empty(&head)) {
264 fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, head.next);
265 assert(0);
266 }
3d8fe307
MD
267 rcu_read_unlock();
268 }
269 printf("OK\n");
582a6ade 270
3d8fe307 271 ret = cds_ja_destroy(test_ja, free_node_cb);
582a6ade
MD
272 if (ret) {
273 fprintf(stderr, "Error destroying judy array\n");
274 return -1;
275 }
276 return 0;
277}
278
279static
280int test_16bit_key(void)
281{
282 int ret;
283 uint64_t key;
284
285 /* Test with 16-bit key */
286 test_ja = cds_ja_new(16);
287 if (!test_ja) {
288 printf("Error allocating judy array.\n");
289 return -1;
290 }
291
292 /* Add keys */
293 printf("Test #1: add keys (16-bit).\n");
4d6ef45e
MD
294 for (key = 0; key < 10000; key++) {
295 //for (key = 0; key < 65536; key+=256) {
582a6ade
MD
296 struct ja_test_node *node =
297 calloc(sizeof(*node), 1);
298
299 ja_test_node_init(node, key);
300 rcu_read_lock();
301 ret = cds_ja_add(test_ja, key, &node->node);
302 rcu_read_unlock();
303 if (ret) {
304 fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
305 ret, key);
306 assert(0);
307 }
308 }
309 printf("OK\n");
310
311 printf("Test #2: successful key lookup (16-bit).\n");
4d6ef45e
MD
312 for (key = 0; key < 10000; key++) {
313 //for (key = 0; key < 65536; key+=256) {
af3cbd45 314 struct cds_hlist_head head;
582a6ade
MD
315
316 rcu_read_lock();
317 head = cds_ja_lookup(test_ja, key);
af3cbd45 318 if (cds_hlist_empty(&head)) {
582a6ade
MD
319 fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
320 assert(0);
321 }
322 rcu_read_unlock();
323 }
324 printf("OK\n");
325 printf("Test #3: unsuccessful key lookup (16-bit).\n");
326 for (key = 11000; key <= 11002; key++) {
af3cbd45 327 struct cds_hlist_head head;
582a6ade
MD
328
329 rcu_read_lock();
330 head = cds_ja_lookup(test_ja, key);
af3cbd45 331 if (!cds_hlist_empty(&head)) {
582a6ade
MD
332 fprintf(stderr,
333 "Error unexpected lookup node %" PRIu64 "\n",
334 key);
335 assert(0);
336 }
337 rcu_read_unlock();
338 }
339 printf("OK\n");
4d6ef45e
MD
340 printf("Test #4: remove keys (16-bit).\n");
341 for (key = 0; key < 10000; key++) {
342 //for (key = 0; key < 65536; key+=256) {
343 struct cds_hlist_head head;
344 struct ja_test_node *node;
345
346 rcu_read_lock();
347 head = cds_ja_lookup(test_ja, key);
348 node = cds_hlist_first_entry_rcu(&head, struct ja_test_node, node.list);
349 if (!node) {
350 fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
351 assert(0);
352 }
353 ret = cds_ja_del(test_ja, key, &node->node);
354 if (ret) {
355 fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key);
356 assert(0);
357 }
358 call_rcu(&node->node.head, free_node_cb);
359 head = cds_ja_lookup(test_ja, key);
360 if (!cds_hlist_empty(&head)) {
361 fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, head.next);
362 assert(0);
363 }
364 rcu_read_unlock();
365 }
366 printf("OK\n");
582a6ade 367
3d8fe307 368 ret = cds_ja_destroy(test_ja, free_node_cb);
582a6ade
MD
369 if (ret) {
370 fprintf(stderr, "Error destroying judy array\n");
371 return -1;
372 }
373 return 0;
374}
375
6fdf58e8
MD
376/*
377 * nr_dup is number of nodes per key.
378 */
582a6ade 379static
6fdf58e8 380int test_sparse_key(unsigned int bits, int nr_dup)
582a6ade 381{
582a6ade 382 uint64_t key, max_key;
6fdf58e8 383 int zerocount, i, ret;
582a6ade
MD
384
385 if (bits == 64)
386 max_key = UINT64_MAX;
387 else
388 max_key = (1ULL << bits) - 1;
389
390 printf("Sparse key test begins for %u-bit keys\n", bits);
391 /* Test with 16-bit key */
392 test_ja = cds_ja_new(bits);
393 if (!test_ja) {
394 printf("Error allocating judy array.\n");
395 return -1;
396 }
397
398 /* Add keys */
399 printf("Test #1: add keys (%u-bit).\n", bits);
6fdf58e8
MD
400 for (i = 0; i < nr_dup; i++) {
401 zerocount = 0;
402 for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
403 struct ja_test_node *node =
404 calloc(sizeof(*node), 1);
582a6ade 405
6fdf58e8
MD
406 ja_test_node_init(node, key);
407 rcu_read_lock();
408 ret = cds_ja_add(test_ja, key, &node->node);
409 rcu_read_unlock();
410 if (ret) {
411 fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
412 ret, key);
413 assert(0);
414 }
415 if (key == 0)
416 zerocount++;
582a6ade 417 }
582a6ade
MD
418 }
419 printf("OK\n");
420
421 printf("Test #2: successful key lookup (%u-bit).\n", bits);
422 zerocount = 0;
423 for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
af3cbd45 424 struct cds_hlist_head head;
6fdf58e8
MD
425 struct ja_test_node *node;
426 struct cds_hlist_node *pos;
427 int count = 0;
582a6ade
MD
428
429 rcu_read_lock();
430 head = cds_ja_lookup(test_ja, key);
af3cbd45 431 if (cds_hlist_empty(&head)) {
582a6ade
MD
432 fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
433 assert(0);
434 }
6fdf58e8
MD
435 cds_hlist_for_each_entry_rcu(node, pos, &head, node.list) {
436 count++;
437 }
438 if (count != nr_dup) {
439 fprintf(stderr, "Unexpected number of match for key %" PRIu64 ", expected %d, got %d.\n", key, nr_dup, count);
440 }
582a6ade
MD
441 rcu_read_unlock();
442 if (key == 0)
443 zerocount++;
444 }
445 printf("OK\n");
446 if (bits > 8) {
447 printf("Test #3: unsuccessful key lookup (%u-bit).\n", bits);
448 zerocount = 0;
449 for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
af3cbd45 450 struct cds_hlist_head head;
582a6ade
MD
451
452 rcu_read_lock();
453 head = cds_ja_lookup(test_ja, key + 42);
af3cbd45 454 if (!cds_hlist_empty(&head)) {
582a6ade
MD
455 fprintf(stderr,
456 "Error unexpected lookup node %" PRIu64 "\n",
457 key + 42);
458 assert(0);
459 }
460 rcu_read_unlock();
461 if (key == 0)
462 zerocount++;
463 }
464 printf("OK\n");
465 }
4d6ef45e
MD
466 printf("Test #4: remove keys (16-bit).\n");
467 zerocount = 0;
468 for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
469 struct cds_hlist_head head;
470 struct ja_test_node *node;
6fdf58e8
MD
471 struct cds_hlist_node *pos;
472 int count = 0;
4d6ef45e
MD
473
474 rcu_read_lock();
475 head = cds_ja_lookup(test_ja, key);
6fdf58e8
MD
476
477
478 cds_hlist_for_each_entry_rcu(node, pos, &head, node.list) {
479 struct cds_hlist_head testhead;
480
481 count++;
482 if (!node) {
483 fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
484 assert(0);
485 }
486 ret = cds_ja_del(test_ja, key, &node->node);
487 if (ret) {
488 fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key);
489 assert(0);
490 }
491 call_rcu(&node->node.head, free_node_cb);
492 testhead = cds_ja_lookup(test_ja, key);
493 if (count < nr_dup && cds_hlist_empty(&testhead)) {
494 fprintf(stderr, "Error: no node found after deletion of some nodes of a key\n");
495 assert(0);
496 }
4d6ef45e 497 }
4d6ef45e
MD
498 head = cds_ja_lookup(test_ja, key);
499 if (!cds_hlist_empty(&head)) {
500 fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, head.next);
501 assert(0);
502 }
503 rcu_read_unlock();
504 if (key == 0)
505 zerocount++;
506 }
507 printf("OK\n");
582a6ade 508
3d8fe307 509 ret = cds_ja_destroy(test_ja, free_node_cb);
582a6ade
MD
510 if (ret) {
511 fprintf(stderr, "Error destroying judy array\n");
512 return -1;
513 }
514 printf("Test ends\n");
515
516 return 0;
517}
518
1a0c0717
MD
519static
520int do_sanity_test(void)
521{
522 int i, j, ret;
582a6ade 523
1a0c0717
MD
524 printf("Sanity test start.\n");
525
526 for (i = 0; i < 3; i++) {
527 ret = test_8bit_key();
528 if (ret) {
529 return ret;
530 }
531 rcu_quiescent_state();
532 }
533 ret = test_16bit_key();
534 if (ret) {
535 return ret;
536 }
537 rcu_quiescent_state();
538
539 /* key bits */
540 for (i = 8; i <= 64; i *= 2) {
541 /* nr of nodes per key */
542 for (j = 1; j < 4; j++) {
543 ret = test_sparse_key(i, j);
544 if (ret) {
545 return ret;
546 }
547 rcu_quiescent_state();
548 }
549 }
550 printf("Sanity test end.\n");
551
552 return 0;
553}
554
555enum urcu_ja_addremove {
556 AR_RANDOM = 0,
557 AR_ADD = 1,
558 AR_REMOVE = -1,
559}; /* 1: add, -1 remove, 0: random */
560
561static enum urcu_ja_addremove addremove; /* 1: add, -1 remove, 0: random */
562
563static
564void test_ja_rw_sigusr1_handler(int signo)
565{
566 switch (addremove) {
567 case AR_ADD:
568 printf("Add/Remove: random.\n");
569 addremove = AR_RANDOM;
570 break;
571 case AR_RANDOM:
572 printf("Add/Remove: remove only.\n");
573 addremove = AR_REMOVE;
574 break;
575 case AR_REMOVE:
576 printf("Add/Remove: add only.\n");
577 addremove = AR_ADD;
578 break;
579 }
580}
581
582static
583void *test_ja_rw_thr_reader(void *_count)
584{
585 unsigned long long *count = _count;
586 struct cds_hlist_head head;
587 uint64_t key;
588
589 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
590 "reader", pthread_self(), (unsigned long) gettid());
591
592 set_affinity();
593
594 rcu_register_thread();
595
596 while (!test_go)
597 {
598 }
599 cmm_smp_mb();
600
601 for (;;) {
602 rcu_read_lock();
603
604 /* note: only looking up ulong keys */
605 key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % lookup_pool_size) + lookup_pool_offset;
4df4328c 606 key *= key_mul;
1a0c0717
MD
607 head = cds_ja_lookup(test_ja, key);
608 if (cds_hlist_empty(&head)) {
609 if (validate_lookup) {
610 printf("[ERROR] Lookup cannot find initial node.\n");
611 exit(-1);
612 }
613 URCU_TLS(lookup_fail)++;
614 } else {
615 URCU_TLS(lookup_ok)++;
616 }
756c692d 617 rcu_debug_yield_read();
1a0c0717
MD
618 if (caa_unlikely(rduration))
619 loop_sleep(rduration);
620 rcu_read_unlock();
621 URCU_TLS(nr_reads)++;
622 if (caa_unlikely(!test_duration_read()))
623 break;
624 if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0))
625 rcu_quiescent_state();
626 }
627
628 rcu_unregister_thread();
629
630 *count = URCU_TLS(nr_reads);
631 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
632 "reader", pthread_self(), (unsigned long) gettid());
633 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
634 pthread_self(), URCU_TLS(lookup_fail),
635 URCU_TLS(lookup_ok));
636 return ((void*)1);
637}
638
34ffee3e
MD
639static
640int is_add(void)
641{
642 return ((unsigned int) rand_r(&URCU_TLS(rand_lookup)) % 100) < add_ratio;
643}
644
1a0c0717
MD
645static
646void *test_ja_rw_thr_writer(void *_count)
647{
648 struct wr_count *count = _count;
649 struct cds_hlist_head head;
650 uint64_t key;
651 int ret;
652
653 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
654 "writer", pthread_self(), (unsigned long) gettid());
655
656 set_affinity();
657
658 rcu_register_thread();
659
660 while (!test_go)
661 {
662 }
663 cmm_smp_mb();
664
665 for (;;) {
34ffee3e
MD
666 if ((addremove == AR_ADD)
667 || (addremove == AR_RANDOM && is_add())) {
1a0c0717
MD
668 struct ja_test_node *node = malloc(sizeof(*node));
669
670 /* note: only inserting ulong keys */
671 key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset;
4df4328c 672 key *= key_mul;
1a0c0717
MD
673 ja_test_node_init(node, key);
674 rcu_read_lock();
675 ret = cds_ja_add(test_ja, key, &node->node);
676 URCU_TLS(nr_add)++;
677 rcu_read_unlock();
678 if (ret) {
679 fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
680 ret, key);
681 assert(0);
682 }
683 } else {
684 struct ja_test_node *node;
685
686 /* May delete */
687 /* note: only deleting ulong keys */
688 key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset;
4df4328c 689 key *= key_mul;
1a0c0717
MD
690
691 rcu_read_lock();
692
693 head = cds_ja_lookup(test_ja, key);
694 node = cds_hlist_first_entry_rcu(&head, struct ja_test_node, node.list);
695 if (node) {
696 ret = cds_ja_del(test_ja, key, &node->node);
d7069878 697 if (!ret) {
1a0c0717 698 call_rcu(&node->node.head, free_node_cb);
1a0c0717 699 URCU_TLS(nr_del)++;
d7069878
MD
700 } else {
701 URCU_TLS(nr_delnoent)++;
1a0c0717
MD
702 }
703 } else {
704 URCU_TLS(nr_delnoent)++;
705 }
6da38aec 706 rcu_read_unlock();
1a0c0717
MD
707 }
708
709 URCU_TLS(nr_writes)++;
710 if (caa_unlikely(!test_duration_write()))
711 break;
712 if (caa_unlikely(wdelay))
713 loop_sleep(wdelay);
714 if (caa_unlikely((URCU_TLS(nr_writes) & ((1 << 10) - 1)) == 0))
715 rcu_quiescent_state();
716 }
717
718 rcu_unregister_thread();
719
720 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
721 "writer", pthread_self(), (unsigned long) gettid());
722 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
723 "nr_delnoent %lu\n", pthread_self(), URCU_TLS(nr_add),
724 URCU_TLS(nr_addexist), URCU_TLS(nr_del),
725 URCU_TLS(nr_delnoent));
726 count->update_ops = URCU_TLS(nr_writes);
727 count->add = URCU_TLS(nr_add);
728 count->add_exist = URCU_TLS(nr_addexist);
729 count->remove = URCU_TLS(nr_del);
730 return ((void*)2);
731}
732
733static
734int do_mt_populate_ja(void)
735{
736 struct cds_hlist_head head;
4df4328c 737 uint64_t iter;
1a0c0717
MD
738 int ret;
739
740 if (!init_populate)
741 return 0;
742
743 printf("Starting rw test\n");
744
4df4328c 745 for (iter = init_pool_offset; iter < init_pool_offset + init_pool_size; iter++) {
1a0c0717 746 struct ja_test_node *node = malloc(sizeof(*node));
4df4328c 747 uint64_t key;
1a0c0717
MD
748
749 /* note: only inserting ulong keys */
4df4328c
MD
750 key = (unsigned long) iter;
751 key *= key_mul;
1a0c0717
MD
752 ja_test_node_init(node, key);
753 rcu_read_lock();
754 ret = cds_ja_add(test_ja, key, &node->node);
755 URCU_TLS(nr_add)++;
756 URCU_TLS(nr_writes)++;
757 rcu_read_unlock();
758 if (ret) {
759 fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
760 ret, key);
761 assert(0);
762 }
763 }
764 return 0;
765}
766
767static
768int do_mt_test(void)
44151f89 769{
44151f89
MD
770 pthread_t *tid_reader, *tid_writer;
771 void *tret;
1a0c0717 772 int ret, i, err;
44151f89
MD
773 unsigned long long *count_reader;
774 struct wr_count *count_writer;
775 unsigned long long tot_reads = 0, tot_writes = 0,
776 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
44151f89 777 unsigned int remain;
1a0c0717
MD
778
779 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
780 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
781 count_reader = malloc(sizeof(*count_reader) * nr_readers);
782 count_writer = malloc(sizeof(*count_writer) * nr_writers);
783
784 printf("Allocating Judy Array for %u-bit keys\n", key_bits);
785 test_ja = cds_ja_new(key_bits);
786 if (!test_ja) {
787 printf("Error allocating judy array.\n");
788 ret = -1;
789 goto end;
790 }
791
792 do_mt_populate_ja();
793
794 next_aff = 0;
795
796 for (i = 0; i < nr_readers; i++) {
797 err = pthread_create(&tid_reader[i],
798 NULL, test_ja_rw_thr_reader,
799 &count_reader[i]);
800 if (err != 0)
801 exit(1);
802 }
803 for (i = 0; i < nr_writers; i++) {
804 err = pthread_create(&tid_writer[i],
805 NULL, test_ja_rw_thr_writer,
806 &count_writer[i]);
807 if (err != 0)
808 exit(1);
809 }
810
811 cmm_smp_mb();
812
813 test_go = 1;
814
815 rcu_thread_offline_qsbr();
816
817 remain = duration;
818 do {
819 remain = sleep(remain);
820 } while (remain > 0);
821
822 test_stop = 1;
823
824 for (i = 0; i < nr_readers; i++) {
825 err = pthread_join(tid_reader[i], &tret);
826 if (err != 0)
827 exit(1);
828 tot_reads += count_reader[i];
829 }
830 for (i = 0; i < nr_writers; i++) {
831 err = pthread_join(tid_writer[i], &tret);
832 if (err != 0)
833 exit(1);
834 tot_writes += count_writer[i].update_ops;
835 tot_add += count_writer[i].add;
836 tot_add_exist += count_writer[i].add_exist;
837 tot_remove += count_writer[i].remove;
838 }
88e115c3 839 rcu_thread_online_qsbr();
1a0c0717
MD
840
841 ret = cds_ja_destroy(test_ja, free_node_cb);
842 if (ret) {
843 fprintf(stderr, "Error destroying judy array\n");
844 goto end;
845 }
1a0c0717
MD
846
847 free(tid_reader);
848 free(tid_writer);
849 free(count_reader);
850 free(count_writer);
851 ret = 0;
852end:
853 return ret;
854}
855
856int main(int argc, char **argv)
857{
858 int i, j, a, ret, err;
a2a7ff59 859 uint64_t key;
1a0c0717 860 struct sigaction act;
44151f89
MD
861
862 if (argc < 4) {
863 show_usage(argc, argv);
864 return -1;
865 }
866
867 err = sscanf(argv[1], "%u", &nr_readers);
868 if (err != 1) {
869 show_usage(argc, argv);
870 return -1;
871 }
872
873 err = sscanf(argv[2], "%u", &nr_writers);
874 if (err != 1) {
875 show_usage(argc, argv);
876 return -1;
877 }
878
879 err = sscanf(argv[3], "%lu", &duration);
880 if (err != 1) {
881 show_usage(argc, argv);
882 return -1;
883 }
884
885 for (i = 4; i < argc; i++) {
886 if (argv[i][0] != '-')
887 continue;
888 switch (argv[i][1]) {
889#ifdef DEBUG_YIELD
890 case 'r':
891 yield_active |= YIELD_READ;
892 break;
893 case 'w':
894 yield_active |= YIELD_WRITE;
895 break;
896#endif
897 case 'a':
898 if (argc < i + 2) {
899 show_usage(argc, argv);
900 return -1;
901 }
902 a = atoi(argv[++i]);
903 cpu_affinities[next_aff++] = a;
904 use_affinity = 1;
905 printf_verbose("Adding CPU %d affinity\n", a);
906 break;
907 case 'c':
908 if (argc < i + 2) {
909 show_usage(argc, argv);
910 return -1;
911 }
912 rduration = atol(argv[++i]);
913 break;
914 case 'd':
915 if (argc < i + 2) {
916 show_usage(argc, argv);
917 return -1;
918 }
919 wdelay = atol(argv[++i]);
920 break;
921 case 'v':
922 verbose_mode = 1;
923 break;
34ffee3e
MD
924 case 'r':
925 add_ratio = atoi(argv[++i]);
44151f89
MD
926 break;
927 case 'k':
46f373a6 928 init_populate = 1;
44151f89
MD
929 break;
930 case 'R':
931 lookup_pool_offset = atol(argv[++i]);
932 break;
933 case 'S':
934 write_pool_offset = atol(argv[++i]);
935 break;
936 case 'T':
937 init_pool_offset = atol(argv[++i]);
938 break;
939 case 'M':
940 lookup_pool_size = atol(argv[++i]);
941 break;
942 case 'N':
943 write_pool_size = atol(argv[++i]);
944 break;
945 case 'O':
946 init_pool_size = atol(argv[++i]);
947 break;
948 case 'V':
949 validate_lookup = 1;
950 break;
1a0c0717
MD
951 case 's':
952 sanity_test = 1;
953 break;
954 case 'B':
955 key_bits = atol(argv[++i]);
956 break;
4df4328c
MD
957 case 'm':
958 key_mul = atoll(argv[++i]);
959 break;
44151f89
MD
960 }
961 }
962
963 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
964 duration, nr_readers, nr_writers);
965 printf_verbose("Writer delay : %lu loops.\n", wdelay);
966 printf_verbose("Reader duration : %lu loops.\n", rduration);
34ffee3e 967 printf_verbose("Add ratio: %u%%.\n", add_ratio);
b31cb285 968 printf_verbose("Key multiplication factor: %" PRIu64 ".\n", key_mul);
44151f89
MD
969 printf_verbose("Init pool size offset %lu size %lu.\n",
970 init_pool_offset, init_pool_size);
971 printf_verbose("Lookup pool size offset %lu size %lu.\n",
972 lookup_pool_offset, lookup_pool_size);
973 printf_verbose("Update pool size offset %lu size %lu.\n",
974 write_pool_offset, write_pool_size);
975 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
976 "main", pthread_self(), (unsigned long)gettid());
977
1a0c0717
MD
978 memset(&act, 0, sizeof(act));
979 ret = sigemptyset(&act.sa_mask);
980 if (ret == -1) {
981 perror("sigemptyset");
982 return -1;
983 }
984 act.sa_handler = test_ja_rw_sigusr1_handler;
985 act.sa_flags = SA_RESTART;
986 ret = sigaction(SIGUSR1, &act, NULL);
987 if (ret == -1) {
988 perror("sigaction");
989 return -1;
990 }
44151f89
MD
991
992 err = create_all_cpu_call_rcu_data(0);
993 if (err) {
994 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
995 }
996
582a6ade 997 rcu_register_thread();
44151f89 998
1a0c0717
MD
999 if (sanity_test) {
1000 ret = do_sanity_test();
44151f89 1001 if (ret) {
1a0c0717 1002 fprintf(stderr, "Error in sanity test\n");
44151f89 1003 }
1a0c0717
MD
1004 } else {
1005 do_mt_test();
44151f89 1006 }
a2a7ff59 1007
582a6ade 1008 rcu_unregister_thread();
1a0c0717 1009 free_all_cpu_call_rcu_data();
44151f89
MD
1010 return 0;
1011
1012#if 0
1013 /*
1014 * Hash Population needs to be seen as a RCU reader
1015 * thread from the point of view of resize.
1016 */
1017 rcu_register_thread();
1018 ret = (get_populate_hash_cb())();
1019 assert(!ret);
1020
1021 rcu_thread_offline();
1022
44151f89
MD
1023 /* teardown counter thread */
1024 act.sa_handler = SIG_IGN;
1025 act.sa_flags = SA_RESTART;
1026 ret = sigaction(SIGUSR2, &act, NULL);
1027 if (ret == -1) {
1028 perror("sigaction");
1029 return -1;
1030 }
1031 {
1032 char msg[1] = { 0x42 };
1033 ssize_t ret;
1034
1035 do {
1036 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
1037 } while (ret == -1L && errno == EINTR);
1038 }
1039
1040 fflush(stdout);
1041 rcu_thread_online();
1042 rcu_read_lock();
1043 printf("Counting nodes... ");
1044 cds_lfht_count_nodes(test_ht, &approx_before, &count, &approx_after);
1045 printf("done.\n");
1046 test_delete_all_nodes(test_ht);
1047 rcu_read_unlock();
1048 rcu_thread_offline();
1049 if (count) {
1050 printf("Approximation before node accounting: %ld nodes.\n",
1051 approx_before);
1052 printf("Nodes deleted from hash table before destroy: "
1053 "%lu nodes.\n",
1054 count);
1055 printf("Approximation after node accounting: %ld nodes.\n",
1056 approx_after);
1057 }
1058 ret = cds_lfht_destroy(test_ht, NULL);
1059 if (ret)
1060 printf_verbose("final delete aborted\n");
1061 else
1062 printf_verbose("final delete success\n");
1063 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
1064 tot_writes);
1065 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
1066 "nr_writers %3u "
1067 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
1068 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
1069 argv[0], duration, nr_readers, rduration,
1070 nr_writers, wdelay, tot_reads, tot_writes,
1071 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
1072 (long long) tot_add + init_populate - tot_remove - count);
1073 rcu_unregister_thread();
44151f89
MD
1074#endif
1075 return 0;
1076}
This page took 0.068306 seconds and 4 git commands to generate.