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