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