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