uatomic: Fix header guard comment
[urcu.git] / tests / benchmark / test_urcu_bp.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-bp.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
88 CPU_ZERO(&mask);
89 CPU_SET(cpu, &mask);
90 sched_setaffinity(0, sizeof(mask), &mask);
91 #endif /* HAVE_SCHED_SETAFFINITY */
92 }
93
94 static DEFINE_URCU_TLS(unsigned long long, nr_writes);
95 static DEFINE_URCU_TLS(unsigned long long, nr_reads);
96
97 static unsigned int nr_readers;
98 static unsigned int nr_writers;
99
100 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
101
102 static
103 void *thr_reader(void *_count)
104 {
105 unsigned long long *count = _count;
106 int *local_ptr;
107
108 printf_verbose("thread_begin %s, tid %lu\n",
109 "reader", urcu_get_thread_id());
110
111 set_affinity();
112
113 rcu_register_thread();
114 urcu_posix_assert(!rcu_read_ongoing());
115
116 wait_until_go();
117
118 for (;;) {
119 rcu_read_lock();
120 urcu_posix_assert(rcu_read_ongoing());
121 local_ptr = rcu_dereference(test_rcu_pointer);
122 rcu_debug_yield_read();
123 if (local_ptr)
124 urcu_posix_assert(*local_ptr == 8);
125 if (caa_unlikely(rduration))
126 loop_sleep(rduration);
127 rcu_read_unlock();
128 URCU_TLS(nr_reads)++;
129 if (caa_unlikely(!test_duration_read()))
130 break;
131 }
132
133 rcu_unregister_thread();
134
135 *count = URCU_TLS(nr_reads);
136 printf_verbose("thread_end %s, tid %lu\n",
137 "reader", urcu_get_thread_id());
138 return ((void*)1);
139
140 }
141
142 static
143 void *thr_writer(void *_count)
144 {
145 unsigned long long *count = _count;
146 int *new, *old;
147
148 printf_verbose("thread_begin %s, tid %lu\n",
149 "writer", urcu_get_thread_id());
150
151 set_affinity();
152
153 wait_until_go();
154
155 for (;;) {
156 new = malloc(sizeof(int));
157 *new = 8;
158 old = rcu_xchg_pointer(&test_rcu_pointer, new);
159 if (caa_unlikely(wduration))
160 loop_sleep(wduration);
161 synchronize_rcu();
162 if (old)
163 *old = 0;
164 free(old);
165 URCU_TLS(nr_writes)++;
166 if (caa_unlikely(!test_duration_write()))
167 break;
168 if (caa_unlikely(wdelay))
169 loop_sleep(wdelay);
170 }
171
172 printf_verbose("thread_end %s, tid %lu\n",
173 "writer", urcu_get_thread_id());
174 *count = URCU_TLS(nr_writes);
175 return ((void*)2);
176 }
177
178 static
179 void show_usage(char **argv)
180 {
181 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
182 argv[0]);
183 printf("OPTIONS:\n");
184 printf(" [-r] [-w] (yield reader and/or writer)\n");
185 printf(" [-d delay] (writer period (us))\n");
186 printf(" [-c duration] (reader C.S. duration (in loops))\n");
187 printf(" [-e duration] (writer C.S. duration (in loops))\n");
188 printf(" [-v] (verbose output)\n");
189 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
190 printf("\n");
191 }
192
193 int main(int argc, char **argv)
194 {
195 int err;
196 pthread_t *tid_reader, *tid_writer;
197 void *tret;
198 unsigned long long *count_reader, *count_writer;
199 unsigned long long tot_reads = 0, tot_writes = 0;
200 int i, a;
201 unsigned int i_thr;
202
203 if (argc < 4) {
204 show_usage(argv);
205 return -1;
206 }
207
208 err = sscanf(argv[1], "%u", &nr_readers);
209 if (err != 1) {
210 show_usage(argv);
211 return -1;
212 }
213
214 err = sscanf(argv[2], "%u", &nr_writers);
215 if (err != 1) {
216 show_usage(argv);
217 return -1;
218 }
219
220 err = sscanf(argv[3], "%lu", &duration);
221 if (err != 1) {
222 show_usage(argv);
223 return -1;
224 }
225
226 for (i = 4; i < argc; i++) {
227 if (argv[i][0] != '-')
228 continue;
229 switch (argv[i][1]) {
230 case 'r':
231 rcu_debug_yield_enable(RCU_YIELD_READ);
232 break;
233 case 'w':
234 rcu_debug_yield_enable(RCU_YIELD_WRITE);
235 break;
236 case 'a':
237 if (argc < i + 2) {
238 show_usage(argv);
239 return -1;
240 }
241 a = atoi(argv[++i]);
242 cpu_affinities[next_aff++] = a;
243 use_affinity = 1;
244 printf_verbose("Adding CPU %d affinity\n", a);
245 break;
246 case 'c':
247 if (argc < i + 2) {
248 show_usage(argv);
249 return -1;
250 }
251 rduration = atol(argv[++i]);
252 break;
253 case 'd':
254 if (argc < i + 2) {
255 show_usage(argv);
256 return -1;
257 }
258 wdelay = atol(argv[++i]);
259 break;
260 case 'e':
261 if (argc < i + 2) {
262 show_usage(argv);
263 return -1;
264 }
265 wduration = atol(argv[++i]);
266 break;
267 case 'v':
268 verbose_mode = 1;
269 break;
270 }
271 }
272
273 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
274 duration, nr_readers, nr_writers);
275 printf_verbose("Writer delay : %lu loops.\n", wdelay);
276 printf_verbose("Reader duration : %lu loops.\n", rduration);
277 printf_verbose("thread %-6s, tid %lu\n",
278 "main", urcu_get_thread_id());
279
280 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
281 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
282 count_reader = calloc(nr_readers, sizeof(*count_reader));
283 count_writer = calloc(nr_writers, sizeof(*count_writer));
284
285 next_aff = 0;
286
287 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
288 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
289 &count_reader[i_thr]);
290 if (err != 0)
291 exit(1);
292 }
293 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
294 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
295 &count_writer[i_thr]);
296 if (err != 0)
297 exit(1);
298 }
299
300 test_for(duration);
301
302 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
303 err = pthread_join(tid_reader[i_thr], &tret);
304 if (err != 0)
305 exit(1);
306 tot_reads += count_reader[i_thr];
307 }
308 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
309 err = pthread_join(tid_writer[i_thr], &tret);
310 if (err != 0)
311 exit(1);
312 tot_writes += count_writer[i_thr];
313 }
314
315 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
316 tot_writes);
317 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
318 "nr_writers %3u "
319 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
320 argv[0], duration, nr_readers, rduration, wduration,
321 nr_writers, wdelay, tot_reads, tot_writes,
322 tot_reads + tot_writes);
323
324 free(test_rcu_pointer);
325 free(tid_reader);
326 free(tid_writer);
327 free(count_reader);
328 free(count_writer);
329
330 return 0;
331 }
This page took 0.036953 seconds and 5 git commands to generate.