RCU RB tree test : add prev/next/max/min test
[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 6
51 //#define NR_RAND 7
52
53 #if defined(_syscall0)
54 _syscall0(pid_t, gettid)
55 #elif defined(__NR_gettid)
56 static inline pid_t gettid(void)
57 {
58 return syscall(__NR_gettid);
59 }
60 #else
61 #warning "use pid as tid"
62 static inline pid_t gettid(void)
63 {
64 return getpid();
65 }
66 #endif
67
68 #include <urcu.h>
69 #include <urcu/rcurbtree.h>
70 #include <urcu-defer.h>
71
72 /* TODO: error handling testing for -ENOMEM */
73 struct rcu_rbtree_node *rbtree_alloc(void)
74 {
75 return calloc(1, sizeof(struct rcu_rbtree_node));
76 }
77
78 void rbtree_free(struct rcu_head *head)
79 {
80 struct rcu_rbtree_node *node =
81 caa_container_of(head, struct rcu_rbtree_node, head);
82 free(node);
83 }
84
85 int tree_comp(void *a, void *b)
86 {
87 if ((unsigned long)a < (unsigned long)b)
88 return -1;
89 else if ((unsigned long)a > (unsigned long)b)
90 return 1;
91 else
92 return 0;
93 }
94
95 static DEFINE_RCU_RBTREE(rbtree, tree_comp, rbtree_alloc, rbtree_free);
96
97 static volatile int test_go, test_stop;
98
99 static unsigned long wdelay;
100
101 static unsigned long duration;
102
103 /* read-side C.S. duration, in loops */
104 static unsigned long rduration;
105
106 /* write-side C.S. duration, in loops */
107 static unsigned long wduration;
108
109 static inline void loop_sleep(unsigned long l)
110 {
111 while(l-- != 0)
112 caa_cpu_relax();
113 }
114
115 static int verbose_mode;
116
117 #define printf_verbose(fmt, args...) \
118 do { \
119 if (verbose_mode) \
120 printf(fmt, args); \
121 } while (0)
122
123 static unsigned int cpu_affinities[NR_CPUS];
124 static unsigned int next_aff = 0;
125 static int use_affinity = 0;
126
127 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
128
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)
133 #endif
134
135 static void set_affinity(void)
136 {
137 cpu_set_t mask;
138 int cpu;
139 int ret;
140
141 if (!use_affinity)
142 return;
143
144 #if HAVE_SCHED_SETAFFINITY
145 ret = pthread_mutex_lock(&affinity_mutex);
146 if (ret) {
147 perror("Error in pthread mutex lock");
148 exit(-1);
149 }
150 cpu = cpu_affinities[next_aff++];
151 ret = pthread_mutex_unlock(&affinity_mutex);
152 if (ret) {
153 perror("Error in pthread mutex unlock");
154 exit(-1);
155 }
156
157 CPU_ZERO(&mask);
158 CPU_SET(cpu, &mask);
159 #if SCHED_SETAFFINITY_ARGS == 2
160 sched_setaffinity(0, &mask);
161 #else
162 sched_setaffinity(0, sizeof(mask), &mask);
163 #endif
164 #endif /* HAVE_SCHED_SETAFFINITY */
165 }
166
167 /*
168 * returns 0 if test should end.
169 */
170 static int test_duration_write(void)
171 {
172 return !test_stop;
173 }
174
175 static int test_duration_read(void)
176 {
177 return !test_stop;
178 }
179
180 static unsigned long long __thread nr_writes;
181 static unsigned long long __thread nr_reads;
182
183 static unsigned int nr_readers;
184 static unsigned int nr_writers;
185
186 static unsigned long global_items;
187 static void **global_key = NULL;
188
189 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
190
191 void rcu_copy_mutex_lock(void)
192 {
193 int ret;
194 ret = pthread_mutex_lock(&rcu_copy_mutex);
195 if (ret) {
196 perror("Error in pthread mutex lock");
197 exit(-1);
198 }
199 }
200
201 void rcu_copy_mutex_unlock(void)
202 {
203 int ret;
204
205 ret = pthread_mutex_unlock(&rcu_copy_mutex);
206 if (ret) {
207 perror("Error in pthread mutex unlock");
208 exit(-1);
209 }
210 }
211
212 static
213 int lookup_index(struct rcu_rbtree_node *node)
214 {
215 int i;
216
217 for (i = 0; i < global_items; i++) {
218 if (node->key == global_key[i])
219 return i;
220 }
221 return -1;
222 }
223
224 void *thr_reader(void *_count)
225 {
226 unsigned long long *count = _count;
227 struct rcu_rbtree_node *node;
228 int i, index;
229 char *lookup_hit;
230
231 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
232 "reader", pthread_self(), (unsigned long)gettid());
233
234 set_affinity();
235
236 rcu_register_thread();
237
238 lookup_hit = malloc(sizeof(*lookup_hit) * global_items);
239
240 while (!test_go)
241 {
242 }
243 cmm_smp_mb();
244
245 for (;;) {
246
247 /* search */
248 for (i = 0; i < global_items; i++) {
249 rcu_read_lock();
250 node = rcu_rbtree_search(&rbtree,
251 rcu_dereference(rbtree.root),
252 global_key[i]);
253 assert(!rcu_rbtree_is_nil(node));
254 rcu_read_unlock();
255 }
256 /* min + next */
257 memset(lookup_hit, 0, sizeof(*lookup_hit) * global_items);
258
259 rcu_read_lock();
260 node = rcu_rbtree_min(&rbtree,
261 rcu_dereference(rbtree.root));
262 while (!rcu_rbtree_is_nil(node)) {
263 index = lookup_index(node);
264 if (index >= 0)
265 lookup_hit[index] = 1;
266 node = rcu_rbtree_next(&rbtree, node);
267 }
268 rcu_read_unlock();
269
270 for (i = 0; i < global_items; i++)
271 assert(lookup_hit[i]);
272
273 /* max + prev */
274 memset(lookup_hit, 0, sizeof(*lookup_hit) * global_items);
275
276 rcu_read_lock();
277 node = rcu_rbtree_max(&rbtree,
278 rcu_dereference(rbtree.root));
279 while (!rcu_rbtree_is_nil(node)) {
280 index = lookup_index(node);
281 if (index >= 0)
282 lookup_hit[index] = 1;
283 node = rcu_rbtree_prev(&rbtree, node);
284 }
285 rcu_read_unlock();
286
287 for (i = 0; i < global_items; i++)
288 assert(lookup_hit[i]);
289
290 debug_yield_read();
291 if (unlikely(rduration))
292 loop_sleep(rduration);
293 nr_reads++;
294 if (unlikely(!test_duration_read()))
295 break;
296 }
297
298 rcu_unregister_thread();
299
300 /* test extra thread registration */
301 rcu_register_thread();
302 rcu_unregister_thread();
303
304 free(lookup_hit);
305
306 *count = nr_reads;
307 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
308 "reader", pthread_self(), (unsigned long)gettid());
309 return ((void*)1);
310
311 }
312
313 void *thr_writer(void *_count)
314 {
315 unsigned long long *count = _count;
316 struct rcu_rbtree_node *node;
317 void *key[NR_RAND];
318 int i;
319
320 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
321 "writer", pthread_self(), (unsigned long)gettid());
322
323 set_affinity();
324
325 rcu_register_thread();
326
327 while (!test_go)
328 {
329 }
330 cmm_smp_mb();
331
332 for (;;) {
333 rcu_copy_mutex_lock();
334
335 for (i = 0; i < NR_RAND; i++) {
336 node = rbtree_alloc();
337 key[i] = (void *)(unsigned long)(rand() % 2048);
338 node->key = key[i];
339 rcu_read_lock();
340 rcu_rbtree_insert(&rbtree, node);
341 rcu_read_unlock();
342 }
343 rcu_copy_mutex_unlock();
344
345 if (unlikely(wduration))
346 loop_sleep(wduration);
347
348 rcu_copy_mutex_lock();
349 for (i = 0; i < NR_RAND; i++) {
350 #if 0
351 node = rcu_rbtree_min(rbtree, rbtree->root);
352 while (!rcu_rbtree_is_nil(node)) {
353 printf("{ 0x%lX p:%lX r:%lX l:%lX %s %s %s} ",
354 (unsigned long)node->key,
355 node->p->key,
356 node->right->key,
357 node->left->key,
358 node->color ? "red" : "black",
359 node->pos ? "right" : "left",
360 node->nil ? "nil" : "");
361 node = rcu_rbtree_next(rbtree, node);
362 }
363 printf("\n");
364 #endif
365 rcu_read_lock();
366 node = rcu_rbtree_search(&rbtree, rbtree.root, key[i]);
367 assert(!rcu_rbtree_is_nil(node));
368 rcu_rbtree_remove(&rbtree, node);
369 rcu_read_unlock();
370 call_rcu(&node->head, rbtree_free);
371 }
372
373 rcu_copy_mutex_unlock();
374 nr_writes++;
375 if (unlikely(!test_duration_write()))
376 break;
377 if (unlikely(wdelay))
378 loop_sleep(wdelay);
379 }
380
381 rcu_unregister_thread();
382
383 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
384 "writer", pthread_self(), (unsigned long)gettid());
385 *count = nr_writes;
386 return ((void*)2);
387 }
388
389 void show_usage(int argc, char **argv)
390 {
391 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
392 #ifdef DEBUG_YIELD
393 printf(" [-r] [-w] (yield reader and/or writer)");
394 #endif
395 printf(" [-d delay] (writer period (us))");
396 printf(" [-c duration] (reader C.S. duration (in loops))");
397 printf(" [-e duration] (writer C.S. duration (in loops))");
398 printf(" [-v] (verbose output)");
399 printf(" [-a cpu#] [-a cpu#]... (affinity)");
400 printf("\n");
401 }
402
403 int main(int argc, char **argv)
404 {
405 int err;
406 pthread_t *tid_reader, *tid_writer;
407 void *tret;
408 unsigned long long *count_reader, *count_writer;
409 unsigned long long tot_reads = 0, tot_writes = 0;
410 int i, a;
411 struct rcu_rbtree_node *node;
412
413 if (argc < 4) {
414 show_usage(argc, argv);
415 return -1;
416 }
417
418 err = sscanf(argv[1], "%u", &nr_readers);
419 if (err != 1) {
420 show_usage(argc, argv);
421 return -1;
422 }
423
424 err = sscanf(argv[2], "%u", &nr_writers);
425 if (err != 1) {
426 show_usage(argc, argv);
427 return -1;
428 }
429
430 err = sscanf(argv[3], "%lu", &duration);
431 if (err != 1) {
432 show_usage(argc, argv);
433 return -1;
434 }
435
436 for (i = 4; i < argc; i++) {
437 if (argv[i][0] != '-')
438 continue;
439 switch (argv[i][1]) {
440 #ifdef DEBUG_YIELD
441 case 'r':
442 yield_active |= YIELD_READ;
443 break;
444 case 'w':
445 yield_active |= YIELD_WRITE;
446 break;
447 #endif
448 case 'a':
449 if (argc < i + 2) {
450 show_usage(argc, argv);
451 return -1;
452 }
453 a = atoi(argv[++i]);
454 cpu_affinities[next_aff++] = a;
455 use_affinity = 1;
456 printf_verbose("Adding CPU %d affinity\n", a);
457 break;
458 case 'c':
459 if (argc < i + 2) {
460 show_usage(argc, argv);
461 return -1;
462 }
463 rduration = atol(argv[++i]);
464 break;
465 case 'd':
466 if (argc < i + 2) {
467 show_usage(argc, argv);
468 return -1;
469 }
470 wdelay = atol(argv[++i]);
471 break;
472 case 'e':
473 if (argc < i + 2) {
474 show_usage(argc, argv);
475 return -1;
476 }
477 wduration = atol(argv[++i]);
478 break;
479 case 'v':
480 verbose_mode = 1;
481 break;
482 case 'g':
483 if (argc < i + 2) {
484 show_usage(argc, argv);
485 return -1;
486 }
487 global_items = atol(argv[++i]);
488 break;
489 }
490 }
491
492 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
493 duration, nr_readers, nr_writers);
494 printf_verbose("Writer delay : %lu loops.\n", wdelay);
495 printf_verbose("Reader duration : %lu loops.\n", rduration);
496 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
497 "main", pthread_self(), (unsigned long)gettid());
498
499 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
500 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
501 count_reader = malloc(sizeof(*count_reader) * nr_readers);
502 count_writer = malloc(sizeof(*count_writer) * nr_writers);
503 global_key = malloc(sizeof(*global_key) * global_items);
504
505 srand(time(NULL));
506
507 next_aff = 0;
508
509 for (i = 0; i < nr_readers; i++) {
510 err = pthread_create(&tid_reader[i], NULL, thr_reader,
511 &count_reader[i]);
512 if (err != 0)
513 exit(1);
514 }
515 for (i = 0; i < nr_writers; i++) {
516 err = pthread_create(&tid_writer[i], NULL, thr_writer,
517 &count_writer[i]);
518 if (err != 0)
519 exit(1);
520 }
521
522 rcu_register_thread();
523 rcu_read_lock();
524 /* Insert items looked up by readers */
525 for (i = 0; i < global_items; i++) {
526 node = rbtree_alloc();
527 global_key[i] = (void *)(unsigned long)(rand() % 2048);
528 node->key = global_key[i];
529 rcu_rbtree_insert(&rbtree, node);
530 }
531 rcu_read_unlock();
532
533 cmm_smp_mb();
534
535 test_go = 1;
536
537 sleep(duration);
538
539 test_stop = 1;
540
541 for (i = 0; i < nr_readers; i++) {
542 err = pthread_join(tid_reader[i], &tret);
543 if (err != 0)
544 exit(1);
545 tot_reads += count_reader[i];
546 }
547 for (i = 0; i < nr_writers; i++) {
548 err = pthread_join(tid_writer[i], &tret);
549 if (err != 0)
550 exit(1);
551 tot_writes += count_writer[i];
552 }
553
554 rcu_read_lock();
555 for (i = 0; i < global_items; i++) {
556 node = rcu_rbtree_search(&rbtree, rbtree.root, global_key[i]);
557 assert(!rcu_rbtree_is_nil(node));
558 rcu_rbtree_remove(&rbtree, node);
559 call_rcu(&node->head, rbtree_free);
560 }
561 rcu_read_unlock();
562 rcu_unregister_thread();
563
564 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
565 tot_writes);
566 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
567 "nr_writers %3u "
568 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
569 "global_items %6lu\n",
570 argv[0], duration, nr_readers, rduration, wduration,
571 nr_writers, wdelay, tot_reads, tot_writes,
572 tot_reads + tot_writes, global_items);
573 free(tid_reader);
574 free(tid_writer);
575 free(count_reader);
576 free(count_writer);
577 free(global_key);
578 return 0;
579 }
This page took 0.041599 seconds and 5 git commands to generate.