Align the summary result
[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
4bad7d45
MD
83static int verbose_mode;
84
85#define printf_verbose(fmt, args...) \
86 do { \
87 if (verbose_mode) \
88 printf(fmt, args); \
89 } while (0)
90
10c48e14
MD
91/*
92 * returns 0 if test should end.
93 */
94static int test_duration_write(void)
95{
78efb485 96 return !test_stop;
10c48e14
MD
97}
98
99static int test_duration_read(void)
100{
78efb485 101 return !test_stop;
10c48e14
MD
102}
103
104static unsigned long long __thread nr_writes;
105static unsigned long long __thread nr_reads;
106
107static unsigned int nr_readers;
108static unsigned int nr_writers;
109
110pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
111
112void 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
122void 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
133void *thr_reader(void *_count)
134{
135 unsigned long long *count = _count;
136
4bad7d45 137 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
10c48e14
MD
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);
8b632bab 147 if (unlikely(rduration))
daddf5b0 148 loop_sleep(rduration);
10c48e14
MD
149 pthread_rwlock_unlock(&lock);
150 nr_reads++;
59d5a406 151 if (unlikely(!test_duration_read()))
10c48e14
MD
152 break;
153 }
154
155 *count = nr_reads;
4bad7d45 156 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
10c48e14
MD
157 "reader", pthread_self(), (unsigned long)gettid());
158 return ((void*)1);
159
160}
161
162void *thr_writer(void *_count)
163{
164 unsigned long long *count = _count;
165
4bad7d45 166 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
10c48e14
MD
167 "writer", pthread_self(), (unsigned long)gettid());
168
169 while (!test_go)
170 {
171 }
7685d963 172 smp_mb();
10c48e14
MD
173
174 for (;;) {
175 pthread_rwlock_wrlock(&lock);
2b4e4125 176 test_array.a = 0;
10c48e14
MD
177 test_array.a = 8;
178 pthread_rwlock_unlock(&lock);
179 nr_writes++;
59d5a406 180 if (unlikely(!test_duration_write()))
10c48e14 181 break;
59d5a406 182 if (unlikely(wdelay))
10c48e14
MD
183 usleep(wdelay);
184 }
185
4bad7d45 186 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
10c48e14
MD
187 "writer", pthread_self(), (unsigned long)gettid());
188 *count = nr_writes;
189 return ((void*)2);
190}
191
192void 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
7685d963 198 printf(" [-d delay] (writer period (us))");
daddf5b0 199 printf(" [-c duration] (reader C.S. duration (in loops))");
4bad7d45 200 printf(" [-v] (verbose output)");
9e97e478 201 printf(" [-a cpu#] [-a cpu#]... (affinity)");
10c48e14
MD
202 printf("\n");
203}
204
9e97e478
MD
205cpu_set_t affinity;
206
10c48e14
MD
207int 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;
9e97e478
MD
214 int i, a;
215 int use_affinity = 0;
10c48e14
MD
216
217 if (argc < 4) {
218 show_usage(argc, argv);
219 return -1;
220 }
7685d963 221 smp_mb();
10c48e14
MD
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
9e97e478
MD
241 CPU_ZERO(&affinity);
242
10c48e14
MD
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
9e97e478
MD
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;
4bad7d45 263 printf_verbose("Adding CPU %d affinity\n", a);
9e97e478 264 break;
8b632bab
MD
265 case 'c':
266 if (argc < i + 2) {
267 show_usage(argc, argv);
268 return -1;
269 }
270 rduration = atoi(argv[++i]);
271 break;
10c48e14
MD
272 case 'd':
273 if (argc < i + 2) {
274 show_usage(argc, argv);
275 return -1;
276 }
277 wdelay = atoi(argv[++i]);
278 break;
4bad7d45
MD
279 case 'v':
280 verbose_mode = 1;
281 break;
10c48e14
MD
282 }
283 }
284
4bad7d45 285 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
10c48e14 286 duration, nr_readers, nr_writers);
4bad7d45
MD
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",
10c48e14 290 "main", pthread_self(), (unsigned long)gettid());
9e97e478
MD
291 if (use_affinity
292 && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
293 perror("sched_setaffinity");
294 exit(-1);
295 }
10c48e14
MD
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
7685d963 315 smp_mb();
10c48e14 316
78efb485
MD
317 test_go = 1;
318
319 sleep(duration);
320
321 test_stop = 1;
322
10c48e14
MD
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 }
4bad7d45
MD
335
336 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
10c48e14 337 tot_writes);
cda3c71d
MD
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",
4bad7d45
MD
341 argv[0], duration, nr_readers, rduration,
342 nr_writers, wdelay, tot_reads, tot_writes,
343 tot_reads + tot_writes);
344
10c48e14
MD
345 free(tid_reader);
346 free(tid_writer);
347 free(count_reader);
348 free(count_writer);
349 return 0;
350}
This page took 0.035973 seconds and 4 git commands to generate.