Cleanup: enable signed/unsigned compare compiler warning
[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;
83e334d0 270 unsigned int i_thr;
de10a585
MD
271
272 if (argc < 4) {
273 show_usage(argc, argv);
274 return -1;
275 }
276
277 err = sscanf(argv[1], "%u", &nr_readers);
278 if (err != 1) {
279 show_usage(argc, argv);
280 return -1;
281 }
282
283 err = sscanf(argv[2], "%u", &nr_writers);
284 if (err != 1) {
285 show_usage(argc, argv);
286 return -1;
287 }
83e334d0 288
de10a585
MD
289 err = sscanf(argv[3], "%lu", &duration);
290 if (err != 1) {
291 show_usage(argc, argv);
292 return -1;
293 }
294
295 for (i = 4; i < argc; i++) {
296 if (argv[i][0] != '-')
297 continue;
298 switch (argv[i][1]) {
de10a585 299 case 'r':
2650042a 300 rcu_debug_yield_enable(RCU_YIELD_READ);
de10a585
MD
301 break;
302 case 'w':
2650042a 303 rcu_debug_yield_enable(RCU_YIELD_WRITE);
de10a585 304 break;
de10a585
MD
305 case 'a':
306 if (argc < i + 2) {
307 show_usage(argc, argv);
308 return -1;
309 }
310 a = atoi(argv[++i]);
311 cpu_affinities[next_aff++] = a;
312 use_affinity = 1;
313 printf_verbose("Adding CPU %d affinity\n", a);
314 break;
315 case 'c':
316 if (argc < i + 2) {
317 show_usage(argc, argv);
318 return -1;
319 }
320 rduration = atol(argv[++i]);
321 break;
322 case 'd':
323 if (argc < i + 2) {
324 show_usage(argc, argv);
325 return -1;
326 }
327 wdelay = atol(argv[++i]);
328 break;
329 case 'e':
330 if (argc < i + 2) {
331 show_usage(argc, argv);
332 return -1;
333 }
334 wduration = atol(argv[++i]);
335 break;
336 case 'v':
337 verbose_mode = 1;
338 break;
339 }
340 }
341
342 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
343 duration, nr_readers, nr_writers);
344 printf_verbose("Writer delay : %lu loops.\n", wdelay);
345 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
346 printf_verbose("thread %-6s, tid %lu\n",
347 "main", urcu_get_thread_id());
de10a585 348
9aa14175
MD
349 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
350 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
351 count_reader = calloc(nr_readers, sizeof(*count_reader));
352 count_writer = calloc(nr_writers, sizeof(*count_writer));
de10a585
MD
353
354 next_aff = 0;
355
83e334d0
MJ
356 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
357 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
358 &count_reader[i_thr]);
de10a585
MD
359 if (err != 0)
360 exit(1);
361 }
83e334d0
MJ
362 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
363 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
364 &count_writer[i_thr]);
de10a585
MD
365 if (err != 0)
366 exit(1);
367 }
368
369 cmm_smp_mb();
370
371 test_go = 1;
372
373 sleep(duration);
374
375 test_stop = 1;
376
83e334d0
MJ
377 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
378 err = pthread_join(tid_reader[i_thr], &tret);
de10a585
MD
379 if (err != 0)
380 exit(1);
83e334d0 381 tot_reads += count_reader[i_thr];
de10a585 382 }
83e334d0
MJ
383 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
384 err = pthread_join(tid_writer[i_thr], &tret);
de10a585
MD
385 if (err != 0)
386 exit(1);
83e334d0 387 tot_writes += count_writer[i_thr];
de10a585 388 }
83e334d0 389
de10a585
MD
390 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
391 tot_writes);
392 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
393 "nr_writers %3u "
394 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
395 argv[0], duration, nr_readers, rduration, wduration,
396 nr_writers, wdelay, tot_reads, tot_writes,
397 tot_reads + tot_writes);
bcc74df3 398 free(test_rcu_pointer);
de10a585
MD
399 free(tid_reader);
400 free(tid_writer);
401 free(count_reader);
402 free(count_writer);
403 return 0;
404}
This page took 0.080534 seconds and 4 git commands to generate.