Remove glibc < 2.4 compat code for sched_setaffinity
[urcu.git] / tests / benchmark / test_urcu.c
CommitLineData
b257a10b
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>
b257a10b 7 *
af02d47e
MD
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.
b257a10b
MD
21 */
22
ac260fd9 23#include <stdio.h>
f69f195a
MD
24#include <pthread.h>
25#include <stdlib.h>
41718ff9 26#include <string.h>
f69f195a
MD
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <unistd.h>
30#include <stdio.h>
41718ff9 31#include <assert.h>
94b343fd 32#include <errno.h>
87bd15cd 33
ec4e58a3 34#include <urcu/arch.h>
bd252a04 35#include <urcu/tls-compat.h>
94df6318 36#include "thread-id.h"
2650042a 37#include <../common/debug-yield.h>
65fcc7e9 38
2a7ac59d
MD
39/* hardcoded number of CPUs */
40#define NR_CPUS 16384
41
121a5d44
MD
42#ifndef DYNAMIC_LINK_TEST
43#define _LGPL_SOURCE
121a5d44 44#endif
ec4e58a3 45#include <urcu.h>
ac260fd9 46
78efb485 47static volatile int test_go, test_stop;
8c86b9a1 48
9e31d0f0 49static unsigned long wdelay;
bb488185 50
bcc74df3 51static int *test_rcu_pointer;
41718ff9 52
cf380c2f 53static unsigned long duration;
cf380c2f 54
daddf5b0 55/* read-side C.S. duration, in loops */
8b632bab
MD
56static unsigned long rduration;
57
31b598e0
MD
58/* write-side C.S. duration, in loops */
59static unsigned long wduration;
60
ab0aacbe 61static inline void loop_sleep(unsigned long loops)
daddf5b0 62{
ab0aacbe 63 while (loops-- != 0)
06f22bdb 64 caa_cpu_relax();
daddf5b0
MD
65}
66
4bad7d45
MD
67static int verbose_mode;
68
69#define printf_verbose(fmt, args...) \
70 do { \
71 if (verbose_mode) \
72 printf(fmt, args); \
73 } while (0)
74
2a7ac59d
MD
75static unsigned int cpu_affinities[NR_CPUS];
76static unsigned int next_aff = 0;
77static int use_affinity = 0;
78
79pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
80
81static void set_affinity(void)
82{
95bc7fb9 83#if HAVE_SCHED_SETAFFINITY
2a7ac59d 84 cpu_set_t mask;
95bc7fb9
MD
85 int cpu, ret;
86#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
87
88 if (!use_affinity)
89 return;
90
d8540fc5 91#if HAVE_SCHED_SETAFFINITY
2a7ac59d
MD
92 ret = pthread_mutex_lock(&affinity_mutex);
93 if (ret) {
94 perror("Error in pthread mutex lock");
95 exit(-1);
96 }
97 cpu = cpu_affinities[next_aff++];
98 ret = pthread_mutex_unlock(&affinity_mutex);
99 if (ret) {
100 perror("Error in pthread mutex unlock");
101 exit(-1);
102 }
d8540fc5 103
2a7ac59d
MD
104 CPU_ZERO(&mask);
105 CPU_SET(cpu, &mask);
106 sched_setaffinity(0, sizeof(mask), &mask);
d8540fc5 107#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
108}
109
cf380c2f
MD
110/*
111 * returns 0 if test should end.
112 */
5873cd17 113static int test_duration_write(void)
cf380c2f 114{
78efb485 115 return !test_stop;
5873cd17
MD
116}
117
118static int test_duration_read(void)
119{
78efb485 120 return !test_stop;
cf380c2f
MD
121}
122
bd252a04
MD
123static DEFINE_URCU_TLS(unsigned long long, nr_writes);
124static DEFINE_URCU_TLS(unsigned long long, nr_reads);
1eec319e 125
8c86b9a1
MD
126static unsigned int nr_readers;
127static unsigned int nr_writers;
128
c265818b
MD
129pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
130
131void rcu_copy_mutex_lock(void)
132{
133 int ret;
134 ret = pthread_mutex_lock(&rcu_copy_mutex);
135 if (ret) {
136 perror("Error in pthread mutex lock");
137 exit(-1);
138 }
139}
140
141void rcu_copy_mutex_unlock(void)
142{
143 int ret;
144
145 ret = pthread_mutex_unlock(&rcu_copy_mutex);
146 if (ret) {
147 perror("Error in pthread mutex unlock");
148 exit(-1);
149 }
150}
f69f195a 151
1eec319e 152void *thr_reader(void *_count)
f69f195a 153{
1eec319e 154 unsigned long long *count = _count;
bcc74df3 155 int *local_ptr;
41718ff9 156
94df6318
MD
157 printf_verbose("thread_begin %s, tid %lu\n",
158 "reader", urcu_get_thread_id());
f69f195a 159
2a7ac59d
MD
160 set_affinity();
161
121a5d44 162 rcu_register_thread();
882f3357 163 assert(!rcu_read_ongoing());
f69f195a 164
8c86b9a1
MD
165 while (!test_go)
166 {
167 }
5481ddb3 168 cmm_smp_mb();
8c86b9a1 169
cf380c2f 170 for (;;) {
1430ee0b 171 rcu_read_lock();
882f3357 172 assert(rcu_read_ongoing());
cf380c2f 173 local_ptr = rcu_dereference(test_rcu_pointer);
1de4df4b 174 rcu_debug_yield_read();
cf380c2f 175 if (local_ptr)
bcc74df3 176 assert(*local_ptr == 8);
a0b7f7ea 177 if (caa_unlikely(rduration))
daddf5b0 178 loop_sleep(rduration);
1430ee0b 179 rcu_read_unlock();
bd252a04 180 URCU_TLS(nr_reads)++;
a0b7f7ea 181 if (caa_unlikely(!test_duration_read()))
cf380c2f 182 break;
41718ff9 183 }
f69f195a 184
121a5d44 185 rcu_unregister_thread();
41718ff9 186
2b514ae2
MD
187 /* test extra thread registration */
188 rcu_register_thread();
189 rcu_unregister_thread();
190
bd252a04 191 *count = URCU_TLS(nr_reads);
94df6318
MD
192 printf_verbose("thread_end %s, tid %lu\n",
193 "reader", urcu_get_thread_id());
f69f195a
MD
194 return ((void*)1);
195
196}
197
1eec319e 198void *thr_writer(void *_count)
f69f195a 199{
1eec319e 200 unsigned long long *count = _count;
bcc74df3 201 int *new, *old;
f69f195a 202
94df6318
MD
203 printf_verbose("thread_begin %s, tid %lu\n",
204 "writer", urcu_get_thread_id());
f69f195a 205
2a7ac59d
MD
206 set_affinity();
207
8c86b9a1
MD
208 while (!test_go)
209 {
210 }
5481ddb3 211 cmm_smp_mb();
8c86b9a1 212
cf380c2f 213 for (;;) {
bcc74df3
MD
214 new = malloc(sizeof(int));
215 assert(new);
216 *new = 8;
d2835e6f 217 old = rcu_xchg_pointer(&test_rcu_pointer, new);
a0b7f7ea 218 if (caa_unlikely(wduration))
31b598e0 219 loop_sleep(wduration);
d2835e6f 220 synchronize_rcu();
cf380c2f 221 if (old)
bcc74df3
MD
222 *old = 0;
223 free(old);
bd252a04 224 URCU_TLS(nr_writes)++;
a0b7f7ea 225 if (caa_unlikely(!test_duration_write()))
cf380c2f 226 break;
a0b7f7ea 227 if (caa_unlikely(wdelay))
9e31d0f0 228 loop_sleep(wdelay);
f69f195a
MD
229 }
230
94df6318
MD
231 printf_verbose("thread_end %s, tid %lu\n",
232 "writer", urcu_get_thread_id());
bd252a04 233 *count = URCU_TLS(nr_writes);
f69f195a
MD
234 return ((void*)2);
235}
ac260fd9 236
1430ee0b
MD
237void show_usage(int argc, char **argv)
238{
06637138
MD
239 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
240 argv[0]);
241 printf("OPTIONS:\n");
06637138 242 printf(" [-r] [-w] (yield reader and/or writer)\n");
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");
1430ee0b
MD
248 printf("\n");
249}
250
cf380c2f 251int main(int argc, char **argv)
ac260fd9 252{
f69f195a 253 int err;
8c86b9a1 254 pthread_t *tid_reader, *tid_writer;
f69f195a 255 void *tret;
8c86b9a1 256 unsigned long long *count_reader, *count_writer;
1eec319e 257 unsigned long long tot_reads = 0, tot_writes = 0;
9e97e478 258 int i, a;
83e334d0 259 unsigned int i_thr;
f69f195a 260
8c86b9a1
MD
261 if (argc < 4) {
262 show_usage(argc, argv);
263 return -1;
264 }
265
266 err = sscanf(argv[1], "%u", &nr_readers);
267 if (err != 1) {
1430ee0b 268 show_usage(argc, argv);
cf380c2f
MD
269 return -1;
270 }
271
8c86b9a1
MD
272 err = sscanf(argv[2], "%u", &nr_writers);
273 if (err != 1) {
274 show_usage(argc, argv);
275 return -1;
276 }
83e334d0 277
8c86b9a1 278 err = sscanf(argv[3], "%lu", &duration);
cf380c2f 279 if (err != 1) {
1430ee0b 280 show_usage(argc, argv);
cf380c2f
MD
281 return -1;
282 }
283
8c86b9a1 284 for (i = 4; i < argc; i++) {
cf380c2f
MD
285 if (argv[i][0] != '-')
286 continue;
287 switch (argv[i][1]) {
288 case 'r':
2650042a 289 rcu_debug_yield_enable(RCU_YIELD_READ);
cf380c2f
MD
290 break;
291 case 'w':
2650042a 292 rcu_debug_yield_enable(RCU_YIELD_WRITE);
cf380c2f 293 break;
9e97e478
MD
294 case 'a':
295 if (argc < i + 2) {
296 show_usage(argc, argv);
297 return -1;
298 }
299 a = atoi(argv[++i]);
2a7ac59d 300 cpu_affinities[next_aff++] = a;
9e97e478 301 use_affinity = 1;
4bad7d45 302 printf_verbose("Adding CPU %d affinity\n", a);
9e97e478 303 break;
8b632bab
MD
304 case 'c':
305 if (argc < i + 2) {
306 show_usage(argc, argv);
307 return -1;
308 }
9e31d0f0 309 rduration = atol(argv[++i]);
8b632bab 310 break;
8c86b9a1 311 case 'd':
7685d963 312 if (argc < i + 2) {
8c86b9a1
MD
313 show_usage(argc, argv);
314 return -1;
315 }
9e31d0f0 316 wdelay = atol(argv[++i]);
bb488185 317 break;
31b598e0
MD
318 case 'e':
319 if (argc < i + 2) {
320 show_usage(argc, argv);
321 return -1;
322 }
323 wduration = atol(argv[++i]);
324 break;
4bad7d45
MD
325 case 'v':
326 verbose_mode = 1;
327 break;
cf380c2f
MD
328 }
329 }
cf380c2f 330
4bad7d45 331 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
8c86b9a1 332 duration, nr_readers, nr_writers);
9e31d0f0 333 printf_verbose("Writer delay : %lu loops.\n", wdelay);
4bad7d45 334 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
335 printf_verbose("thread %-6s, tid %lu\n",
336 "main", urcu_get_thread_id());
87bd15cd 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));
8c86b9a1 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]);
f69f195a
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]);
f69f195a
MD
354 if (err != 0)
355 exit(1);
356 }
357
5481ddb3 358 cmm_smp_mb();
8c86b9a1 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);
f69f195a
MD
368 if (err != 0)
369 exit(1);
83e334d0 370 tot_reads += count_reader[i_thr];
f69f195a 371 }
83e334d0
MJ
372 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
373 err = pthread_join(tid_writer[i_thr], &tret);
f69f195a
MD
374 if (err != 0)
375 exit(1);
83e334d0 376 tot_writes += count_writer[i_thr];
f69f195a 377 }
83e334d0 378
4bad7d45 379 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
1eec319e 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);
bcc74df3 387 free(test_rcu_pointer);
8c86b9a1
MD
388 free(tid_reader);
389 free(tid_writer);
390 free(count_reader);
391 free(count_writer);
f69f195a 392 return 0;
ac260fd9 393}
This page took 0.064792 seconds and 4 git commands to generate.