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 #include "../config.h"
29 #include <sys/types.h>
34 #include <sys/syscall.h>
39 #include <urcu/arch.h>
41 /* hardcoded number of CPUs */
44 #if defined(_syscall0)
45 _syscall0(pid_t
, gettid
)
46 #elif defined(__NR_gettid)
47 static inline pid_t
gettid(void)
49 return syscall(__NR_gettid
);
52 #warning "use pid as tid"
53 static inline pid_t
gettid(void)
59 #ifndef DYNAMIC_LINK_TEST
62 #define debug_yield_read()
65 #include <urcu-rbtree.h>
66 #include <urcu-defer.h>
68 static struct rcu_rbtree_node
*rbtree_root
= &rcu_rbtree_nil
;
70 /* TODO: error handling testing for -ENOMEM */
71 struct rcu_rbtree_node
*rbtree_alloc(void)
73 return malloc(sizeof(struct rcu_rbtree_node
));
76 void rbtree_free(struct rcu_rbtree_node
*node
)
81 int tree_comp(void *a
, void *b
)
83 if ((unsigned long)a
< (unsigned long)b
)
85 else if ((unsigned long)a
> (unsigned long)b
)
91 static volatile int test_go
, test_stop
;
93 static unsigned long wdelay
;
95 static unsigned long duration
;
97 /* read-side C.S. duration, in loops */
98 static unsigned long rduration
;
100 /* write-side C.S. duration, in loops */
101 static unsigned long wduration
;
103 static inline void loop_sleep(unsigned long l
)
109 static int verbose_mode
;
111 #define printf_verbose(fmt, args...) \
117 static unsigned int cpu_affinities
[NR_CPUS
];
118 static unsigned int next_aff
= 0;
119 static int use_affinity
= 0;
121 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
123 #ifndef HAVE_CPU_SET_T
124 typedef unsigned long cpu_set_t
;
125 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
126 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
129 static void set_affinity(void)
138 #if HAVE_SCHED_SETAFFINITY
139 ret
= pthread_mutex_lock(&affinity_mutex
);
141 perror("Error in pthread mutex lock");
144 cpu
= cpu_affinities
[next_aff
++];
145 ret
= pthread_mutex_unlock(&affinity_mutex
);
147 perror("Error in pthread mutex unlock");
153 #if SCHED_SETAFFINITY_ARGS == 2
154 sched_setaffinity(0, &mask
);
156 sched_setaffinity(0, sizeof(mask
), &mask
);
158 #endif /* HAVE_SCHED_SETAFFINITY */
162 * returns 0 if test should end.
164 static int test_duration_write(void)
169 static int test_duration_read(void)
174 static unsigned long long __thread nr_writes
;
175 static unsigned long long __thread nr_reads
;
177 static unsigned int nr_readers
;
178 static unsigned int nr_writers
;
180 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
182 void rcu_copy_mutex_lock(void)
185 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
187 perror("Error in pthread mutex lock");
192 void rcu_copy_mutex_unlock(void)
196 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
198 perror("Error in pthread mutex unlock");
203 void *thr_reader(void *_count
)
205 unsigned long long *count
= _count
;
207 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
208 "reader", pthread_self(), (unsigned long)gettid());
212 rcu_register_thread();
223 if (unlikely(rduration
))
224 loop_sleep(rduration
);
227 if (unlikely(!test_duration_read()))
231 rcu_unregister_thread();
233 /* test extra thread registration */
234 rcu_register_thread();
235 rcu_unregister_thread();
238 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
239 "reader", pthread_self(), (unsigned long)gettid());
244 void *thr_writer(void *_count
)
246 unsigned long long *count
= _count
;
247 struct rcu_rbtree_node
*node
;
249 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
250 "writer", pthread_self(), (unsigned long)gettid());
254 rcu_defer_register_thread();
262 node
= rbtree_alloc();
263 rcu_copy_mutex_lock();
264 node
->key
= (void *)(unsigned long)(rand() % 2048);
265 rcu_rbtree_insert(&rbtree_root
, node
, tree_comp
, rbtree_alloc
,
267 if (unlikely(wduration
))
268 loop_sleep(wduration
);
270 rcu_rbtree_remove(&rbtree_root
, node
, tree_comp
, rbtree_alloc
,
272 defer_rcu((void (*)(void *))rbtree_free
, node
);
274 rcu_copy_mutex_unlock();
276 if (unlikely(!test_duration_write()))
278 if (unlikely(wdelay
))
282 rcu_defer_unregister_thread();
284 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
285 "writer", pthread_self(), (unsigned long)gettid());
290 void show_usage(int argc
, char **argv
)
292 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
294 printf(" [-r] [-w] (yield reader and/or writer)");
296 printf(" [-d delay] (writer period (us))");
297 printf(" [-c duration] (reader C.S. duration (in loops))");
298 printf(" [-e duration] (writer C.S. duration (in loops))");
299 printf(" [-v] (verbose output)");
300 printf(" [-a cpu#] [-a cpu#]... (affinity)");
304 int main(int argc
, char **argv
)
307 pthread_t
*tid_reader
, *tid_writer
;
309 unsigned long long *count_reader
, *count_writer
;
310 unsigned long long tot_reads
= 0, tot_writes
= 0;
314 show_usage(argc
, argv
);
318 err
= sscanf(argv
[1], "%u", &nr_readers
);
320 show_usage(argc
, argv
);
324 err
= sscanf(argv
[2], "%u", &nr_writers
);
326 show_usage(argc
, argv
);
330 err
= sscanf(argv
[3], "%lu", &duration
);
332 show_usage(argc
, argv
);
336 for (i
= 4; i
< argc
; i
++) {
337 if (argv
[i
][0] != '-')
339 switch (argv
[i
][1]) {
342 yield_active
|= YIELD_READ
;
345 yield_active
|= YIELD_WRITE
;
350 show_usage(argc
, argv
);
354 cpu_affinities
[next_aff
++] = a
;
356 printf_verbose("Adding CPU %d affinity\n", a
);
360 show_usage(argc
, argv
);
363 rduration
= atol(argv
[++i
]);
367 show_usage(argc
, argv
);
370 wdelay
= atol(argv
[++i
]);
374 show_usage(argc
, argv
);
377 wduration
= atol(argv
[++i
]);
385 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
386 duration
, nr_readers
, nr_writers
);
387 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
388 printf_verbose("Reader duration : %lu loops.\n", rduration
);
389 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
390 "main", pthread_self(), (unsigned long)gettid());
392 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
393 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
394 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
395 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
401 for (i
= 0; i
< nr_readers
; i
++) {
402 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
407 for (i
= 0; i
< nr_writers
; i
++) {
408 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
422 for (i
= 0; i
< nr_readers
; i
++) {
423 err
= pthread_join(tid_reader
[i
], &tret
);
426 tot_reads
+= count_reader
[i
];
428 for (i
= 0; i
< nr_writers
; i
++) {
429 err
= pthread_join(tid_writer
[i
], &tret
);
432 tot_writes
+= count_writer
[i
];
435 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
437 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
439 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
440 argv
[0], duration
, nr_readers
, rduration
, wduration
,
441 nr_writers
, wdelay
, tot_reads
, tot_writes
,
442 tot_reads
+ tot_writes
);