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