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 /* number of insert/delete */
55 #if defined(_syscall0)
56 _syscall0(pid_t
, gettid
)
57 #elif defined(__NR_gettid)
58 static inline pid_t
gettid(void)
60 return syscall(__NR_gettid
);
63 #warning "use pid as tid"
64 static inline pid_t
gettid(void)
71 #include <urcu/rcurbtree.h>
72 #include <urcu-defer.h>
74 int tree_comp(void *a
, void *b
)
76 if ((unsigned long)a
< (unsigned long)b
)
78 else if ((unsigned long)a
> (unsigned long)b
)
84 static DEFINE_RCU_RBTREE(rbtree
, tree_comp
, malloc
, free
, call_rcu
);
86 static volatile int test_go
, test_stop
;
88 static unsigned long wdelay
;
90 static unsigned long duration
;
92 /* read-side C.S. duration, in loops */
93 static unsigned long rduration
;
95 /* write-side C.S. duration, in loops */
96 static unsigned long wduration
;
98 static inline void loop_sleep(unsigned long l
)
104 static int verbose_mode
;
106 #define printf_verbose(fmt, args...) \
112 static unsigned int cpu_affinities
[NR_CPUS
];
113 static unsigned int next_aff
= 0;
114 static int use_affinity
= 0;
116 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
118 #ifndef HAVE_CPU_SET_T
119 typedef unsigned long cpu_set_t
;
120 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
121 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
124 static void set_affinity(void)
133 #if HAVE_SCHED_SETAFFINITY
134 ret
= pthread_mutex_lock(&affinity_mutex
);
136 perror("Error in pthread mutex lock");
139 cpu
= cpu_affinities
[next_aff
++];
140 ret
= pthread_mutex_unlock(&affinity_mutex
);
142 perror("Error in pthread mutex unlock");
148 #if SCHED_SETAFFINITY_ARGS == 2
149 sched_setaffinity(0, &mask
);
151 sched_setaffinity(0, sizeof(mask
), &mask
);
153 #endif /* HAVE_SCHED_SETAFFINITY */
157 * returns 0 if test should end.
159 static int test_duration_write(void)
164 static int test_duration_read(void)
169 static unsigned long long __thread nr_writes
;
170 static unsigned long long __thread nr_reads
;
172 static unsigned int nr_readers
;
173 static unsigned int nr_writers
;
175 static unsigned long global_items
;
176 static void **global_key
= NULL
;
178 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
180 void rcu_copy_mutex_lock(void)
183 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
185 perror("Error in pthread mutex lock");
190 void rcu_copy_mutex_unlock(void)
194 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
196 perror("Error in pthread mutex unlock");
202 void set_lookup_index(struct rcu_rbtree_node
*node
,
207 for (i
= 0; i
< global_items
; i
++) {
208 if (node
->begin
== global_key
[i
]
216 void *thr_reader(void *_count
)
218 unsigned long long *count
= _count
;
219 struct rcu_rbtree_node
*node
;
223 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
224 "reader", pthread_self(), (unsigned long)gettid());
228 rcu_register_thread();
230 lookup_hit
= malloc(sizeof(*lookup_hit
) * global_items
);
238 /* search bottom of range */
239 for (i
= 0; i
< global_items
; i
++) {
241 node
= rcu_rbtree_search(&rbtree
,
242 rcu_dereference(rbtree
.root
),
244 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
248 /* search end of range */
249 for (i
= 0; i
< global_items
; i
++) {
251 node
= rcu_rbtree_search(&rbtree
,
252 rcu_dereference(rbtree
.root
),
253 (void*) ((unsigned long) global_key
[i
] + 3));
254 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
258 /* search range (middle) */
259 for (i
= 0; i
< global_items
; i
++) {
261 node
= rcu_rbtree_search_range(&rbtree
,
262 rcu_dereference(rbtree
.root
),
263 (void*) ((unsigned long) global_key
[i
] + 1),
264 (void*) ((unsigned long) global_key
[i
] + 2));
265 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
269 /* search begin key */
270 for (i
= 0; i
< global_items
; i
++) {
272 node
= rcu_rbtree_search_begin_key(&rbtree
,
273 rcu_dereference(rbtree
.root
),
275 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
280 memset(lookup_hit
, 0, sizeof(*lookup_hit
) * global_items
);
283 node
= rcu_rbtree_min(&rbtree
,
284 rcu_dereference(rbtree
.root
));
285 while (!rcu_rbtree_is_nil(&rbtree
, node
)) {
286 set_lookup_index(node
, lookup_hit
);
287 node
= rcu_rbtree_next(&rbtree
, node
);
291 for (i
= 0; i
< global_items
; i
++)
292 assert(lookup_hit
[i
]);
295 memset(lookup_hit
, 0, sizeof(*lookup_hit
) * global_items
);
298 node
= rcu_rbtree_max(&rbtree
,
299 rcu_dereference(rbtree
.root
));
300 while (!rcu_rbtree_is_nil(&rbtree
, node
)) {
301 set_lookup_index(node
, lookup_hit
);
302 node
= rcu_rbtree_prev(&rbtree
, node
);
306 for (i
= 0; i
< global_items
; i
++)
307 assert(lookup_hit
[i
]);
310 if (unlikely(rduration
))
311 loop_sleep(rduration
);
313 if (unlikely(!test_duration_read()))
317 rcu_unregister_thread();
319 /* test extra thread registration */
320 rcu_register_thread();
321 rcu_unregister_thread();
326 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
327 "reader", pthread_self(), (unsigned long)gettid());
332 void *thr_writer(void *_count
)
334 unsigned long long *count
= _count
;
335 struct rcu_rbtree_node
*node
;
339 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
340 "writer", pthread_self(), (unsigned long)gettid());
346 rcu_register_thread();
354 rcu_copy_mutex_lock();
356 for (i
= 0; i
< NR_RAND
; i
++) {
357 //key[i] = (void *)(unsigned long)(rand() % 2048);
358 key
[i
] = (void *)(unsigned long)(((unsigned long) rand() * 4) % 2048);
359 //For more collisions
360 //key[i] = (void *)(unsigned long)(rand() % 6);
361 //node->begin = key[i];
362 //node->end = (void *)((unsigned long) key[i] + 1);
363 //node->end = (void *)((unsigned long) key[i] + 4);
365 rcu_rbtree_insert(&rbtree
, key
[i
],
366 (void *)((unsigned long) key
[i
] + 4));
369 rcu_copy_mutex_unlock();
371 if (unlikely(wduration
))
372 loop_sleep(wduration
);
374 rcu_copy_mutex_lock();
375 for (i
= 0; i
< NR_RAND
; i
++) {
377 node
= rcu_rbtree_min(rbtree
, rbtree
->root
);
378 while (!rcu_rbtree_is_nil(&rbtree
, node
)) {
379 printf("{ 0x%lX p:%lX r:%lX l:%lX %s %s %s} ",
380 (unsigned long)node
->key
,
384 node
->color
? "red" : "black",
385 node
->pos
? "right" : "left",
386 node
->nil
? "nil" : "");
387 node
= rcu_rbtree_next(rbtree
, node
);
392 node
= rcu_rbtree_search(&rbtree
, rbtree
.root
, key
[i
]);
393 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
394 rcu_rbtree_remove(&rbtree
, node
);
398 rcu_copy_mutex_unlock();
400 if (unlikely(!test_duration_write()))
402 if (unlikely(wdelay
))
406 rcu_unregister_thread();
408 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
409 "writer", pthread_self(), (unsigned long)gettid());
414 void show_usage(int argc
, char **argv
)
416 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
418 printf(" [-r] [-w] (yield reader and/or writer)");
420 printf(" [-d delay] (writer period (us))");
421 printf(" [-c duration] (reader C.S. duration (in loops))");
422 printf(" [-e duration] (writer C.S. duration (in loops))");
423 printf(" [-v] (verbose output)");
424 printf(" [-a cpu#] [-a cpu#]... (affinity)");
428 int main(int argc
, char **argv
)
431 pthread_t
*tid_reader
, *tid_writer
;
433 unsigned long long *count_reader
, *count_writer
;
434 unsigned long long tot_reads
= 0, tot_writes
= 0;
436 struct rcu_rbtree_node
*node
;
439 show_usage(argc
, argv
);
443 err
= sscanf(argv
[1], "%u", &nr_readers
);
445 show_usage(argc
, argv
);
449 err
= sscanf(argv
[2], "%u", &nr_writers
);
451 show_usage(argc
, argv
);
455 err
= sscanf(argv
[3], "%lu", &duration
);
457 show_usage(argc
, argv
);
461 for (i
= 4; i
< argc
; i
++) {
462 if (argv
[i
][0] != '-')
464 switch (argv
[i
][1]) {
467 yield_active
|= YIELD_READ
;
470 yield_active
|= YIELD_WRITE
;
475 show_usage(argc
, argv
);
479 cpu_affinities
[next_aff
++] = a
;
481 printf_verbose("Adding CPU %d affinity\n", a
);
485 show_usage(argc
, argv
);
488 rduration
= atol(argv
[++i
]);
492 show_usage(argc
, argv
);
495 wdelay
= atol(argv
[++i
]);
499 show_usage(argc
, argv
);
502 wduration
= atol(argv
[++i
]);
509 show_usage(argc
, argv
);
512 global_items
= atol(argv
[++i
]);
517 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
518 duration
, nr_readers
, nr_writers
);
519 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
520 printf_verbose("Reader duration : %lu loops.\n", rduration
);
521 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
522 "main", pthread_self(), (unsigned long)gettid());
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 global_key
= malloc(sizeof(*global_key
) * global_items
);
534 for (i
= 0; i
< nr_readers
; i
++) {
535 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
540 for (i
= 0; i
< nr_writers
; i
++) {
541 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
547 rcu_register_thread();
549 /* Insert items looked up by readers */
550 for (i
= 0; i
< global_items
; i
++) {
551 global_key
[i
] = (void *)(unsigned long)(((unsigned long) rand() * 4) % 2048);
552 //global_key[i] = (void *)(unsigned long)(rand() % 2048);
553 //For more collisions
554 //global_key[i] = (void *)(unsigned long)(rand() % 6);
555 //node->begin = global_key[i];
556 //node->end = (void *)((unsigned long) global_key[i] + 1);
557 //node->end = (void *)((unsigned long) global_key[i] + 4);
558 rcu_rbtree_insert(&rbtree
, global_key
[i
],
559 (void *)((unsigned long) global_key
[i
] + 4));
571 for (i
= 0; i
< nr_readers
; i
++) {
572 err
= pthread_join(tid_reader
[i
], &tret
);
575 tot_reads
+= count_reader
[i
];
577 for (i
= 0; i
< nr_writers
; i
++) {
578 err
= pthread_join(tid_writer
[i
], &tret
);
581 tot_writes
+= count_writer
[i
];
585 for (i
= 0; i
< global_items
; i
++) {
586 node
= rcu_rbtree_search(&rbtree
, rbtree
.root
, global_key
[i
]);
587 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
588 rcu_rbtree_remove(&rbtree
, node
);
591 rcu_unregister_thread();
593 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
595 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
597 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
598 "global_items %6lu\n",
599 argv
[0], duration
, nr_readers
, rduration
, wduration
,
600 nr_writers
, wdelay
, tot_reads
, tot_writes
,
601 tot_reads
+ tot_writes
, global_items
);