Using AM_PROG_MKDIR_P for compatibility
[urcu.git] / tests / test_rwlock.c
CommitLineData
10c48e14
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
9e97e478 23#define _GNU_SOURCE
10c48e14
MD
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>
9e97e478 34#include <sched.h>
10c48e14 35
ec4e58a3 36#include <urcu/arch.h>
7685d963 37
2a7ac59d
MD
38/* hardcoded number of CPUs */
39#define NR_CPUS 16384
40
10c48e14
MD
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
ec4e58a3 61#include <urcu.h>
10c48e14
MD
62
63struct test_array {
64 int a;
65};
66
67pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
68
78efb485 69static volatile int test_go, test_stop;
10c48e14 70
9e31d0f0 71static unsigned long wdelay;
10c48e14 72
2b4e4125 73static volatile struct test_array test_array = { 8 };
10c48e14
MD
74
75static unsigned long duration;
10c48e14 76
daddf5b0 77/* read-side C.S. duration, in loops */
8b632bab
MD
78static unsigned long rduration;
79
daddf5b0
MD
80static inline void loop_sleep(unsigned long l)
81{
82 while(l-- != 0)
83 cpu_relax();
84}
85
4bad7d45
MD
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
2a7ac59d
MD
94static unsigned int cpu_affinities[NR_CPUS];
95static unsigned int next_aff = 0;
96static int use_affinity = 0;
97
6af882ba
MD
98pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
99
2a7ac59d
MD
100static void set_affinity(void)
101{
102 cpu_set_t mask;
103 int cpu;
6af882ba 104 int ret;
2a7ac59d
MD
105
106 if (!use_affinity)
107 return;
108
6af882ba
MD
109 ret = pthread_mutex_lock(&affinity_mutex);
110 if (ret) {
111 perror("Error in pthread mutex lock");
112 exit(-1);
113 }
2a7ac59d 114 cpu = cpu_affinities[next_aff++];
6af882ba
MD
115 ret = pthread_mutex_unlock(&affinity_mutex);
116 if (ret) {
117 perror("Error in pthread mutex unlock");
118 exit(-1);
119 }
2a7ac59d
MD
120 CPU_ZERO(&mask);
121 CPU_SET(cpu, &mask);
122 sched_setaffinity(0, sizeof(mask), &mask);
123}
124
10c48e14
MD
125/*
126 * returns 0 if test should end.
127 */
128static int test_duration_write(void)
129{
78efb485 130 return !test_stop;
10c48e14
MD
131}
132
133static int test_duration_read(void)
134{
78efb485 135 return !test_stop;
10c48e14
MD
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
4bad7d45 171 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
10c48e14
MD
172 "reader", pthread_self(), (unsigned long)gettid());
173
2a7ac59d
MD
174 set_affinity();
175
10c48e14
MD
176 while (!test_go)
177 {
178 }
179
180 for (;;) {
181 pthread_rwlock_rdlock(&lock);
182 assert(test_array.a == 8);
8b632bab 183 if (unlikely(rduration))
daddf5b0 184 loop_sleep(rduration);
10c48e14
MD
185 pthread_rwlock_unlock(&lock);
186 nr_reads++;
59d5a406 187 if (unlikely(!test_duration_read()))
10c48e14
MD
188 break;
189 }
190
191 *count = nr_reads;
4bad7d45 192 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
10c48e14
MD
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
4bad7d45 202 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
10c48e14
MD
203 "writer", pthread_self(), (unsigned long)gettid());
204
2a7ac59d
MD
205 set_affinity();
206
10c48e14
MD
207 while (!test_go)
208 {
209 }
7685d963 210 smp_mb();
10c48e14
MD
211
212 for (;;) {
213 pthread_rwlock_wrlock(&lock);
2b4e4125 214 test_array.a = 0;
10c48e14
MD
215 test_array.a = 8;
216 pthread_rwlock_unlock(&lock);
217 nr_writes++;
59d5a406 218 if (unlikely(!test_duration_write()))
10c48e14 219 break;
59d5a406 220 if (unlikely(wdelay))
9e31d0f0 221 loop_sleep(wdelay);
10c48e14
MD
222 }
223
4bad7d45 224 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
10c48e14
MD
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
7685d963 236 printf(" [-d delay] (writer period (us))");
daddf5b0 237 printf(" [-c duration] (reader C.S. duration (in loops))");
4bad7d45 238 printf(" [-v] (verbose output)");
9e97e478 239 printf(" [-a cpu#] [-a cpu#]... (affinity)");
10c48e14
MD
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;
9e97e478 250 int i, a;
10c48e14
MD
251
252 if (argc < 4) {
253 show_usage(argc, argv);
254 return -1;
255 }
7685d963 256 smp_mb();
10c48e14
MD
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
9e97e478
MD
288 case 'a':
289 if (argc < i + 2) {
290 show_usage(argc, argv);
291 return -1;
292 }
293 a = atoi(argv[++i]);
2a7ac59d 294 cpu_affinities[next_aff++] = a;
9e97e478 295 use_affinity = 1;
4bad7d45 296 printf_verbose("Adding CPU %d affinity\n", a);
9e97e478 297 break;
8b632bab
MD
298 case 'c':
299 if (argc < i + 2) {
300 show_usage(argc, argv);
301 return -1;
302 }
9e31d0f0 303 rduration = atol(argv[++i]);
8b632bab 304 break;
10c48e14
MD
305 case 'd':
306 if (argc < i + 2) {
307 show_usage(argc, argv);
308 return -1;
309 }
9e31d0f0 310 wdelay = atol(argv[++i]);
10c48e14 311 break;
4bad7d45
MD
312 case 'v':
313 verbose_mode = 1;
314 break;
10c48e14
MD
315 }
316 }
317
4bad7d45 318 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
10c48e14 319 duration, nr_readers, nr_writers);
9e31d0f0 320 printf_verbose("Writer delay : %lu loops.\n", wdelay);
4bad7d45
MD
321 printf_verbose("Reader duration : %lu loops.\n", rduration);
322 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
10c48e14
MD
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
2a7ac59d
MD
330 next_aff = 0;
331
10c48e14
MD
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
7685d963 345 smp_mb();
10c48e14 346
78efb485
MD
347 test_go = 1;
348
349 sleep(duration);
350
351 test_stop = 1;
352
10c48e14
MD
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 }
4bad7d45
MD
365
366 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
10c48e14 367 tot_writes);
6a190115 368 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
cda3c71d 369 "nr_writers %3u "
9e31d0f0 370 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
4bad7d45
MD
371 argv[0], duration, nr_readers, rduration,
372 nr_writers, wdelay, tot_reads, tot_writes,
373 tot_reads + tot_writes);
374
10c48e14
MD
375 free(tid_reader);
376 free(tid_writer);
377 free(count_reader);
378 free(count_writer);
379 return 0;
380}
This page took 0.060253 seconds and 4 git commands to generate.