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