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