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