Fix: add missing regtest and benchmark files to dist tarball
[urcu.git] / tests / benchmark / test_urcu_qsbr.c
CommitLineData
de10a585
MD
1/*
2 * test_urcu.c
3 *
4 * Userspace RCU library - test program
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#define _GNU_SOURCE
f5ab766e 24#include "config.h"
de10a585
MD
25#include <stdio.h>
26#include <pthread.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <unistd.h>
32#include <stdio.h>
33#include <assert.h>
de10a585
MD
34#include <errno.h>
35
36#include <urcu/arch.h>
bd252a04 37#include <urcu/tls-compat.h>
2953b501 38#include "cpuset.h"
94df6318 39#include "thread-id.h"
2650042a 40#include "../common/debug-yield.h"
de10a585
MD
41
42/* hardcoded number of CPUs */
43#define NR_CPUS 16384
44
de10a585
MD
45#ifndef DYNAMIC_LINK_TEST
46#define _LGPL_SOURCE
de10a585
MD
47#endif
48#include "urcu-qsbr.h"
49
de10a585
MD
50static volatile int test_go, test_stop;
51
52static unsigned long wdelay;
53
bcc74df3 54static int *test_rcu_pointer;
de10a585
MD
55
56static unsigned long duration;
57
58/* read-side C.S. duration, in loops */
59static unsigned long rduration;
60
61/* write-side C.S. duration, in loops */
62static unsigned long wduration;
63
ab0aacbe 64static inline void loop_sleep(unsigned long loops)
de10a585 65{
ab0aacbe 66 while (loops-- != 0)
de10a585
MD
67 caa_cpu_relax();
68}
69
70static int verbose_mode;
71
72#define printf_verbose(fmt, args...) \
73 do { \
74 if (verbose_mode) \
75 printf(fmt, args); \
76 } while (0)
77
78static unsigned int cpu_affinities[NR_CPUS];
79static unsigned int next_aff = 0;
80static int use_affinity = 0;
81
82pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
83
de10a585
MD
84static void set_affinity(void)
85{
95bc7fb9 86#if HAVE_SCHED_SETAFFINITY
de10a585 87 cpu_set_t mask;
95bc7fb9
MD
88 int cpu, ret;
89#endif /* HAVE_SCHED_SETAFFINITY */
de10a585
MD
90
91 if (!use_affinity)
92 return;
93
94#if HAVE_SCHED_SETAFFINITY
95 ret = pthread_mutex_lock(&affinity_mutex);
96 if (ret) {
97 perror("Error in pthread mutex lock");
98 exit(-1);
99 }
100 cpu = cpu_affinities[next_aff++];
101 ret = pthread_mutex_unlock(&affinity_mutex);
102 if (ret) {
103 perror("Error in pthread mutex unlock");
104 exit(-1);
105 }
106 CPU_ZERO(&mask);
107 CPU_SET(cpu, &mask);
108#if SCHED_SETAFFINITY_ARGS == 2
109 sched_setaffinity(0, &mask);
110#else
111 sched_setaffinity(0, sizeof(mask), &mask);
112#endif
113#endif /* HAVE_SCHED_SETAFFINITY */
114}
115
116/*
117 * returns 0 if test should end.
118 */
119static int test_duration_write(void)
120{
121 return !test_stop;
122}
123
124static int test_duration_read(void)
125{
126 return !test_stop;
127}
128
bd252a04
MD
129static DEFINE_URCU_TLS(unsigned long long, nr_writes);
130static DEFINE_URCU_TLS(unsigned long long, nr_reads);
de10a585
MD
131
132static unsigned int nr_readers;
133static unsigned int nr_writers;
134
135pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
136
137void rcu_copy_mutex_lock(void)
138{
139 int ret;
140 ret = pthread_mutex_lock(&rcu_copy_mutex);
141 if (ret) {
142 perror("Error in pthread mutex lock");
143 exit(-1);
144 }
145}
146
147void rcu_copy_mutex_unlock(void)
148{
149 int ret;
150
151 ret = pthread_mutex_unlock(&rcu_copy_mutex);
152 if (ret) {
153 perror("Error in pthread mutex unlock");
154 exit(-1);
155 }
156}
157
de10a585
MD
158void *thr_reader(void *_count)
159{
160 unsigned long long *count = _count;
bcc74df3 161 int *local_ptr;
de10a585 162
94df6318
MD
163 printf_verbose("thread_begin %s, tid %lu\n",
164 "reader", urcu_get_thread_id());
de10a585
MD
165
166 set_affinity();
167
168 rcu_register_thread();
169
882f3357
MD
170 assert(rcu_read_ongoing());
171 rcu_thread_offline();
172 assert(!rcu_read_ongoing());
173 rcu_thread_online();
174
de10a585
MD
175 while (!test_go)
176 {
177 }
178 cmm_smp_mb();
179
180 for (;;) {
181 rcu_read_lock();
882f3357 182 assert(rcu_read_ongoing());
de10a585 183 local_ptr = rcu_dereference(test_rcu_pointer);
1de4df4b 184 rcu_debug_yield_read();
de10a585 185 if (local_ptr)
bcc74df3 186 assert(*local_ptr == 8);
a0b7f7ea 187 if (caa_unlikely(rduration))
de10a585
MD
188 loop_sleep(rduration);
189 rcu_read_unlock();
bd252a04 190 URCU_TLS(nr_reads)++;
de10a585 191 /* QS each 1024 reads */
bd252a04 192 if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0))
de10a585 193 rcu_quiescent_state();
a0b7f7ea 194 if (caa_unlikely(!test_duration_read()))
de10a585
MD
195 break;
196 }
197
198 rcu_unregister_thread();
199
200 /* test extra thread registration */
201 rcu_register_thread();
202 rcu_unregister_thread();
203
bd252a04 204 *count = URCU_TLS(nr_reads);
94df6318
MD
205 printf_verbose("thread_end %s, tid %lu\n",
206 "reader", urcu_get_thread_id());
de10a585
MD
207 return ((void*)1);
208
209}
210
211void *thr_writer(void *_count)
212{
213 unsigned long long *count = _count;
bcc74df3 214 int *new, *old;
de10a585 215
94df6318
MD
216 printf_verbose("thread_begin %s, tid %lu\n",
217 "writer", urcu_get_thread_id());
de10a585
MD
218
219 set_affinity();
220
221 while (!test_go)
222 {
223 }
224 cmm_smp_mb();
225
226 for (;;) {
bcc74df3
MD
227 new = malloc(sizeof(int));
228 assert(new);
229 *new = 8;
de10a585 230 old = rcu_xchg_pointer(&test_rcu_pointer, new);
a0b7f7ea 231 if (caa_unlikely(wduration))
de10a585
MD
232 loop_sleep(wduration);
233 synchronize_rcu();
de10a585 234 if (old)
bcc74df3
MD
235 *old = 0;
236 free(old);
bd252a04 237 URCU_TLS(nr_writes)++;
a0b7f7ea 238 if (caa_unlikely(!test_duration_write()))
de10a585 239 break;
a0b7f7ea 240 if (caa_unlikely(wdelay))
de10a585
MD
241 loop_sleep(wdelay);
242 }
243
94df6318
MD
244 printf_verbose("thread_end %s, tid %lu\n",
245 "writer", urcu_get_thread_id());
bd252a04 246 *count = URCU_TLS(nr_writes);
de10a585
MD
247 return ((void*)2);
248}
249
250void show_usage(int argc, char **argv)
251{
06637138
MD
252 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
253 argv[0]);
254 printf("OPTIONS:\n");
06637138 255 printf(" [-r] [-w] (yield reader and/or writer)\n");
06637138
MD
256 printf(" [-d delay] (writer period (us))\n");
257 printf(" [-c duration] (reader C.S. duration (in loops))\n");
258 printf(" [-e duration] (writer C.S. duration (in loops))\n");
259 printf(" [-v] (verbose output)\n");
260 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
de10a585
MD
261 printf("\n");
262}
263
264int main(int argc, char **argv)
265{
266 int err;
267 pthread_t *tid_reader, *tid_writer;
268 void *tret;
269 unsigned long long *count_reader, *count_writer;
270 unsigned long long tot_reads = 0, tot_writes = 0;
271 int i, a;
272
273 if (argc < 4) {
274 show_usage(argc, argv);
275 return -1;
276 }
277
278 err = sscanf(argv[1], "%u", &nr_readers);
279 if (err != 1) {
280 show_usage(argc, argv);
281 return -1;
282 }
283
284 err = sscanf(argv[2], "%u", &nr_writers);
285 if (err != 1) {
286 show_usage(argc, argv);
287 return -1;
288 }
289
290 err = sscanf(argv[3], "%lu", &duration);
291 if (err != 1) {
292 show_usage(argc, argv);
293 return -1;
294 }
295
296 for (i = 4; i < argc; i++) {
297 if (argv[i][0] != '-')
298 continue;
299 switch (argv[i][1]) {
de10a585 300 case 'r':
2650042a 301 rcu_debug_yield_enable(RCU_YIELD_READ);
de10a585
MD
302 break;
303 case 'w':
2650042a 304 rcu_debug_yield_enable(RCU_YIELD_WRITE);
de10a585 305 break;
de10a585
MD
306 case 'a':
307 if (argc < i + 2) {
308 show_usage(argc, argv);
309 return -1;
310 }
311 a = atoi(argv[++i]);
312 cpu_affinities[next_aff++] = a;
313 use_affinity = 1;
314 printf_verbose("Adding CPU %d affinity\n", a);
315 break;
316 case 'c':
317 if (argc < i + 2) {
318 show_usage(argc, argv);
319 return -1;
320 }
321 rduration = atol(argv[++i]);
322 break;
323 case 'd':
324 if (argc < i + 2) {
325 show_usage(argc, argv);
326 return -1;
327 }
328 wdelay = atol(argv[++i]);
329 break;
330 case 'e':
331 if (argc < i + 2) {
332 show_usage(argc, argv);
333 return -1;
334 }
335 wduration = atol(argv[++i]);
336 break;
337 case 'v':
338 verbose_mode = 1;
339 break;
340 }
341 }
342
343 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
344 duration, nr_readers, nr_writers);
345 printf_verbose("Writer delay : %lu loops.\n", wdelay);
346 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
347 printf_verbose("thread %-6s, tid %lu\n",
348 "main", urcu_get_thread_id());
de10a585 349
9aa14175
MD
350 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
351 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
352 count_reader = calloc(nr_readers, sizeof(*count_reader));
353 count_writer = calloc(nr_writers, sizeof(*count_writer));
de10a585
MD
354
355 next_aff = 0;
356
357 for (i = 0; i < nr_readers; i++) {
358 err = pthread_create(&tid_reader[i], NULL, thr_reader,
359 &count_reader[i]);
360 if (err != 0)
361 exit(1);
362 }
363 for (i = 0; i < nr_writers; i++) {
364 err = pthread_create(&tid_writer[i], NULL, thr_writer,
365 &count_writer[i]);
366 if (err != 0)
367 exit(1);
368 }
369
370 cmm_smp_mb();
371
372 test_go = 1;
373
374 sleep(duration);
375
376 test_stop = 1;
377
378 for (i = 0; i < nr_readers; i++) {
379 err = pthread_join(tid_reader[i], &tret);
380 if (err != 0)
381 exit(1);
382 tot_reads += count_reader[i];
383 }
384 for (i = 0; i < nr_writers; i++) {
385 err = pthread_join(tid_writer[i], &tret);
386 if (err != 0)
387 exit(1);
388 tot_writes += count_writer[i];
389 }
390
391 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
392 tot_writes);
393 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
394 "nr_writers %3u "
395 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
396 argv[0], duration, nr_readers, rduration, wduration,
397 nr_writers, wdelay, tot_reads, tot_writes,
398 tot_reads + tot_writes);
bcc74df3 399 free(test_rcu_pointer);
de10a585
MD
400 free(tid_reader);
401 free(tid_writer);
402 free(count_reader);
403 free(count_writer);
404 return 0;
405}
This page took 0.04831 seconds and 4 git commands to generate.