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