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