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