Using AM_PROG_MKDIR_P for compatibility
[urcu.git] / tests / 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 <urcu/arch.h>
37
38/* hardcoded number of CPUs */
39#define NR_CPUS 16384
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
94static unsigned int cpu_affinities[NR_CPUS];
95static unsigned int next_aff = 0;
96static int use_affinity = 0;
97
98pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
99
100static void set_affinity(void)
101{
102 cpu_set_t mask;
103 int cpu;
104 int ret;
105
106 if (!use_affinity)
107 return;
108
109 ret = pthread_mutex_lock(&affinity_mutex);
110 if (ret) {
111 perror("Error in pthread mutex lock");
112 exit(-1);
113 }
114 cpu = cpu_affinities[next_aff++];
115 ret = pthread_mutex_unlock(&affinity_mutex);
116 if (ret) {
117 perror("Error in pthread mutex unlock");
118 exit(-1);
119 }
120 CPU_ZERO(&mask);
121 CPU_SET(cpu, &mask);
122 sched_setaffinity(0, sizeof(mask), &mask);
123}
124
125/*
126 * returns 0 if test should end.
127 */
128static int test_duration_write(void)
129{
130 return !test_stop;
131}
132
133static int test_duration_read(void)
134{
135 return !test_stop;
136}
137
138static unsigned long long __thread nr_writes;
139static unsigned long long __thread nr_reads;
140
141static unsigned int nr_readers;
142static unsigned int nr_writers;
143
144pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
145
146void rcu_copy_mutex_lock(void)
147{
148 int ret;
149 ret = pthread_mutex_lock(&rcu_copy_mutex);
150 if (ret) {
151 perror("Error in pthread mutex lock");
152 exit(-1);
153 }
154}
155
156void rcu_copy_mutex_unlock(void)
157{
158 int ret;
159
160 ret = pthread_mutex_unlock(&rcu_copy_mutex);
161 if (ret) {
162 perror("Error in pthread mutex unlock");
163 exit(-1);
164 }
165}
166
167void *thr_reader(void *_count)
168{
169 unsigned long long *count = _count;
170
171 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
172 "reader", pthread_self(), (unsigned long)gettid());
173
174 set_affinity();
175
176 while (!test_go)
177 {
178 }
179
180 for (;;) {
181 pthread_rwlock_rdlock(&lock);
182 assert(test_array.a == 8);
183 if (unlikely(rduration))
184 loop_sleep(rduration);
185 pthread_rwlock_unlock(&lock);
186 nr_reads++;
187 if (unlikely(!test_duration_read()))
188 break;
189 }
190
191 *count = nr_reads;
192 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
193 "reader", pthread_self(), (unsigned long)gettid());
194 return ((void*)1);
195
196}
197
198void *thr_writer(void *_count)
199{
200 unsigned long long *count = _count;
201
202 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
203 "writer", pthread_self(), (unsigned long)gettid());
204
205 set_affinity();
206
207 while (!test_go)
208 {
209 }
210 smp_mb();
211
212 for (;;) {
213 pthread_rwlock_wrlock(&lock);
214 test_array.a = 0;
215 test_array.a = 8;
216 pthread_rwlock_unlock(&lock);
217 nr_writes++;
218 if (unlikely(!test_duration_write()))
219 break;
220 if (unlikely(wdelay))
221 loop_sleep(wdelay);
222 }
223
224 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
225 "writer", pthread_self(), (unsigned long)gettid());
226 *count = nr_writes;
227 return ((void*)2);
228}
229
230void show_usage(int argc, char **argv)
231{
232 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
233#ifdef DEBUG_YIELD
234 printf(" [-r] [-w] (yield reader and/or writer)");
235#endif
236 printf(" [-d delay] (writer period (us))");
237 printf(" [-c duration] (reader C.S. duration (in loops))");
238 printf(" [-v] (verbose output)");
239 printf(" [-a cpu#] [-a cpu#]... (affinity)");
240 printf("\n");
241}
242
243int main(int argc, char **argv)
244{
245 int err;
246 pthread_t *tid_reader, *tid_writer;
247 void *tret;
248 unsigned long long *count_reader, *count_writer;
249 unsigned long long tot_reads = 0, tot_writes = 0;
250 int i, a;
251
252 if (argc < 4) {
253 show_usage(argc, argv);
254 return -1;
255 }
256 smp_mb();
257
258 err = sscanf(argv[1], "%u", &nr_readers);
259 if (err != 1) {
260 show_usage(argc, argv);
261 return -1;
262 }
263
264 err = sscanf(argv[2], "%u", &nr_writers);
265 if (err != 1) {
266 show_usage(argc, argv);
267 return -1;
268 }
269
270 err = sscanf(argv[3], "%lu", &duration);
271 if (err != 1) {
272 show_usage(argc, argv);
273 return -1;
274 }
275
276 for (i = 4; i < argc; i++) {
277 if (argv[i][0] != '-')
278 continue;
279 switch (argv[i][1]) {
280#ifdef DEBUG_YIELD
281 case 'r':
282 yield_active |= YIELD_READ;
283 break;
284 case 'w':
285 yield_active |= YIELD_WRITE;
286 break;
287#endif
288 case 'a':
289 if (argc < i + 2) {
290 show_usage(argc, argv);
291 return -1;
292 }
293 a = atoi(argv[++i]);
294 cpu_affinities[next_aff++] = a;
295 use_affinity = 1;
296 printf_verbose("Adding CPU %d affinity\n", a);
297 break;
298 case 'c':
299 if (argc < i + 2) {
300 show_usage(argc, argv);
301 return -1;
302 }
303 rduration = atol(argv[++i]);
304 break;
305 case 'd':
306 if (argc < i + 2) {
307 show_usage(argc, argv);
308 return -1;
309 }
310 wdelay = atol(argv[++i]);
311 break;
312 case 'v':
313 verbose_mode = 1;
314 break;
315 }
316 }
317
318 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
319 duration, nr_readers, nr_writers);
320 printf_verbose("Writer delay : %lu loops.\n", wdelay);
321 printf_verbose("Reader duration : %lu loops.\n", rduration);
322 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
323 "main", pthread_self(), (unsigned long)gettid());
324
325 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
326 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
327 count_reader = malloc(sizeof(*count_reader) * nr_readers);
328 count_writer = malloc(sizeof(*count_writer) * nr_writers);
329
330 next_aff = 0;
331
332 for (i = 0; i < nr_readers; i++) {
333 err = pthread_create(&tid_reader[i], NULL, thr_reader,
334 &count_reader[i]);
335 if (err != 0)
336 exit(1);
337 }
338 for (i = 0; i < nr_writers; i++) {
339 err = pthread_create(&tid_writer[i], NULL, thr_writer,
340 &count_writer[i]);
341 if (err != 0)
342 exit(1);
343 }
344
345 smp_mb();
346
347 test_go = 1;
348
349 sleep(duration);
350
351 test_stop = 1;
352
353 for (i = 0; i < nr_readers; i++) {
354 err = pthread_join(tid_reader[i], &tret);
355 if (err != 0)
356 exit(1);
357 tot_reads += count_reader[i];
358 }
359 for (i = 0; i < nr_writers; i++) {
360 err = pthread_join(tid_writer[i], &tret);
361 if (err != 0)
362 exit(1);
363 tot_writes += count_writer[i];
364 }
365
366 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
367 tot_writes);
368 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
369 "nr_writers %3u "
370 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
371 argv[0], duration, nr_readers, rduration,
372 nr_writers, wdelay, tot_reads, tot_writes,
373 tot_reads + tot_writes);
374
375 free(tid_reader);
376 free(tid_writer);
377 free(count_reader);
378 free(count_writer);
379 return 0;
380}
This page took 0.022988 seconds and 4 git commands to generate.