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 /* TODO: error handling testing for -ENOMEM */
75 struct rcu_rbtree_node
*rbtree_alloc(void)
77 return calloc(1, sizeof(struct rcu_rbtree_node
));
80 void rbtree_free(struct rcu_head
*head
)
82 struct rcu_rbtree_node
*node
=
83 caa_container_of(head
, struct rcu_rbtree_node
, head
);
87 int tree_comp(void *a
, void *b
)
89 if ((unsigned long)a
< (unsigned long)b
)
91 else if ((unsigned long)a
> (unsigned long)b
)
97 static DEFINE_RCU_RBTREE(rbtree
, tree_comp
, rbtree_alloc
, rbtree_free
);
99 static volatile int test_go
, test_stop
;
101 static unsigned long wdelay
;
103 static unsigned long duration
;
105 /* read-side C.S. duration, in loops */
106 static unsigned long rduration
;
108 /* write-side C.S. duration, in loops */
109 static unsigned long wduration
;
111 static inline void loop_sleep(unsigned long l
)
117 static int verbose_mode
;
119 #define printf_verbose(fmt, args...) \
125 static unsigned int cpu_affinities
[NR_CPUS
];
126 static unsigned int next_aff
= 0;
127 static int use_affinity
= 0;
129 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
131 #ifndef HAVE_CPU_SET_T
132 typedef unsigned long cpu_set_t
;
133 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
134 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
137 static void set_affinity(void)
146 #if HAVE_SCHED_SETAFFINITY
147 ret
= pthread_mutex_lock(&affinity_mutex
);
149 perror("Error in pthread mutex lock");
152 cpu
= cpu_affinities
[next_aff
++];
153 ret
= pthread_mutex_unlock(&affinity_mutex
);
155 perror("Error in pthread mutex unlock");
161 #if SCHED_SETAFFINITY_ARGS == 2
162 sched_setaffinity(0, &mask
);
164 sched_setaffinity(0, sizeof(mask
), &mask
);
166 #endif /* HAVE_SCHED_SETAFFINITY */
170 * returns 0 if test should end.
172 static int test_duration_write(void)
177 static int test_duration_read(void)
182 static unsigned long long __thread nr_writes
;
183 static unsigned long long __thread nr_reads
;
185 static unsigned int nr_readers
;
186 static unsigned int nr_writers
;
188 static unsigned long global_items
;
189 static void **global_key
= NULL
;
191 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
193 void rcu_copy_mutex_lock(void)
196 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
198 perror("Error in pthread mutex lock");
203 void rcu_copy_mutex_unlock(void)
207 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
209 perror("Error in pthread mutex unlock");
215 void set_lookup_index(struct rcu_rbtree_node
*node
,
220 for (i
= 0; i
< global_items
; i
++) {
221 if (node
->begin
== global_key
[i
]
229 void *thr_reader(void *_count
)
231 unsigned long long *count
= _count
;
232 struct rcu_rbtree_node
*node
;
236 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
237 "reader", pthread_self(), (unsigned long)gettid());
241 rcu_register_thread();
243 lookup_hit
= malloc(sizeof(*lookup_hit
) * global_items
);
251 /* search bottom of range */
252 for (i
= 0; i
< global_items
; i
++) {
254 node
= rcu_rbtree_search(&rbtree
,
255 rcu_dereference(rbtree
.root
),
257 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
261 /* search end of range */
262 for (i
= 0; i
< global_items
; i
++) {
264 node
= rcu_rbtree_search(&rbtree
,
265 rcu_dereference(rbtree
.root
),
266 (void*) ((unsigned long) global_key
[i
] + 3));
267 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
271 /* search range (middle) */
272 for (i
= 0; i
< global_items
; i
++) {
274 node
= rcu_rbtree_search_range(&rbtree
,
275 rcu_dereference(rbtree
.root
),
276 (void*) ((unsigned long) global_key
[i
] + 1),
277 (void*) ((unsigned long) global_key
[i
] + 2));
278 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
282 /* search begin key */
283 for (i
= 0; i
< global_items
; i
++) {
285 node
= rcu_rbtree_search_begin_key(&rbtree
,
286 rcu_dereference(rbtree
.root
),
288 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
293 memset(lookup_hit
, 0, sizeof(*lookup_hit
) * global_items
);
296 node
= rcu_rbtree_min(&rbtree
,
297 rcu_dereference(rbtree
.root
));
298 while (!rcu_rbtree_is_nil(&rbtree
, node
)) {
299 set_lookup_index(node
, lookup_hit
);
300 node
= rcu_rbtree_next(&rbtree
, node
);
304 for (i
= 0; i
< global_items
; i
++)
305 assert(lookup_hit
[i
]);
308 memset(lookup_hit
, 0, sizeof(*lookup_hit
) * global_items
);
311 node
= rcu_rbtree_max(&rbtree
,
312 rcu_dereference(rbtree
.root
));
313 while (!rcu_rbtree_is_nil(&rbtree
, node
)) {
314 set_lookup_index(node
, lookup_hit
);
315 node
= rcu_rbtree_prev(&rbtree
, node
);
319 for (i
= 0; i
< global_items
; i
++)
320 assert(lookup_hit
[i
]);
323 if (unlikely(rduration
))
324 loop_sleep(rduration
);
326 if (unlikely(!test_duration_read()))
330 rcu_unregister_thread();
332 /* test extra thread registration */
333 rcu_register_thread();
334 rcu_unregister_thread();
339 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
340 "reader", pthread_self(), (unsigned long)gettid());
345 void *thr_writer(void *_count
)
347 unsigned long long *count
= _count
;
348 struct rcu_rbtree_node
*node
;
352 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
353 "writer", pthread_self(), (unsigned long)gettid());
359 rcu_register_thread();
367 rcu_copy_mutex_lock();
369 for (i
= 0; i
< NR_RAND
; i
++) {
370 node
= rbtree_alloc();
371 //key[i] = (void *)(unsigned long)(rand() % 2048);
372 key
[i
] = (void *)(unsigned long)((rand() * 4) % 2048);
373 //For more collisions
374 //key[i] = (void *)(unsigned long)(rand() % 6);
375 node
->begin
= key
[i
];
376 //node->end = (void *)((unsigned long) key[i] + 4);
377 node
->end
= (void *)((unsigned long) key
[i
] + 4);
379 rcu_rbtree_insert(&rbtree
, node
);
382 rcu_copy_mutex_unlock();
384 if (unlikely(wduration
))
385 loop_sleep(wduration
);
387 rcu_copy_mutex_lock();
388 for (i
= 0; i
< NR_RAND
; i
++) {
390 node
= rcu_rbtree_min(rbtree
, rbtree
->root
);
391 while (!rcu_rbtree_is_nil(&rbtree
, node
)) {
392 printf("{ 0x%lX p:%lX r:%lX l:%lX %s %s %s} ",
393 (unsigned long)node
->key
,
397 node
->color
? "red" : "black",
398 node
->pos
? "right" : "left",
399 node
->nil
? "nil" : "");
400 node
= rcu_rbtree_next(rbtree
, node
);
405 node
= rcu_rbtree_search(&rbtree
, rbtree
.root
, key
[i
]);
406 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
407 rcu_rbtree_remove(&rbtree
, node
);
409 call_rcu(&node
->head
, rbtree_free
);
412 rcu_copy_mutex_unlock();
414 if (unlikely(!test_duration_write()))
416 if (unlikely(wdelay
))
420 rcu_unregister_thread();
422 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
423 "writer", pthread_self(), (unsigned long)gettid());
428 void show_usage(int argc
, char **argv
)
430 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
432 printf(" [-r] [-w] (yield reader and/or writer)");
434 printf(" [-d delay] (writer period (us))");
435 printf(" [-c duration] (reader C.S. duration (in loops))");
436 printf(" [-e duration] (writer C.S. duration (in loops))");
437 printf(" [-v] (verbose output)");
438 printf(" [-a cpu#] [-a cpu#]... (affinity)");
442 int main(int argc
, char **argv
)
445 pthread_t
*tid_reader
, *tid_writer
;
447 unsigned long long *count_reader
, *count_writer
;
448 unsigned long long tot_reads
= 0, tot_writes
= 0;
450 struct rcu_rbtree_node
*node
;
453 show_usage(argc
, argv
);
457 err
= sscanf(argv
[1], "%u", &nr_readers
);
459 show_usage(argc
, argv
);
463 err
= sscanf(argv
[2], "%u", &nr_writers
);
465 show_usage(argc
, argv
);
469 err
= sscanf(argv
[3], "%lu", &duration
);
471 show_usage(argc
, argv
);
475 for (i
= 4; i
< argc
; i
++) {
476 if (argv
[i
][0] != '-')
478 switch (argv
[i
][1]) {
481 yield_active
|= YIELD_READ
;
484 yield_active
|= YIELD_WRITE
;
489 show_usage(argc
, argv
);
493 cpu_affinities
[next_aff
++] = a
;
495 printf_verbose("Adding CPU %d affinity\n", a
);
499 show_usage(argc
, argv
);
502 rduration
= atol(argv
[++i
]);
506 show_usage(argc
, argv
);
509 wdelay
= atol(argv
[++i
]);
513 show_usage(argc
, argv
);
516 wduration
= atol(argv
[++i
]);
523 show_usage(argc
, argv
);
526 global_items
= atol(argv
[++i
]);
531 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
532 duration
, nr_readers
, nr_writers
);
533 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
534 printf_verbose("Reader duration : %lu loops.\n", rduration
);
535 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
536 "main", pthread_self(), (unsigned long)gettid());
538 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
539 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
540 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
541 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
542 global_key
= malloc(sizeof(*global_key
) * global_items
);
548 for (i
= 0; i
< nr_readers
; i
++) {
549 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
554 for (i
= 0; i
< nr_writers
; i
++) {
555 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
561 rcu_register_thread();
563 /* Insert items looked up by readers */
564 for (i
= 0; i
< global_items
; i
++) {
565 node
= rbtree_alloc();
566 global_key
[i
] = (void *)(unsigned long)((rand() * 4) % 2048);
567 //global_key[i] = (void *)(unsigned long)(rand() % 2048);
568 //For more collisions
569 global_key
[i
] = (void *)(unsigned long)(rand() % 6);
570 node
->begin
= global_key
[i
];
571 //node->end = (void *)((unsigned long) global_key[i] + 1);
572 node
->end
= (void *)((unsigned long) global_key
[i
] + 4);
573 rcu_rbtree_insert(&rbtree
, node
);
585 for (i
= 0; i
< nr_readers
; i
++) {
586 err
= pthread_join(tid_reader
[i
], &tret
);
589 tot_reads
+= count_reader
[i
];
591 for (i
= 0; i
< nr_writers
; i
++) {
592 err
= pthread_join(tid_writer
[i
], &tret
);
595 tot_writes
+= count_writer
[i
];
599 for (i
= 0; i
< global_items
; i
++) {
600 node
= rcu_rbtree_search(&rbtree
, rbtree
.root
, global_key
[i
]);
601 assert(!rcu_rbtree_is_nil(&rbtree
, node
));
602 rcu_rbtree_remove(&rbtree
, node
);
603 call_rcu(&node
->head
, rbtree_free
);
606 rcu_unregister_thread();
608 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
610 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
612 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
613 "global_items %6lu\n",
614 argv
[0], duration
, nr_readers
, rduration
, wduration
,
615 nr_writers
, wdelay
, tot_reads
, tot_writes
,
616 tot_reads
+ tot_writes
, global_items
);