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