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