fix runtests.sh
[urcu.git] / 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 <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>
34#include <sched.h>
35
36#include "arch.h"
37
38#if defined(_syscall0)
39_syscall0(pid_t, gettid)
40#elif defined(__NR_gettid)
41static inline pid_t gettid(void)
42{
43 return syscall(__NR_gettid);
44}
45#else
46#warning "use pid as tid"
47static inline pid_t gettid(void)
48{
49 return getpid();
50}
51#endif
52
53#ifndef DYNAMIC_LINK_TEST
54#define _LGPL_SOURCE
55#else
56#define debug_yield_read()
57#endif
58#include "urcu.h"
59
60struct test_array {
61 int a;
62};
63
64pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
65
66static volatile int test_go, test_stop;
67
68static int wdelay;
69
70static volatile struct test_array test_array = { 8 };
71
72static unsigned long duration;
73
74/*
75 * returns 0 if test should end.
76 */
77static int test_duration_write(void)
78{
79 return !test_stop;
80}
81
82static int test_duration_read(void)
83{
84 return !test_stop;
85}
86
87static unsigned long long __thread nr_writes;
88static unsigned long long __thread nr_reads;
89
90static unsigned int nr_readers;
91static unsigned int nr_writers;
92
93pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
94
95void rcu_copy_mutex_lock(void)
96{
97 int ret;
98 ret = pthread_mutex_lock(&rcu_copy_mutex);
99 if (ret) {
100 perror("Error in pthread mutex lock");
101 exit(-1);
102 }
103}
104
105void rcu_copy_mutex_unlock(void)
106{
107 int ret;
108
109 ret = pthread_mutex_unlock(&rcu_copy_mutex);
110 if (ret) {
111 perror("Error in pthread mutex unlock");
112 exit(-1);
113 }
114}
115
116void *thr_reader(void *_count)
117{
118 unsigned long long *count = _count;
119
120 printf("thread_begin %s, thread id : %lx, tid %lu\n",
121 "reader", pthread_self(), (unsigned long)gettid());
122
123 while (!test_go)
124 {
125 }
126
127 for (;;) {
128 pthread_rwlock_rdlock(&lock);
129 assert(test_array.a == 8);
130 pthread_rwlock_unlock(&lock);
131 nr_reads++;
132 if (unlikely(!test_duration_read()))
133 break;
134 }
135
136 *count = nr_reads;
137 printf("thread_end %s, thread id : %lx, tid %lu\n",
138 "reader", pthread_self(), (unsigned long)gettid());
139 return ((void*)1);
140
141}
142
143void *thr_writer(void *_count)
144{
145 unsigned long long *count = _count;
146
147 printf("thread_begin %s, thread id : %lx, tid %lu\n",
148 "writer", pthread_self(), (unsigned long)gettid());
149
150 while (!test_go)
151 {
152 }
153 smp_mb();
154
155 for (;;) {
156 pthread_rwlock_wrlock(&lock);
157 test_array.a = 0;
158 test_array.a = 8;
159 pthread_rwlock_unlock(&lock);
160 nr_writes++;
161 if (unlikely(!test_duration_write()))
162 break;
163 if (unlikely(wdelay))
164 usleep(wdelay);
165 }
166
167 printf("thread_end %s, thread id : %lx, tid %lu\n",
168 "writer", pthread_self(), (unsigned long)gettid());
169 *count = nr_writes;
170 return ((void*)2);
171}
172
173void show_usage(int argc, char **argv)
174{
175 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
176#ifdef DEBUG_YIELD
177 printf(" [-r] [-w] (yield reader and/or writer)");
178#endif
179 printf(" [-d delay] (writer period (us))");
180 printf(" [-a cpu#] [-a cpu#]... (affinity)");
181 printf("\n");
182}
183
184cpu_set_t affinity;
185
186int main(int argc, char **argv)
187{
188 int err;
189 pthread_t *tid_reader, *tid_writer;
190 void *tret;
191 unsigned long long *count_reader, *count_writer;
192 unsigned long long tot_reads = 0, tot_writes = 0;
193 int i, a;
194 int use_affinity = 0;
195
196 if (argc < 4) {
197 show_usage(argc, argv);
198 return -1;
199 }
200 smp_mb();
201
202 err = sscanf(argv[1], "%u", &nr_readers);
203 if (err != 1) {
204 show_usage(argc, argv);
205 return -1;
206 }
207
208 err = sscanf(argv[2], "%u", &nr_writers);
209 if (err != 1) {
210 show_usage(argc, argv);
211 return -1;
212 }
213
214 err = sscanf(argv[3], "%lu", &duration);
215 if (err != 1) {
216 show_usage(argc, argv);
217 return -1;
218 }
219
220 CPU_ZERO(&affinity);
221
222 for (i = 4; i < argc; i++) {
223 if (argv[i][0] != '-')
224 continue;
225 switch (argv[i][1]) {
226#ifdef DEBUG_YIELD
227 case 'r':
228 yield_active |= YIELD_READ;
229 break;
230 case 'w':
231 yield_active |= YIELD_WRITE;
232 break;
233#endif
234 case 'a':
235 if (argc < i + 2) {
236 show_usage(argc, argv);
237 return -1;
238 }
239 a = atoi(argv[++i]);
240 CPU_SET(a, &affinity);
241 use_affinity = 1;
242 printf("Adding CPU %d affinity\n", a);
243 break;
244 case 'd':
245 if (argc < i + 2) {
246 show_usage(argc, argv);
247 return -1;
248 }
249 wdelay = atoi(argv[++i]);
250 break;
251 }
252 }
253
254 printf("running test for %lu seconds, %u readers, %u writers.\n",
255 duration, nr_readers, nr_writers);
256 printf("Writer delay : %u us.\n", wdelay);
257 printf("thread %-6s, thread id : %lx, tid %lu\n",
258 "main", pthread_self(), (unsigned long)gettid());
259 if (use_affinity
260 && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
261 perror("sched_setaffinity");
262 exit(-1);
263 }
264
265 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
266 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
267 count_reader = malloc(sizeof(*count_reader) * nr_readers);
268 count_writer = malloc(sizeof(*count_writer) * nr_writers);
269
270 for (i = 0; i < nr_readers; i++) {
271 err = pthread_create(&tid_reader[i], NULL, thr_reader,
272 &count_reader[i]);
273 if (err != 0)
274 exit(1);
275 }
276 for (i = 0; i < nr_writers; i++) {
277 err = pthread_create(&tid_writer[i], NULL, thr_writer,
278 &count_writer[i]);
279 if (err != 0)
280 exit(1);
281 }
282
283 smp_mb();
284
285 test_go = 1;
286
287 sleep(duration);
288
289 test_stop = 1;
290
291 for (i = 0; i < nr_readers; i++) {
292 err = pthread_join(tid_reader[i], &tret);
293 if (err != 0)
294 exit(1);
295 tot_reads += count_reader[i];
296 }
297 for (i = 0; i < nr_writers; i++) {
298 err = pthread_join(tid_writer[i], &tret);
299 if (err != 0)
300 exit(1);
301 tot_writes += count_writer[i];
302 }
303
304 printf("total number of reads : %llu, writes %llu\n", tot_reads,
305 tot_writes);
306 free(tid_reader);
307 free(tid_writer);
308 free(count_reader);
309 free(count_writer);
310 return 0;
311}
This page took 0.022965 seconds and 4 git commands to generate.