rbtree test: add missing call_rcu per-cpu threads teardown
[userspace-rcu.git] / tests / test_urcu_rbtree.c
1 /*
2 * test_urcu_rbtree.c
3 *
4 * Userspace RCU library - test program for RB tree
5 *
6 * Copyright February 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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 #ifndef DYNAMIC_LINK_TEST
25 #define _LGPL_SOURCE
26 #else
27 #define debug_yield_read()
28 #endif
29 #include "../config.h"
30 #include <stdio.h>
31 #include <pthread.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <assert.h>
39 #include <sys/syscall.h>
40 #include <sched.h>
41 #include <errno.h>
42 #include <time.h>
43
44 #include <urcu/arch.h>
45
46 extern int __thread disable_debug;
47
48 /* hardcoded number of CPUs */
49 #define NR_CPUS 16384
50
51 /* Default number of insert/delete */
52 #define DEFAULT_NR_RAND 6
53
54 /* Default number of global items (used by readers for lookups) */
55 #define DEFAULT_NR_GLOBAL 10
56
57 #if defined(_syscall0)
58 _syscall0(pid_t, gettid)
59 #elif defined(__NR_gettid)
60 static inline pid_t gettid(void)
61 {
62 return syscall(__NR_gettid);
63 }
64 #else
65 #warning "use pid as tid"
66 static inline pid_t gettid(void)
67 {
68 return getpid();
69 }
70 #endif
71
72 #include <urcu.h>
73 #include <urcu/rcurbtree.h>
74 #include <urcu-defer.h>
75
76 int tree_comp(void *a, void *b)
77 {
78 if ((unsigned long)a < (unsigned long)b)
79 return -1;
80 else if ((unsigned long)a > (unsigned long)b)
81 return 1;
82 else
83 return 0;
84 }
85
86 static DEFINE_RCU_RBTREE(rbtree, tree_comp, malloc, free, call_rcu);
87
88 static volatile int test_go, test_stop;
89
90 static unsigned long wdelay;
91
92 static unsigned long duration;
93
94 /* read-side C.S. duration, in loops */
95 static unsigned long rduration;
96
97 /* write-side C.S. duration, in loops */
98 static unsigned long wduration;
99
100 static unsigned long nr_rand_items = DEFAULT_NR_RAND;
101
102 static int opt_search_begin,
103 opt_search_bottom,
104 opt_search_end,
105 opt_search_mid,
106 opt_iter_min_max,
107 opt_iter_max_min,
108 opt_benchmark;
109
110 static inline void loop_sleep(unsigned long l)
111 {
112 while (l-- != 0)
113 caa_cpu_relax();
114 }
115
116 static int verbose_mode;
117
118 #define printf_verbose(fmt, args...) \
119 do { \
120 if (verbose_mode) \
121 printf(fmt, args); \
122 } while (0)
123
124 static unsigned int cpu_affinities[NR_CPUS];
125 static unsigned int next_aff = 0;
126 static int use_affinity = 0;
127
128 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
129
130 #ifndef HAVE_CPU_SET_T
131 typedef unsigned long cpu_set_t;
132 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
133 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
134 #endif
135
136 static void set_affinity(void)
137 {
138 cpu_set_t mask;
139 int cpu;
140 int ret;
141
142 if (!use_affinity)
143 return;
144
145 #if HAVE_SCHED_SETAFFINITY
146 ret = pthread_mutex_lock(&affinity_mutex);
147 if (ret) {
148 perror("Error in pthread mutex lock");
149 exit(-1);
150 }
151 cpu = cpu_affinities[next_aff++];
152 ret = pthread_mutex_unlock(&affinity_mutex);
153 if (ret) {
154 perror("Error in pthread mutex unlock");
155 exit(-1);
156 }
157
158 CPU_ZERO(&mask);
159 CPU_SET(cpu, &mask);
160 #if SCHED_SETAFFINITY_ARGS == 2
161 sched_setaffinity(0, &mask);
162 #else
163 sched_setaffinity(0, sizeof(mask), &mask);
164 #endif
165 #endif /* HAVE_SCHED_SETAFFINITY */
166 }
167
168 /*
169 * returns 0 if test should end.
170 */
171 static int test_duration_write(void)
172 {
173 return !test_stop;
174 }
175
176 static int test_duration_read(void)
177 {
178 return !test_stop;
179 }
180
181 static unsigned long long __thread nr_writes;
182 static unsigned long long __thread nr_reads;
183
184 static unsigned int nr_readers;
185 static unsigned int nr_writers;
186
187 static unsigned long global_items = DEFAULT_NR_GLOBAL;
188 static void **global_key = NULL;
189
190 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
191
192 void rcu_copy_mutex_lock(void)
193 {
194 int ret;
195 ret = pthread_mutex_lock(&rcu_copy_mutex);
196 if (ret) {
197 perror("Error in pthread mutex lock");
198 exit(-1);
199 }
200 }
201
202 void rcu_copy_mutex_unlock(void)
203 {
204 int ret;
205
206 ret = pthread_mutex_unlock(&rcu_copy_mutex);
207 if (ret) {
208 perror("Error in pthread mutex unlock");
209 exit(-1);
210 }
211 }
212
213 static
214 void set_lookup_index(struct rcu_rbtree_node *node,
215 char *lookup_hit)
216 {
217 int i;
218
219 for (i = 0; i < global_items; i++) {
220 if (node->begin == global_key[i]
221 && !lookup_hit[i]) {
222 lookup_hit[i] = 1;
223 break;
224 }
225 }
226 }
227
228 void *thr_reader(void *_count)
229 {
230 unsigned long long *count = _count;
231 struct rcu_rbtree_node *node;
232 int i, index;
233 char *lookup_hit;
234
235 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
236 "reader", pthread_self(), (unsigned long)gettid());
237
238 set_affinity();
239
240 rcu_register_thread();
241
242 lookup_hit = malloc(sizeof(*lookup_hit) * global_items);
243
244 while (!test_go)
245 {
246 }
247 cmm_smp_mb();
248
249 for (;;) {
250 /* search begin key */
251 if (opt_search_begin) {
252 for (i = 0; i < global_items; i++) {
253 rcu_read_lock();
254 node = rcu_rbtree_search_begin_key(&rbtree,
255 rcu_dereference(rbtree.root),
256 global_key[i]);
257 assert(!rcu_rbtree_is_nil(&rbtree, node));
258 rcu_read_unlock();
259 nr_reads++;
260 }
261 }
262
263 /* search bottom of range */
264 if (opt_search_bottom) {
265 for (i = 0; i < global_items; i++) {
266 rcu_read_lock();
267 node = rcu_rbtree_search(&rbtree,
268 rcu_dereference(rbtree.root),
269 global_key[i]);
270 assert(!rcu_rbtree_is_nil(&rbtree, node));
271 rcu_read_unlock();
272 nr_reads++;
273 }
274 }
275
276 /* search end of range */
277 if (opt_search_end) {
278 for (i = 0; i < global_items; i++) {
279 rcu_read_lock();
280 node = rcu_rbtree_search(&rbtree,
281 rcu_dereference(rbtree.root),
282 (void*) ((unsigned long) global_key[i] + 3));
283 assert(!rcu_rbtree_is_nil(&rbtree, node));
284 rcu_read_unlock();
285 nr_reads++;
286 }
287 }
288
289 /* search range (middle) */
290 if (opt_search_mid) {
291 for (i = 0; i < global_items; i++) {
292 rcu_read_lock();
293 node = rcu_rbtree_search_range(&rbtree,
294 rcu_dereference(rbtree.root),
295 (void*) ((unsigned long) global_key[i] + 1),
296 (void*) ((unsigned long) global_key[i] + 2));
297 assert(!rcu_rbtree_is_nil(&rbtree, node));
298 rcu_read_unlock();
299 nr_reads++;
300 }
301 }
302
303 /* min + next */
304 if (opt_iter_min_max) {
305 memset(lookup_hit, 0, sizeof(*lookup_hit) * global_items);
306
307 rcu_read_lock();
308 node = rcu_rbtree_min(&rbtree,
309 rcu_dereference(rbtree.root));
310 while (!rcu_rbtree_is_nil(&rbtree, node)) {
311 if (!opt_benchmark)
312 set_lookup_index(node, lookup_hit);
313 node = rcu_rbtree_next(&rbtree, node);
314 nr_reads++;
315 }
316 rcu_read_unlock();
317
318 if (!opt_benchmark) {
319 for (i = 0; i < global_items; i++)
320 assert(lookup_hit[i]);
321 }
322 }
323
324 /* max + prev */
325 if (opt_iter_max_min) {
326 memset(lookup_hit, 0, sizeof(*lookup_hit) * global_items);
327
328 rcu_read_lock();
329 node = rcu_rbtree_max(&rbtree,
330 rcu_dereference(rbtree.root));
331 while (!rcu_rbtree_is_nil(&rbtree, node)) {
332 if (!opt_benchmark)
333 set_lookup_index(node, lookup_hit);
334 node = rcu_rbtree_prev(&rbtree, node);
335 nr_reads++;
336 }
337 rcu_read_unlock();
338
339 if (!opt_benchmark) {
340 for (i = 0; i < global_items; i++)
341 assert(lookup_hit[i]);
342 }
343 }
344
345 debug_yield_read();
346 if (unlikely(rduration))
347 loop_sleep(rduration);
348 if (unlikely(!test_duration_read()))
349 break;
350 }
351
352 rcu_unregister_thread();
353
354 /* test extra thread registration */
355 rcu_register_thread();
356 rcu_unregister_thread();
357
358 free(lookup_hit);
359
360 *count = nr_reads;
361 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
362 "reader", pthread_self(), (unsigned long)gettid());
363 return ((void*)1);
364
365 }
366
367 void *thr_writer(void *_count)
368 {
369 unsigned long long *count = _count;
370 struct rcu_rbtree_node *node;
371 void **key;
372 int i;
373
374 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
375 "writer", pthread_self(), (unsigned long)gettid());
376
377 set_affinity();
378
379 key = malloc(sizeof(*key) * nr_rand_items);
380 assert(key);
381 //disable_debug = 1;
382
383 rcu_register_thread();
384
385 while (!test_go)
386 {
387 }
388 cmm_smp_mb();
389
390 for (;;) {
391 rcu_copy_mutex_lock();
392
393 for (i = 0; i < nr_rand_items; i++) {
394 //key[i] = (void *)(unsigned long)(rand() % 2048);
395 key[i] = (void *)(unsigned long)(((unsigned long) rand() * 4) % 2048);
396 //For more collisions
397 //key[i] = (void *)(unsigned long)(rand() % 6);
398 //node->begin = key[i];
399 //node->end = (void *)((unsigned long) key[i] + 1);
400 //node->end = (void *)((unsigned long) key[i] + 4);
401 rcu_read_lock();
402 rcu_rbtree_insert(&rbtree, key[i],
403 (void *)((unsigned long) key[i] + 4));
404 rcu_read_unlock();
405 nr_writes++;
406 }
407 rcu_copy_mutex_unlock();
408
409 if (unlikely(wduration))
410 loop_sleep(wduration);
411
412 rcu_copy_mutex_lock();
413 for (i = 0; i < nr_rand_items; i++) {
414 #if 0
415 node = rcu_rbtree_min(rbtree, rbtree->root);
416 while (!rcu_rbtree_is_nil(&rbtree, node)) {
417 printf("{ 0x%lX p:%lX r:%lX l:%lX %s %s %s} ",
418 (unsigned long)node->key,
419 node->p->key,
420 node->right->key,
421 node->left->key,
422 node->color ? "red" : "black",
423 node->pos ? "right" : "left",
424 node->nil ? "nil" : "");
425 node = rcu_rbtree_next(rbtree, node);
426 }
427 printf("\n");
428 #endif
429 rcu_read_lock();
430 node = rcu_rbtree_search(&rbtree, rbtree.root, key[i]);
431 assert(!rcu_rbtree_is_nil(&rbtree, node));
432 rcu_rbtree_remove(&rbtree, node);
433 rcu_read_unlock();
434 nr_writes++;
435 }
436
437 rcu_copy_mutex_unlock();
438 if (unlikely(!test_duration_write()))
439 break;
440 if (unlikely(wdelay))
441 loop_sleep(wdelay);
442 }
443
444 rcu_unregister_thread();
445
446 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
447 "writer", pthread_self(), (unsigned long)gettid());
448 *count = nr_writes;
449 free(key);
450 return ((void*)2);
451 }
452
453 void show_usage(int argc, char **argv)
454 {
455 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
456 #ifdef DEBUG_YIELD
457 printf(" [-r] [-w] (yield reader and/or writer)");
458 #endif
459 printf(" [-d delay] (writer period (us))");
460 printf(" [-c duration] (reader C.S. duration (in loops))");
461 printf(" [-e duration] (writer C.S. duration (in loops))");
462 printf(" [-v] (verbose output)");
463 printf(" [-a cpu#] [-a cpu#]... (affinity)");
464 printf(" [-g items] Initially populate n items (for reader lookups)");
465 printf(" [-f items] Writers add n random items and then remove these items");
466 printf(" [-b] Reader: search begin key");
467 printf(" [-n] Reader: search bottom of range");
468 printf(" [-N] Reader: search end of range");
469 printf(" [-m] Reader: search range (middle)");
470 printf(" [-i] Reader: iterate from min to max");
471 printf(" [-I] Reader: iterate from max to min");
472 printf(" [-B] Benchmark mode (no validation)");
473 printf("\n");
474 }
475
476 int main(int argc, char **argv)
477 {
478 int err;
479 pthread_t *tid_reader, *tid_writer;
480 void *tret;
481 unsigned long long *count_reader, *count_writer;
482 unsigned long long tot_reads = 0, tot_writes = 0;
483 int i, a;
484 struct rcu_rbtree_node *node;
485
486 if (argc < 4) {
487 show_usage(argc, argv);
488 return -1;
489 }
490
491 err = sscanf(argv[1], "%u", &nr_readers);
492 if (err != 1) {
493 show_usage(argc, argv);
494 return -1;
495 }
496
497 err = sscanf(argv[2], "%u", &nr_writers);
498 if (err != 1) {
499 show_usage(argc, argv);
500 return -1;
501 }
502
503 err = sscanf(argv[3], "%lu", &duration);
504 if (err != 1) {
505 show_usage(argc, argv);
506 return -1;
507 }
508
509 for (i = 4; i < argc; i++) {
510 if (argv[i][0] != '-')
511 continue;
512 switch (argv[i][1]) {
513 #ifdef DEBUG_YIELD
514 case 'r':
515 yield_active |= YIELD_READ;
516 break;
517 case 'w':
518 yield_active |= YIELD_WRITE;
519 break;
520 #endif
521 case 'a':
522 if (argc < i + 2) {
523 show_usage(argc, argv);
524 return -1;
525 }
526 a = atoi(argv[++i]);
527 cpu_affinities[next_aff++] = a;
528 use_affinity = 1;
529 printf_verbose("Adding CPU %d affinity\n", a);
530 break;
531 case 'c':
532 if (argc < i + 2) {
533 show_usage(argc, argv);
534 return -1;
535 }
536 rduration = atol(argv[++i]);
537 break;
538 case 'd':
539 if (argc < i + 2) {
540 show_usage(argc, argv);
541 return -1;
542 }
543 wdelay = atol(argv[++i]);
544 break;
545 case 'e':
546 if (argc < i + 2) {
547 show_usage(argc, argv);
548 return -1;
549 }
550 wduration = atol(argv[++i]);
551 break;
552 case 'v':
553 verbose_mode = 1;
554 break;
555 case 'g':
556 if (argc < i + 2) {
557 show_usage(argc, argv);
558 return -1;
559 }
560 global_items = atol(argv[++i]);
561 break;
562 case 'b':
563 opt_search_begin = 1;
564 break;
565 case 'n':
566 opt_search_bottom = 1;
567 break;
568 case 'N':
569 opt_search_end = 1;
570 break;
571 case 'm':
572 opt_search_mid = 1;
573 break;
574 case 'i':
575 opt_iter_min_max = 1;
576 break;
577 case 'I':
578 opt_iter_max_min = 1;
579 break;
580 case 'B':
581 opt_benchmark = 1;
582 break;
583 case 'f':
584 if (argc < i + 2) {
585 show_usage(argc, argv);
586 return -1;
587 }
588 nr_rand_items = atol(argv[++i]);
589 break;
590 }
591 }
592
593 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
594 duration, nr_readers, nr_writers);
595 printf_verbose("Writer delay : %lu loops.\n", wdelay);
596 printf_verbose("Reader duration : %lu loops.\n", rduration);
597 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
598 "main", pthread_self(), (unsigned long)gettid());
599
600 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
601 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
602 count_reader = malloc(sizeof(*count_reader) * nr_readers);
603 count_writer = malloc(sizeof(*count_writer) * nr_writers);
604 global_key = malloc(sizeof(*global_key) * global_items);
605
606 srand(time(NULL));
607
608 err = create_all_cpu_call_rcu_data(0);
609 assert(!err);
610
611 next_aff = 0;
612
613 for (i = 0; i < nr_readers; i++) {
614 err = pthread_create(&tid_reader[i], NULL, thr_reader,
615 &count_reader[i]);
616 if (err != 0)
617 exit(1);
618 }
619 for (i = 0; i < nr_writers; i++) {
620 err = pthread_create(&tid_writer[i], NULL, thr_writer,
621 &count_writer[i]);
622 if (err != 0)
623 exit(1);
624 }
625
626 rcu_register_thread();
627 rcu_read_lock();
628 /* Insert items looked up by readers */
629 for (i = 0; i < global_items; i++) {
630 global_key[i] = (void *)(unsigned long)(((unsigned long) rand() * 4) % 2048);
631 //global_key[i] = (void *)(unsigned long)(rand() % 2048);
632 //For more collisions
633 //global_key[i] = (void *)(unsigned long)(rand() % 6);
634 //node->begin = global_key[i];
635 //node->end = (void *)((unsigned long) global_key[i] + 1);
636 //node->end = (void *)((unsigned long) global_key[i] + 4);
637 rcu_rbtree_insert(&rbtree, global_key[i],
638 (void *)((unsigned long) global_key[i] + 4));
639 }
640 rcu_read_unlock();
641
642 cmm_smp_mb();
643
644 test_go = 1;
645
646 sleep(duration);
647
648 test_stop = 1;
649
650 for (i = 0; i < nr_readers; i++) {
651 err = pthread_join(tid_reader[i], &tret);
652 if (err != 0)
653 exit(1);
654 tot_reads += count_reader[i];
655 }
656 for (i = 0; i < nr_writers; i++) {
657 err = pthread_join(tid_writer[i], &tret);
658 if (err != 0)
659 exit(1);
660 tot_writes += count_writer[i];
661 }
662
663 rcu_read_lock();
664 for (i = 0; i < global_items; i++) {
665 node = rcu_rbtree_search(&rbtree, rbtree.root, global_key[i]);
666 assert(!rcu_rbtree_is_nil(&rbtree, node));
667 rcu_rbtree_remove(&rbtree, node);
668 }
669 rcu_read_unlock();
670 rcu_unregister_thread();
671
672 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
673 tot_writes);
674 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
675 "nr_writers %3u "
676 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
677 "global_items %6lu rand_items %6lu\n",
678 argv[0], duration, nr_readers, rduration, wduration,
679 nr_writers, wdelay, tot_reads, tot_writes,
680 tot_reads + tot_writes, global_items, nr_rand_items);
681 free_all_cpu_call_rcu_data();
682 free(tid_reader);
683 free(tid_writer);
684 free(count_reader);
685 free(count_writer);
686 free(global_key);
687 return 0;
688 }
This page took 0.04366 seconds and 5 git commands to generate.