Version 0.8.0
[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
23#define _GNU_SOURCE
f5ab766e 24#include "config.h"
51299083
MD
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>
94b343fd 34#include <errno.h>
51299083 35
ec4e58a3 36#include <urcu/arch.h>
bd252a04 37#include <urcu/tls-compat.h>
2953b501 38#include "cpuset.h"
94df6318 39#include "thread-id.h"
65fcc7e9 40
2a7ac59d
MD
41/* hardcoded number of CPUs */
42#define NR_CPUS 16384
43
51299083
MD
44#ifndef DYNAMIC_LINK_TEST
45#define _LGPL_SOURCE
46#else
47#define debug_yield_read()
48#endif
ec4e58a3 49#include <urcu.h>
51299083
MD
50
51struct test_array {
52 int a;
53};
54
55static pthread_mutex_t lock;
56
57static volatile int test_go, test_stop;
58
9e31d0f0 59static unsigned long wdelay;
51299083
MD
60
61static volatile struct test_array test_array = { 8 };
62
63static unsigned long duration;
64
daddf5b0 65/* read-side C.S. duration, in loops */
8b632bab
MD
66static unsigned long rduration;
67
31b598e0
MD
68/* write-side C.S. duration, in loops */
69static unsigned long wduration;
70
ab0aacbe 71static inline void loop_sleep(unsigned long loops)
daddf5b0 72{
ab0aacbe 73 while (loops-- != 0)
06f22bdb 74 caa_cpu_relax();
daddf5b0
MD
75}
76
4bad7d45
MD
77static int verbose_mode;
78
79#define printf_verbose(fmt, args...) \
80 do { \
81 if (verbose_mode) \
82 printf(fmt, args); \
83 } while (0)
84
2a7ac59d
MD
85static unsigned int cpu_affinities[NR_CPUS];
86static unsigned int next_aff = 0;
87static int use_affinity = 0;
88
6af882ba
MD
89pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
90
2a7ac59d
MD
91static void set_affinity(void)
92{
95bc7fb9 93#if HAVE_SCHED_SETAFFINITY
2a7ac59d 94 cpu_set_t mask;
95bc7fb9
MD
95 int cpu, ret;
96#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
97
98 if (!use_affinity)
99 return;
100
d8540fc5 101#if HAVE_SCHED_SETAFFINITY
6af882ba
MD
102 ret = pthread_mutex_lock(&affinity_mutex);
103 if (ret) {
104 perror("Error in pthread mutex lock");
105 exit(-1);
106 }
2a7ac59d 107 cpu = cpu_affinities[next_aff++];
6af882ba
MD
108 ret = pthread_mutex_unlock(&affinity_mutex);
109 if (ret) {
110 perror("Error in pthread mutex unlock");
111 exit(-1);
112 }
2a7ac59d
MD
113 CPU_ZERO(&mask);
114 CPU_SET(cpu, &mask);
d8540fc5
PA
115#if SCHED_SETAFFINITY_ARGS == 2
116 sched_setaffinity(0, &mask);
117#else
2a7ac59d 118 sched_setaffinity(0, sizeof(mask), &mask);
d8540fc5
PA
119#endif
120#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
121}
122
51299083
MD
123/*
124 * returns 0 if test should end.
125 */
126static int test_duration_write(void)
127{
128 return !test_stop;
129}
130
131static int test_duration_read(void)
132{
133 return !test_stop;
134}
135
bd252a04
MD
136static DEFINE_URCU_TLS(unsigned long long, nr_writes);
137static DEFINE_URCU_TLS(unsigned long long, nr_reads);
51299083 138
55271c13 139static
06f22bdb 140unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_writes;
55271c13 141static
06f22bdb 142unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_reads;
51299083
MD
143
144static unsigned int nr_readers;
145static unsigned int nr_writers;
146
147pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
148
149void 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
159void 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
170void *thr_reader(void *data)
171{
172 unsigned long tidx = (unsigned long)data;
173
94df6318
MD
174 printf_verbose("thread_begin %s, tid %lu\n",
175 "reader", urcu_get_thread_id());
51299083 176
2a7ac59d
MD
177 set_affinity();
178
51299083
MD
179 while (!test_go)
180 {
181 }
182
183 for (;;) {
184 pthread_mutex_lock(&lock);
185 assert(test_array.a == 8);
a0b7f7ea 186 if (caa_unlikely(rduration))
daddf5b0 187 loop_sleep(rduration);
51299083 188 pthread_mutex_unlock(&lock);
bd252a04 189 URCU_TLS(nr_reads)++;
a0b7f7ea 190 if (caa_unlikely(!test_duration_read()))
51299083
MD
191 break;
192 }
193
bd252a04 194 tot_nr_reads[tidx] = URCU_TLS(nr_reads);
94df6318
MD
195 printf_verbose("thread_end %s, tid %lu\n",
196 "reader", urcu_get_thread_id());
51299083
MD
197 return ((void*)1);
198
199}
200
201void *thr_writer(void *data)
202{
203 unsigned long wtidx = (unsigned long)data;
204
94df6318
MD
205 printf_verbose("thread_begin %s, tid %lu\n",
206 "writer", urcu_get_thread_id());
51299083 207
2a7ac59d
MD
208 set_affinity();
209
51299083
MD
210 while (!test_go)
211 {
212 }
5481ddb3 213 cmm_smp_mb();
51299083
MD
214
215 for (;;) {
216 pthread_mutex_lock(&lock);
217 test_array.a = 0;
218 test_array.a = 8;
a0b7f7ea 219 if (caa_unlikely(wduration))
31b598e0 220 loop_sleep(wduration);
51299083 221 pthread_mutex_unlock(&lock);
bd252a04 222 URCU_TLS(nr_writes)++;
a0b7f7ea 223 if (caa_unlikely(!test_duration_write()))
51299083 224 break;
a0b7f7ea 225 if (caa_unlikely(wdelay))
9e31d0f0 226 loop_sleep(wdelay);
51299083
MD
227 }
228
94df6318
MD
229 printf_verbose("thread_end %s, tid %lu\n",
230 "writer", urcu_get_thread_id());
bd252a04 231 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
51299083
MD
232 return ((void*)2);
233}
234
235void show_usage(int argc, char **argv)
236{
06637138
MD
237 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
238 argv[0]);
239 printf("OPTIONS:\n");
51299083 240#ifdef DEBUG_YIELD
06637138 241 printf(" [-r] [-w] (yield reader and/or writer)\n");
51299083 242#endif
06637138
MD
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");
51299083
MD
248 printf("\n");
249}
250
51299083
MD
251int 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;
51299083
MD
259
260 if (argc < 4) {
261 show_usage(argc, argv);
262 return -1;
263 }
5481ddb3 264 cmm_smp_mb();
51299083
MD
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
51299083
MD
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]);
2a7ac59d 302 cpu_affinities[next_aff++] = a;
51299083 303 use_affinity = 1;
4bad7d45 304 printf_verbose("Adding CPU %d affinity\n", a);
51299083 305 break;
8b632bab
MD
306 case 'c':
307 if (argc < i + 2) {
308 show_usage(argc, argv);
309 return -1;
310 }
9e31d0f0 311 rduration = atol(argv[++i]);
8b632bab 312 break;
51299083
MD
313 case 'd':
314 if (argc < i + 2) {
315 show_usage(argc, argv);
316 return -1;
317 }
9e31d0f0 318 wdelay = atol(argv[++i]);
51299083 319 break;
31b598e0
MD
320 case 'e':
321 if (argc < i + 2) {
322 show_usage(argc, argv);
323 return -1;
324 }
325 wduration = atol(argv[++i]);
326 break;
4bad7d45
MD
327 case 'v':
328 verbose_mode = 1;
329 break;
51299083
MD
330 }
331 }
332
4bad7d45 333 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
51299083 334 duration, nr_readers, nr_writers);
9e31d0f0 335 printf_verbose("Writer delay : %lu loops.\n", wdelay);
4bad7d45 336 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
337 printf_verbose("thread %-6s, tid %lu\n",
338 "main", urcu_get_thread_id());
51299083 339
9aa14175
MD
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));
51299083 346
2a7ac59d
MD
347 next_aff = 0;
348
51299083
MD
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
5481ddb3 362 cmm_smp_mb();
51299083
MD
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 }
4bad7d45
MD
382
383 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
51299083 384 tot_writes);
a50a7b43 385 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
cda3c71d 386 "nr_writers %3u "
9e31d0f0 387 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
a50a7b43 388 argv[0], duration, nr_readers, rduration, wduration,
4bad7d45
MD
389 nr_writers, wdelay, tot_reads, tot_writes,
390 tot_reads + tot_writes);
391
51299083
MD
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.045738 seconds and 4 git commands to generate.