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