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