Build system: use SPDX identifiers
[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>
94b343fd 31#include <errno.h>
87bd15cd 32
01477510 33#include <urcu/assert.h>
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{
2388c075 83#ifdef 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
2388c075 91#ifdef 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
61c3fb60 131static
1eec319e 132void *thr_reader(void *_count)
f69f195a 133{
1eec319e 134 unsigned long long *count = _count;
bcc74df3 135 int *local_ptr;
41718ff9 136
94df6318
MD
137 printf_verbose("thread_begin %s, tid %lu\n",
138 "reader", urcu_get_thread_id());
f69f195a 139
2a7ac59d
MD
140 set_affinity();
141
121a5d44 142 rcu_register_thread();
01477510 143 urcu_posix_assert(!rcu_read_ongoing());
f69f195a 144
8c86b9a1
MD
145 while (!test_go)
146 {
147 }
5481ddb3 148 cmm_smp_mb();
8c86b9a1 149
cf380c2f 150 for (;;) {
1430ee0b 151 rcu_read_lock();
01477510 152 urcu_posix_assert(rcu_read_ongoing());
cf380c2f 153 local_ptr = rcu_dereference(test_rcu_pointer);
1de4df4b 154 rcu_debug_yield_read();
cf380c2f 155 if (local_ptr)
01477510 156 urcu_posix_assert(*local_ptr == 8);
a0b7f7ea 157 if (caa_unlikely(rduration))
daddf5b0 158 loop_sleep(rduration);
1430ee0b 159 rcu_read_unlock();
bd252a04 160 URCU_TLS(nr_reads)++;
a0b7f7ea 161 if (caa_unlikely(!test_duration_read()))
cf380c2f 162 break;
41718ff9 163 }
f69f195a 164
121a5d44 165 rcu_unregister_thread();
41718ff9 166
2b514ae2
MD
167 /* test extra thread registration */
168 rcu_register_thread();
169 rcu_unregister_thread();
170
bd252a04 171 *count = URCU_TLS(nr_reads);
94df6318
MD
172 printf_verbose("thread_end %s, tid %lu\n",
173 "reader", urcu_get_thread_id());
f69f195a
MD
174 return ((void*)1);
175
176}
177
61c3fb60 178static
1eec319e 179void *thr_writer(void *_count)
f69f195a 180{
1eec319e 181 unsigned long long *count = _count;
bcc74df3 182 int *new, *old;
f69f195a 183
94df6318
MD
184 printf_verbose("thread_begin %s, tid %lu\n",
185 "writer", urcu_get_thread_id());
f69f195a 186
2a7ac59d
MD
187 set_affinity();
188
8c86b9a1
MD
189 while (!test_go)
190 {
191 }
5481ddb3 192 cmm_smp_mb();
8c86b9a1 193
cf380c2f 194 for (;;) {
bcc74df3 195 new = malloc(sizeof(int));
01477510 196 urcu_posix_assert(new);
bcc74df3 197 *new = 8;
d2835e6f 198 old = rcu_xchg_pointer(&test_rcu_pointer, new);
a0b7f7ea 199 if (caa_unlikely(wduration))
31b598e0 200 loop_sleep(wduration);
d2835e6f 201 synchronize_rcu();
cf380c2f 202 if (old)
bcc74df3
MD
203 *old = 0;
204 free(old);
bd252a04 205 URCU_TLS(nr_writes)++;
a0b7f7ea 206 if (caa_unlikely(!test_duration_write()))
cf380c2f 207 break;
a0b7f7ea 208 if (caa_unlikely(wdelay))
9e31d0f0 209 loop_sleep(wdelay);
f69f195a
MD
210 }
211
94df6318
MD
212 printf_verbose("thread_end %s, tid %lu\n",
213 "writer", urcu_get_thread_id());
bd252a04 214 *count = URCU_TLS(nr_writes);
f69f195a
MD
215 return ((void*)2);
216}
ac260fd9 217
61c3fb60 218static
70469b43 219void show_usage(char **argv)
1430ee0b 220{
06637138
MD
221 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
222 argv[0]);
223 printf("OPTIONS:\n");
06637138 224 printf(" [-r] [-w] (yield reader and/or writer)\n");
06637138
MD
225 printf(" [-d delay] (writer period (us))\n");
226 printf(" [-c duration] (reader C.S. duration (in loops))\n");
227 printf(" [-e duration] (writer C.S. duration (in loops))\n");
228 printf(" [-v] (verbose output)\n");
229 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
1430ee0b
MD
230 printf("\n");
231}
232
cf380c2f 233int main(int argc, char **argv)
ac260fd9 234{
f69f195a 235 int err;
8c86b9a1 236 pthread_t *tid_reader, *tid_writer;
f69f195a 237 void *tret;
8c86b9a1 238 unsigned long long *count_reader, *count_writer;
1eec319e 239 unsigned long long tot_reads = 0, tot_writes = 0;
9e97e478 240 int i, a;
83e334d0 241 unsigned int i_thr;
f69f195a 242
8c86b9a1 243 if (argc < 4) {
70469b43 244 show_usage(argv);
8c86b9a1
MD
245 return -1;
246 }
247
248 err = sscanf(argv[1], "%u", &nr_readers);
249 if (err != 1) {
70469b43 250 show_usage(argv);
cf380c2f
MD
251 return -1;
252 }
253
8c86b9a1
MD
254 err = sscanf(argv[2], "%u", &nr_writers);
255 if (err != 1) {
70469b43 256 show_usage(argv);
8c86b9a1
MD
257 return -1;
258 }
83e334d0 259
8c86b9a1 260 err = sscanf(argv[3], "%lu", &duration);
cf380c2f 261 if (err != 1) {
70469b43 262 show_usage(argv);
cf380c2f
MD
263 return -1;
264 }
265
8c86b9a1 266 for (i = 4; i < argc; i++) {
cf380c2f
MD
267 if (argv[i][0] != '-')
268 continue;
269 switch (argv[i][1]) {
270 case 'r':
2650042a 271 rcu_debug_yield_enable(RCU_YIELD_READ);
cf380c2f
MD
272 break;
273 case 'w':
2650042a 274 rcu_debug_yield_enable(RCU_YIELD_WRITE);
cf380c2f 275 break;
9e97e478
MD
276 case 'a':
277 if (argc < i + 2) {
70469b43 278 show_usage(argv);
9e97e478
MD
279 return -1;
280 }
281 a = atoi(argv[++i]);
2a7ac59d 282 cpu_affinities[next_aff++] = a;
9e97e478 283 use_affinity = 1;
4bad7d45 284 printf_verbose("Adding CPU %d affinity\n", a);
9e97e478 285 break;
8b632bab
MD
286 case 'c':
287 if (argc < i + 2) {
70469b43 288 show_usage(argv);
8b632bab
MD
289 return -1;
290 }
9e31d0f0 291 rduration = atol(argv[++i]);
8b632bab 292 break;
8c86b9a1 293 case 'd':
7685d963 294 if (argc < i + 2) {
70469b43 295 show_usage(argv);
8c86b9a1
MD
296 return -1;
297 }
9e31d0f0 298 wdelay = atol(argv[++i]);
bb488185 299 break;
31b598e0
MD
300 case 'e':
301 if (argc < i + 2) {
70469b43 302 show_usage(argv);
31b598e0
MD
303 return -1;
304 }
305 wduration = atol(argv[++i]);
306 break;
4bad7d45
MD
307 case 'v':
308 verbose_mode = 1;
309 break;
cf380c2f
MD
310 }
311 }
cf380c2f 312
4bad7d45 313 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
8c86b9a1 314 duration, nr_readers, nr_writers);
9e31d0f0 315 printf_verbose("Writer delay : %lu loops.\n", wdelay);
4bad7d45 316 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
317 printf_verbose("thread %-6s, tid %lu\n",
318 "main", urcu_get_thread_id());
87bd15cd 319
9aa14175
MD
320 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
321 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
322 count_reader = calloc(nr_readers, sizeof(*count_reader));
323 count_writer = calloc(nr_writers, sizeof(*count_writer));
8c86b9a1 324
2a7ac59d
MD
325 next_aff = 0;
326
83e334d0
MJ
327 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
328 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
329 &count_reader[i_thr]);
f69f195a
MD
330 if (err != 0)
331 exit(1);
332 }
83e334d0
MJ
333 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
334 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
335 &count_writer[i_thr]);
f69f195a
MD
336 if (err != 0)
337 exit(1);
338 }
339
5481ddb3 340 cmm_smp_mb();
8c86b9a1 341
78efb485
MD
342 test_go = 1;
343
344 sleep(duration);
345
346 test_stop = 1;
347
83e334d0
MJ
348 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
349 err = pthread_join(tid_reader[i_thr], &tret);
f69f195a
MD
350 if (err != 0)
351 exit(1);
83e334d0 352 tot_reads += count_reader[i_thr];
f69f195a 353 }
83e334d0
MJ
354 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
355 err = pthread_join(tid_writer[i_thr], &tret);
f69f195a
MD
356 if (err != 0)
357 exit(1);
83e334d0 358 tot_writes += count_writer[i_thr];
f69f195a 359 }
83e334d0 360
4bad7d45 361 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
1eec319e 362 tot_writes);
a50a7b43 363 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
cda3c71d 364 "nr_writers %3u "
9e31d0f0 365 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
a50a7b43 366 argv[0], duration, nr_readers, rduration, wduration,
4bad7d45
MD
367 nr_writers, wdelay, tot_reads, tot_writes,
368 tot_reads + tot_writes);
bcc74df3 369 free(test_rcu_pointer);
8c86b9a1
MD
370 free(tid_reader);
371 free(tid_writer);
372 free(count_reader);
373 free(count_writer);
f69f195a 374 return 0;
ac260fd9 375}
This page took 0.068453 seconds and 4 git commands to generate.