uatomic: Fix header guard comment
[urcu.git] / tests / benchmark / test_urcu_qsbr.c
CommitLineData
ce29b371
MJ
1// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2//
3// SPDX-License-Identifier: GPL-2.0-or-later
4
de10a585 5/*
de10a585 6 * Userspace RCU library - test program
de10a585
MD
7 */
8
de10a585
MD
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>
de10a585
MD
17#include <errno.h>
18
19#include <urcu/arch.h>
01477510 20#include <urcu/assert.h>
bd252a04 21#include <urcu/tls-compat.h>
94df6318 22#include "thread-id.h"
2650042a 23#include "../common/debug-yield.h"
de10a585
MD
24
25/* hardcoded number of CPUs */
26#define NR_CPUS 16384
27
de10a585
MD
28#ifndef DYNAMIC_LINK_TEST
29#define _LGPL_SOURCE
de10a585
MD
30#endif
31#include "urcu-qsbr.h"
32
de10a585
MD
33static unsigned long wdelay;
34
bcc74df3 35static int *test_rcu_pointer;
de10a585
MD
36
37static unsigned long duration;
38
39/* read-side C.S. duration, in loops */
40static unsigned long rduration;
41
42/* write-side C.S. duration, in loops */
43static unsigned long wduration;
44
ab0aacbe 45static inline void loop_sleep(unsigned long loops)
de10a585 46{
ab0aacbe 47 while (loops-- != 0)
de10a585
MD
48 caa_cpu_relax();
49}
50
51static int verbose_mode;
52
53#define printf_verbose(fmt, args...) \
54 do { \
55 if (verbose_mode) \
56 printf(fmt, args); \
57 } while (0)
58
59static unsigned int cpu_affinities[NR_CPUS];
60static unsigned int next_aff = 0;
61static int use_affinity = 0;
62
63pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
64
de10a585
MD
65static void set_affinity(void)
66{
2388c075 67#ifdef HAVE_SCHED_SETAFFINITY
de10a585 68 cpu_set_t mask;
95bc7fb9
MD
69 int cpu, ret;
70#endif /* HAVE_SCHED_SETAFFINITY */
de10a585
MD
71
72 if (!use_affinity)
73 return;
74
2388c075 75#ifdef HAVE_SCHED_SETAFFINITY
de10a585
MD
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);
de10a585 89 sched_setaffinity(0, sizeof(mask), &mask);
de10a585
MD
90#endif /* HAVE_SCHED_SETAFFINITY */
91}
92
bd252a04
MD
93static DEFINE_URCU_TLS(unsigned long long, nr_writes);
94static DEFINE_URCU_TLS(unsigned long long, nr_reads);
de10a585
MD
95
96static unsigned int nr_readers;
97static unsigned int nr_writers;
98
99pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
100
61c3fb60 101static
de10a585
MD
102void *thr_reader(void *_count)
103{
104 unsigned long long *count = _count;
bcc74df3 105 int *local_ptr;
de10a585 106
94df6318
MD
107 printf_verbose("thread_begin %s, tid %lu\n",
108 "reader", urcu_get_thread_id());
de10a585
MD
109
110 set_affinity();
111
112 rcu_register_thread();
113
01477510 114 urcu_posix_assert(rcu_read_ongoing());
882f3357 115 rcu_thread_offline();
01477510 116 urcu_posix_assert(!rcu_read_ongoing());
882f3357
MD
117 rcu_thread_online();
118
9e4e7ad1 119 wait_until_go();
de10a585
MD
120
121 for (;;) {
122 rcu_read_lock();
01477510 123 urcu_posix_assert(rcu_read_ongoing());
de10a585 124 local_ptr = rcu_dereference(test_rcu_pointer);
1de4df4b 125 rcu_debug_yield_read();
de10a585 126 if (local_ptr)
01477510 127 urcu_posix_assert(*local_ptr == 8);
a0b7f7ea 128 if (caa_unlikely(rduration))
de10a585
MD
129 loop_sleep(rduration);
130 rcu_read_unlock();
bd252a04 131 URCU_TLS(nr_reads)++;
de10a585 132 /* QS each 1024 reads */
bd252a04 133 if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0))
de10a585 134 rcu_quiescent_state();
a0b7f7ea 135 if (caa_unlikely(!test_duration_read()))
de10a585
MD
136 break;
137 }
138
139 rcu_unregister_thread();
140
141 /* test extra thread registration */
142 rcu_register_thread();
143 rcu_unregister_thread();
144
bd252a04 145 *count = URCU_TLS(nr_reads);
94df6318
MD
146 printf_verbose("thread_end %s, tid %lu\n",
147 "reader", urcu_get_thread_id());
de10a585
MD
148 return ((void*)1);
149
150}
151
61c3fb60 152static
de10a585
MD
153void *thr_writer(void *_count)
154{
155 unsigned long long *count = _count;
bcc74df3 156 int *new, *old;
de10a585 157
94df6318
MD
158 printf_verbose("thread_begin %s, tid %lu\n",
159 "writer", urcu_get_thread_id());
de10a585
MD
160
161 set_affinity();
162
9e4e7ad1 163 wait_until_go();
de10a585
MD
164
165 for (;;) {
bcc74df3 166 new = malloc(sizeof(int));
01477510 167 urcu_posix_assert(new);
bcc74df3 168 *new = 8;
de10a585 169 old = rcu_xchg_pointer(&test_rcu_pointer, new);
a0b7f7ea 170 if (caa_unlikely(wduration))
de10a585
MD
171 loop_sleep(wduration);
172 synchronize_rcu();
de10a585 173 if (old)
bcc74df3
MD
174 *old = 0;
175 free(old);
bd252a04 176 URCU_TLS(nr_writes)++;
a0b7f7ea 177 if (caa_unlikely(!test_duration_write()))
de10a585 178 break;
a0b7f7ea 179 if (caa_unlikely(wdelay))
de10a585
MD
180 loop_sleep(wdelay);
181 }
182
94df6318
MD
183 printf_verbose("thread_end %s, tid %lu\n",
184 "writer", urcu_get_thread_id());
bd252a04 185 *count = URCU_TLS(nr_writes);
de10a585
MD
186 return ((void*)2);
187}
188
61c3fb60 189static
70469b43 190void show_usage(char **argv)
de10a585 191{
06637138
MD
192 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
193 argv[0]);
194 printf("OPTIONS:\n");
06637138 195 printf(" [-r] [-w] (yield reader and/or writer)\n");
06637138
MD
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");
de10a585
MD
201 printf("\n");
202}
203
204int 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;
83e334d0 212 unsigned int i_thr;
de10a585
MD
213
214 if (argc < 4) {
70469b43 215 show_usage(argv);
de10a585
MD
216 return -1;
217 }
218
219 err = sscanf(argv[1], "%u", &nr_readers);
220 if (err != 1) {
70469b43 221 show_usage(argv);
de10a585
MD
222 return -1;
223 }
224
225 err = sscanf(argv[2], "%u", &nr_writers);
226 if (err != 1) {
70469b43 227 show_usage(argv);
de10a585
MD
228 return -1;
229 }
83e334d0 230
de10a585
MD
231 err = sscanf(argv[3], "%lu", &duration);
232 if (err != 1) {
70469b43 233 show_usage(argv);
de10a585
MD
234 return -1;
235 }
236
237 for (i = 4; i < argc; i++) {
238 if (argv[i][0] != '-')
239 continue;
240 switch (argv[i][1]) {
de10a585 241 case 'r':
2650042a 242 rcu_debug_yield_enable(RCU_YIELD_READ);
de10a585
MD
243 break;
244 case 'w':
2650042a 245 rcu_debug_yield_enable(RCU_YIELD_WRITE);
de10a585 246 break;
de10a585
MD
247 case 'a':
248 if (argc < i + 2) {
70469b43 249 show_usage(argv);
de10a585
MD
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) {
70469b43 259 show_usage(argv);
de10a585
MD
260 return -1;
261 }
262 rduration = atol(argv[++i]);
263 break;
264 case 'd':
265 if (argc < i + 2) {
70469b43 266 show_usage(argv);
de10a585
MD
267 return -1;
268 }
269 wdelay = atol(argv[++i]);
270 break;
271 case 'e':
272 if (argc < i + 2) {
70469b43 273 show_usage(argv);
de10a585
MD
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);
94df6318
MD
288 printf_verbose("thread %-6s, tid %lu\n",
289 "main", urcu_get_thread_id());
de10a585 290
9aa14175
MD
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));
de10a585
MD
295
296 next_aff = 0;
297
83e334d0
MJ
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]);
de10a585
MD
301 if (err != 0)
302 exit(1);
303 }
83e334d0
MJ
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]);
de10a585
MD
307 if (err != 0)
308 exit(1);
309 }
310
9e4e7ad1 311 test_for(duration);
de10a585 312
83e334d0
MJ
313 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
314 err = pthread_join(tid_reader[i_thr], &tret);
de10a585
MD
315 if (err != 0)
316 exit(1);
83e334d0 317 tot_reads += count_reader[i_thr];
de10a585 318 }
83e334d0
MJ
319 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
320 err = pthread_join(tid_writer[i_thr], &tret);
de10a585
MD
321 if (err != 0)
322 exit(1);
83e334d0 323 tot_writes += count_writer[i_thr];
de10a585 324 }
83e334d0 325
de10a585
MD
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);
bcc74df3 334 free(test_rcu_pointer);
de10a585
MD
335 free(tid_reader);
336 free(tid_writer);
337 free(count_reader);
338 free(count_writer);
339 return 0;
340}
This page took 0.061578 seconds and 5 git commands to generate.