cleanup: explicitly mark unused parameters (-Wunused-parameter)
[urcu.git] / tests / benchmark / test_rwlock.c
CommitLineData
10c48e14
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>
10c48e14
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#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>
10c48e14 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
10c48e14
MD
41#ifndef DYNAMIC_LINK_TEST
42#define _LGPL_SOURCE
10c48e14 43#endif
ec4e58a3 44#include <urcu.h>
10c48e14
MD
45
46struct test_array {
47 int a;
48};
49
89a6a9ce
MJ
50/*
51 * static rwlock initializer is broken on Cygwin. Use runtime
52 * initialization.
53 */
54pthread_rwlock_t lock;
10c48e14 55
78efb485 56static volatile int test_go, test_stop;
10c48e14 57
9e31d0f0 58static unsigned long wdelay;
10c48e14 59
2b4e4125 60static volatile struct test_array test_array = { 8 };
10c48e14
MD
61
62static unsigned long duration;
10c48e14 63
daddf5b0 64/* read-side C.S. duration, in loops */
8b632bab
MD
65static unsigned long rduration;
66
31b598e0
MD
67/* write-side C.S. duration, in loops */
68static unsigned long wduration;
69
ab0aacbe 70static inline void loop_sleep(unsigned long loops)
daddf5b0 71{
ab0aacbe 72 while (loops-- != 0)
06f22bdb 73 caa_cpu_relax();
daddf5b0
MD
74}
75
4bad7d45
MD
76static int verbose_mode;
77
78#define printf_verbose(fmt, args...) \
79 do { \
80 if (verbose_mode) \
81 printf(fmt, args); \
82 } while (0)
83
2a7ac59d
MD
84static unsigned int cpu_affinities[NR_CPUS];
85static unsigned int next_aff = 0;
86static int use_affinity = 0;
87
6af882ba
MD
88pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
89
2a7ac59d
MD
90static void set_affinity(void)
91{
95bc7fb9 92#if HAVE_SCHED_SETAFFINITY
2a7ac59d 93 cpu_set_t mask;
95bc7fb9
MD
94 int cpu, ret;
95#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
96
97 if (!use_affinity)
98 return;
99
d8540fc5 100#if HAVE_SCHED_SETAFFINITY
6af882ba
MD
101 ret = pthread_mutex_lock(&affinity_mutex);
102 if (ret) {
103 perror("Error in pthread mutex lock");
104 exit(-1);
105 }
2a7ac59d 106 cpu = cpu_affinities[next_aff++];
6af882ba
MD
107 ret = pthread_mutex_unlock(&affinity_mutex);
108 if (ret) {
109 perror("Error in pthread mutex unlock");
110 exit(-1);
111 }
d8540fc5 112
2a7ac59d
MD
113 CPU_ZERO(&mask);
114 CPU_SET(cpu, &mask);
115 sched_setaffinity(0, sizeof(mask), &mask);
d8540fc5 116#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
117}
118
10c48e14
MD
119/*
120 * returns 0 if test should end.
121 */
122static int test_duration_write(void)
123{
78efb485 124 return !test_stop;
10c48e14
MD
125}
126
127static int test_duration_read(void)
128{
78efb485 129 return !test_stop;
10c48e14
MD
130}
131
bd252a04
MD
132static DEFINE_URCU_TLS(unsigned long long, nr_writes);
133static DEFINE_URCU_TLS(unsigned long long, nr_reads);
10c48e14
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
10c48e14
MD
141void *thr_reader(void *_count)
142{
143 unsigned long long *count = _count;
144
94df6318
MD
145 printf_verbose("thread_begin %s, tid %lu\n",
146 "reader", urcu_get_thread_id());
10c48e14 147
2a7ac59d
MD
148 set_affinity();
149
10c48e14
MD
150 while (!test_go)
151 {
152 }
153
154 for (;;) {
89a6a9ce
MJ
155 int a, ret;
156
157 ret = pthread_rwlock_rdlock(&lock);
158 if (ret) {
159 fprintf(stderr, "reader pthread_rwlock_rdlock: %s\n", strerror(ret));
160 abort();
161 }
efa8d2c8 162
efa8d2c8
MD
163 a = test_array.a;
164 assert(a == 8);
a0b7f7ea 165 if (caa_unlikely(rduration))
daddf5b0 166 loop_sleep(rduration);
89a6a9ce
MJ
167
168 ret = pthread_rwlock_unlock(&lock);
169 if (ret) {
170 fprintf(stderr, "reader pthread_rwlock_unlock: %s\n", strerror(ret));
171 abort();
172 }
173
bd252a04 174 URCU_TLS(nr_reads)++;
a0b7f7ea 175 if (caa_unlikely(!test_duration_read()))
10c48e14
MD
176 break;
177 }
178
bd252a04 179 *count = URCU_TLS(nr_reads);
aae4fe1d
MJ
180
181 printf_verbose("thread_end %s, tid %lu, count %llu\n",
182 "reader", urcu_get_thread_id(), *count);
10c48e14
MD
183 return ((void*)1);
184
185}
186
61c3fb60 187static
10c48e14
MD
188void *thr_writer(void *_count)
189{
190 unsigned long long *count = _count;
191
94df6318
MD
192 printf_verbose("thread_begin %s, tid %lu\n",
193 "writer", urcu_get_thread_id());
10c48e14 194
2a7ac59d
MD
195 set_affinity();
196
10c48e14
MD
197 while (!test_go)
198 {
199 }
5481ddb3 200 cmm_smp_mb();
10c48e14
MD
201
202 for (;;) {
89a6a9ce
MJ
203 int ret;
204
205 ret = pthread_rwlock_wrlock(&lock);
206 if (ret) {
207 fprintf(stderr, "writer pthread_rwlock_wrlock: %s\n", strerror(ret));
208 abort();
209 }
210
2b4e4125 211 test_array.a = 0;
10c48e14 212 test_array.a = 8;
a0b7f7ea 213 if (caa_unlikely(wduration))
31b598e0 214 loop_sleep(wduration);
89a6a9ce
MJ
215
216 ret = pthread_rwlock_unlock(&lock);
217 if (ret) {
218 fprintf(stderr, "writer pthread_rwlock_unlock: %s\n", strerror(ret));
219 abort();
220 }
221
bd252a04 222 URCU_TLS(nr_writes)++;
a0b7f7ea 223 if (caa_unlikely(!test_duration_write()))
10c48e14 224 break;
a0b7f7ea 225 if (caa_unlikely(wdelay))
9e31d0f0 226 loop_sleep(wdelay);
10c48e14 227 }
bd252a04 228 *count = URCU_TLS(nr_writes);
aae4fe1d
MJ
229
230 printf_verbose("thread_end %s, tid %lu, count %llu\n",
231 "writer", urcu_get_thread_id(), *count);
10c48e14
MD
232 return ((void*)2);
233}
234
61c3fb60 235static
70469b43 236void show_usage(char **argv)
10c48e14 237{
06637138
MD
238 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
239 argv[0]);
240 printf("OPTIONS:\n");
06637138
MD
241 printf(" [-d delay] (writer period (us))\n");
242 printf(" [-c duration] (reader C.S. duration (in loops))\n");
243 printf(" [-e duration] (writer C.S. duration (in loops))\n");
244 printf(" [-v] (verbose output)\n");
245 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
10c48e14
MD
246 printf("\n");
247}
248
249int main(int argc, char **argv)
250{
251 int err;
252 pthread_t *tid_reader, *tid_writer;
253 void *tret;
254 unsigned long long *count_reader, *count_writer;
255 unsigned long long tot_reads = 0, tot_writes = 0;
9e97e478 256 int i, a;
83e334d0 257 unsigned int i_thr;
10c48e14
MD
258
259 if (argc < 4) {
70469b43 260 show_usage(argv);
10c48e14
MD
261 return -1;
262 }
5481ddb3 263 cmm_smp_mb();
10c48e14
MD
264
265 err = sscanf(argv[1], "%u", &nr_readers);
266 if (err != 1) {
70469b43 267 show_usage(argv);
10c48e14
MD
268 return -1;
269 }
270
271 err = sscanf(argv[2], "%u", &nr_writers);
272 if (err != 1) {
70469b43 273 show_usage(argv);
10c48e14
MD
274 return -1;
275 }
276
277 err = sscanf(argv[3], "%lu", &duration);
278 if (err != 1) {
70469b43 279 show_usage(argv);
10c48e14
MD
280 return -1;
281 }
282
283 for (i = 4; i < argc; i++) {
284 if (argv[i][0] != '-')
285 continue;
286 switch (argv[i][1]) {
9e97e478
MD
287 case 'a':
288 if (argc < i + 2) {
70469b43 289 show_usage(argv);
9e97e478
MD
290 return -1;
291 }
292 a = atoi(argv[++i]);
2a7ac59d 293 cpu_affinities[next_aff++] = a;
9e97e478 294 use_affinity = 1;
4bad7d45 295 printf_verbose("Adding CPU %d affinity\n", a);
9e97e478 296 break;
8b632bab
MD
297 case 'c':
298 if (argc < i + 2) {
70469b43 299 show_usage(argv);
8b632bab
MD
300 return -1;
301 }
9e31d0f0 302 rduration = atol(argv[++i]);
8b632bab 303 break;
10c48e14
MD
304 case 'd':
305 if (argc < i + 2) {
70469b43 306 show_usage(argv);
10c48e14
MD
307 return -1;
308 }
9e31d0f0 309 wdelay = atol(argv[++i]);
10c48e14 310 break;
31b598e0
MD
311 case 'e':
312 if (argc < i + 2) {
70469b43 313 show_usage(argv);
31b598e0
MD
314 return -1;
315 }
316 wduration = atol(argv[++i]);
317 break;
4bad7d45
MD
318 case 'v':
319 verbose_mode = 1;
320 break;
10c48e14
MD
321 }
322 }
323
4bad7d45 324 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
10c48e14 325 duration, nr_readers, nr_writers);
9e31d0f0 326 printf_verbose("Writer delay : %lu loops.\n", wdelay);
aae4fe1d 327 printf_verbose("Writer duration : %lu loops.\n", wduration);
4bad7d45 328 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
329 printf_verbose("thread %-6s, tid %lu\n",
330 "main", urcu_get_thread_id());
10c48e14 331
89a6a9ce
MJ
332 err = pthread_rwlock_init(&lock, NULL);
333 if (err != 0) {
334 fprintf(stderr, "pthread_rwlock_init: (%d) %s\n", err, strerror(err));
335 exit(1);
336 }
337
9aa14175
MD
338 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
339 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
340 count_reader = calloc(nr_readers, sizeof(*count_reader));
341 count_writer = calloc(nr_writers, sizeof(*count_writer));
10c48e14 342
2a7ac59d
MD
343 next_aff = 0;
344
83e334d0
MJ
345 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
346 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
347 &count_reader[i_thr]);
10c48e14
MD
348 if (err != 0)
349 exit(1);
350 }
83e334d0
MJ
351 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
352 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
353 &count_writer[i_thr]);
10c48e14
MD
354 if (err != 0)
355 exit(1);
356 }
357
5481ddb3 358 cmm_smp_mb();
10c48e14 359
78efb485
MD
360 test_go = 1;
361
362 sleep(duration);
363
364 test_stop = 1;
365
83e334d0
MJ
366 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
367 err = pthread_join(tid_reader[i_thr], &tret);
10c48e14
MD
368 if (err != 0)
369 exit(1);
83e334d0 370 tot_reads += count_reader[i_thr];
10c48e14 371 }
83e334d0
MJ
372 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
373 err = pthread_join(tid_writer[i_thr], &tret);
10c48e14
MD
374 if (err != 0)
375 exit(1);
83e334d0 376 tot_writes += count_writer[i_thr];
10c48e14 377 }
4bad7d45
MD
378
379 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
10c48e14 380 tot_writes);
a50a7b43 381 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
cda3c71d 382 "nr_writers %3u "
9e31d0f0 383 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
a50a7b43 384 argv[0], duration, nr_readers, rduration, wduration,
4bad7d45
MD
385 nr_writers, wdelay, tot_reads, tot_writes,
386 tot_reads + tot_writes);
387
89a6a9ce
MJ
388 err = pthread_rwlock_destroy(&lock);
389 if (err != 0) {
390 fprintf(stderr, "pthread_rwlock_destroy: (%d) %s\n", err, strerror(err));
391 exit(1);
392 }
10c48e14
MD
393 free(tid_reader);
394 free(tid_writer);
395 free(count_reader);
396 free(count_writer);
397 return 0;
398}
This page took 0.061594 seconds and 4 git commands to generate.