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