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