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