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