hlist: implement cds_hlist_first_entry_rcu
[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;
55
56int count_pipe[2];
57
58int verbose_mode;
59
60unsigned int cpu_affinities[NR_CPUS];
61unsigned int next_aff = 0;
62int use_affinity = 0;
63
64pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
65
66DEFINE_URCU_TLS(unsigned long long, nr_writes);
67DEFINE_URCU_TLS(unsigned long long, nr_reads);
68
69unsigned int nr_readers;
70unsigned int nr_writers;
71
72static pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
73
74void set_affinity(void)
75{
76 cpu_set_t mask;
77 int cpu;
78 int ret;
79
80 if (!use_affinity)
81 return;
82
83#if HAVE_SCHED_SETAFFINITY
84 ret = pthread_mutex_lock(&affinity_mutex);
85 if (ret) {
86 perror("Error in pthread mutex lock");
87 exit(-1);
88 }
89 cpu = cpu_affinities[next_aff++];
90 ret = pthread_mutex_unlock(&affinity_mutex);
91 if (ret) {
92 perror("Error in pthread mutex unlock");
93 exit(-1);
94 }
95 CPU_ZERO(&mask);
96 CPU_SET(cpu, &mask);
97#if SCHED_SETAFFINITY_ARGS == 2
98 sched_setaffinity(0, &mask);
99#else
100 sched_setaffinity(0, sizeof(mask), &mask);
101#endif
102#endif /* HAVE_SCHED_SETAFFINITY */
103}
104
105void rcu_copy_mutex_lock(void)
106{
107 int ret;
108 ret = pthread_mutex_lock(&rcu_copy_mutex);
109 if (ret) {
110 perror("Error in pthread mutex lock");
111 exit(-1);
112 }
113}
114
115void rcu_copy_mutex_unlock(void)
116{
117 int ret;
118
119 ret = pthread_mutex_unlock(&rcu_copy_mutex);
120 if (ret) {
121 perror("Error in pthread mutex unlock");
122 exit(-1);
123 }
124}
125
126#if 0
127void free_node_cb(struct rcu_head *head)
128{
129 struct ja_test_node *node =
130 caa_container_of(head, struct ja_test_node, head);
131 free(node);
132}
133
134static
135void test_delete_all_nodes(struct cds_lfht *ht)
136{
137 struct cds_lfht_iter iter;
138 struct lfht_test_node *node;
139 unsigned long count = 0;
140
141 cds_lfht_for_each_entry(ht, &iter, node, node) {
142 int ret;
143
144 ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
145 assert(!ret);
146 call_rcu(&node->head, free_node_cb);
147 count++;
148 }
149 printf("deleted %lu nodes.\n", count);
150}
151#endif
152
153void show_usage(int argc, char **argv)
154{
155 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]);
156#ifdef DEBUG_YIELD
157 printf(" [-r] [-w] (yield reader and/or writer)\n");
158#endif
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");
163printf(" [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");
173 printf("\n\n");
174}
175
582a6ade
MD
176
177static
178int test_8bit_key(void)
179{
180 int ret;
181 uint64_t key;
182
183 /* Test with 8-bit key */
184 test_ja = cds_ja_new(8);
185 if (!test_ja) {
186 printf("Error allocating judy array.\n");
187 return -1;
188 }
189
190 /* Add keys */
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);
195
196 ja_test_node_init(node, key);
197 rcu_read_lock();
198 ret = cds_ja_add(test_ja, key, &node->node);
199 rcu_read_unlock();
200 if (ret) {
201 fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
202 ret, key);
203 assert(0);
204 }
205 }
206 printf("OK\n");
207
208 printf("Test #2: successful key lookup (8-bit).\n");
209 for (key = 0; key < 200; key++) {
af3cbd45 210 struct cds_hlist_head head;
582a6ade
MD
211
212 rcu_read_lock();
213 head = cds_ja_lookup(test_ja, key);
af3cbd45 214 if (cds_hlist_empty(&head)) {
582a6ade
MD
215 fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
216 assert(0);
217 }
218 rcu_read_unlock();
219 }
220 printf("OK\n");
221 printf("Test #3: unsuccessful key lookup (8-bit).\n");
222 for (key = 200; key < 240; key++) {
af3cbd45 223 struct cds_hlist_head head;
582a6ade
MD
224
225 rcu_read_lock();
226 head = cds_ja_lookup(test_ja, key);
af3cbd45 227 if (!cds_hlist_empty(&head)) {
582a6ade
MD
228 fprintf(stderr,
229 "Error unexpected lookup node %" PRIu64 "\n",
230 key);
231 assert(0);
232 }
233 rcu_read_unlock();
234 }
235 printf("OK\n");
236
237 ret = cds_ja_destroy(test_ja);
238 if (ret) {
239 fprintf(stderr, "Error destroying judy array\n");
240 return -1;
241 }
242 return 0;
243}
244
245static
246int test_16bit_key(void)
247{
248 int ret;
249 uint64_t key;
250
251 /* Test with 16-bit key */
252 test_ja = cds_ja_new(16);
253 if (!test_ja) {
254 printf("Error allocating judy array.\n");
255 return -1;
256 }
257
258 /* Add keys */
259 printf("Test #1: add keys (16-bit).\n");
260 //for (key = 0; key < 10000; key++) {
261 for (key = 0; key < 65536; key+=256) {
262 struct ja_test_node *node =
263 calloc(sizeof(*node), 1);
264
265 ja_test_node_init(node, key);
266 rcu_read_lock();
267 ret = cds_ja_add(test_ja, key, &node->node);
268 rcu_read_unlock();
269 if (ret) {
270 fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
271 ret, key);
272 assert(0);
273 }
274 }
275 printf("OK\n");
276
277 printf("Test #2: successful key lookup (16-bit).\n");
278 //for (key = 0; key < 10000; key++) {
279 for (key = 0; key < 65536; key+=256) {
af3cbd45 280 struct cds_hlist_head head;
582a6ade
MD
281
282 rcu_read_lock();
283 head = cds_ja_lookup(test_ja, key);
af3cbd45 284 if (cds_hlist_empty(&head)) {
582a6ade
MD
285 fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
286 assert(0);
287 }
288 rcu_read_unlock();
289 }
290 printf("OK\n");
291 printf("Test #3: unsuccessful key lookup (16-bit).\n");
292 for (key = 11000; key <= 11002; key++) {
af3cbd45 293 struct cds_hlist_head head;
582a6ade
MD
294
295 rcu_read_lock();
296 head = cds_ja_lookup(test_ja, key);
af3cbd45 297 if (!cds_hlist_empty(&head)) {
582a6ade
MD
298 fprintf(stderr,
299 "Error unexpected lookup node %" PRIu64 "\n",
300 key);
301 assert(0);
302 }
303 rcu_read_unlock();
304 }
305 printf("OK\n");
306
307 ret = cds_ja_destroy(test_ja);
308 if (ret) {
309 fprintf(stderr, "Error destroying judy array\n");
310 return -1;
311 }
312 return 0;
313}
314
315static
316int test_sparse_key(unsigned int bits)
317{
318 int ret;
319 uint64_t key, max_key;
320 int zerocount;
321
322 if (bits == 64)
323 max_key = UINT64_MAX;
324 else
325 max_key = (1ULL << bits) - 1;
326
327 printf("Sparse key test begins for %u-bit keys\n", bits);
328 /* Test with 16-bit key */
329 test_ja = cds_ja_new(bits);
330 if (!test_ja) {
331 printf("Error allocating judy array.\n");
332 return -1;
333 }
334
335 /* Add keys */
336 printf("Test #1: add keys (%u-bit).\n", bits);
337 zerocount = 0;
338 for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
339 struct ja_test_node *node =
340 calloc(sizeof(*node), 1);
341
342 ja_test_node_init(node, key);
343 rcu_read_lock();
344 ret = cds_ja_add(test_ja, key, &node->node);
345 rcu_read_unlock();
346 if (ret) {
347 fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
348 ret, key);
349 assert(0);
350 }
351 if (key == 0)
352 zerocount++;
353 }
354 printf("OK\n");
355
356 printf("Test #2: successful key lookup (%u-bit).\n", bits);
357 zerocount = 0;
358 for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
af3cbd45 359 struct cds_hlist_head head;
582a6ade
MD
360
361 rcu_read_lock();
362 head = cds_ja_lookup(test_ja, key);
af3cbd45 363 if (cds_hlist_empty(&head)) {
582a6ade
MD
364 fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
365 assert(0);
366 }
367 rcu_read_unlock();
368 if (key == 0)
369 zerocount++;
370 }
371 printf("OK\n");
372 if (bits > 8) {
373 printf("Test #3: unsuccessful key lookup (%u-bit).\n", bits);
374 zerocount = 0;
375 for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
af3cbd45 376 struct cds_hlist_head head;
582a6ade
MD
377
378 rcu_read_lock();
379 head = cds_ja_lookup(test_ja, key + 42);
af3cbd45 380 if (!cds_hlist_empty(&head)) {
582a6ade
MD
381 fprintf(stderr,
382 "Error unexpected lookup node %" PRIu64 "\n",
383 key + 42);
384 assert(0);
385 }
386 rcu_read_unlock();
387 if (key == 0)
388 zerocount++;
389 }
390 printf("OK\n");
391 }
392
393 ret = cds_ja_destroy(test_ja);
394 if (ret) {
395 fprintf(stderr, "Error destroying judy array\n");
396 return -1;
397 }
398 printf("Test ends\n");
399
400 return 0;
401}
402
403
44151f89
MD
404int main(int argc, char **argv)
405{
406 int err;
407 pthread_t *tid_reader, *tid_writer;
408 void *tret;
409 unsigned long long *count_reader;
410 struct wr_count *count_writer;
411 unsigned long long tot_reads = 0, tot_writes = 0,
412 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
413 int i, a, ret;
414 unsigned int remain;
a2a7ff59 415 uint64_t key;
44151f89
MD
416
417 if (argc < 4) {
418 show_usage(argc, argv);
419 return -1;
420 }
421
422 err = sscanf(argv[1], "%u", &nr_readers);
423 if (err != 1) {
424 show_usage(argc, argv);
425 return -1;
426 }
427
428 err = sscanf(argv[2], "%u", &nr_writers);
429 if (err != 1) {
430 show_usage(argc, argv);
431 return -1;
432 }
433
434 err = sscanf(argv[3], "%lu", &duration);
435 if (err != 1) {
436 show_usage(argc, argv);
437 return -1;
438 }
439
440 for (i = 4; i < argc; i++) {
441 if (argv[i][0] != '-')
442 continue;
443 switch (argv[i][1]) {
444#ifdef DEBUG_YIELD
445 case 'r':
446 yield_active |= YIELD_READ;
447 break;
448 case 'w':
449 yield_active |= YIELD_WRITE;
450 break;
451#endif
452 case 'a':
453 if (argc < i + 2) {
454 show_usage(argc, argv);
455 return -1;
456 }
457 a = atoi(argv[++i]);
458 cpu_affinities[next_aff++] = a;
459 use_affinity = 1;
460 printf_verbose("Adding CPU %d affinity\n", a);
461 break;
462 case 'c':
463 if (argc < i + 2) {
464 show_usage(argc, argv);
465 return -1;
466 }
467 rduration = atol(argv[++i]);
468 break;
469 case 'd':
470 if (argc < i + 2) {
471 show_usage(argc, argv);
472 return -1;
473 }
474 wdelay = atol(argv[++i]);
475 break;
476 case 'v':
477 verbose_mode = 1;
478 break;
479 case 'i':
480 add_only = 1;
481 break;
482 case 'k':
483 init_populate = atol(argv[++i]);
484 break;
485 case 'R':
486 lookup_pool_offset = atol(argv[++i]);
487 break;
488 case 'S':
489 write_pool_offset = atol(argv[++i]);
490 break;
491 case 'T':
492 init_pool_offset = atol(argv[++i]);
493 break;
494 case 'M':
495 lookup_pool_size = atol(argv[++i]);
496 break;
497 case 'N':
498 write_pool_size = atol(argv[++i]);
499 break;
500 case 'O':
501 init_pool_size = atol(argv[++i]);
502 break;
503 case 'V':
504 validate_lookup = 1;
505 break;
506 }
507 }
508
509 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
510 duration, nr_readers, nr_writers);
511 printf_verbose("Writer delay : %lu loops.\n", wdelay);
512 printf_verbose("Reader duration : %lu loops.\n", rduration);
513 printf_verbose("Mode:%s.\n",
514 add_only ? " add only" : " add/delete");
515 printf_verbose("Init pool size offset %lu size %lu.\n",
516 init_pool_offset, init_pool_size);
517 printf_verbose("Lookup pool size offset %lu size %lu.\n",
518 lookup_pool_offset, lookup_pool_size);
519 printf_verbose("Update pool size offset %lu size %lu.\n",
520 write_pool_offset, write_pool_size);
521 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
522 "main", pthread_self(), (unsigned long)gettid());
523
524 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
525 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
526 count_reader = malloc(sizeof(*count_reader) * nr_readers);
527 count_writer = malloc(sizeof(*count_writer) * nr_writers);
528
529 err = create_all_cpu_call_rcu_data(0);
530 if (err) {
531 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
532 }
533
582a6ade 534 rcu_register_thread();
44151f89 535
582a6ade 536 printf("Test start.\n");
44151f89 537
582a6ade
MD
538 for (i = 0; i < 3; i++) {
539 ret = test_8bit_key();
44151f89 540 if (ret) {
582a6ade 541 return ret;
44151f89 542 }
582a6ade 543 rcu_quiescent_state();
44151f89 544 }
582a6ade
MD
545 ret = test_16bit_key();
546 if (ret) {
547 return ret;
548 }
549 rcu_quiescent_state();
a2a7ff59 550
582a6ade
MD
551 ret = test_sparse_key(8);
552 if (ret) {
553 return ret;
554 }
555 rcu_quiescent_state();
a2a7ff59 556
582a6ade
MD
557 ret = test_sparse_key(16);
558 if (ret) {
559 return ret;
a2a7ff59 560 }
582a6ade 561 rcu_quiescent_state();
a2a7ff59 562
582a6ade
MD
563 ret = test_sparse_key(32);
564 if (ret) {
565 return ret;
a2a7ff59 566 }
582a6ade 567 rcu_quiescent_state();
44151f89 568
582a6ade 569 ret = test_sparse_key(64);
44151f89 570 if (ret) {
582a6ade 571 return ret;
44151f89 572 }
582a6ade
MD
573 rcu_quiescent_state();
574
44151f89 575 printf("Test end.\n");
582a6ade 576 rcu_unregister_thread();
44151f89
MD
577 return 0;
578
579#if 0
580 /*
581 * Hash Population needs to be seen as a RCU reader
582 * thread from the point of view of resize.
583 */
584 rcu_register_thread();
585 ret = (get_populate_hash_cb())();
586 assert(!ret);
587
588 rcu_thread_offline();
589
590 next_aff = 0;
591
592 for (i = 0; i < nr_readers; i++) {
593 err = pthread_create(&tid_reader[i],
594 NULL, get_thr_reader_cb(),
595 &count_reader[i]);
596 if (err != 0)
597 exit(1);
598 }
599 for (i = 0; i < nr_writers; i++) {
600 err = pthread_create(&tid_writer[i],
601 NULL, get_thr_writer_cb(),
602 &count_writer[i]);
603 if (err != 0)
604 exit(1);
605 }
606
607 cmm_smp_mb();
608
609 test_go = 1;
610
611 remain = duration;
612 do {
613 remain = sleep(remain);
614 } while (remain > 0);
615
616 test_stop = 1;
617
618 for (i = 0; i < nr_readers; i++) {
619 err = pthread_join(tid_reader[i], &tret);
620 if (err != 0)
621 exit(1);
622 tot_reads += count_reader[i];
623 }
624 for (i = 0; i < nr_writers; i++) {
625 err = pthread_join(tid_writer[i], &tret);
626 if (err != 0)
627 exit(1);
628 tot_writes += count_writer[i].update_ops;
629 tot_add += count_writer[i].add;
630 tot_add_exist += count_writer[i].add_exist;
631 tot_remove += count_writer[i].remove;
632 }
633
634 /* teardown counter thread */
635 act.sa_handler = SIG_IGN;
636 act.sa_flags = SA_RESTART;
637 ret = sigaction(SIGUSR2, &act, NULL);
638 if (ret == -1) {
639 perror("sigaction");
640 return -1;
641 }
642 {
643 char msg[1] = { 0x42 };
644 ssize_t ret;
645
646 do {
647 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
648 } while (ret == -1L && errno == EINTR);
649 }
650
651 fflush(stdout);
652 rcu_thread_online();
653 rcu_read_lock();
654 printf("Counting nodes... ");
655 cds_lfht_count_nodes(test_ht, &approx_before, &count, &approx_after);
656 printf("done.\n");
657 test_delete_all_nodes(test_ht);
658 rcu_read_unlock();
659 rcu_thread_offline();
660 if (count) {
661 printf("Approximation before node accounting: %ld nodes.\n",
662 approx_before);
663 printf("Nodes deleted from hash table before destroy: "
664 "%lu nodes.\n",
665 count);
666 printf("Approximation after node accounting: %ld nodes.\n",
667 approx_after);
668 }
669 ret = cds_lfht_destroy(test_ht, NULL);
670 if (ret)
671 printf_verbose("final delete aborted\n");
672 else
673 printf_verbose("final delete success\n");
674 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
675 tot_writes);
676 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
677 "nr_writers %3u "
678 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
679 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
680 argv[0], duration, nr_readers, rduration,
681 nr_writers, wdelay, tot_reads, tot_writes,
682 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
683 (long long) tot_add + init_populate - tot_remove - count);
684 rcu_unregister_thread();
685 free_all_cpu_call_rcu_data();
686 free(tid_reader);
687 free(tid_writer);
688 free(count_reader);
689 free(count_writer);
690#endif
691 return 0;
692}
This page took 0.047728 seconds and 4 git commands to generate.