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