Add RCU read-side 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 4
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 void *thr_reader(void *_count)
213 {
214 unsigned long long *count = _count;
215 struct rcu_rbtree_node *node;
216 int i;
217
218 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
219 "reader", pthread_self(), (unsigned long)gettid());
220
221 set_affinity();
222
223 rcu_register_thread();
224
225 while (!test_go)
226 {
227 }
228 cmm_smp_mb();
229
230 for (;;) {
231 rcu_read_lock();
232
233 for (i = 0; i < global_items; i++) {
234 node = rcu_rbtree_search(&rbtree, rbtree.root,
235 global_key[i]);
236 assert(!rcu_rbtree_is_nil(node));
237 }
238 debug_yield_read();
239 if (unlikely(rduration))
240 loop_sleep(rduration);
241 rcu_read_unlock();
242 nr_reads++;
243 if (unlikely(!test_duration_read()))
244 break;
245 }
246
247 rcu_unregister_thread();
248
249 /* test extra thread registration */
250 rcu_register_thread();
251 rcu_unregister_thread();
252
253 *count = nr_reads;
254 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
255 "reader", pthread_self(), (unsigned long)gettid());
256 return ((void*)1);
257
258 }
259
260 void *thr_writer(void *_count)
261 {
262 unsigned long long *count = _count;
263 struct rcu_rbtree_node *node;
264 void *key[NR_RAND];
265 int i;
266
267 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
268 "writer", pthread_self(), (unsigned long)gettid());
269
270 set_affinity();
271
272 rcu_register_thread();
273
274 while (!test_go)
275 {
276 }
277 cmm_smp_mb();
278
279 for (;;) {
280 rcu_copy_mutex_lock();
281 rcu_read_lock();
282
283 for (i = 0; i < NR_RAND; i++) {
284 node = rbtree_alloc();
285 key[i] = (void *)(unsigned long)(rand() % 2048);
286 node->key = key[i];
287 rcu_rbtree_insert(&rbtree, node);
288 }
289
290 if (unlikely(wduration))
291 loop_sleep(wduration);
292
293 for (i = 0; i < NR_RAND; i++) {
294 #if 0
295 node = rcu_rbtree_min(rbtree, rbtree->root);
296 while (!rcu_rbtree_is_nil(node)) {
297 printf("{ 0x%lX p:%lX r:%lX l:%lX %s %s %s} ",
298 (unsigned long)node->key,
299 node->p->key,
300 node->right->key,
301 node->left->key,
302 node->color ? "red" : "black",
303 node->pos ? "right" : "left",
304 node->nil ? "nil" : "");
305 node = rcu_rbtree_next(rbtree, node);
306 }
307 printf("\n");
308 #endif
309 node = rcu_rbtree_search(&rbtree, rbtree.root, key[i]);
310 assert(!rcu_rbtree_is_nil(node));
311 rcu_rbtree_remove(&rbtree, node);
312 call_rcu(&node->head, rbtree_free);
313 }
314
315 rcu_read_unlock();
316 rcu_copy_mutex_unlock();
317 nr_writes++;
318 if (unlikely(!test_duration_write()))
319 break;
320 if (unlikely(wdelay))
321 loop_sleep(wdelay);
322 }
323
324 rcu_unregister_thread();
325
326 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
327 "writer", pthread_self(), (unsigned long)gettid());
328 *count = nr_writes;
329 return ((void*)2);
330 }
331
332 void show_usage(int argc, char **argv)
333 {
334 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
335 #ifdef DEBUG_YIELD
336 printf(" [-r] [-w] (yield reader and/or writer)");
337 #endif
338 printf(" [-d delay] (writer period (us))");
339 printf(" [-c duration] (reader C.S. duration (in loops))");
340 printf(" [-e duration] (writer C.S. duration (in loops))");
341 printf(" [-v] (verbose output)");
342 printf(" [-a cpu#] [-a cpu#]... (affinity)");
343 printf("\n");
344 }
345
346 int main(int argc, char **argv)
347 {
348 int err;
349 pthread_t *tid_reader, *tid_writer;
350 void *tret;
351 unsigned long long *count_reader, *count_writer;
352 unsigned long long tot_reads = 0, tot_writes = 0;
353 int i, a;
354 struct rcu_rbtree_node *node;
355
356 if (argc < 4) {
357 show_usage(argc, argv);
358 return -1;
359 }
360
361 err = sscanf(argv[1], "%u", &nr_readers);
362 if (err != 1) {
363 show_usage(argc, argv);
364 return -1;
365 }
366
367 err = sscanf(argv[2], "%u", &nr_writers);
368 if (err != 1) {
369 show_usage(argc, argv);
370 return -1;
371 }
372
373 err = sscanf(argv[3], "%lu", &duration);
374 if (err != 1) {
375 show_usage(argc, argv);
376 return -1;
377 }
378
379 for (i = 4; i < argc; i++) {
380 if (argv[i][0] != '-')
381 continue;
382 switch (argv[i][1]) {
383 #ifdef DEBUG_YIELD
384 case 'r':
385 yield_active |= YIELD_READ;
386 break;
387 case 'w':
388 yield_active |= YIELD_WRITE;
389 break;
390 #endif
391 case 'a':
392 if (argc < i + 2) {
393 show_usage(argc, argv);
394 return -1;
395 }
396 a = atoi(argv[++i]);
397 cpu_affinities[next_aff++] = a;
398 use_affinity = 1;
399 printf_verbose("Adding CPU %d affinity\n", a);
400 break;
401 case 'c':
402 if (argc < i + 2) {
403 show_usage(argc, argv);
404 return -1;
405 }
406 rduration = atol(argv[++i]);
407 break;
408 case 'd':
409 if (argc < i + 2) {
410 show_usage(argc, argv);
411 return -1;
412 }
413 wdelay = atol(argv[++i]);
414 break;
415 case 'e':
416 if (argc < i + 2) {
417 show_usage(argc, argv);
418 return -1;
419 }
420 wduration = atol(argv[++i]);
421 break;
422 case 'v':
423 verbose_mode = 1;
424 break;
425 case 'g':
426 if (argc < i + 2) {
427 show_usage(argc, argv);
428 return -1;
429 }
430 global_items = atol(argv[++i]);
431 break;
432 }
433 }
434
435 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
436 duration, nr_readers, nr_writers);
437 printf_verbose("Writer delay : %lu loops.\n", wdelay);
438 printf_verbose("Reader duration : %lu loops.\n", rduration);
439 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
440 "main", pthread_self(), (unsigned long)gettid());
441
442 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
443 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
444 count_reader = malloc(sizeof(*count_reader) * nr_readers);
445 count_writer = malloc(sizeof(*count_writer) * nr_writers);
446 global_key = malloc(sizeof(*global_key) * global_items);
447
448 srand(time(NULL));
449
450 next_aff = 0;
451
452 for (i = 0; i < nr_readers; i++) {
453 err = pthread_create(&tid_reader[i], NULL, thr_reader,
454 &count_reader[i]);
455 if (err != 0)
456 exit(1);
457 }
458 for (i = 0; i < nr_writers; i++) {
459 err = pthread_create(&tid_writer[i], NULL, thr_writer,
460 &count_writer[i]);
461 if (err != 0)
462 exit(1);
463 }
464
465 /* Insert items looked up by readers */
466 for (i = 0; i < global_items; i++) {
467 node = rbtree_alloc();
468 global_key[i] = (void *)(unsigned long)(rand() % 2048);
469 node->key = global_key[i];
470 rcu_rbtree_insert(&rbtree, node);
471 }
472
473 cmm_smp_mb();
474
475 test_go = 1;
476
477 sleep(duration);
478
479 test_stop = 1;
480
481 for (i = 0; i < nr_readers; i++) {
482 err = pthread_join(tid_reader[i], &tret);
483 if (err != 0)
484 exit(1);
485 tot_reads += count_reader[i];
486 }
487 for (i = 0; i < nr_writers; i++) {
488 err = pthread_join(tid_writer[i], &tret);
489 if (err != 0)
490 exit(1);
491 tot_writes += count_writer[i];
492 }
493
494 for (i = 0; i < global_items; i++) {
495 node = rcu_rbtree_search(&rbtree, rbtree.root, global_key[i]);
496 assert(!rcu_rbtree_is_nil(node));
497 rcu_rbtree_remove(&rbtree, node);
498 call_rcu(&node->head, rbtree_free);
499 }
500
501 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
502 tot_writes);
503 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
504 "nr_writers %3u "
505 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
506 "global_items %6lu\n",
507 argv[0], duration, nr_readers, rduration, wduration,
508 nr_writers, wdelay, tot_reads, tot_writes,
509 tot_reads + tot_writes, global_items);
510 free(tid_reader);
511 free(tid_writer);
512 free(count_reader);
513 free(count_writer);
514 free(global_key);
515 return 0;
516 }
This page took 0.039316 seconds and 5 git commands to generate.