Remove glibc < 2.4 compat code for sched_setaffinity
[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
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 *_count)
162{
163 unsigned long long *count = _count;
164
94df6318
MD
165 printf_verbose("thread_begin %s, tid %lu\n",
166 "reader", urcu_get_thread_id());
10c48e14 167
2a7ac59d
MD
168 set_affinity();
169
10c48e14
MD
170 while (!test_go)
171 {
172 }
173
174 for (;;) {
89a6a9ce
MJ
175 int a, ret;
176
177 ret = pthread_rwlock_rdlock(&lock);
178 if (ret) {
179 fprintf(stderr, "reader pthread_rwlock_rdlock: %s\n", strerror(ret));
180 abort();
181 }
efa8d2c8 182
efa8d2c8
MD
183 a = test_array.a;
184 assert(a == 8);
a0b7f7ea 185 if (caa_unlikely(rduration))
daddf5b0 186 loop_sleep(rduration);
89a6a9ce
MJ
187
188 ret = pthread_rwlock_unlock(&lock);
189 if (ret) {
190 fprintf(stderr, "reader pthread_rwlock_unlock: %s\n", strerror(ret));
191 abort();
192 }
193
bd252a04 194 URCU_TLS(nr_reads)++;
a0b7f7ea 195 if (caa_unlikely(!test_duration_read()))
10c48e14
MD
196 break;
197 }
198
bd252a04 199 *count = URCU_TLS(nr_reads);
aae4fe1d
MJ
200
201 printf_verbose("thread_end %s, tid %lu, count %llu\n",
202 "reader", urcu_get_thread_id(), *count);
10c48e14
MD
203 return ((void*)1);
204
205}
206
207void *thr_writer(void *_count)
208{
209 unsigned long long *count = _count;
210
94df6318
MD
211 printf_verbose("thread_begin %s, tid %lu\n",
212 "writer", urcu_get_thread_id());
10c48e14 213
2a7ac59d
MD
214 set_affinity();
215
10c48e14
MD
216 while (!test_go)
217 {
218 }
5481ddb3 219 cmm_smp_mb();
10c48e14
MD
220
221 for (;;) {
89a6a9ce
MJ
222 int ret;
223
224 ret = pthread_rwlock_wrlock(&lock);
225 if (ret) {
226 fprintf(stderr, "writer pthread_rwlock_wrlock: %s\n", strerror(ret));
227 abort();
228 }
229
2b4e4125 230 test_array.a = 0;
10c48e14 231 test_array.a = 8;
a0b7f7ea 232 if (caa_unlikely(wduration))
31b598e0 233 loop_sleep(wduration);
89a6a9ce
MJ
234
235 ret = pthread_rwlock_unlock(&lock);
236 if (ret) {
237 fprintf(stderr, "writer pthread_rwlock_unlock: %s\n", strerror(ret));
238 abort();
239 }
240
bd252a04 241 URCU_TLS(nr_writes)++;
a0b7f7ea 242 if (caa_unlikely(!test_duration_write()))
10c48e14 243 break;
a0b7f7ea 244 if (caa_unlikely(wdelay))
9e31d0f0 245 loop_sleep(wdelay);
10c48e14 246 }
bd252a04 247 *count = URCU_TLS(nr_writes);
aae4fe1d
MJ
248
249 printf_verbose("thread_end %s, tid %lu, count %llu\n",
250 "writer", urcu_get_thread_id(), *count);
10c48e14
MD
251 return ((void*)2);
252}
253
254void show_usage(int argc, char **argv)
255{
06637138
MD
256 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
257 argv[0]);
258 printf("OPTIONS:\n");
06637138
MD
259 printf(" [-d delay] (writer period (us))\n");
260 printf(" [-c duration] (reader C.S. duration (in loops))\n");
261 printf(" [-e duration] (writer C.S. duration (in loops))\n");
262 printf(" [-v] (verbose output)\n");
263 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
10c48e14
MD
264 printf("\n");
265}
266
267int main(int argc, char **argv)
268{
269 int err;
270 pthread_t *tid_reader, *tid_writer;
271 void *tret;
272 unsigned long long *count_reader, *count_writer;
273 unsigned long long tot_reads = 0, tot_writes = 0;
9e97e478 274 int i, a;
83e334d0 275 unsigned int i_thr;
10c48e14
MD
276
277 if (argc < 4) {
278 show_usage(argc, argv);
279 return -1;
280 }
5481ddb3 281 cmm_smp_mb();
10c48e14
MD
282
283 err = sscanf(argv[1], "%u", &nr_readers);
284 if (err != 1) {
285 show_usage(argc, argv);
286 return -1;
287 }
288
289 err = sscanf(argv[2], "%u", &nr_writers);
290 if (err != 1) {
291 show_usage(argc, argv);
292 return -1;
293 }
294
295 err = sscanf(argv[3], "%lu", &duration);
296 if (err != 1) {
297 show_usage(argc, argv);
298 return -1;
299 }
300
301 for (i = 4; i < argc; i++) {
302 if (argv[i][0] != '-')
303 continue;
304 switch (argv[i][1]) {
9e97e478
MD
305 case 'a':
306 if (argc < i + 2) {
307 show_usage(argc, argv);
308 return -1;
309 }
310 a = atoi(argv[++i]);
2a7ac59d 311 cpu_affinities[next_aff++] = a;
9e97e478 312 use_affinity = 1;
4bad7d45 313 printf_verbose("Adding CPU %d affinity\n", a);
9e97e478 314 break;
8b632bab
MD
315 case 'c':
316 if (argc < i + 2) {
317 show_usage(argc, argv);
318 return -1;
319 }
9e31d0f0 320 rduration = atol(argv[++i]);
8b632bab 321 break;
10c48e14
MD
322 case 'd':
323 if (argc < i + 2) {
324 show_usage(argc, argv);
325 return -1;
326 }
9e31d0f0 327 wdelay = atol(argv[++i]);
10c48e14 328 break;
31b598e0
MD
329 case 'e':
330 if (argc < i + 2) {
331 show_usage(argc, argv);
332 return -1;
333 }
334 wduration = atol(argv[++i]);
335 break;
4bad7d45
MD
336 case 'v':
337 verbose_mode = 1;
338 break;
10c48e14
MD
339 }
340 }
341
4bad7d45 342 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
10c48e14 343 duration, nr_readers, nr_writers);
9e31d0f0 344 printf_verbose("Writer delay : %lu loops.\n", wdelay);
aae4fe1d 345 printf_verbose("Writer duration : %lu loops.\n", wduration);
4bad7d45 346 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
347 printf_verbose("thread %-6s, tid %lu\n",
348 "main", urcu_get_thread_id());
10c48e14 349
89a6a9ce
MJ
350 err = pthread_rwlock_init(&lock, NULL);
351 if (err != 0) {
352 fprintf(stderr, "pthread_rwlock_init: (%d) %s\n", err, strerror(err));
353 exit(1);
354 }
355
9aa14175
MD
356 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
357 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
358 count_reader = calloc(nr_readers, sizeof(*count_reader));
359 count_writer = calloc(nr_writers, sizeof(*count_writer));
10c48e14 360
2a7ac59d
MD
361 next_aff = 0;
362
83e334d0
MJ
363 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
364 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
365 &count_reader[i_thr]);
10c48e14
MD
366 if (err != 0)
367 exit(1);
368 }
83e334d0
MJ
369 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
370 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
371 &count_writer[i_thr]);
10c48e14
MD
372 if (err != 0)
373 exit(1);
374 }
375
5481ddb3 376 cmm_smp_mb();
10c48e14 377
78efb485
MD
378 test_go = 1;
379
380 sleep(duration);
381
382 test_stop = 1;
383
83e334d0
MJ
384 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
385 err = pthread_join(tid_reader[i_thr], &tret);
10c48e14
MD
386 if (err != 0)
387 exit(1);
83e334d0 388 tot_reads += count_reader[i_thr];
10c48e14 389 }
83e334d0
MJ
390 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
391 err = pthread_join(tid_writer[i_thr], &tret);
10c48e14
MD
392 if (err != 0)
393 exit(1);
83e334d0 394 tot_writes += count_writer[i_thr];
10c48e14 395 }
4bad7d45
MD
396
397 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
10c48e14 398 tot_writes);
a50a7b43 399 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
cda3c71d 400 "nr_writers %3u "
9e31d0f0 401 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
a50a7b43 402 argv[0], duration, nr_readers, rduration, wduration,
4bad7d45
MD
403 nr_writers, wdelay, tot_reads, tot_writes,
404 tot_reads + tot_writes);
405
89a6a9ce
MJ
406 err = pthread_rwlock_destroy(&lock);
407 if (err != 0) {
408 fprintf(stderr, "pthread_rwlock_destroy: (%d) %s\n", err, strerror(err));
409 exit(1);
410 }
10c48e14
MD
411 free(tid_reader);
412 free(tid_writer);
413 free(count_reader);
414 free(count_writer);
415 return 0;
416}
This page took 0.0612510000000001 seconds and 4 git commands to generate.