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