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