Rename all memory primitives with prefix cmm_
[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@efficios.com>
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
82/* write-side C.S. duration, in loops */
83static unsigned long wduration;
84
85static inline void loop_sleep(unsigned long l)
86{
87 while(l-- != 0)
88 cpu_relax();
89}
90
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
99static unsigned int cpu_affinities[NR_CPUS];
100static unsigned int next_aff = 0;
101static int use_affinity = 0;
102
103pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
104
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
111static void set_affinity(void)
112{
113 cpu_set_t mask;
114 int cpu;
115 int ret;
116
117 if (!use_affinity)
118 return;
119
120#if HAVE_SCHED_SETAFFINITY
121 ret = pthread_mutex_lock(&affinity_mutex);
122 if (ret) {
123 perror("Error in pthread mutex lock");
124 exit(-1);
125 }
126 cpu = cpu_affinities[next_aff++];
127 ret = pthread_mutex_unlock(&affinity_mutex);
128 if (ret) {
129 perror("Error in pthread mutex unlock");
130 exit(-1);
131 }
132
133 CPU_ZERO(&mask);
134 CPU_SET(cpu, &mask);
135#if SCHED_SETAFFINITY_ARGS == 2
136 sched_setaffinity(0, &mask);
137#else
138 sched_setaffinity(0, sizeof(mask), &mask);
139#endif
140#endif /* HAVE_SCHED_SETAFFINITY */
141}
142
143/*
144 * returns 0 if test should end.
145 */
146static int test_duration_write(void)
147{
148 return !test_stop;
149}
150
151static int test_duration_read(void)
152{
153 return !test_stop;
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
189 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
190 "reader", pthread_self(), (unsigned long)gettid());
191
192 set_affinity();
193
194 while (!test_go)
195 {
196 }
197
198 for (;;) {
199 pthread_rwlock_rdlock(&lock);
200 assert(test_array.a == 8);
201 if (unlikely(rduration))
202 loop_sleep(rduration);
203 pthread_rwlock_unlock(&lock);
204 nr_reads++;
205 if (unlikely(!test_duration_read()))
206 break;
207 }
208
209 *count = nr_reads;
210 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
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
220 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
221 "writer", pthread_self(), (unsigned long)gettid());
222
223 set_affinity();
224
225 while (!test_go)
226 {
227 }
228 cmm_smp_mb();
229
230 for (;;) {
231 pthread_rwlock_wrlock(&lock);
232 test_array.a = 0;
233 test_array.a = 8;
234 if (unlikely(wduration))
235 loop_sleep(wduration);
236 pthread_rwlock_unlock(&lock);
237 nr_writes++;
238 if (unlikely(!test_duration_write()))
239 break;
240 if (unlikely(wdelay))
241 loop_sleep(wdelay);
242 }
243
244 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
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
256 printf(" [-d delay] (writer period (us))");
257 printf(" [-c duration] (reader C.S. duration (in loops))");
258 printf(" [-e duration] (writer C.S. duration (in loops))");
259 printf(" [-v] (verbose output)");
260 printf(" [-a cpu#] [-a cpu#]... (affinity)");
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;
271 int i, a;
272
273 if (argc < 4) {
274 show_usage(argc, argv);
275 return -1;
276 }
277 cmm_smp_mb();
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
309 case 'a':
310 if (argc < i + 2) {
311 show_usage(argc, argv);
312 return -1;
313 }
314 a = atoi(argv[++i]);
315 cpu_affinities[next_aff++] = a;
316 use_affinity = 1;
317 printf_verbose("Adding CPU %d affinity\n", a);
318 break;
319 case 'c':
320 if (argc < i + 2) {
321 show_usage(argc, argv);
322 return -1;
323 }
324 rduration = atol(argv[++i]);
325 break;
326 case 'd':
327 if (argc < i + 2) {
328 show_usage(argc, argv);
329 return -1;
330 }
331 wdelay = atol(argv[++i]);
332 break;
333 case 'e':
334 if (argc < i + 2) {
335 show_usage(argc, argv);
336 return -1;
337 }
338 wduration = atol(argv[++i]);
339 break;
340 case 'v':
341 verbose_mode = 1;
342 break;
343 }
344 }
345
346 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
347 duration, nr_readers, nr_writers);
348 printf_verbose("Writer delay : %lu loops.\n", wdelay);
349 printf_verbose("Reader duration : %lu loops.\n", rduration);
350 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
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
358 next_aff = 0;
359
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
373 cmm_smp_mb();
374
375 test_go = 1;
376
377 sleep(duration);
378
379 test_stop = 1;
380
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 }
393
394 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
395 tot_writes);
396 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
397 "nr_writers %3u "
398 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
399 argv[0], duration, nr_readers, rduration, wduration,
400 nr_writers, wdelay, tot_reads, tot_writes,
401 tot_reads + tot_writes);
402
403 free(tid_reader);
404 free(tid_writer);
405 free(count_reader);
406 free(count_writer);
407 return 0;
408}
This page took 0.023159 seconds and 4 git commands to generate.