uatomic/x86: Remove redundant memory barriers
[urcu.git] / tests / benchmark / test_urcu_assign.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
074d8438 5/*
074d8438 6 * Userspace RCU library - test program
074d8438
MD
7 */
8
074d8438
MD
9#include <stdio.h>
10#include <pthread.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/types.h>
14#include <sys/wait.h>
15#include <unistd.h>
16#include <stdio.h>
94b343fd 17#include <errno.h>
074d8438
MD
18
19#include <urcu/arch.h>
01477510 20#include <urcu/assert.h>
bd252a04 21#include <urcu/tls-compat.h>
94df6318 22#include "thread-id.h"
2650042a 23#include "../common/debug-yield.h"
65fcc7e9 24
074d8438
MD
25/* hardcoded number of CPUs */
26#define NR_CPUS 16384
27
074d8438
MD
28#ifndef DYNAMIC_LINK_TEST
29#define _LGPL_SOURCE
074d8438
MD
30#endif
31#include <urcu.h>
32
33struct test_array {
34 int a;
35};
36
074d8438
MD
37static unsigned long wdelay;
38
39static struct test_array *test_rcu_pointer;
40
41static unsigned long duration;
42
43/* read-side C.S. duration, in loops */
44static unsigned long rduration;
45
31b598e0
MD
46/* write-side C.S. duration, in loops */
47static unsigned long wduration;
48
ab0aacbe 49static inline void loop_sleep(unsigned long loops)
074d8438 50{
ab0aacbe 51 while (loops-- != 0)
06f22bdb 52 caa_cpu_relax();
074d8438
MD
53}
54
55static int verbose_mode;
56
57#define printf_verbose(fmt, args...) \
58 do { \
59 if (verbose_mode) \
60 printf(fmt, args); \
61 } while (0)
62
63static unsigned int cpu_affinities[NR_CPUS];
64static unsigned int next_aff = 0;
65static int use_affinity = 0;
66
67pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
68
69static void set_affinity(void)
70{
2388c075 71#ifdef HAVE_SCHED_SETAFFINITY
074d8438 72 cpu_set_t mask;
95bc7fb9
MD
73 int cpu, ret;
74#endif /* HAVE_SCHED_SETAFFINITY */
074d8438
MD
75
76 if (!use_affinity)
77 return;
78
2388c075 79#ifdef HAVE_SCHED_SETAFFINITY
074d8438
MD
80 ret = pthread_mutex_lock(&affinity_mutex);
81 if (ret) {
82 perror("Error in pthread mutex lock");
83 exit(-1);
84 }
85 cpu = cpu_affinities[next_aff++];
86 ret = pthread_mutex_unlock(&affinity_mutex);
87 if (ret) {
88 perror("Error in pthread mutex unlock");
89 exit(-1);
90 }
d8540fc5 91
074d8438
MD
92 CPU_ZERO(&mask);
93 CPU_SET(cpu, &mask);
94 sched_setaffinity(0, sizeof(mask), &mask);
d8540fc5 95#endif /* HAVE_SCHED_SETAFFINITY */
074d8438
MD
96}
97
bd252a04
MD
98static DEFINE_URCU_TLS(unsigned long long, nr_writes);
99static DEFINE_URCU_TLS(unsigned long long, nr_reads);
074d8438
MD
100
101static unsigned int nr_readers;
102static unsigned int nr_writers;
103
104pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
105
61c3fb60 106static
074d8438
MD
107void rcu_copy_mutex_lock(void)
108{
109 int ret;
110 ret = pthread_mutex_lock(&rcu_copy_mutex);
111 if (ret) {
112 perror("Error in pthread mutex lock");
113 exit(-1);
114 }
115}
116
61c3fb60 117static
074d8438
MD
118void rcu_copy_mutex_unlock(void)
119{
120 int ret;
121
122 ret = pthread_mutex_unlock(&rcu_copy_mutex);
123 if (ret) {
124 perror("Error in pthread mutex unlock");
125 exit(-1);
126 }
127}
128
129/*
130 * malloc/free are reusing memory areas too quickly, which does not let us
131 * test races appropriately. Use a large circular array for allocations.
d4267b0b
MD
132 * ARRAY_SIZE is larger than nr_writers, and we keep the mutex across
133 * both alloc and free, which insures we never run over our tail.
074d8438
MD
134 */
135#define ARRAY_SIZE (1048576 * nr_writers)
83e334d0
MJ
136#define ARRAY_POISON (int) 0xDEADBEEF
137static unsigned int array_index;
074d8438
MD
138static struct test_array *test_array;
139
140static struct test_array *test_array_alloc(void)
141{
142 struct test_array *ret;
143 int index;
144
074d8438 145 index = array_index % ARRAY_SIZE;
01477510 146 urcu_posix_assert(test_array[index].a == ARRAY_POISON ||
074d8438
MD
147 test_array[index].a == 0);
148 ret = &test_array[index];
149 array_index++;
150 if (array_index == ARRAY_SIZE)
151 array_index = 0;
074d8438
MD
152 return ret;
153}
154
155static void test_array_free(struct test_array *ptr)
156{
157 if (!ptr)
158 return;
074d8438 159 ptr->a = ARRAY_POISON;
074d8438
MD
160}
161
61c3fb60 162static
074d8438
MD
163void *thr_reader(void *_count)
164{
165 unsigned long long *count = _count;
166 struct test_array *local_ptr;
167
94df6318
MD
168 printf_verbose("thread_begin %s, tid %lu\n",
169 "reader", urcu_get_thread_id());
074d8438
MD
170
171 set_affinity();
172
173 rcu_register_thread();
174
9e4e7ad1 175 wait_until_go();
074d8438
MD
176
177 for (;;) {
178 rcu_read_lock();
179 local_ptr = rcu_dereference(test_rcu_pointer);
1de4df4b 180 rcu_debug_yield_read();
074d8438 181 if (local_ptr)
01477510 182 urcu_posix_assert(local_ptr->a == 8);
a0b7f7ea 183 if (caa_unlikely(rduration))
074d8438
MD
184 loop_sleep(rduration);
185 rcu_read_unlock();
bd252a04 186 URCU_TLS(nr_reads)++;
a0b7f7ea 187 if (caa_unlikely(!test_duration_read()))
074d8438
MD
188 break;
189 }
190
191 rcu_unregister_thread();
192
bd252a04 193 *count = URCU_TLS(nr_reads);
94df6318
MD
194 printf_verbose("thread_end %s, tid %lu\n",
195 "reader", urcu_get_thread_id());
074d8438
MD
196 return ((void*)1);
197
198}
199
61c3fb60 200static
074d8438
MD
201void *thr_writer(void *_count)
202{
203 unsigned long long *count = _count;
204 struct test_array *new, *old;
205
94df6318
MD
206 printf_verbose("thread_begin %s, tid %lu\n",
207 "writer", urcu_get_thread_id());
074d8438
MD
208
209 set_affinity();
210
9e4e7ad1 211 wait_until_go();
074d8438
MD
212
213 for (;;) {
d4267b0b 214 rcu_copy_mutex_lock();
074d8438
MD
215 new = test_array_alloc();
216 new->a = 8;
074d8438
MD
217 old = test_rcu_pointer;
218 rcu_assign_pointer(test_rcu_pointer, new);
a0b7f7ea 219 if (caa_unlikely(wduration))
31b598e0 220 loop_sleep(wduration);
074d8438
MD
221 synchronize_rcu();
222 if (old)
223 old->a = 0;
224 test_array_free(old);
d4267b0b 225 rcu_copy_mutex_unlock();
bd252a04 226 URCU_TLS(nr_writes)++;
a0b7f7ea 227 if (caa_unlikely(!test_duration_write()))
074d8438 228 break;
a0b7f7ea 229 if (caa_unlikely(wdelay))
074d8438
MD
230 loop_sleep(wdelay);
231 }
232
94df6318
MD
233 printf_verbose("thread_end %s, tid %lu\n",
234 "writer", urcu_get_thread_id());
bd252a04 235 *count = URCU_TLS(nr_writes);
074d8438
MD
236 return ((void*)2);
237}
238
61c3fb60 239static
70469b43 240void show_usage(char **argv)
074d8438 241{
06637138
MD
242 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
243 argv[0]);
244 printf("OPTIONS:\n");
06637138 245 printf(" [-r] [-w] (yield reader and/or writer)\n");
06637138
MD
246 printf(" [-d delay] (writer period (us))\n");
247 printf(" [-c duration] (reader C.S. duration (in loops))\n");
248 printf(" [-e duration] (writer C.S. duration (in loops))\n");
249 printf(" [-v] (verbose output)\n");
250 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
074d8438
MD
251 printf("\n");
252}
253
254int main(int argc, char **argv)
255{
256 int err;
257 pthread_t *tid_reader, *tid_writer;
258 void *tret;
259 unsigned long long *count_reader, *count_writer;
260 unsigned long long tot_reads = 0, tot_writes = 0;
261 int i, a;
83e334d0 262 unsigned int i_thr;
074d8438
MD
263
264 if (argc < 4) {
70469b43 265 show_usage(argv);
074d8438
MD
266 return -1;
267 }
268
269 err = sscanf(argv[1], "%u", &nr_readers);
270 if (err != 1) {
70469b43 271 show_usage(argv);
074d8438
MD
272 return -1;
273 }
274
275 err = sscanf(argv[2], "%u", &nr_writers);
276 if (err != 1) {
70469b43 277 show_usage(argv);
074d8438
MD
278 return -1;
279 }
83e334d0 280
074d8438
MD
281 err = sscanf(argv[3], "%lu", &duration);
282 if (err != 1) {
70469b43 283 show_usage(argv);
074d8438
MD
284 return -1;
285 }
286
287 for (i = 4; i < argc; i++) {
288 if (argv[i][0] != '-')
289 continue;
290 switch (argv[i][1]) {
074d8438 291 case 'r':
2650042a 292 rcu_debug_yield_enable(RCU_YIELD_READ);
074d8438
MD
293 break;
294 case 'w':
2650042a 295 rcu_debug_yield_enable(RCU_YIELD_WRITE);
074d8438 296 break;
074d8438
MD
297 case 'a':
298 if (argc < i + 2) {
70469b43 299 show_usage(argv);
074d8438
MD
300 return -1;
301 }
302 a = atoi(argv[++i]);
303 cpu_affinities[next_aff++] = a;
304 use_affinity = 1;
305 printf_verbose("Adding CPU %d affinity\n", a);
306 break;
307 case 'c':
308 if (argc < i + 2) {
70469b43 309 show_usage(argv);
074d8438
MD
310 return -1;
311 }
312 rduration = atol(argv[++i]);
313 break;
314 case 'd':
315 if (argc < i + 2) {
70469b43 316 show_usage(argv);
074d8438
MD
317 return -1;
318 }
319 wdelay = atol(argv[++i]);
320 break;
31b598e0
MD
321 case 'e':
322 if (argc < i + 2) {
70469b43 323 show_usage(argv);
31b598e0
MD
324 return -1;
325 }
326 wduration = atol(argv[++i]);
327 break;
074d8438
MD
328 case 'v':
329 verbose_mode = 1;
330 break;
331 }
332 }
333
334 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
335 duration, nr_readers, nr_writers);
336 printf_verbose("Writer delay : %lu loops.\n", wdelay);
337 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
338 printf_verbose("thread %-6s, tid %lu\n",
339 "main", urcu_get_thread_id());
074d8438 340
92e8d9d6 341 test_array = calloc(1, sizeof(*test_array) * ARRAY_SIZE);
9aa14175
MD
342 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
343 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
344 count_reader = calloc(nr_readers, sizeof(*count_reader));
345 count_writer = calloc(nr_writers, sizeof(*count_writer));
074d8438
MD
346
347 next_aff = 0;
348
83e334d0
MJ
349 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
350 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
351 &count_reader[i_thr]);
074d8438
MD
352 if (err != 0)
353 exit(1);
354 }
83e334d0
MJ
355 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
356 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
357 &count_writer[i_thr]);
074d8438
MD
358 if (err != 0)
359 exit(1);
360 }
361
9e4e7ad1 362 test_for(duration);
074d8438 363
83e334d0
MJ
364 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
365 err = pthread_join(tid_reader[i_thr], &tret);
074d8438
MD
366 if (err != 0)
367 exit(1);
83e334d0 368 tot_reads += count_reader[i_thr];
074d8438 369 }
83e334d0
MJ
370 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
371 err = pthread_join(tid_writer[i_thr], &tret);
074d8438
MD
372 if (err != 0)
373 exit(1);
83e334d0 374 tot_writes += count_writer[i_thr];
074d8438 375 }
83e334d0 376
074d8438
MD
377 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
378 tot_writes);
a50a7b43 379 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
074d8438
MD
380 "nr_writers %3u "
381 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
a50a7b43 382 argv[0], duration, nr_readers, rduration, wduration,
074d8438
MD
383 nr_writers, wdelay, tot_reads, tot_writes,
384 tot_reads + tot_writes);
83e334d0 385
074d8438
MD
386 test_array_free(test_rcu_pointer);
387 free(test_array);
388 free(tid_reader);
389 free(tid_writer);
390 free(count_reader);
391 free(count_writer);
83e334d0 392
074d8438
MD
393 return 0;
394}
This page took 0.063148 seconds and 4 git commands to generate.