fix: HAVE_SCHED_SETAFFINITY is not defined
[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{
2388c075 88#ifdef 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
2388c075 96#ifdef 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
61c3fb60 140static
51299083
MD
141void *thr_reader(void *data)
142{
143 unsigned long tidx = (unsigned long)data;
144
94df6318
MD
145 printf_verbose("thread_begin %s, tid %lu\n",
146 "reader", urcu_get_thread_id());
51299083 147
2a7ac59d
MD
148 set_affinity();
149
51299083
MD
150 while (!test_go)
151 {
152 }
153
154 for (;;) {
19d0e7ef
MD
155 int v;
156
51299083 157 pthread_mutex_lock(&lock);
19d0e7ef
MD
158 v = test_array.a;
159 assert(v == 8);
a0b7f7ea 160 if (caa_unlikely(rduration))
daddf5b0 161 loop_sleep(rduration);
51299083 162 pthread_mutex_unlock(&lock);
bd252a04 163 URCU_TLS(nr_reads)++;
a0b7f7ea 164 if (caa_unlikely(!test_duration_read()))
51299083
MD
165 break;
166 }
167
bd252a04 168 tot_nr_reads[tidx] = URCU_TLS(nr_reads);
94df6318
MD
169 printf_verbose("thread_end %s, tid %lu\n",
170 "reader", urcu_get_thread_id());
51299083
MD
171 return ((void*)1);
172
173}
174
61c3fb60 175static
51299083
MD
176void *thr_writer(void *data)
177{
178 unsigned long wtidx = (unsigned long)data;
179
94df6318
MD
180 printf_verbose("thread_begin %s, tid %lu\n",
181 "writer", urcu_get_thread_id());
51299083 182
2a7ac59d
MD
183 set_affinity();
184
51299083
MD
185 while (!test_go)
186 {
187 }
5481ddb3 188 cmm_smp_mb();
51299083
MD
189
190 for (;;) {
191 pthread_mutex_lock(&lock);
192 test_array.a = 0;
193 test_array.a = 8;
a0b7f7ea 194 if (caa_unlikely(wduration))
31b598e0 195 loop_sleep(wduration);
51299083 196 pthread_mutex_unlock(&lock);
bd252a04 197 URCU_TLS(nr_writes)++;
a0b7f7ea 198 if (caa_unlikely(!test_duration_write()))
51299083 199 break;
a0b7f7ea 200 if (caa_unlikely(wdelay))
9e31d0f0 201 loop_sleep(wdelay);
51299083
MD
202 }
203
94df6318
MD
204 printf_verbose("thread_end %s, tid %lu\n",
205 "writer", urcu_get_thread_id());
bd252a04 206 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
51299083
MD
207 return ((void*)2);
208}
209
61c3fb60 210static
70469b43 211void show_usage(char **argv)
51299083 212{
06637138
MD
213 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
214 argv[0]);
215 printf("OPTIONS:\n");
06637138
MD
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");
51299083
MD
221 printf("\n");
222}
223
51299083
MD
224int 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;
83e334d0 232 unsigned int i_thr;
51299083
MD
233
234 if (argc < 4) {
70469b43 235 show_usage(argv);
51299083
MD
236 return -1;
237 }
5481ddb3 238 cmm_smp_mb();
51299083
MD
239
240 err = sscanf(argv[1], "%u", &nr_readers);
241 if (err != 1) {
70469b43 242 show_usage(argv);
51299083
MD
243 return -1;
244 }
245
246 err = sscanf(argv[2], "%u", &nr_writers);
247 if (err != 1) {
70469b43 248 show_usage(argv);
51299083
MD
249 return -1;
250 }
83e334d0 251
51299083
MD
252 err = sscanf(argv[3], "%lu", &duration);
253 if (err != 1) {
70469b43 254 show_usage(argv);
51299083
MD
255 return -1;
256 }
257
51299083
MD
258 for (i = 4; i < argc; i++) {
259 if (argv[i][0] != '-')
260 continue;
261 switch (argv[i][1]) {
51299083
MD
262 case 'a':
263 if (argc < i + 2) {
70469b43 264 show_usage(argv);
51299083
MD
265 return -1;
266 }
267 a = atoi(argv[++i]);
2a7ac59d 268 cpu_affinities[next_aff++] = a;
51299083 269 use_affinity = 1;
4bad7d45 270 printf_verbose("Adding CPU %d affinity\n", a);
51299083 271 break;
8b632bab
MD
272 case 'c':
273 if (argc < i + 2) {
70469b43 274 show_usage(argv);
8b632bab
MD
275 return -1;
276 }
9e31d0f0 277 rduration = atol(argv[++i]);
8b632bab 278 break;
51299083
MD
279 case 'd':
280 if (argc < i + 2) {
70469b43 281 show_usage(argv);
51299083
MD
282 return -1;
283 }
9e31d0f0 284 wdelay = atol(argv[++i]);
51299083 285 break;
31b598e0
MD
286 case 'e':
287 if (argc < i + 2) {
70469b43 288 show_usage(argv);
31b598e0
MD
289 return -1;
290 }
291 wduration = atol(argv[++i]);
292 break;
4bad7d45
MD
293 case 'v':
294 verbose_mode = 1;
295 break;
51299083
MD
296 }
297 }
298
4bad7d45 299 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
51299083 300 duration, nr_readers, nr_writers);
9e31d0f0 301 printf_verbose("Writer delay : %lu loops.\n", wdelay);
4bad7d45 302 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
303 printf_verbose("thread %-6s, tid %lu\n",
304 "main", urcu_get_thread_id());
51299083 305
9aa14175
MD
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));
51299083 312
2a7ac59d
MD
313 next_aff = 0;
314
83e334d0
MJ
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);
51299083
MD
318 if (err != 0)
319 exit(1);
320 }
83e334d0
MJ
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);
51299083
MD
324 if (err != 0)
325 exit(1);
326 }
327
5481ddb3 328 cmm_smp_mb();
51299083
MD
329
330 test_go = 1;
331
332 sleep(duration);
333
334 test_stop = 1;
335
83e334d0
MJ
336 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
337 err = pthread_join(tid_reader[i_thr], &tret);
51299083
MD
338 if (err != 0)
339 exit(1);
83e334d0 340 tot_reads += tot_nr_reads[i_thr];
51299083 341 }
83e334d0
MJ
342 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
343 err = pthread_join(tid_writer[i_thr], &tret);
51299083
MD
344 if (err != 0)
345 exit(1);
83e334d0 346 tot_writes += tot_nr_writes[i_thr];
51299083 347 }
4bad7d45
MD
348
349 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
51299083 350 tot_writes);
a50a7b43 351 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
cda3c71d 352 "nr_writers %3u "
9e31d0f0 353 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
a50a7b43 354 argv[0], duration, nr_readers, rduration, wduration,
4bad7d45
MD
355 nr_writers, wdelay, tot_reads, tot_writes,
356 tot_reads + tot_writes);
357
51299083
MD
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.059309 seconds and 4 git commands to generate.