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