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