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