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