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 */
52 #if defined(_syscall0)
53 _syscall0(pid_t
, gettid
)
54 #elif defined(__NR_gettid)
55 static inline pid_t
gettid(void)
57 return syscall(__NR_gettid
);
60 #warning "use pid as tid"
61 static inline pid_t
gettid(void)
68 #include <urcu/rcurbtree.h>
69 #include <urcu-defer.h>
71 /* TODO: error handling testing for -ENOMEM */
72 struct rcu_rbtree_node
*rbtree_alloc(void)
74 return calloc(1, sizeof(struct rcu_rbtree_node
));
77 void rbtree_free(struct rcu_head
*head
)
79 struct rcu_rbtree_node
*node
=
80 caa_container_of(head
, struct rcu_rbtree_node
, head
);
84 int tree_comp(void *a
, void *b
)
86 if ((unsigned long)a
< (unsigned long)b
)
88 else if ((unsigned long)a
> (unsigned long)b
)
94 static DEFINE_RCU_RBTREE(rbtree
, tree_comp
, rbtree_alloc
, rbtree_free
);
96 static volatile int test_go
, test_stop
;
98 static unsigned long wdelay
;
100 static unsigned long duration
;
102 /* read-side C.S. duration, in loops */
103 static unsigned long rduration
;
105 /* write-side C.S. duration, in loops */
106 static unsigned long wduration
;
108 static inline void loop_sleep(unsigned long l
)
114 static int verbose_mode
;
116 #define printf_verbose(fmt, args...) \
122 static unsigned int cpu_affinities
[NR_CPUS
];
123 static unsigned int next_aff
= 0;
124 static int use_affinity
= 0;
126 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
128 #ifndef HAVE_CPU_SET_T
129 typedef unsigned long cpu_set_t
;
130 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
131 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
134 static void set_affinity(void)
143 #if HAVE_SCHED_SETAFFINITY
144 ret
= pthread_mutex_lock(&affinity_mutex
);
146 perror("Error in pthread mutex lock");
149 cpu
= cpu_affinities
[next_aff
++];
150 ret
= pthread_mutex_unlock(&affinity_mutex
);
152 perror("Error in pthread mutex unlock");
158 #if SCHED_SETAFFINITY_ARGS == 2
159 sched_setaffinity(0, &mask
);
161 sched_setaffinity(0, sizeof(mask
), &mask
);
163 #endif /* HAVE_SCHED_SETAFFINITY */
167 * returns 0 if test should end.
169 static int test_duration_write(void)
174 static int test_duration_read(void)
179 static unsigned long long __thread nr_writes
;
180 static unsigned long long __thread nr_reads
;
182 static unsigned int nr_readers
;
183 static unsigned int nr_writers
;
185 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
187 void rcu_copy_mutex_lock(void)
190 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
192 perror("Error in pthread mutex lock");
197 void rcu_copy_mutex_unlock(void)
201 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
203 perror("Error in pthread mutex unlock");
208 void *thr_reader(void *_count
)
210 unsigned long long *count
= _count
;
212 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
213 "reader", pthread_self(), (unsigned long)gettid());
217 rcu_register_thread();
228 if (unlikely(rduration
))
229 loop_sleep(rduration
);
232 if (unlikely(!test_duration_read()))
236 rcu_unregister_thread();
238 /* test extra thread registration */
239 rcu_register_thread();
240 rcu_unregister_thread();
243 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
244 "reader", pthread_self(), (unsigned long)gettid());
249 void *thr_writer(void *_count
)
251 unsigned long long *count
= _count
;
252 struct rcu_rbtree_node
*node
;
256 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
257 "writer", pthread_self(), (unsigned long)gettid());
261 rcu_register_thread();
269 rcu_copy_mutex_lock();
272 for (i
= 0; i
< NR_RAND
; i
++) {
273 node
= rbtree_alloc();
274 key
[i
] = (void *)(unsigned long)(rand() % 2048);
276 rcu_rbtree_insert(&rbtree
, node
);
279 if (unlikely(wduration
))
280 loop_sleep(wduration
);
282 for (i
= 0; i
< NR_RAND
; i
++) {
284 node
= rcu_rbtree_min(rbtree
, rbtree
->root
);
285 while (!rcu_rbtree_is_nil(node
)) {
286 printf("{ 0x%lX p:%lX r:%lX l:%lX %s %s %s} ",
287 (unsigned long)node
->key
,
291 node
->color
? "red" : "black",
292 node
->pos
? "right" : "left",
293 node
->nil
? "nil" : "");
294 node
= rcu_rbtree_next(rbtree
, node
);
298 node
= rcu_rbtree_search(&rbtree
, rbtree
.root
, key
[i
]);
299 assert(!rcu_rbtree_is_nil(node
));
300 rcu_rbtree_remove(&rbtree
, node
);
301 call_rcu(&node
->head
, rbtree_free
);
305 rcu_copy_mutex_unlock();
307 if (unlikely(!test_duration_write()))
309 if (unlikely(wdelay
))
313 rcu_unregister_thread();
315 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
316 "writer", pthread_self(), (unsigned long)gettid());
321 void show_usage(int argc
, char **argv
)
323 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
325 printf(" [-r] [-w] (yield reader and/or writer)");
327 printf(" [-d delay] (writer period (us))");
328 printf(" [-c duration] (reader C.S. duration (in loops))");
329 printf(" [-e duration] (writer C.S. duration (in loops))");
330 printf(" [-v] (verbose output)");
331 printf(" [-a cpu#] [-a cpu#]... (affinity)");
335 int main(int argc
, char **argv
)
338 pthread_t
*tid_reader
, *tid_writer
;
340 unsigned long long *count_reader
, *count_writer
;
341 unsigned long long tot_reads
= 0, tot_writes
= 0;
345 show_usage(argc
, argv
);
349 err
= sscanf(argv
[1], "%u", &nr_readers
);
351 show_usage(argc
, argv
);
355 err
= sscanf(argv
[2], "%u", &nr_writers
);
357 show_usage(argc
, argv
);
361 err
= sscanf(argv
[3], "%lu", &duration
);
363 show_usage(argc
, argv
);
367 for (i
= 4; i
< argc
; i
++) {
368 if (argv
[i
][0] != '-')
370 switch (argv
[i
][1]) {
373 yield_active
|= YIELD_READ
;
376 yield_active
|= YIELD_WRITE
;
381 show_usage(argc
, argv
);
385 cpu_affinities
[next_aff
++] = a
;
387 printf_verbose("Adding CPU %d affinity\n", a
);
391 show_usage(argc
, argv
);
394 rduration
= atol(argv
[++i
]);
398 show_usage(argc
, argv
);
401 wdelay
= atol(argv
[++i
]);
405 show_usage(argc
, argv
);
408 wduration
= atol(argv
[++i
]);
416 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
417 duration
, nr_readers
, nr_writers
);
418 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
419 printf_verbose("Reader duration : %lu loops.\n", rduration
);
420 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
421 "main", pthread_self(), (unsigned long)gettid());
423 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
424 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
425 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
426 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
432 for (i
= 0; i
< nr_readers
; i
++) {
433 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
438 for (i
= 0; i
< nr_writers
; i
++) {
439 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
453 for (i
= 0; i
< nr_readers
; i
++) {
454 err
= pthread_join(tid_reader
[i
], &tret
);
457 tot_reads
+= count_reader
[i
];
459 for (i
= 0; i
< nr_writers
; i
++) {
460 err
= pthread_join(tid_writer
[i
], &tret
);
463 tot_writes
+= count_writer
[i
];
466 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
468 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
470 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
471 argv
[0], duration
, nr_readers
, rduration
, wduration
,
472 nr_writers
, wdelay
, tot_reads
, tot_writes
,
473 tot_reads
+ tot_writes
);