4 * Userspace RCU library - test program for RB tree
6 * Copyright February 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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 #ifndef DYNAMIC_LINK_TEST
27 #define debug_yield_read()
29 #include "../config.h"
34 #include <sys/types.h>
39 #include <sys/syscall.h>
44 #include <urcu/arch.h>
46 extern int __thread disable_debug
;
48 /* hardcoded number of CPUs */
51 /* Default number of insert/delete */
52 #define DEFAULT_NR_RAND 6
54 /* Default number of global items (used by readers for lookups) */
55 #define DEFAULT_NR_GLOBAL 10
57 #if defined(_syscall0)
58 _syscall0(pid_t
, gettid
)
59 #elif defined(__NR_gettid)
60 static inline pid_t
gettid(void)
62 return syscall(__NR_gettid
);
65 #warning "use pid as tid"
66 static inline pid_t
gettid(void)
73 #include <urcu/rcurbtree.h>
74 #include <urcu-defer.h>
76 int tree_comp(void *a
, void *b
)
78 if ((unsigned long)a
< (unsigned long)b
)
80 else if ((unsigned long)a
> (unsigned long)b
)
86 static DEFINE_RCU_RBTREE(rbtree
, tree_comp
, malloc
, free
, call_rcu
);
88 static volatile int test_go
, test_stop
;
90 static unsigned long wdelay
;
92 static unsigned long duration
;
94 /* read-side C.S. duration, in loops */
95 static unsigned long rduration
;
97 /* write-side C.S. duration, in loops */
98 static unsigned long wduration
;
100 static unsigned long nr_rand_items
= DEFAULT_NR_RAND
;
102 static int opt_search_begin
,
110 static inline void loop_sleep(unsigned long l
)
116 static int verbose_mode
;
118 #define printf_verbose(fmt, args...) \
124 static unsigned int cpu_affinities
[NR_CPUS
];
125 static unsigned int next_aff
= 0;
126 static int use_affinity
= 0;
128 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
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)
136 static void set_affinity(void)
145 #if HAVE_SCHED_SETAFFINITY
146 ret
= pthread_mutex_lock(&affinity_mutex
);
148 perror("Error in pthread mutex lock");
151 cpu
= cpu_affinities
[next_aff
++];
152 ret
= pthread_mutex_unlock(&affinity_mutex
);
154 perror("Error in pthread mutex unlock");
160 #if SCHED_SETAFFINITY_ARGS == 2
161 sched_setaffinity(0, &mask
);
163 sched_setaffinity(0, sizeof(mask
), &mask
);
165 #endif /* HAVE_SCHED_SETAFFINITY */
169 * returns 0 if test should end.
171 static int test_duration_write(void)
176 static int test_duration_read(void)
181 static unsigned long long __thread nr_writes
;
182 static unsigned long long __thread nr_reads
;
184 static unsigned int nr_readers
;
185 static unsigned int nr_writers
;
187 static unsigned long global_items
= DEFAULT_NR_GLOBAL
;
188 static void **global_key
= NULL
;
190 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
192 void rcu_copy_mutex_lock(void)
195 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
197 perror("Error in pthread mutex lock");
202 void rcu_copy_mutex_unlock(void)
206 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
208 perror("Error in pthread mutex unlock");
214 void set_lookup_index(struct rcu_rbtree_node
*node
,
219 for (i
= 0; i
< global_items
; i
++) {
220 if (node
->begin
== global_key
[i
]
228 void *thr_reader(void *_count
)
230 unsigned long long *count
= _count
;
231 struct rcu_rbtree_node
*node
;
235 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
236 "reader", pthread_self(), (unsigned long)gettid());
240 rcu_register_thread();
242 lookup_hit
= malloc(sizeof(*lookup_hit
) * global_items
);
250 /* search begin key */
251 if (opt_search_begin
) {
252 for (i
= 0; i
< global_items
; i
++) {
254 node
= rcu_rbtree_search_begin_key(&rbtree
,
255 rcu_dereference(rbtree
.root
),
257 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
263 /* search bottom of range */
264 if (opt_search_bottom
) {
265 for (i
= 0; i
< global_items
; i
++) {
267 node
= rcu_rbtree_search(&rbtree
,
268 rcu_dereference(rbtree
.root
),
270 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
276 /* search end of range */
277 if (opt_search_end
) {
278 for (i
= 0; i
< global_items
; i
++) {
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
));
289 /* search range (middle) */
290 if (opt_search_mid
) {
291 for (i
= 0; i
< global_items
; i
++) {
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
));
304 if (opt_iter_min_max
) {
305 memset(lookup_hit
, 0, sizeof(*lookup_hit
) * global_items
);
308 node
= rcu_rbtree_min(&rbtree
,
309 rcu_dereference(rbtree
.root
));
310 while (!rcu_rbtree_is_nil(&rbtree
, node
)) {
312 set_lookup_index(node
, lookup_hit
);
313 node
= rcu_rbtree_next(&rbtree
, node
);
318 if (!opt_benchmark
) {
319 for (i
= 0; i
< global_items
; i
++)
320 assert(lookup_hit
[i
]);
325 if (opt_iter_max_min
) {
326 memset(lookup_hit
, 0, sizeof(*lookup_hit
) * global_items
);
329 node
= rcu_rbtree_max(&rbtree
,
330 rcu_dereference(rbtree
.root
));
331 while (!rcu_rbtree_is_nil(&rbtree
, node
)) {
333 set_lookup_index(node
, lookup_hit
);
334 node
= rcu_rbtree_prev(&rbtree
, node
);
339 if (!opt_benchmark
) {
340 for (i
= 0; i
< global_items
; i
++)
341 assert(lookup_hit
[i
]);
346 if (unlikely(rduration
))
347 loop_sleep(rduration
);
348 if (unlikely(!test_duration_read()))
352 rcu_unregister_thread();
354 /* test extra thread registration */
355 rcu_register_thread();
356 rcu_unregister_thread();
361 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
362 "reader", pthread_self(), (unsigned long)gettid());
367 void *thr_writer(void *_count
)
369 unsigned long long *count
= _count
;
370 struct rcu_rbtree_node
*node
;
374 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
375 "writer", pthread_self(), (unsigned long)gettid());
379 key
= malloc(sizeof(*key
) * nr_rand_items
);
383 rcu_register_thread();
391 rcu_copy_mutex_lock();
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);
402 rcu_rbtree_insert(&rbtree
, key
[i
],
403 (void *)((unsigned long) key
[i
] + 4));
407 rcu_copy_mutex_unlock();
409 if (unlikely(wduration
))
410 loop_sleep(wduration
);
412 rcu_copy_mutex_lock();
413 for (i
= 0; i
< nr_rand_items
; i
++) {
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
,
422 node
->color
? "red" : "black",
423 node
->pos
? "right" : "left",
424 node
->nil
? "nil" : "");
425 node
= rcu_rbtree_next(rbtree
, node
);
430 node
= rcu_rbtree_search(&rbtree
, rbtree
.root
, key
[i
]);
431 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
432 rcu_rbtree_remove(&rbtree
, node
);
437 rcu_copy_mutex_unlock();
438 if (unlikely(!test_duration_write()))
440 if (unlikely(wdelay
))
444 rcu_unregister_thread();
446 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
447 "writer", pthread_self(), (unsigned long)gettid());
453 void show_usage(int argc
, char **argv
)
455 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
457 printf(" [-r] [-w] (yield reader and/or writer)");
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)");
476 int main(int argc
, char **argv
)
479 pthread_t
*tid_reader
, *tid_writer
;
481 unsigned long long *count_reader
, *count_writer
;
482 unsigned long long tot_reads
= 0, tot_writes
= 0;
484 struct rcu_rbtree_node
*node
;
487 show_usage(argc
, argv
);
491 err
= sscanf(argv
[1], "%u", &nr_readers
);
493 show_usage(argc
, argv
);
497 err
= sscanf(argv
[2], "%u", &nr_writers
);
499 show_usage(argc
, argv
);
503 err
= sscanf(argv
[3], "%lu", &duration
);
505 show_usage(argc
, argv
);
509 for (i
= 4; i
< argc
; i
++) {
510 if (argv
[i
][0] != '-')
512 switch (argv
[i
][1]) {
515 yield_active
|= YIELD_READ
;
518 yield_active
|= YIELD_WRITE
;
523 show_usage(argc
, argv
);
527 cpu_affinities
[next_aff
++] = a
;
529 printf_verbose("Adding CPU %d affinity\n", a
);
533 show_usage(argc
, argv
);
536 rduration
= atol(argv
[++i
]);
540 show_usage(argc
, argv
);
543 wdelay
= atol(argv
[++i
]);
547 show_usage(argc
, argv
);
550 wduration
= atol(argv
[++i
]);
557 show_usage(argc
, argv
);
560 global_items
= atol(argv
[++i
]);
563 opt_search_begin
= 1;
566 opt_search_bottom
= 1;
575 opt_iter_min_max
= 1;
578 opt_iter_max_min
= 1;
585 show_usage(argc
, argv
);
588 nr_rand_items
= atol(argv
[++i
]);
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());
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
);
608 err
= create_all_cpu_call_rcu_data(0);
613 for (i
= 0; i
< nr_readers
; i
++) {
614 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
619 for (i
= 0; i
< nr_writers
; i
++) {
620 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
626 rcu_register_thread();
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));
650 for (i
= 0; i
< nr_readers
; i
++) {
651 err
= pthread_join(tid_reader
[i
], &tret
);
654 tot_reads
+= count_reader
[i
];
656 for (i
= 0; i
< nr_writers
; i
++) {
657 err
= pthread_join(tid_writer
[i
], &tret
);
660 tot_writes
+= count_writer
[i
];
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
);
670 rcu_unregister_thread();
672 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
674 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
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();