uatomic/x86: Remove redundant memory barriers
[urcu.git] / tests / benchmark / test_urcu_defer.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 (with automatic reclamation)
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 #include <urcu-defer.h>
33
34 struct test_array {
35 int a;
36 };
37
38 static unsigned long wdelay;
39
40 static struct test_array *test_rcu_pointer;
41
42 static unsigned long duration;
43
44 /* read-side C.S. duration, in loops */
45 static unsigned long rduration;
46
47 /* write-side C.S. duration, in loops */
48 static unsigned long wduration;
49
50 static inline void loop_sleep(unsigned long loops)
51 {
52 while (loops-- != 0)
53 caa_cpu_relax();
54 }
55
56 static int verbose_mode;
57
58 #define printf_verbose(fmt, args...) \
59 do { \
60 if (verbose_mode) \
61 printf(fmt, args); \
62 } while (0)
63
64 static unsigned int cpu_affinities[NR_CPUS];
65 static unsigned int next_aff = 0;
66 static int use_affinity = 0;
67
68 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
69
70 static void set_affinity(void)
71 {
72 #ifdef HAVE_SCHED_SETAFFINITY
73 cpu_set_t mask;
74 int cpu, ret;
75 #endif /* HAVE_SCHED_SETAFFINITY */
76
77 if (!use_affinity)
78 return;
79
80 #ifdef HAVE_SCHED_SETAFFINITY
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 }
92
93 CPU_ZERO(&mask);
94 CPU_SET(cpu, &mask);
95 sched_setaffinity(0, sizeof(mask), &mask);
96 #endif /* HAVE_SCHED_SETAFFINITY */
97 }
98
99 static DEFINE_URCU_TLS(unsigned long long, nr_writes);
100 static DEFINE_URCU_TLS(unsigned long long, nr_reads);
101
102 static
103 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_writes;
104
105 static unsigned int nr_readers;
106 static unsigned int nr_writers;
107
108 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
109
110 static
111 void *thr_reader(void *_count)
112 {
113 unsigned long long *count = _count;
114 struct test_array *local_ptr;
115
116 printf_verbose("thread_begin %s, tid %lu\n",
117 "reader", urcu_get_thread_id());
118
119 set_affinity();
120
121 rcu_register_thread();
122
123 wait_until_go();
124
125 for (;;) {
126 rcu_read_lock();
127 local_ptr = rcu_dereference(test_rcu_pointer);
128 rcu_debug_yield_read();
129 if (local_ptr)
130 urcu_posix_assert(local_ptr->a == 8);
131 if (caa_unlikely(rduration))
132 loop_sleep(rduration);
133 rcu_read_unlock();
134 URCU_TLS(nr_reads)++;
135 if (caa_unlikely(!test_duration_read()))
136 break;
137 }
138
139 rcu_unregister_thread();
140
141 *count = URCU_TLS(nr_reads);
142 printf_verbose("thread_end %s, tid %lu\n",
143 "reader", urcu_get_thread_id());
144 return ((void*)1);
145
146 }
147
148 static void test_cb2(void *data __attribute__((unused)))
149 {
150 }
151
152 static void test_cb1(void *data __attribute__((unused)))
153 {
154 }
155
156 static
157 void *thr_writer(void *data)
158 {
159 unsigned long wtidx = (unsigned long)data;
160 struct test_array *new, *old = NULL;
161 int ret;
162
163 printf_verbose("thread_begin %s, tid %lu\n",
164 "writer", urcu_get_thread_id());
165
166 set_affinity();
167
168 ret = rcu_defer_register_thread();
169 if (ret) {
170 printf("Error in rcu_defer_register_thread\n");
171 exit(-1);
172 }
173
174 wait_until_go();
175
176 for (;;) {
177 new = malloc(sizeof(*new));
178 new->a = 8;
179 old = rcu_xchg_pointer(&test_rcu_pointer, new);
180 if (caa_unlikely(wduration))
181 loop_sleep(wduration);
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);
190 URCU_TLS(nr_writes)++;
191 if (caa_unlikely(!test_duration_write()))
192 break;
193 if (caa_unlikely(wdelay))
194 loop_sleep(wdelay);
195 }
196
197 rcu_defer_unregister_thread();
198
199 printf_verbose("thread_end %s, tid %lu\n",
200 "writer", urcu_get_thread_id());
201 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
202 return ((void*)2);
203 }
204
205 static
206 void show_usage(char **argv)
207 {
208 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
209 argv[0]);
210 printf("OPTIONS:\n");
211 printf(" [-r] [-w] (yield reader and/or writer)\n");
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");
217 printf("\n");
218 }
219
220 int 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;
228 unsigned int i_thr;
229
230 if (argc < 4) {
231 show_usage(argv);
232 return -1;
233 }
234
235 err = sscanf(argv[1], "%u", &nr_readers);
236 if (err != 1) {
237 show_usage(argv);
238 return -1;
239 }
240
241 err = sscanf(argv[2], "%u", &nr_writers);
242 if (err != 1) {
243 show_usage(argv);
244 return -1;
245 }
246
247 err = sscanf(argv[3], "%lu", &duration);
248 if (err != 1) {
249 show_usage(argv);
250 return -1;
251 }
252
253 for (i = 4; i < argc; i++) {
254 if (argv[i][0] != '-')
255 continue;
256 switch (argv[i][1]) {
257 case 'r':
258 rcu_debug_yield_enable(RCU_YIELD_READ);
259 break;
260 case 'w':
261 rcu_debug_yield_enable(RCU_YIELD_WRITE);
262 break;
263 case 'a':
264 if (argc < i + 2) {
265 show_usage(argv);
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) {
275 show_usage(argv);
276 return -1;
277 }
278 rduration = atol(argv[++i]);
279 break;
280 case 'd':
281 if (argc < i + 2) {
282 show_usage(argv);
283 return -1;
284 }
285 wdelay = atol(argv[++i]);
286 break;
287 case 'e':
288 if (argc < i + 2) {
289 show_usage(argv);
290 return -1;
291 }
292 wduration = atol(argv[++i]);
293 break;
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);
304 printf_verbose("thread %-6s, tid %lu\n",
305 "main", urcu_get_thread_id());
306
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));
311
312 next_aff = 0;
313
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]);
317 if (err != 0)
318 exit(1);
319 }
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);
323 if (err != 0)
324 exit(1);
325 }
326
327 test_for(duration);
328
329 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
330 err = pthread_join(tid_reader[i_thr], &tret);
331 if (err != 0)
332 exit(1);
333 tot_reads += count_reader[i_thr];
334 }
335 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
336 err = pthread_join(tid_writer[i_thr], &tret);
337 if (err != 0)
338 exit(1);
339 tot_writes += tot_nr_writes[i_thr];
340 }
341
342 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
343 tot_writes);
344 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
345 "nr_writers %3u "
346 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
347 argv[0], duration, nr_readers, rduration, wduration,
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.035912 seconds and 5 git commands to generate.