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