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