Reimplement basic non-RCU rbtree
[userspace-rcu.git] / tests / test_urcu_rbtree.c
1 /*
2 * test_urcu_rbtree.c
3 *
4 * Userspace RCU library - test program for RB tree
5 *
6 * Copyright February 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
12 *
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.
17 *
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.
21 */
22
23 #define _GNU_SOURCE
24 #ifndef DYNAMIC_LINK_TEST
25 #define _LGPL_SOURCE
26 #else
27 #define debug_yield_read()
28 #endif
29 #include "../config.h"
30 #include <stdio.h>
31 #include <pthread.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <assert.h>
39 #include <sys/syscall.h>
40 #include <sched.h>
41 #include <errno.h>
42 #include <time.h>
43
44 #include <urcu/arch.h>
45
46 /* hardcoded number of CPUs */
47 #define NR_CPUS 16384
48
49 /* number of insert/delete */
50 #define NR_RAND 4
51
52 #if defined(_syscall0)
53 _syscall0(pid_t, gettid)
54 #elif defined(__NR_gettid)
55 static inline pid_t gettid(void)
56 {
57 return syscall(__NR_gettid);
58 }
59 #else
60 #warning "use pid as tid"
61 static inline pid_t gettid(void)
62 {
63 return getpid();
64 }
65 #endif
66
67 #include <urcu.h>
68 #include <urcu/rcurbtree.h>
69 #include <urcu-defer.h>
70
71 /* TODO: error handling testing for -ENOMEM */
72 struct rcu_rbtree_node *rbtree_alloc(void)
73 {
74 return calloc(1, sizeof(struct rcu_rbtree_node));
75 }
76
77 void rbtree_free(void *node)
78 {
79 free(node);
80 }
81
82 int tree_comp(void *a, void *b)
83 {
84 if ((unsigned long)a < (unsigned long)b)
85 return -1;
86 else if ((unsigned long)a > (unsigned long)b)
87 return 1;
88 else
89 return 0;
90 }
91
92 static DEFINE_RCU_RBTREE(rbtree, tree_comp, rbtree_alloc, rbtree_free);
93
94 static volatile int test_go, test_stop;
95
96 static unsigned long wdelay;
97
98 static unsigned long duration;
99
100 /* read-side C.S. duration, in loops */
101 static unsigned long rduration;
102
103 /* write-side C.S. duration, in loops */
104 static unsigned long wduration;
105
106 static inline void loop_sleep(unsigned long l)
107 {
108 while(l-- != 0)
109 caa_cpu_relax();
110 }
111
112 static int verbose_mode;
113
114 #define printf_verbose(fmt, args...) \
115 do { \
116 if (verbose_mode) \
117 printf(fmt, args); \
118 } while (0)
119
120 static unsigned int cpu_affinities[NR_CPUS];
121 static unsigned int next_aff = 0;
122 static int use_affinity = 0;
123
124 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
125
126 #ifndef HAVE_CPU_SET_T
127 typedef unsigned long cpu_set_t;
128 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
129 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
130 #endif
131
132 static void set_affinity(void)
133 {
134 cpu_set_t mask;
135 int cpu;
136 int ret;
137
138 if (!use_affinity)
139 return;
140
141 #if HAVE_SCHED_SETAFFINITY
142 ret = pthread_mutex_lock(&affinity_mutex);
143 if (ret) {
144 perror("Error in pthread mutex lock");
145 exit(-1);
146 }
147 cpu = cpu_affinities[next_aff++];
148 ret = pthread_mutex_unlock(&affinity_mutex);
149 if (ret) {
150 perror("Error in pthread mutex unlock");
151 exit(-1);
152 }
153
154 CPU_ZERO(&mask);
155 CPU_SET(cpu, &mask);
156 #if SCHED_SETAFFINITY_ARGS == 2
157 sched_setaffinity(0, &mask);
158 #else
159 sched_setaffinity(0, sizeof(mask), &mask);
160 #endif
161 #endif /* HAVE_SCHED_SETAFFINITY */
162 }
163
164 /*
165 * returns 0 if test should end.
166 */
167 static int test_duration_write(void)
168 {
169 return !test_stop;
170 }
171
172 static int test_duration_read(void)
173 {
174 return !test_stop;
175 }
176
177 static unsigned long long __thread nr_writes;
178 static unsigned long long __thread nr_reads;
179
180 static unsigned int nr_readers;
181 static unsigned int nr_writers;
182
183 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
184
185 void rcu_copy_mutex_lock(void)
186 {
187 int ret;
188 ret = pthread_mutex_lock(&rcu_copy_mutex);
189 if (ret) {
190 perror("Error in pthread mutex lock");
191 exit(-1);
192 }
193 }
194
195 void rcu_copy_mutex_unlock(void)
196 {
197 int ret;
198
199 ret = pthread_mutex_unlock(&rcu_copy_mutex);
200 if (ret) {
201 perror("Error in pthread mutex unlock");
202 exit(-1);
203 }
204 }
205
206 void *thr_reader(void *_count)
207 {
208 unsigned long long *count = _count;
209
210 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
211 "reader", pthread_self(), (unsigned long)gettid());
212
213 set_affinity();
214
215 rcu_register_thread();
216
217 while (!test_go)
218 {
219 }
220 cmm_smp_mb();
221
222 for (;;) {
223 rcu_read_lock();
224
225 debug_yield_read();
226 if (unlikely(rduration))
227 loop_sleep(rduration);
228 rcu_read_unlock();
229 nr_reads++;
230 if (unlikely(!test_duration_read()))
231 break;
232 }
233
234 rcu_unregister_thread();
235
236 /* test extra thread registration */
237 rcu_register_thread();
238 rcu_unregister_thread();
239
240 *count = nr_reads;
241 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
242 "reader", pthread_self(), (unsigned long)gettid());
243 return ((void*)1);
244
245 }
246
247 void *thr_writer(void *_count)
248 {
249 unsigned long long *count = _count;
250 struct rcu_rbtree_node *node;
251 void *key[NR_RAND];
252 int i;
253
254 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
255 "writer", pthread_self(), (unsigned long)gettid());
256
257 set_affinity();
258
259 rcu_defer_register_thread();
260
261 while (!test_go)
262 {
263 }
264 cmm_smp_mb();
265
266 for (;;) {
267 rcu_copy_mutex_lock();
268
269 for (i = 0; i < NR_RAND; i++) {
270 node = rbtree_alloc();
271 key[i] = (void *)(unsigned long)(rand() % 2048);
272 node->key = key[i];
273 rcu_rbtree_insert(&rbtree, node);
274 }
275
276 if (unlikely(wduration))
277 loop_sleep(wduration);
278
279 for (i = 0; i < NR_RAND; i++) {
280 #if 0
281 node = rcu_rbtree_min(rbtree, rbtree->root);
282 while (!rcu_rbtree_is_nil(node)) {
283 printf("{ 0x%lX p:%lX r:%lX l:%lX %s %s %s} ",
284 (unsigned long)node->key,
285 node->p->key,
286 node->right->key,
287 node->left->key,
288 node->color ? "red" : "black",
289 node->pos ? "right" : "left",
290 node->nil ? "nil" : "");
291 node = rcu_rbtree_next(rbtree, node);
292 }
293 printf("\n");
294 #endif
295 node = rcu_rbtree_search(&rbtree, rbtree.root, key[i]);
296 assert(!rcu_rbtree_is_nil(node));
297 rcu_rbtree_remove(&rbtree, node);
298 defer_rcu((void (*)(void *))rbtree_free, node);
299 }
300
301 rcu_copy_mutex_unlock();
302 nr_writes++;
303 if (unlikely(!test_duration_write()))
304 break;
305 if (unlikely(wdelay))
306 loop_sleep(wdelay);
307 }
308
309 rcu_defer_unregister_thread();
310
311 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
312 "writer", pthread_self(), (unsigned long)gettid());
313 *count = nr_writes;
314 return ((void*)2);
315 }
316
317 void show_usage(int argc, char **argv)
318 {
319 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
320 #ifdef DEBUG_YIELD
321 printf(" [-r] [-w] (yield reader and/or writer)");
322 #endif
323 printf(" [-d delay] (writer period (us))");
324 printf(" [-c duration] (reader C.S. duration (in loops))");
325 printf(" [-e duration] (writer C.S. duration (in loops))");
326 printf(" [-v] (verbose output)");
327 printf(" [-a cpu#] [-a cpu#]... (affinity)");
328 printf("\n");
329 }
330
331 int main(int argc, char **argv)
332 {
333 int err;
334 pthread_t *tid_reader, *tid_writer;
335 void *tret;
336 unsigned long long *count_reader, *count_writer;
337 unsigned long long tot_reads = 0, tot_writes = 0;
338 int i, a;
339
340 if (argc < 4) {
341 show_usage(argc, argv);
342 return -1;
343 }
344
345 err = sscanf(argv[1], "%u", &nr_readers);
346 if (err != 1) {
347 show_usage(argc, argv);
348 return -1;
349 }
350
351 err = sscanf(argv[2], "%u", &nr_writers);
352 if (err != 1) {
353 show_usage(argc, argv);
354 return -1;
355 }
356
357 err = sscanf(argv[3], "%lu", &duration);
358 if (err != 1) {
359 show_usage(argc, argv);
360 return -1;
361 }
362
363 for (i = 4; i < argc; i++) {
364 if (argv[i][0] != '-')
365 continue;
366 switch (argv[i][1]) {
367 #ifdef DEBUG_YIELD
368 case 'r':
369 yield_active |= YIELD_READ;
370 break;
371 case 'w':
372 yield_active |= YIELD_WRITE;
373 break;
374 #endif
375 case 'a':
376 if (argc < i + 2) {
377 show_usage(argc, argv);
378 return -1;
379 }
380 a = atoi(argv[++i]);
381 cpu_affinities[next_aff++] = a;
382 use_affinity = 1;
383 printf_verbose("Adding CPU %d affinity\n", a);
384 break;
385 case 'c':
386 if (argc < i + 2) {
387 show_usage(argc, argv);
388 return -1;
389 }
390 rduration = atol(argv[++i]);
391 break;
392 case 'd':
393 if (argc < i + 2) {
394 show_usage(argc, argv);
395 return -1;
396 }
397 wdelay = atol(argv[++i]);
398 break;
399 case 'e':
400 if (argc < i + 2) {
401 show_usage(argc, argv);
402 return -1;
403 }
404 wduration = atol(argv[++i]);
405 break;
406 case 'v':
407 verbose_mode = 1;
408 break;
409 }
410 }
411
412 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
413 duration, nr_readers, nr_writers);
414 printf_verbose("Writer delay : %lu loops.\n", wdelay);
415 printf_verbose("Reader duration : %lu loops.\n", rduration);
416 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
417 "main", pthread_self(), (unsigned long)gettid());
418
419 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
420 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
421 count_reader = malloc(sizeof(*count_reader) * nr_readers);
422 count_writer = malloc(sizeof(*count_writer) * nr_writers);
423
424 srand(time(NULL));
425
426 next_aff = 0;
427
428 for (i = 0; i < nr_readers; i++) {
429 err = pthread_create(&tid_reader[i], NULL, thr_reader,
430 &count_reader[i]);
431 if (err != 0)
432 exit(1);
433 }
434 for (i = 0; i < nr_writers; i++) {
435 err = pthread_create(&tid_writer[i], NULL, thr_writer,
436 &count_writer[i]);
437 if (err != 0)
438 exit(1);
439 }
440
441 cmm_smp_mb();
442
443 test_go = 1;
444
445 sleep(duration);
446
447 test_stop = 1;
448
449 for (i = 0; i < nr_readers; i++) {
450 err = pthread_join(tid_reader[i], &tret);
451 if (err != 0)
452 exit(1);
453 tot_reads += count_reader[i];
454 }
455 for (i = 0; i < nr_writers; i++) {
456 err = pthread_join(tid_writer[i], &tret);
457 if (err != 0)
458 exit(1);
459 tot_writes += count_writer[i];
460 }
461
462 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
463 tot_writes);
464 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
465 "nr_writers %3u "
466 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
467 argv[0], duration, nr_readers, rduration, wduration,
468 nr_writers, wdelay, tot_reads, tot_writes,
469 tot_reads + tot_writes);
470 free(tid_reader);
471 free(tid_writer);
472 free(count_reader);
473 free(count_writer);
474 return 0;
475 }
This page took 0.039472 seconds and 5 git commands to generate.