Fix static linking: fix symbol name namespaces
[urcu.git] / tests / test_urcu.c
CommitLineData
b257a10b
MD
1/*
2 * test_urcu.c
3 *
4 * Userspace RCU library - test program
5 *
6982d6d7 6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
b257a10b 7 *
af02d47e
MD
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.
b257a10b
MD
21 */
22
9e97e478 23#define _GNU_SOURCE
d8540fc5 24#include "../config.h"
ac260fd9 25#include <stdio.h>
f69f195a
MD
26#include <pthread.h>
27#include <stdlib.h>
41718ff9 28#include <string.h>
f69f195a
MD
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <unistd.h>
32#include <stdio.h>
41718ff9 33#include <assert.h>
9e97e478 34#include <sched.h>
94b343fd 35#include <errno.h>
87bd15cd 36
ec4e58a3 37#include <urcu/arch.h>
bd252a04 38#include <urcu/tls-compat.h>
7685d963 39
65fcc7e9
MD
40#ifdef __linux__
41#include <syscall.h>
42#endif
43
2a7ac59d
MD
44/* hardcoded number of CPUs */
45#define NR_CPUS 16384
46
87bd15cd
BW
47#if defined(_syscall0)
48_syscall0(pid_t, gettid)
49#elif defined(__NR_gettid)
50static inline pid_t gettid(void)
51{
52 return syscall(__NR_gettid);
53}
54#else
55#warning "use pid as tid"
56static inline pid_t gettid(void)
57{
58 return getpid();
59}
60#endif
61
121a5d44
MD
62#ifndef DYNAMIC_LINK_TEST
63#define _LGPL_SOURCE
64#else
1de4df4b 65#define rcu_debug_yield_read()
121a5d44 66#endif
ec4e58a3 67#include <urcu.h>
ac260fd9 68
41718ff9
MD
69struct test_array {
70 int a;
41718ff9
MD
71};
72
78efb485 73static volatile int test_go, test_stop;
8c86b9a1 74
9e31d0f0 75static unsigned long wdelay;
bb488185 76
41718ff9
MD
77static struct test_array *test_rcu_pointer;
78
cf380c2f 79static unsigned long duration;
cf380c2f 80
daddf5b0 81/* read-side C.S. duration, in loops */
8b632bab
MD
82static unsigned long rduration;
83
31b598e0
MD
84/* write-side C.S. duration, in loops */
85static unsigned long wduration;
86
ab0aacbe 87static inline void loop_sleep(unsigned long loops)
daddf5b0 88{
ab0aacbe 89 while (loops-- != 0)
06f22bdb 90 caa_cpu_relax();
daddf5b0
MD
91}
92
4bad7d45
MD
93static int verbose_mode;
94
95#define printf_verbose(fmt, args...) \
96 do { \
97 if (verbose_mode) \
98 printf(fmt, args); \
99 } while (0)
100
2a7ac59d
MD
101static unsigned int cpu_affinities[NR_CPUS];
102static unsigned int next_aff = 0;
103static int use_affinity = 0;
104
105pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
106
d8540fc5
PA
107#ifndef HAVE_CPU_SET_T
108typedef unsigned long cpu_set_t;
109# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
110# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
111#endif
112
2a7ac59d
MD
113static void set_affinity(void)
114{
95bc7fb9 115#if HAVE_SCHED_SETAFFINITY
2a7ac59d 116 cpu_set_t mask;
95bc7fb9
MD
117 int cpu, ret;
118#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
119
120 if (!use_affinity)
121 return;
122
d8540fc5 123#if HAVE_SCHED_SETAFFINITY
2a7ac59d
MD
124 ret = pthread_mutex_lock(&affinity_mutex);
125 if (ret) {
126 perror("Error in pthread mutex lock");
127 exit(-1);
128 }
129 cpu = cpu_affinities[next_aff++];
130 ret = pthread_mutex_unlock(&affinity_mutex);
131 if (ret) {
132 perror("Error in pthread mutex unlock");
133 exit(-1);
134 }
d8540fc5 135
2a7ac59d
MD
136 CPU_ZERO(&mask);
137 CPU_SET(cpu, &mask);
d8540fc5
PA
138#if SCHED_SETAFFINITY_ARGS == 2
139 sched_setaffinity(0, &mask);
140#else
2a7ac59d 141 sched_setaffinity(0, sizeof(mask), &mask);
d8540fc5
PA
142#endif
143#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
144}
145
cf380c2f
MD
146/*
147 * returns 0 if test should end.
148 */
5873cd17 149static int test_duration_write(void)
cf380c2f 150{
78efb485 151 return !test_stop;
5873cd17
MD
152}
153
154static int test_duration_read(void)
155{
78efb485 156 return !test_stop;
cf380c2f
MD
157}
158
bd252a04
MD
159static DEFINE_URCU_TLS(unsigned long long, nr_writes);
160static DEFINE_URCU_TLS(unsigned long long, nr_reads);
1eec319e 161
8c86b9a1
MD
162static unsigned int nr_readers;
163static unsigned int nr_writers;
164
c265818b
MD
165pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
166
167void rcu_copy_mutex_lock(void)
168{
169 int ret;
170 ret = pthread_mutex_lock(&rcu_copy_mutex);
171 if (ret) {
172 perror("Error in pthread mutex lock");
173 exit(-1);
174 }
175}
176
177void rcu_copy_mutex_unlock(void)
178{
179 int ret;
180
181 ret = pthread_mutex_unlock(&rcu_copy_mutex);
182 if (ret) {
183 perror("Error in pthread mutex unlock");
184 exit(-1);
185 }
186}
f69f195a 187
bb488185
MD
188/*
189 * malloc/free are reusing memory areas too quickly, which does not let us
190 * test races appropriately. Use a large circular array for allocations.
d4267b0b
MD
191 * ARRAY_SIZE is larger than nr_writers, and we keep the mutex across
192 * both alloc and free, which insures we never run over our tail.
bb488185 193 */
8c86b9a1 194#define ARRAY_SIZE (1048576 * nr_writers)
bb488185
MD
195#define ARRAY_POISON 0xDEADBEEF
196static int array_index;
8c86b9a1 197static struct test_array *test_array;
bb488185
MD
198
199static struct test_array *test_array_alloc(void)
200{
201 struct test_array *ret;
202 int index;
203
bb488185
MD
204 index = array_index % ARRAY_SIZE;
205 assert(test_array[index].a == ARRAY_POISON ||
206 test_array[index].a == 0);
207 ret = &test_array[index];
208 array_index++;
209 if (array_index == ARRAY_SIZE)
210 array_index = 0;
bb488185
MD
211 return ret;
212}
213
214static void test_array_free(struct test_array *ptr)
215{
216 if (!ptr)
217 return;
bb488185 218 ptr->a = ARRAY_POISON;
bb488185
MD
219}
220
1eec319e 221void *thr_reader(void *_count)
f69f195a 222{
1eec319e 223 unsigned long long *count = _count;
41718ff9
MD
224 struct test_array *local_ptr;
225
4bad7d45 226 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
87bd15cd 227 "reader", pthread_self(), (unsigned long)gettid());
f69f195a 228
2a7ac59d
MD
229 set_affinity();
230
121a5d44 231 rcu_register_thread();
f69f195a 232
8c86b9a1
MD
233 while (!test_go)
234 {
235 }
5481ddb3 236 cmm_smp_mb();
8c86b9a1 237
cf380c2f 238 for (;;) {
1430ee0b 239 rcu_read_lock();
cf380c2f 240 local_ptr = rcu_dereference(test_rcu_pointer);
1de4df4b 241 rcu_debug_yield_read();
cf380c2f
MD
242 if (local_ptr)
243 assert(local_ptr->a == 8);
a0b7f7ea 244 if (caa_unlikely(rduration))
daddf5b0 245 loop_sleep(rduration);
1430ee0b 246 rcu_read_unlock();
bd252a04 247 URCU_TLS(nr_reads)++;
a0b7f7ea 248 if (caa_unlikely(!test_duration_read()))
cf380c2f 249 break;
41718ff9 250 }
f69f195a 251
121a5d44 252 rcu_unregister_thread();
41718ff9 253
2b514ae2
MD
254 /* test extra thread registration */
255 rcu_register_thread();
256 rcu_unregister_thread();
257
bd252a04 258 *count = URCU_TLS(nr_reads);
4bad7d45 259 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
cf380c2f 260 "reader", pthread_self(), (unsigned long)gettid());
f69f195a
MD
261 return ((void*)1);
262
263}
264
1eec319e 265void *thr_writer(void *_count)
f69f195a 266{
1eec319e 267 unsigned long long *count = _count;
41718ff9 268 struct test_array *new, *old;
f69f195a 269
4bad7d45 270 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
87bd15cd 271 "writer", pthread_self(), (unsigned long)gettid());
f69f195a 272
2a7ac59d
MD
273 set_affinity();
274
8c86b9a1
MD
275 while (!test_go)
276 {
277 }
5481ddb3 278 cmm_smp_mb();
8c86b9a1 279
cf380c2f 280 for (;;) {
d4267b0b 281 rcu_copy_mutex_lock();
bb488185 282 new = test_array_alloc();
ad6ce6ae 283 new->a = 8;
d2835e6f 284 old = rcu_xchg_pointer(&test_rcu_pointer, new);
a0b7f7ea 285 if (caa_unlikely(wduration))
31b598e0 286 loop_sleep(wduration);
d2835e6f 287 synchronize_rcu();
cf380c2f 288 if (old)
8c8eed97 289 old->a = 0;
bb488185 290 test_array_free(old);
d4267b0b 291 rcu_copy_mutex_unlock();
bd252a04 292 URCU_TLS(nr_writes)++;
a0b7f7ea 293 if (caa_unlikely(!test_duration_write()))
cf380c2f 294 break;
a0b7f7ea 295 if (caa_unlikely(wdelay))
9e31d0f0 296 loop_sleep(wdelay);
f69f195a
MD
297 }
298
4bad7d45 299 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
cf380c2f 300 "writer", pthread_self(), (unsigned long)gettid());
bd252a04 301 *count = URCU_TLS(nr_writes);
f69f195a
MD
302 return ((void*)2);
303}
ac260fd9 304
1430ee0b
MD
305void show_usage(int argc, char **argv)
306{
8c86b9a1 307 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
1430ee0b
MD
308#ifdef DEBUG_YIELD
309 printf(" [-r] [-w] (yield reader and/or writer)");
310#endif
7685d963 311 printf(" [-d delay] (writer period (us))");
daddf5b0 312 printf(" [-c duration] (reader C.S. duration (in loops))");
31b598e0 313 printf(" [-e duration] (writer C.S. duration (in loops))");
4bad7d45 314 printf(" [-v] (verbose output)");
9e97e478 315 printf(" [-a cpu#] [-a cpu#]... (affinity)");
1430ee0b
MD
316 printf("\n");
317}
318
cf380c2f 319int main(int argc, char **argv)
ac260fd9 320{
f69f195a 321 int err;
8c86b9a1 322 pthread_t *tid_reader, *tid_writer;
f69f195a 323 void *tret;
8c86b9a1 324 unsigned long long *count_reader, *count_writer;
1eec319e 325 unsigned long long tot_reads = 0, tot_writes = 0;
9e97e478 326 int i, a;
f69f195a 327
8c86b9a1
MD
328 if (argc < 4) {
329 show_usage(argc, argv);
330 return -1;
331 }
332
333 err = sscanf(argv[1], "%u", &nr_readers);
334 if (err != 1) {
1430ee0b 335 show_usage(argc, argv);
cf380c2f
MD
336 return -1;
337 }
338
8c86b9a1
MD
339 err = sscanf(argv[2], "%u", &nr_writers);
340 if (err != 1) {
341 show_usage(argc, argv);
342 return -1;
343 }
344
345 err = sscanf(argv[3], "%lu", &duration);
cf380c2f 346 if (err != 1) {
1430ee0b 347 show_usage(argc, argv);
cf380c2f
MD
348 return -1;
349 }
350
8c86b9a1 351 for (i = 4; i < argc; i++) {
cf380c2f
MD
352 if (argv[i][0] != '-')
353 continue;
354 switch (argv[i][1]) {
bb488185 355#ifdef DEBUG_YIELD
cf380c2f 356 case 'r':
1de4df4b 357 rcu_yield_active |= RCU_YIELD_READ;
cf380c2f
MD
358 break;
359 case 'w':
1de4df4b 360 rcu_yield_active |= RCU_YIELD_WRITE;
cf380c2f 361 break;
bb488185 362#endif
9e97e478
MD
363 case 'a':
364 if (argc < i + 2) {
365 show_usage(argc, argv);
366 return -1;
367 }
368 a = atoi(argv[++i]);
2a7ac59d 369 cpu_affinities[next_aff++] = a;
9e97e478 370 use_affinity = 1;
4bad7d45 371 printf_verbose("Adding CPU %d affinity\n", a);
9e97e478 372 break;
8b632bab
MD
373 case 'c':
374 if (argc < i + 2) {
375 show_usage(argc, argv);
376 return -1;
377 }
9e31d0f0 378 rduration = atol(argv[++i]);
8b632bab 379 break;
8c86b9a1 380 case 'd':
7685d963 381 if (argc < i + 2) {
8c86b9a1
MD
382 show_usage(argc, argv);
383 return -1;
384 }
9e31d0f0 385 wdelay = atol(argv[++i]);
bb488185 386 break;
31b598e0
MD
387 case 'e':
388 if (argc < i + 2) {
389 show_usage(argc, argv);
390 return -1;
391 }
392 wduration = atol(argv[++i]);
393 break;
4bad7d45
MD
394 case 'v':
395 verbose_mode = 1;
396 break;
cf380c2f
MD
397 }
398 }
cf380c2f 399
4bad7d45 400 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
8c86b9a1 401 duration, nr_readers, nr_writers);
9e31d0f0 402 printf_verbose("Writer delay : %lu loops.\n", wdelay);
4bad7d45
MD
403 printf_verbose("Reader duration : %lu loops.\n", rduration);
404 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
87bd15cd
BW
405 "main", pthread_self(), (unsigned long)gettid());
406
92e8d9d6 407 test_array = calloc(1, sizeof(*test_array) * ARRAY_SIZE);
8c86b9a1
MD
408 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
409 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
410 count_reader = malloc(sizeof(*count_reader) * nr_readers);
411 count_writer = malloc(sizeof(*count_writer) * nr_writers);
412
2a7ac59d
MD
413 next_aff = 0;
414
8c86b9a1 415 for (i = 0; i < nr_readers; i++) {
1eec319e
MD
416 err = pthread_create(&tid_reader[i], NULL, thr_reader,
417 &count_reader[i]);
f69f195a
MD
418 if (err != 0)
419 exit(1);
420 }
8c86b9a1 421 for (i = 0; i < nr_writers; i++) {
1eec319e
MD
422 err = pthread_create(&tid_writer[i], NULL, thr_writer,
423 &count_writer[i]);
f69f195a
MD
424 if (err != 0)
425 exit(1);
426 }
427
5481ddb3 428 cmm_smp_mb();
8c86b9a1 429
78efb485
MD
430 test_go = 1;
431
432 sleep(duration);
433
434 test_stop = 1;
435
8c86b9a1 436 for (i = 0; i < nr_readers; i++) {
f69f195a
MD
437 err = pthread_join(tid_reader[i], &tret);
438 if (err != 0)
439 exit(1);
1eec319e 440 tot_reads += count_reader[i];
f69f195a 441 }
8c86b9a1 442 for (i = 0; i < nr_writers; i++) {
f69f195a
MD
443 err = pthread_join(tid_writer[i], &tret);
444 if (err != 0)
445 exit(1);
1eec319e 446 tot_writes += count_writer[i];
f69f195a 447 }
5873cd17 448
4bad7d45 449 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
1eec319e 450 tot_writes);
a50a7b43 451 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
cda3c71d 452 "nr_writers %3u "
9e31d0f0 453 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
a50a7b43 454 argv[0], duration, nr_readers, rduration, wduration,
4bad7d45
MD
455 nr_writers, wdelay, tot_reads, tot_writes,
456 tot_reads + tot_writes);
bb488185 457 test_array_free(test_rcu_pointer);
8c86b9a1
MD
458 free(test_array);
459 free(tid_reader);
460 free(tid_writer);
461 free(count_reader);
462 free(count_writer);
f69f195a 463 return 0;
ac260fd9 464}
This page took 0.052063 seconds and 4 git commands to generate.