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