Test qsbr debug
[urcu.git] / test_rwlock.c
... / ...
CommitLineData
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/* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
39#define CACHE_LINE_SIZE 4096
40
41#if defined(_syscall0)
42_syscall0(pid_t, gettid)
43#elif defined(__NR_gettid)
44static inline pid_t gettid(void)
45{
46 return syscall(__NR_gettid);
47}
48#else
49#warning "use pid as tid"
50static inline pid_t gettid(void)
51{
52 return getpid();
53}
54#endif
55
56#ifndef DYNAMIC_LINK_TEST
57#define _LGPL_SOURCE
58#else
59#define debug_yield_read()
60#endif
61#include "urcu.h"
62
63struct test_array {
64 int a;
65};
66
67pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
68
69static volatile int test_go, test_stop;
70
71static unsigned long wdelay;
72
73static volatile struct test_array test_array = { 8 };
74
75static unsigned long duration;
76
77/* read-side C.S. duration, in loops */
78static unsigned long rduration;
79
80static inline void loop_sleep(unsigned long l)
81{
82 while(l-- != 0)
83 cpu_relax();
84}
85
86static int verbose_mode;
87
88#define printf_verbose(fmt, args...) \
89 do { \
90 if (verbose_mode) \
91 printf(fmt, args); \
92 } while (0)
93
94/*
95 * returns 0 if test should end.
96 */
97static int test_duration_write(void)
98{
99 return !test_stop;
100}
101
102static int test_duration_read(void)
103{
104 return !test_stop;
105}
106
107static unsigned long long __thread nr_writes;
108static unsigned long long __thread nr_reads;
109
110static unsigned int nr_readers;
111static unsigned int nr_writers;
112
113pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
114
115void rcu_copy_mutex_lock(void)
116{
117 int ret;
118 ret = pthread_mutex_lock(&rcu_copy_mutex);
119 if (ret) {
120 perror("Error in pthread mutex lock");
121 exit(-1);
122 }
123}
124
125void rcu_copy_mutex_unlock(void)
126{
127 int ret;
128
129 ret = pthread_mutex_unlock(&rcu_copy_mutex);
130 if (ret) {
131 perror("Error in pthread mutex unlock");
132 exit(-1);
133 }
134}
135
136void *thr_reader(void *_count)
137{
138 unsigned long long *count = _count;
139
140 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
141 "reader", pthread_self(), (unsigned long)gettid());
142
143 while (!test_go)
144 {
145 }
146
147 for (;;) {
148 pthread_rwlock_rdlock(&lock);
149 assert(test_array.a == 8);
150 if (unlikely(rduration))
151 loop_sleep(rduration);
152 pthread_rwlock_unlock(&lock);
153 nr_reads++;
154 if (unlikely(!test_duration_read()))
155 break;
156 }
157
158 *count = nr_reads;
159 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
160 "reader", pthread_self(), (unsigned long)gettid());
161 return ((void*)1);
162
163}
164
165void *thr_writer(void *_count)
166{
167 unsigned long long *count = _count;
168
169 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
170 "writer", pthread_self(), (unsigned long)gettid());
171
172 while (!test_go)
173 {
174 }
175 smp_mb();
176
177 for (;;) {
178 pthread_rwlock_wrlock(&lock);
179 test_array.a = 0;
180 test_array.a = 8;
181 pthread_rwlock_unlock(&lock);
182 nr_writes++;
183 if (unlikely(!test_duration_write()))
184 break;
185 if (unlikely(wdelay))
186 loop_sleep(wdelay);
187 }
188
189 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
190 "writer", pthread_self(), (unsigned long)gettid());
191 *count = nr_writes;
192 return ((void*)2);
193}
194
195void show_usage(int argc, char **argv)
196{
197 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
198#ifdef DEBUG_YIELD
199 printf(" [-r] [-w] (yield reader and/or writer)");
200#endif
201 printf(" [-d delay] (writer period (us))");
202 printf(" [-c duration] (reader C.S. duration (in loops))");
203 printf(" [-v] (verbose output)");
204 printf(" [-a cpu#] [-a cpu#]... (affinity)");
205 printf("\n");
206}
207
208cpu_set_t affinity;
209
210int main(int argc, char **argv)
211{
212 int err;
213 pthread_t *tid_reader, *tid_writer;
214 void *tret;
215 unsigned long long *count_reader, *count_writer;
216 unsigned long long tot_reads = 0, tot_writes = 0;
217 int i, a;
218 int use_affinity = 0;
219
220 if (argc < 4) {
221 show_usage(argc, argv);
222 return -1;
223 }
224 smp_mb();
225
226 err = sscanf(argv[1], "%u", &nr_readers);
227 if (err != 1) {
228 show_usage(argc, argv);
229 return -1;
230 }
231
232 err = sscanf(argv[2], "%u", &nr_writers);
233 if (err != 1) {
234 show_usage(argc, argv);
235 return -1;
236 }
237
238 err = sscanf(argv[3], "%lu", &duration);
239 if (err != 1) {
240 show_usage(argc, argv);
241 return -1;
242 }
243
244 CPU_ZERO(&affinity);
245
246 for (i = 4; i < argc; i++) {
247 if (argv[i][0] != '-')
248 continue;
249 switch (argv[i][1]) {
250#ifdef DEBUG_YIELD
251 case 'r':
252 yield_active |= YIELD_READ;
253 break;
254 case 'w':
255 yield_active |= YIELD_WRITE;
256 break;
257#endif
258 case 'a':
259 if (argc < i + 2) {
260 show_usage(argc, argv);
261 return -1;
262 }
263 a = atoi(argv[++i]);
264 CPU_SET(a, &affinity);
265 use_affinity = 1;
266 printf_verbose("Adding CPU %d affinity\n", a);
267 break;
268 case 'c':
269 if (argc < i + 2) {
270 show_usage(argc, argv);
271 return -1;
272 }
273 rduration = atol(argv[++i]);
274 break;
275 case 'd':
276 if (argc < i + 2) {
277 show_usage(argc, argv);
278 return -1;
279 }
280 wdelay = atol(argv[++i]);
281 break;
282 case 'v':
283 verbose_mode = 1;
284 break;
285 }
286 }
287
288 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
289 duration, nr_readers, nr_writers);
290 printf_verbose("Writer delay : %lu loops.\n", wdelay);
291 printf_verbose("Reader duration : %lu loops.\n", rduration);
292 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
293 "main", pthread_self(), (unsigned long)gettid());
294 if (use_affinity
295 && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
296 perror("sched_setaffinity");
297 exit(-1);
298 }
299
300 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
301 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
302 count_reader = malloc(sizeof(*count_reader) * nr_readers);
303 count_writer = malloc(sizeof(*count_writer) * nr_writers);
304
305 for (i = 0; i < nr_readers; i++) {
306 err = pthread_create(&tid_reader[i], NULL, thr_reader,
307 &count_reader[i]);
308 if (err != 0)
309 exit(1);
310 }
311 for (i = 0; i < nr_writers; i++) {
312 err = pthread_create(&tid_writer[i], NULL, thr_writer,
313 &count_writer[i]);
314 if (err != 0)
315 exit(1);
316 }
317
318 smp_mb();
319
320 test_go = 1;
321
322 sleep(duration);
323
324 test_stop = 1;
325
326 for (i = 0; i < nr_readers; i++) {
327 err = pthread_join(tid_reader[i], &tret);
328 if (err != 0)
329 exit(1);
330 tot_reads += count_reader[i];
331 }
332 for (i = 0; i < nr_writers; i++) {
333 err = pthread_join(tid_writer[i], &tret);
334 if (err != 0)
335 exit(1);
336 tot_writes += count_writer[i];
337 }
338
339 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
340 tot_writes);
341 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
342 "nr_writers %3u "
343 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
344 argv[0], duration, nr_readers, rduration,
345 nr_writers, wdelay, tot_reads, tot_writes,
346 tot_reads + tot_writes);
347
348 free(tid_reader);
349 free(tid_writer);
350 free(count_reader);
351 free(count_writer);
352 return 0;
353}
This page took 0.022998 seconds and 4 git commands to generate.