Remove glibc < 2.4 compat code for sched_setaffinity
[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 <assert.h>
32 #include <errno.h>
33
34 #include <urcu/arch.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 #if 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 #if 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 void rcu_copy_mutex_lock(void)
141 {
142 int ret;
143 ret = pthread_mutex_lock(&rcu_copy_mutex);
144 if (ret) {
145 perror("Error in pthread mutex lock");
146 exit(-1);
147 }
148 }
149
150 void rcu_copy_mutex_unlock(void)
151 {
152 int ret;
153
154 ret = pthread_mutex_unlock(&rcu_copy_mutex);
155 if (ret) {
156 perror("Error in pthread mutex unlock");
157 exit(-1);
158 }
159 }
160
161 void *thr_reader(void *data)
162 {
163 unsigned long tidx = (unsigned long)data;
164
165 printf_verbose("thread_begin %s, tid %lu\n",
166 "reader", urcu_get_thread_id());
167
168 set_affinity();
169
170 while (!test_go)
171 {
172 }
173
174 for (;;) {
175 int v;
176
177 pthread_mutex_lock(&lock);
178 v = test_array.a;
179 assert(v == 8);
180 if (caa_unlikely(rduration))
181 loop_sleep(rduration);
182 pthread_mutex_unlock(&lock);
183 URCU_TLS(nr_reads)++;
184 if (caa_unlikely(!test_duration_read()))
185 break;
186 }
187
188 tot_nr_reads[tidx] = URCU_TLS(nr_reads);
189 printf_verbose("thread_end %s, tid %lu\n",
190 "reader", urcu_get_thread_id());
191 return ((void*)1);
192
193 }
194
195 void *thr_writer(void *data)
196 {
197 unsigned long wtidx = (unsigned long)data;
198
199 printf_verbose("thread_begin %s, tid %lu\n",
200 "writer", urcu_get_thread_id());
201
202 set_affinity();
203
204 while (!test_go)
205 {
206 }
207 cmm_smp_mb();
208
209 for (;;) {
210 pthread_mutex_lock(&lock);
211 test_array.a = 0;
212 test_array.a = 8;
213 if (caa_unlikely(wduration))
214 loop_sleep(wduration);
215 pthread_mutex_unlock(&lock);
216 URCU_TLS(nr_writes)++;
217 if (caa_unlikely(!test_duration_write()))
218 break;
219 if (caa_unlikely(wdelay))
220 loop_sleep(wdelay);
221 }
222
223 printf_verbose("thread_end %s, tid %lu\n",
224 "writer", urcu_get_thread_id());
225 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
226 return ((void*)2);
227 }
228
229 void show_usage(int argc, char **argv)
230 {
231 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
232 argv[0]);
233 printf("OPTIONS:\n");
234 printf(" [-d delay] (writer period (us))\n");
235 printf(" [-c duration] (reader C.S. duration (in loops))\n");
236 printf(" [-e duration] (writer C.S. duration (in loops))\n");
237 printf(" [-v] (verbose output)\n");
238 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
239 printf("\n");
240 }
241
242 int main(int argc, char **argv)
243 {
244 int err;
245 pthread_t *tid_reader, *tid_writer;
246 void *tret;
247 unsigned long long *count_reader, *count_writer;
248 unsigned long long tot_reads = 0, tot_writes = 0;
249 int i, a;
250 unsigned int i_thr;
251
252 if (argc < 4) {
253 show_usage(argc, argv);
254 return -1;
255 }
256 cmm_smp_mb();
257
258 err = sscanf(argv[1], "%u", &nr_readers);
259 if (err != 1) {
260 show_usage(argc, argv);
261 return -1;
262 }
263
264 err = sscanf(argv[2], "%u", &nr_writers);
265 if (err != 1) {
266 show_usage(argc, argv);
267 return -1;
268 }
269
270 err = sscanf(argv[3], "%lu", &duration);
271 if (err != 1) {
272 show_usage(argc, argv);
273 return -1;
274 }
275
276 for (i = 4; i < argc; i++) {
277 if (argv[i][0] != '-')
278 continue;
279 switch (argv[i][1]) {
280 case 'a':
281 if (argc < i + 2) {
282 show_usage(argc, argv);
283 return -1;
284 }
285 a = atoi(argv[++i]);
286 cpu_affinities[next_aff++] = a;
287 use_affinity = 1;
288 printf_verbose("Adding CPU %d affinity\n", a);
289 break;
290 case 'c':
291 if (argc < i + 2) {
292 show_usage(argc, argv);
293 return -1;
294 }
295 rduration = atol(argv[++i]);
296 break;
297 case 'd':
298 if (argc < i + 2) {
299 show_usage(argc, argv);
300 return -1;
301 }
302 wdelay = atol(argv[++i]);
303 break;
304 case 'e':
305 if (argc < i + 2) {
306 show_usage(argc, argv);
307 return -1;
308 }
309 wduration = atol(argv[++i]);
310 break;
311 case 'v':
312 verbose_mode = 1;
313 break;
314 }
315 }
316
317 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
318 duration, nr_readers, nr_writers);
319 printf_verbose("Writer delay : %lu loops.\n", wdelay);
320 printf_verbose("Reader duration : %lu loops.\n", rduration);
321 printf_verbose("thread %-6s, tid %lu\n",
322 "main", urcu_get_thread_id());
323
324 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
325 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
326 count_reader = calloc(nr_readers, sizeof(*count_reader));
327 count_writer = calloc(nr_writers, sizeof(*count_writer));
328 tot_nr_reads = calloc(nr_readers, sizeof(*tot_nr_reads));
329 tot_nr_writes = calloc(nr_writers, sizeof(*tot_nr_writes));
330
331 next_aff = 0;
332
333 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
334 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
335 (void *)(long)i_thr);
336 if (err != 0)
337 exit(1);
338 }
339 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
340 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
341 (void *)(long)i_thr);
342 if (err != 0)
343 exit(1);
344 }
345
346 cmm_smp_mb();
347
348 test_go = 1;
349
350 sleep(duration);
351
352 test_stop = 1;
353
354 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
355 err = pthread_join(tid_reader[i_thr], &tret);
356 if (err != 0)
357 exit(1);
358 tot_reads += tot_nr_reads[i_thr];
359 }
360 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
361 err = pthread_join(tid_writer[i_thr], &tret);
362 if (err != 0)
363 exit(1);
364 tot_writes += tot_nr_writes[i_thr];
365 }
366
367 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
368 tot_writes);
369 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
370 "nr_writers %3u "
371 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
372 argv[0], duration, nr_readers, rduration, wduration,
373 nr_writers, wdelay, tot_reads, tot_writes,
374 tot_reads + tot_writes);
375
376 free(tid_reader);
377 free(tid_writer);
378 free(count_reader);
379 free(count_writer);
380 free(tot_nr_reads);
381 free(tot_nr_writes);
382 return 0;
383 }
This page took 0.036111 seconds and 4 git commands to generate.