uatomic/x86: Remove redundant memory barriers
[urcu.git] / tests / benchmark / test_urcu_assign.c
1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 //
3 // SPDX-License-Identifier: GPL-2.0-or-later
4
5 /*
6 * Userspace RCU library - test program
7 */
8
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>
17 #include <errno.h>
18
19 #include <urcu/arch.h>
20 #include <urcu/assert.h>
21 #include <urcu/tls-compat.h>
22 #include "thread-id.h"
23 #include "../common/debug-yield.h"
24
25 /* hardcoded number of CPUs */
26 #define NR_CPUS 16384
27
28 #ifndef DYNAMIC_LINK_TEST
29 #define _LGPL_SOURCE
30 #endif
31 #include <urcu.h>
32
33 struct test_array {
34 int a;
35 };
36
37 static unsigned long wdelay;
38
39 static struct test_array *test_rcu_pointer;
40
41 static unsigned long duration;
42
43 /* read-side C.S. duration, in loops */
44 static unsigned long rduration;
45
46 /* write-side C.S. duration, in loops */
47 static unsigned long wduration;
48
49 static inline void loop_sleep(unsigned long loops)
50 {
51 while (loops-- != 0)
52 caa_cpu_relax();
53 }
54
55 static int verbose_mode;
56
57 #define printf_verbose(fmt, args...) \
58 do { \
59 if (verbose_mode) \
60 printf(fmt, args); \
61 } while (0)
62
63 static unsigned int cpu_affinities[NR_CPUS];
64 static unsigned int next_aff = 0;
65 static int use_affinity = 0;
66
67 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
68
69 static void set_affinity(void)
70 {
71 #ifdef HAVE_SCHED_SETAFFINITY
72 cpu_set_t mask;
73 int cpu, ret;
74 #endif /* HAVE_SCHED_SETAFFINITY */
75
76 if (!use_affinity)
77 return;
78
79 #ifdef HAVE_SCHED_SETAFFINITY
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 }
91
92 CPU_ZERO(&mask);
93 CPU_SET(cpu, &mask);
94 sched_setaffinity(0, sizeof(mask), &mask);
95 #endif /* HAVE_SCHED_SETAFFINITY */
96 }
97
98 static DEFINE_URCU_TLS(unsigned long long, nr_writes);
99 static DEFINE_URCU_TLS(unsigned long long, nr_reads);
100
101 static unsigned int nr_readers;
102 static unsigned int nr_writers;
103
104 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
105
106 static
107 void 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
117 static
118 void 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.
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.
134 */
135 #define ARRAY_SIZE (1048576 * nr_writers)
136 #define ARRAY_POISON (int) 0xDEADBEEF
137 static unsigned int array_index;
138 static struct test_array *test_array;
139
140 static struct test_array *test_array_alloc(void)
141 {
142 struct test_array *ret;
143 int index;
144
145 index = array_index % ARRAY_SIZE;
146 urcu_posix_assert(test_array[index].a == ARRAY_POISON ||
147 test_array[index].a == 0);
148 ret = &test_array[index];
149 array_index++;
150 if (array_index == ARRAY_SIZE)
151 array_index = 0;
152 return ret;
153 }
154
155 static void test_array_free(struct test_array *ptr)
156 {
157 if (!ptr)
158 return;
159 ptr->a = ARRAY_POISON;
160 }
161
162 static
163 void *thr_reader(void *_count)
164 {
165 unsigned long long *count = _count;
166 struct test_array *local_ptr;
167
168 printf_verbose("thread_begin %s, tid %lu\n",
169 "reader", urcu_get_thread_id());
170
171 set_affinity();
172
173 rcu_register_thread();
174
175 wait_until_go();
176
177 for (;;) {
178 rcu_read_lock();
179 local_ptr = rcu_dereference(test_rcu_pointer);
180 rcu_debug_yield_read();
181 if (local_ptr)
182 urcu_posix_assert(local_ptr->a == 8);
183 if (caa_unlikely(rduration))
184 loop_sleep(rduration);
185 rcu_read_unlock();
186 URCU_TLS(nr_reads)++;
187 if (caa_unlikely(!test_duration_read()))
188 break;
189 }
190
191 rcu_unregister_thread();
192
193 *count = URCU_TLS(nr_reads);
194 printf_verbose("thread_end %s, tid %lu\n",
195 "reader", urcu_get_thread_id());
196 return ((void*)1);
197
198 }
199
200 static
201 void *thr_writer(void *_count)
202 {
203 unsigned long long *count = _count;
204 struct test_array *new, *old;
205
206 printf_verbose("thread_begin %s, tid %lu\n",
207 "writer", urcu_get_thread_id());
208
209 set_affinity();
210
211 wait_until_go();
212
213 for (;;) {
214 rcu_copy_mutex_lock();
215 new = test_array_alloc();
216 new->a = 8;
217 old = test_rcu_pointer;
218 rcu_assign_pointer(test_rcu_pointer, new);
219 if (caa_unlikely(wduration))
220 loop_sleep(wduration);
221 synchronize_rcu();
222 if (old)
223 old->a = 0;
224 test_array_free(old);
225 rcu_copy_mutex_unlock();
226 URCU_TLS(nr_writes)++;
227 if (caa_unlikely(!test_duration_write()))
228 break;
229 if (caa_unlikely(wdelay))
230 loop_sleep(wdelay);
231 }
232
233 printf_verbose("thread_end %s, tid %lu\n",
234 "writer", urcu_get_thread_id());
235 *count = URCU_TLS(nr_writes);
236 return ((void*)2);
237 }
238
239 static
240 void show_usage(char **argv)
241 {
242 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
243 argv[0]);
244 printf("OPTIONS:\n");
245 printf(" [-r] [-w] (yield reader and/or writer)\n");
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");
251 printf("\n");
252 }
253
254 int 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;
262 unsigned int i_thr;
263
264 if (argc < 4) {
265 show_usage(argv);
266 return -1;
267 }
268
269 err = sscanf(argv[1], "%u", &nr_readers);
270 if (err != 1) {
271 show_usage(argv);
272 return -1;
273 }
274
275 err = sscanf(argv[2], "%u", &nr_writers);
276 if (err != 1) {
277 show_usage(argv);
278 return -1;
279 }
280
281 err = sscanf(argv[3], "%lu", &duration);
282 if (err != 1) {
283 show_usage(argv);
284 return -1;
285 }
286
287 for (i = 4; i < argc; i++) {
288 if (argv[i][0] != '-')
289 continue;
290 switch (argv[i][1]) {
291 case 'r':
292 rcu_debug_yield_enable(RCU_YIELD_READ);
293 break;
294 case 'w':
295 rcu_debug_yield_enable(RCU_YIELD_WRITE);
296 break;
297 case 'a':
298 if (argc < i + 2) {
299 show_usage(argv);
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) {
309 show_usage(argv);
310 return -1;
311 }
312 rduration = atol(argv[++i]);
313 break;
314 case 'd':
315 if (argc < i + 2) {
316 show_usage(argv);
317 return -1;
318 }
319 wdelay = atol(argv[++i]);
320 break;
321 case 'e':
322 if (argc < i + 2) {
323 show_usage(argv);
324 return -1;
325 }
326 wduration = atol(argv[++i]);
327 break;
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);
338 printf_verbose("thread %-6s, tid %lu\n",
339 "main", urcu_get_thread_id());
340
341 test_array = calloc(1, sizeof(*test_array) * ARRAY_SIZE);
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));
346
347 next_aff = 0;
348
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]);
352 if (err != 0)
353 exit(1);
354 }
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]);
358 if (err != 0)
359 exit(1);
360 }
361
362 test_for(duration);
363
364 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
365 err = pthread_join(tid_reader[i_thr], &tret);
366 if (err != 0)
367 exit(1);
368 tot_reads += count_reader[i_thr];
369 }
370 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
371 err = pthread_join(tid_writer[i_thr], &tret);
372 if (err != 0)
373 exit(1);
374 tot_writes += count_writer[i_thr];
375 }
376
377 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
378 tot_writes);
379 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
380 "nr_writers %3u "
381 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
382 argv[0], duration, nr_readers, rduration, wduration,
383 nr_writers, wdelay, tot_reads, tot_writes,
384 tot_reads + tot_writes);
385
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);
392
393 return 0;
394 }
This page took 0.039217 seconds and 5 git commands to generate.