Remove glibc < 2.4 compat code for sched_setaffinity
[urcu.git] / tests / benchmark / test_mutex.c
CommitLineData
51299083
MD
1/*
2 * test_urcu.c
3 *
4 * Userspace RCU library - test program
5 *
6982d6d7 6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
51299083
MD
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
51299083
MD
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>
94b343fd 32#include <errno.h>
51299083 33
ec4e58a3 34#include <urcu/arch.h>
bd252a04 35#include <urcu/tls-compat.h>
94df6318 36#include "thread-id.h"
65fcc7e9 37
2a7ac59d
MD
38/* hardcoded number of CPUs */
39#define NR_CPUS 16384
40
51299083
MD
41#ifndef DYNAMIC_LINK_TEST
42#define _LGPL_SOURCE
51299083 43#endif
ec4e58a3 44#include <urcu.h>
51299083
MD
45
46struct test_array {
47 int a;
48};
49
f61dfd97 50static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
51299083
MD
51
52static volatile int test_go, test_stop;
53
9e31d0f0 54static unsigned long wdelay;
51299083
MD
55
56static volatile struct test_array test_array = { 8 };
57
58static unsigned long duration;
59
daddf5b0 60/* read-side C.S. duration, in loops */
8b632bab
MD
61static unsigned long rduration;
62
31b598e0
MD
63/* write-side C.S. duration, in loops */
64static unsigned long wduration;
65
ab0aacbe 66static inline void loop_sleep(unsigned long loops)
daddf5b0 67{
ab0aacbe 68 while (loops-- != 0)
06f22bdb 69 caa_cpu_relax();
daddf5b0
MD
70}
71
4bad7d45
MD
72static int verbose_mode;
73
74#define printf_verbose(fmt, args...) \
75 do { \
76 if (verbose_mode) \
77 printf(fmt, args); \
78 } while (0)
79
2a7ac59d
MD
80static unsigned int cpu_affinities[NR_CPUS];
81static unsigned int next_aff = 0;
82static int use_affinity = 0;
83
6af882ba
MD
84pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
85
2a7ac59d
MD
86static void set_affinity(void)
87{
95bc7fb9 88#if HAVE_SCHED_SETAFFINITY
2a7ac59d 89 cpu_set_t mask;
95bc7fb9
MD
90 int cpu, ret;
91#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
92
93 if (!use_affinity)
94 return;
95
d8540fc5 96#if HAVE_SCHED_SETAFFINITY
6af882ba
MD
97 ret = pthread_mutex_lock(&affinity_mutex);
98 if (ret) {
99 perror("Error in pthread mutex lock");
100 exit(-1);
101 }
2a7ac59d 102 cpu = cpu_affinities[next_aff++];
6af882ba
MD
103 ret = pthread_mutex_unlock(&affinity_mutex);
104 if (ret) {
105 perror("Error in pthread mutex unlock");
106 exit(-1);
107 }
2a7ac59d
MD
108 CPU_ZERO(&mask);
109 CPU_SET(cpu, &mask);
110 sched_setaffinity(0, sizeof(mask), &mask);
d8540fc5 111#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
112}
113
51299083
MD
114/*
115 * returns 0 if test should end.
116 */
117static int test_duration_write(void)
118{
119 return !test_stop;
120}
121
122static int test_duration_read(void)
123{
124 return !test_stop;
125}
126
bd252a04
MD
127static DEFINE_URCU_TLS(unsigned long long, nr_writes);
128static DEFINE_URCU_TLS(unsigned long long, nr_reads);
51299083 129
55271c13 130static
06f22bdb 131unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_writes;
55271c13 132static
06f22bdb 133unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_reads;
51299083
MD
134
135static unsigned int nr_readers;
136static unsigned int nr_writers;
137
138pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
139
140void 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
150void 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
161void *thr_reader(void *data)
162{
163 unsigned long tidx = (unsigned long)data;
164
94df6318
MD
165 printf_verbose("thread_begin %s, tid %lu\n",
166 "reader", urcu_get_thread_id());
51299083 167
2a7ac59d
MD
168 set_affinity();
169
51299083
MD
170 while (!test_go)
171 {
172 }
173
174 for (;;) {
19d0e7ef
MD
175 int v;
176
51299083 177 pthread_mutex_lock(&lock);
19d0e7ef
MD
178 v = test_array.a;
179 assert(v == 8);
a0b7f7ea 180 if (caa_unlikely(rduration))
daddf5b0 181 loop_sleep(rduration);
51299083 182 pthread_mutex_unlock(&lock);
bd252a04 183 URCU_TLS(nr_reads)++;
a0b7f7ea 184 if (caa_unlikely(!test_duration_read()))
51299083
MD
185 break;
186 }
187
bd252a04 188 tot_nr_reads[tidx] = URCU_TLS(nr_reads);
94df6318
MD
189 printf_verbose("thread_end %s, tid %lu\n",
190 "reader", urcu_get_thread_id());
51299083
MD
191 return ((void*)1);
192
193}
194
195void *thr_writer(void *data)
196{
197 unsigned long wtidx = (unsigned long)data;
198
94df6318
MD
199 printf_verbose("thread_begin %s, tid %lu\n",
200 "writer", urcu_get_thread_id());
51299083 201
2a7ac59d
MD
202 set_affinity();
203
51299083
MD
204 while (!test_go)
205 {
206 }
5481ddb3 207 cmm_smp_mb();
51299083
MD
208
209 for (;;) {
210 pthread_mutex_lock(&lock);
211 test_array.a = 0;
212 test_array.a = 8;
a0b7f7ea 213 if (caa_unlikely(wduration))
31b598e0 214 loop_sleep(wduration);
51299083 215 pthread_mutex_unlock(&lock);
bd252a04 216 URCU_TLS(nr_writes)++;
a0b7f7ea 217 if (caa_unlikely(!test_duration_write()))
51299083 218 break;
a0b7f7ea 219 if (caa_unlikely(wdelay))
9e31d0f0 220 loop_sleep(wdelay);
51299083
MD
221 }
222
94df6318
MD
223 printf_verbose("thread_end %s, tid %lu\n",
224 "writer", urcu_get_thread_id());
bd252a04 225 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
51299083
MD
226 return ((void*)2);
227}
228
229void show_usage(int argc, char **argv)
230{
06637138
MD
231 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
232 argv[0]);
233 printf("OPTIONS:\n");
06637138
MD
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");
51299083
MD
239 printf("\n");
240}
241
51299083
MD
242int 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;
83e334d0 250 unsigned int i_thr;
51299083
MD
251
252 if (argc < 4) {
253 show_usage(argc, argv);
254 return -1;
255 }
5481ddb3 256 cmm_smp_mb();
51299083
MD
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 }
83e334d0 269
51299083
MD
270 err = sscanf(argv[3], "%lu", &duration);
271 if (err != 1) {
272 show_usage(argc, argv);
273 return -1;
274 }
275
51299083
MD
276 for (i = 4; i < argc; i++) {
277 if (argv[i][0] != '-')
278 continue;
279 switch (argv[i][1]) {
51299083
MD
280 case 'a':
281 if (argc < i + 2) {
282 show_usage(argc, argv);
283 return -1;
284 }
285 a = atoi(argv[++i]);
2a7ac59d 286 cpu_affinities[next_aff++] = a;
51299083 287 use_affinity = 1;
4bad7d45 288 printf_verbose("Adding CPU %d affinity\n", a);
51299083 289 break;
8b632bab
MD
290 case 'c':
291 if (argc < i + 2) {
292 show_usage(argc, argv);
293 return -1;
294 }
9e31d0f0 295 rduration = atol(argv[++i]);
8b632bab 296 break;
51299083
MD
297 case 'd':
298 if (argc < i + 2) {
299 show_usage(argc, argv);
300 return -1;
301 }
9e31d0f0 302 wdelay = atol(argv[++i]);
51299083 303 break;
31b598e0
MD
304 case 'e':
305 if (argc < i + 2) {
306 show_usage(argc, argv);
307 return -1;
308 }
309 wduration = atol(argv[++i]);
310 break;
4bad7d45
MD
311 case 'v':
312 verbose_mode = 1;
313 break;
51299083
MD
314 }
315 }
316
4bad7d45 317 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
51299083 318 duration, nr_readers, nr_writers);
9e31d0f0 319 printf_verbose("Writer delay : %lu loops.\n", wdelay);
4bad7d45 320 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
321 printf_verbose("thread %-6s, tid %lu\n",
322 "main", urcu_get_thread_id());
51299083 323
9aa14175
MD
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));
51299083 330
2a7ac59d
MD
331 next_aff = 0;
332
83e334d0
MJ
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);
51299083
MD
336 if (err != 0)
337 exit(1);
338 }
83e334d0
MJ
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);
51299083
MD
342 if (err != 0)
343 exit(1);
344 }
345
5481ddb3 346 cmm_smp_mb();
51299083
MD
347
348 test_go = 1;
349
350 sleep(duration);
351
352 test_stop = 1;
353
83e334d0
MJ
354 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
355 err = pthread_join(tid_reader[i_thr], &tret);
51299083
MD
356 if (err != 0)
357 exit(1);
83e334d0 358 tot_reads += tot_nr_reads[i_thr];
51299083 359 }
83e334d0
MJ
360 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
361 err = pthread_join(tid_writer[i_thr], &tret);
51299083
MD
362 if (err != 0)
363 exit(1);
83e334d0 364 tot_writes += tot_nr_writes[i_thr];
51299083 365 }
4bad7d45
MD
366
367 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
51299083 368 tot_writes);
a50a7b43 369 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
cda3c71d 370 "nr_writers %3u "
9e31d0f0 371 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
a50a7b43 372 argv[0], duration, nr_readers, rduration, wduration,
4bad7d45
MD
373 nr_writers, wdelay, tot_reads, tot_writes,
374 tot_reads + tot_writes);
375
51299083
MD
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.058429 seconds and 4 git commands to generate.