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