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 /* hardcoded number of CPUs */
49 /* number of insert/delete */
53 #if defined(_syscall0)
54 _syscall0(pid_t
, gettid
)
55 #elif defined(__NR_gettid)
56 static inline pid_t
gettid(void)
58 return syscall(__NR_gettid
);
61 #warning "use pid as tid"
62 static inline pid_t
gettid(void)
69 #include <urcu/rcurbtree.h>
70 #include <urcu-defer.h>
72 /* TODO: error handling testing for -ENOMEM */
73 struct rcu_rbtree_node
*rbtree_alloc(void)
75 return calloc(1, sizeof(struct rcu_rbtree_node
));
78 void rbtree_free(struct rcu_head
*head
)
80 struct rcu_rbtree_node
*node
=
81 caa_container_of(head
, struct rcu_rbtree_node
, head
);
85 int tree_comp(void *a
, void *b
)
87 if ((unsigned long)a
< (unsigned long)b
)
89 else if ((unsigned long)a
> (unsigned long)b
)
95 static DEFINE_RCU_RBTREE(rbtree
, tree_comp
, rbtree_alloc
, rbtree_free
);
97 static volatile int test_go
, test_stop
;
99 static unsigned long wdelay
;
101 static unsigned long duration
;
103 /* read-side C.S. duration, in loops */
104 static unsigned long rduration
;
106 /* write-side C.S. duration, in loops */
107 static unsigned long wduration
;
109 static inline void loop_sleep(unsigned long l
)
115 static int verbose_mode
;
117 #define printf_verbose(fmt, args...) \
123 static unsigned int cpu_affinities
[NR_CPUS
];
124 static unsigned int next_aff
= 0;
125 static int use_affinity
= 0;
127 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
129 #ifndef HAVE_CPU_SET_T
130 typedef unsigned long cpu_set_t
;
131 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
132 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
135 static void set_affinity(void)
144 #if HAVE_SCHED_SETAFFINITY
145 ret
= pthread_mutex_lock(&affinity_mutex
);
147 perror("Error in pthread mutex lock");
150 cpu
= cpu_affinities
[next_aff
++];
151 ret
= pthread_mutex_unlock(&affinity_mutex
);
153 perror("Error in pthread mutex unlock");
159 #if SCHED_SETAFFINITY_ARGS == 2
160 sched_setaffinity(0, &mask
);
162 sched_setaffinity(0, sizeof(mask
), &mask
);
164 #endif /* HAVE_SCHED_SETAFFINITY */
168 * returns 0 if test should end.
170 static int test_duration_write(void)
175 static int test_duration_read(void)
180 static unsigned long long __thread nr_writes
;
181 static unsigned long long __thread nr_reads
;
183 static unsigned int nr_readers
;
184 static unsigned int nr_writers
;
186 static unsigned long global_items
;
187 static void **global_key
= NULL
;
189 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
191 void rcu_copy_mutex_lock(void)
194 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
196 perror("Error in pthread mutex lock");
201 void rcu_copy_mutex_unlock(void)
205 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
207 perror("Error in pthread mutex unlock");
212 void *thr_reader(void *_count
)
214 unsigned long long *count
= _count
;
215 struct rcu_rbtree_node
*node
;
218 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
219 "reader", pthread_self(), (unsigned long)gettid());
223 rcu_register_thread();
233 for (i
= 0; i
< global_items
; i
++) {
234 node
= rcu_rbtree_search(&rbtree
, rbtree
.root
,
236 assert(!rcu_rbtree_is_nil(node
));
239 if (unlikely(rduration
))
240 loop_sleep(rduration
);
243 if (unlikely(!test_duration_read()))
247 rcu_unregister_thread();
249 /* test extra thread registration */
250 rcu_register_thread();
251 rcu_unregister_thread();
254 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
255 "reader", pthread_self(), (unsigned long)gettid());
260 void *thr_writer(void *_count
)
262 unsigned long long *count
= _count
;
263 struct rcu_rbtree_node
*node
;
267 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
268 "writer", pthread_self(), (unsigned long)gettid());
272 rcu_register_thread();
280 rcu_copy_mutex_lock();
283 for (i
= 0; i
< NR_RAND
; i
++) {
284 node
= rbtree_alloc();
285 key
[i
] = (void *)(unsigned long)(rand() % 2048);
287 rcu_rbtree_insert(&rbtree
, node
);
290 if (unlikely(wduration
))
291 loop_sleep(wduration
);
293 for (i
= 0; i
< NR_RAND
; i
++) {
295 node
= rcu_rbtree_min(rbtree
, rbtree
->root
);
296 while (!rcu_rbtree_is_nil(node
)) {
297 printf("{ 0x%lX p:%lX r:%lX l:%lX %s %s %s} ",
298 (unsigned long)node
->key
,
302 node
->color
? "red" : "black",
303 node
->pos
? "right" : "left",
304 node
->nil
? "nil" : "");
305 node
= rcu_rbtree_next(rbtree
, node
);
309 node
= rcu_rbtree_search(&rbtree
, rbtree
.root
, key
[i
]);
310 assert(!rcu_rbtree_is_nil(node
));
311 rcu_rbtree_remove(&rbtree
, node
);
312 call_rcu(&node
->head
, rbtree_free
);
316 rcu_copy_mutex_unlock();
318 if (unlikely(!test_duration_write()))
320 if (unlikely(wdelay
))
324 rcu_unregister_thread();
326 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
327 "writer", pthread_self(), (unsigned long)gettid());
332 void show_usage(int argc
, char **argv
)
334 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
336 printf(" [-r] [-w] (yield reader and/or writer)");
338 printf(" [-d delay] (writer period (us))");
339 printf(" [-c duration] (reader C.S. duration (in loops))");
340 printf(" [-e duration] (writer C.S. duration (in loops))");
341 printf(" [-v] (verbose output)");
342 printf(" [-a cpu#] [-a cpu#]... (affinity)");
346 int main(int argc
, char **argv
)
349 pthread_t
*tid_reader
, *tid_writer
;
351 unsigned long long *count_reader
, *count_writer
;
352 unsigned long long tot_reads
= 0, tot_writes
= 0;
354 struct rcu_rbtree_node
*node
;
357 show_usage(argc
, argv
);
361 err
= sscanf(argv
[1], "%u", &nr_readers
);
363 show_usage(argc
, argv
);
367 err
= sscanf(argv
[2], "%u", &nr_writers
);
369 show_usage(argc
, argv
);
373 err
= sscanf(argv
[3], "%lu", &duration
);
375 show_usage(argc
, argv
);
379 for (i
= 4; i
< argc
; i
++) {
380 if (argv
[i
][0] != '-')
382 switch (argv
[i
][1]) {
385 yield_active
|= YIELD_READ
;
388 yield_active
|= YIELD_WRITE
;
393 show_usage(argc
, argv
);
397 cpu_affinities
[next_aff
++] = a
;
399 printf_verbose("Adding CPU %d affinity\n", a
);
403 show_usage(argc
, argv
);
406 rduration
= atol(argv
[++i
]);
410 show_usage(argc
, argv
);
413 wdelay
= atol(argv
[++i
]);
417 show_usage(argc
, argv
);
420 wduration
= atol(argv
[++i
]);
427 show_usage(argc
, argv
);
430 global_items
= atol(argv
[++i
]);
435 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
436 duration
, nr_readers
, nr_writers
);
437 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
438 printf_verbose("Reader duration : %lu loops.\n", rduration
);
439 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
440 "main", pthread_self(), (unsigned long)gettid());
442 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
443 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
444 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
445 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
446 global_key
= malloc(sizeof(*global_key
) * global_items
);
452 for (i
= 0; i
< nr_readers
; i
++) {
453 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
458 for (i
= 0; i
< nr_writers
; i
++) {
459 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
465 rcu_register_thread();
467 /* Insert items looked up by readers */
468 for (i
= 0; i
< global_items
; i
++) {
469 node
= rbtree_alloc();
470 global_key
[i
] = (void *)(unsigned long)(rand() % 2048);
471 node
->key
= global_key
[i
];
472 rcu_rbtree_insert(&rbtree
, node
);
484 for (i
= 0; i
< nr_readers
; i
++) {
485 err
= pthread_join(tid_reader
[i
], &tret
);
488 tot_reads
+= count_reader
[i
];
490 for (i
= 0; i
< nr_writers
; i
++) {
491 err
= pthread_join(tid_writer
[i
], &tret
);
494 tot_writes
+= count_writer
[i
];
498 for (i
= 0; i
< global_items
; i
++) {
499 node
= rcu_rbtree_search(&rbtree
, rbtree
.root
, global_key
[i
]);
500 assert(!rcu_rbtree_is_nil(node
));
501 rcu_rbtree_remove(&rbtree
, node
);
502 call_rcu(&node
->head
, rbtree_free
);
505 rcu_unregister_thread();
507 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
509 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
511 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
512 "global_items %6lu\n",
513 argv
[0], duration
, nr_readers
, rduration
, wduration
,
514 nr_writers
, wdelay
, tot_reads
, tot_writes
,
515 tot_reads
+ tot_writes
, global_items
);