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