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