Tests: Add tap-driver.sh for automake < 1.12
[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>
2953b501 36#include "cpuset.h"
94df6318 37#include "thread-id.h"
65fcc7e9 38
2a7ac59d
MD
39/* hardcoded number of CPUs */
40#define NR_CPUS 16384
41
10c48e14
MD
42#ifndef DYNAMIC_LINK_TEST
43#define _LGPL_SOURCE
10c48e14 44#endif
ec4e58a3 45#include <urcu.h>
10c48e14
MD
46
47struct test_array {
48 int a;
49};
50
51pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
52
78efb485 53static volatile int test_go, test_stop;
10c48e14 54
9e31d0f0 55static unsigned long wdelay;
10c48e14 56
2b4e4125 57static volatile struct test_array test_array = { 8 };
10c48e14
MD
58
59static unsigned long duration;
10c48e14 60
daddf5b0 61/* read-side C.S. duration, in loops */
8b632bab
MD
62static unsigned long rduration;
63
31b598e0
MD
64/* write-side C.S. duration, in loops */
65static unsigned long wduration;
66
ab0aacbe 67static inline void loop_sleep(unsigned long loops)
daddf5b0 68{
ab0aacbe 69 while (loops-- != 0)
06f22bdb 70 caa_cpu_relax();
daddf5b0
MD
71}
72
4bad7d45
MD
73static int verbose_mode;
74
75#define printf_verbose(fmt, args...) \
76 do { \
77 if (verbose_mode) \
78 printf(fmt, args); \
79 } while (0)
80
2a7ac59d
MD
81static unsigned int cpu_affinities[NR_CPUS];
82static unsigned int next_aff = 0;
83static int use_affinity = 0;
84
6af882ba
MD
85pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
86
2a7ac59d
MD
87static void set_affinity(void)
88{
95bc7fb9 89#if HAVE_SCHED_SETAFFINITY
2a7ac59d 90 cpu_set_t mask;
95bc7fb9
MD
91 int cpu, ret;
92#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
93
94 if (!use_affinity)
95 return;
96
d8540fc5 97#if HAVE_SCHED_SETAFFINITY
6af882ba
MD
98 ret = pthread_mutex_lock(&affinity_mutex);
99 if (ret) {
100 perror("Error in pthread mutex lock");
101 exit(-1);
102 }
2a7ac59d 103 cpu = cpu_affinities[next_aff++];
6af882ba
MD
104 ret = pthread_mutex_unlock(&affinity_mutex);
105 if (ret) {
106 perror("Error in pthread mutex unlock");
107 exit(-1);
108 }
d8540fc5 109
2a7ac59d
MD
110 CPU_ZERO(&mask);
111 CPU_SET(cpu, &mask);
d8540fc5
PA
112#if SCHED_SETAFFINITY_ARGS == 2
113 sched_setaffinity(0, &mask);
114#else
2a7ac59d 115 sched_setaffinity(0, sizeof(mask), &mask);
d8540fc5
PA
116#endif
117#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
118}
119
10c48e14
MD
120/*
121 * returns 0 if test should end.
122 */
123static int test_duration_write(void)
124{
78efb485 125 return !test_stop;
10c48e14
MD
126}
127
128static int test_duration_read(void)
129{
78efb485 130 return !test_stop;
10c48e14
MD
131}
132
bd252a04
MD
133static DEFINE_URCU_TLS(unsigned long long, nr_writes);
134static DEFINE_URCU_TLS(unsigned long long, nr_reads);
10c48e14
MD
135
136static unsigned int nr_readers;
137static unsigned int nr_writers;
138
139pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
140
141void rcu_copy_mutex_lock(void)
142{
143 int ret;
144 ret = pthread_mutex_lock(&rcu_copy_mutex);
145 if (ret) {
146 perror("Error in pthread mutex lock");
147 exit(-1);
148 }
149}
150
151void rcu_copy_mutex_unlock(void)
152{
153 int ret;
154
155 ret = pthread_mutex_unlock(&rcu_copy_mutex);
156 if (ret) {
157 perror("Error in pthread mutex unlock");
158 exit(-1);
159 }
160}
161
162void *thr_reader(void *_count)
163{
164 unsigned long long *count = _count;
165
94df6318
MD
166 printf_verbose("thread_begin %s, tid %lu\n",
167 "reader", urcu_get_thread_id());
10c48e14 168
2a7ac59d
MD
169 set_affinity();
170
10c48e14
MD
171 while (!test_go)
172 {
173 }
174
175 for (;;) {
efa8d2c8
MD
176 int a;
177
10c48e14 178 pthread_rwlock_rdlock(&lock);
efa8d2c8
MD
179 a = test_array.a;
180 assert(a == 8);
a0b7f7ea 181 if (caa_unlikely(rduration))
daddf5b0 182 loop_sleep(rduration);
10c48e14 183 pthread_rwlock_unlock(&lock);
bd252a04 184 URCU_TLS(nr_reads)++;
a0b7f7ea 185 if (caa_unlikely(!test_duration_read()))
10c48e14
MD
186 break;
187 }
188
bd252a04 189 *count = URCU_TLS(nr_reads);
94df6318
MD
190 printf_verbose("thread_end %s, tid %lu\n",
191 "reader", urcu_get_thread_id());
10c48e14
MD
192 return ((void*)1);
193
194}
195
196void *thr_writer(void *_count)
197{
198 unsigned long long *count = _count;
199
94df6318
MD
200 printf_verbose("thread_begin %s, tid %lu\n",
201 "writer", urcu_get_thread_id());
10c48e14 202
2a7ac59d
MD
203 set_affinity();
204
10c48e14
MD
205 while (!test_go)
206 {
207 }
5481ddb3 208 cmm_smp_mb();
10c48e14
MD
209
210 for (;;) {
211 pthread_rwlock_wrlock(&lock);
2b4e4125 212 test_array.a = 0;
10c48e14 213 test_array.a = 8;
a0b7f7ea 214 if (caa_unlikely(wduration))
31b598e0 215 loop_sleep(wduration);
10c48e14 216 pthread_rwlock_unlock(&lock);
bd252a04 217 URCU_TLS(nr_writes)++;
a0b7f7ea 218 if (caa_unlikely(!test_duration_write()))
10c48e14 219 break;
a0b7f7ea 220 if (caa_unlikely(wdelay))
9e31d0f0 221 loop_sleep(wdelay);
10c48e14
MD
222 }
223
94df6318
MD
224 printf_verbose("thread_end %s, tid %lu\n",
225 "writer", urcu_get_thread_id());
bd252a04 226 *count = URCU_TLS(nr_writes);
10c48e14
MD
227 return ((void*)2);
228}
229
230void show_usage(int argc, char **argv)
231{
06637138
MD
232 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
233 argv[0]);
234 printf("OPTIONS:\n");
06637138
MD
235 printf(" [-d delay] (writer period (us))\n");
236 printf(" [-c duration] (reader C.S. duration (in loops))\n");
237 printf(" [-e duration] (writer C.S. duration (in loops))\n");
238 printf(" [-v] (verbose output)\n");
239 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
10c48e14
MD
240 printf("\n");
241}
242
243int main(int argc, char **argv)
244{
245 int err;
246 pthread_t *tid_reader, *tid_writer;
247 void *tret;
248 unsigned long long *count_reader, *count_writer;
249 unsigned long long tot_reads = 0, tot_writes = 0;
9e97e478 250 int i, a;
10c48e14
MD
251
252 if (argc < 4) {
253 show_usage(argc, argv);
254 return -1;
255 }
5481ddb3 256 cmm_smp_mb();
10c48e14
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 }
269
270 err = sscanf(argv[3], "%lu", &duration);
271 if (err != 1) {
272 show_usage(argc, argv);
273 return -1;
274 }
275
276 for (i = 4; i < argc; i++) {
277 if (argv[i][0] != '-')
278 continue;
279 switch (argv[i][1]) {
9e97e478
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;
9e97e478 287 use_affinity = 1;
4bad7d45 288 printf_verbose("Adding CPU %d affinity\n", a);
9e97e478 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;
10c48e14
MD
297 case 'd':
298 if (argc < i + 2) {
299 show_usage(argc, argv);
300 return -1;
301 }
9e31d0f0 302 wdelay = atol(argv[++i]);
10c48e14 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;
10c48e14
MD
314 }
315 }
316
4bad7d45 317 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
10c48e14 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());
10c48e14 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));
10c48e14 328
2a7ac59d
MD
329 next_aff = 0;
330
10c48e14
MD
331 for (i = 0; i < nr_readers; i++) {
332 err = pthread_create(&tid_reader[i], NULL, thr_reader,
333 &count_reader[i]);
334 if (err != 0)
335 exit(1);
336 }
337 for (i = 0; i < nr_writers; i++) {
338 err = pthread_create(&tid_writer[i], NULL, thr_writer,
339 &count_writer[i]);
340 if (err != 0)
341 exit(1);
342 }
343
5481ddb3 344 cmm_smp_mb();
10c48e14 345
78efb485
MD
346 test_go = 1;
347
348 sleep(duration);
349
350 test_stop = 1;
351
10c48e14
MD
352 for (i = 0; i < nr_readers; i++) {
353 err = pthread_join(tid_reader[i], &tret);
354 if (err != 0)
355 exit(1);
356 tot_reads += count_reader[i];
357 }
358 for (i = 0; i < nr_writers; i++) {
359 err = pthread_join(tid_writer[i], &tret);
360 if (err != 0)
361 exit(1);
362 tot_writes += count_writer[i];
363 }
4bad7d45
MD
364
365 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
10c48e14 366 tot_writes);
a50a7b43 367 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
cda3c71d 368 "nr_writers %3u "
9e31d0f0 369 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
a50a7b43 370 argv[0], duration, nr_readers, rduration, wduration,
4bad7d45
MD
371 nr_writers, wdelay, tot_reads, tot_writes,
372 tot_reads + tot_writes);
373
10c48e14
MD
374 free(tid_reader);
375 free(tid_writer);
376 free(count_reader);
377 free(count_writer);
378 return 0;
379}
This page took 0.055929 seconds and 4 git commands to generate.