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