urcu: declare test_urcu_multiflavor functions
[urcu.git] / tests / test_urcu_wfs.c
CommitLineData
a5eb5ffc
MD
1/*
2 * test_urcu_wfs.c
3 *
4 * Userspace RCU library - example RCU-based lock-free stack
5 *
6 * Copyright February 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright February 2010 - Paolo Bonzini <pbonzini@redhat.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#define _GNU_SOURCE
25#include "../config.h"
26#include <stdio.h>
27#include <pthread.h>
28#include <stdlib.h>
29#include <stdint.h>
30#include <stdbool.h>
31#include <string.h>
32#include <sys/types.h>
33#include <sys/wait.h>
34#include <unistd.h>
35#include <stdio.h>
36#include <assert.h>
a5eb5ffc
MD
37#include <sched.h>
38#include <errno.h>
39
40#include <urcu/arch.h>
bd252a04 41#include <urcu/tls-compat.h>
a5eb5ffc 42
65fcc7e9
MD
43#ifdef __linux__
44#include <syscall.h>
45#endif
46
a5eb5ffc
MD
47/* hardcoded number of CPUs */
48#define NR_CPUS 16384
49
50#if defined(_syscall0)
51_syscall0(pid_t, gettid)
52#elif defined(__NR_gettid)
53static inline pid_t gettid(void)
54{
55 return syscall(__NR_gettid);
56}
57#else
58#warning "use pid as tid"
59static inline pid_t gettid(void)
60{
61 return getpid();
62}
63#endif
64
65#ifndef DYNAMIC_LINK_TEST
66#define _LGPL_SOURCE
67#endif
68#include <urcu.h>
cb3f3d6b 69#include <urcu/wfstack.h>
a5eb5ffc 70
69328034
MD
71/*
72 * External synchronization used.
73 */
74enum test_sync {
75 TEST_SYNC_NONE = 0,
76 TEST_SYNC_MUTEX,
77};
78
79static enum test_sync test_sync;
80
0ee400bd
MD
81static int test_force_sync;
82
d05e9936 83static volatile int test_go, test_stop_enqueue, test_stop_dequeue;
a5eb5ffc
MD
84
85static unsigned long rduration;
86
87static unsigned long duration;
88
89/* read-side C.S. duration, in loops */
90static unsigned long wdelay;
91
ab0aacbe 92static inline void loop_sleep(unsigned long loops)
a5eb5ffc 93{
ab0aacbe 94 while (loops-- != 0)
06f22bdb 95 caa_cpu_relax();
a5eb5ffc
MD
96}
97
98static int verbose_mode;
99
d05e9936
MD
100static int test_pop, test_pop_all, test_wait_empty;
101static int test_enqueue_stopped;
69328034 102
a5eb5ffc
MD
103#define printf_verbose(fmt, args...) \
104 do { \
105 if (verbose_mode) \
69328034 106 printf(fmt, ## args); \
a5eb5ffc
MD
107 } while (0)
108
109static unsigned int cpu_affinities[NR_CPUS];
110static unsigned int next_aff = 0;
111static int use_affinity = 0;
112
113pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
114
115#ifndef HAVE_CPU_SET_T
116typedef unsigned long cpu_set_t;
117# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
118# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
119#endif
120
121static void set_affinity(void)
122{
95bc7fb9 123#if HAVE_SCHED_SETAFFINITY
a5eb5ffc 124 cpu_set_t mask;
95bc7fb9
MD
125 int cpu, ret;
126#endif /* HAVE_SCHED_SETAFFINITY */
a5eb5ffc
MD
127
128 if (!use_affinity)
129 return;
130
131#if HAVE_SCHED_SETAFFINITY
132 ret = pthread_mutex_lock(&affinity_mutex);
133 if (ret) {
134 perror("Error in pthread mutex lock");
135 exit(-1);
136 }
137 cpu = cpu_affinities[next_aff++];
138 ret = pthread_mutex_unlock(&affinity_mutex);
139 if (ret) {
140 perror("Error in pthread mutex unlock");
141 exit(-1);
142 }
143
144 CPU_ZERO(&mask);
145 CPU_SET(cpu, &mask);
146#if SCHED_SETAFFINITY_ARGS == 2
147 sched_setaffinity(0, &mask);
148#else
149 sched_setaffinity(0, sizeof(mask), &mask);
150#endif
151#endif /* HAVE_SCHED_SETAFFINITY */
152}
153
154/*
155 * returns 0 if test should end.
156 */
157static int test_duration_dequeue(void)
158{
d05e9936 159 return !test_stop_dequeue;
a5eb5ffc
MD
160}
161
162static int test_duration_enqueue(void)
163{
d05e9936 164 return !test_stop_enqueue;
a5eb5ffc
MD
165}
166
bd252a04
MD
167static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
168static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
a5eb5ffc 169
bd252a04
MD
170static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
171static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
d05e9936
MD
172static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues);
173static DEFINE_URCU_TLS(unsigned long long, nr_pop_all);
a5eb5ffc
MD
174
175static unsigned int nr_enqueuers;
176static unsigned int nr_dequeuers;
177
16aa9ee8 178static struct cds_wfs_stack s;
a5eb5ffc 179
98fbc848 180static void *thr_enqueuer(void *_count)
a5eb5ffc
MD
181{
182 unsigned long long *count = _count;
d05e9936 183 bool was_nonempty;
a5eb5ffc
MD
184
185 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
6ff854ac
MD
186 "enqueuer", (unsigned long) pthread_self(),
187 (unsigned long) gettid());
a5eb5ffc
MD
188
189 set_affinity();
190
a5eb5ffc
MD
191 while (!test_go)
192 {
193 }
5481ddb3 194 cmm_smp_mb();
a5eb5ffc
MD
195
196 for (;;) {
16aa9ee8 197 struct cds_wfs_node *node = malloc(sizeof(*node));
a5eb5ffc
MD
198 if (!node)
199 goto fail;
16aa9ee8 200 cds_wfs_node_init(node);
d05e9936 201 was_nonempty = cds_wfs_push(&s, node);
bd252a04 202 URCU_TLS(nr_successful_enqueues)++;
d05e9936
MD
203 if (!was_nonempty)
204 URCU_TLS(nr_empty_dest_enqueues)++;
a5eb5ffc 205
a0b7f7ea 206 if (caa_unlikely(wdelay))
a5eb5ffc
MD
207 loop_sleep(wdelay);
208fail:
bd252a04 209 URCU_TLS(nr_enqueues)++;
a0b7f7ea 210 if (caa_unlikely(!test_duration_enqueue()))
a5eb5ffc
MD
211 break;
212 }
213
d05e9936 214 uatomic_inc(&test_enqueue_stopped);
bd252a04
MD
215 count[0] = URCU_TLS(nr_enqueues);
216 count[1] = URCU_TLS(nr_successful_enqueues);
d05e9936 217 count[2] = URCU_TLS(nr_empty_dest_enqueues);
a5eb5ffc 218 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
d05e9936
MD
219 "enqueues %llu successful_enqueues %llu, "
220 "empty_dest_enqueues %llu\n",
6ff854ac
MD
221 pthread_self(),
222 (unsigned long) gettid(),
d05e9936
MD
223 URCU_TLS(nr_enqueues),
224 URCU_TLS(nr_successful_enqueues),
225 URCU_TLS(nr_empty_dest_enqueues));
a5eb5ffc
MD
226 return ((void*)1);
227
228}
229
69328034
MD
230static void do_test_pop(enum test_sync sync)
231{
232 struct cds_wfs_node *node;
233
234 if (sync == TEST_SYNC_MUTEX)
235 cds_wfs_pop_lock(&s);
236 node = __cds_wfs_pop_blocking(&s);
237 if (sync == TEST_SYNC_MUTEX)
238 cds_wfs_pop_unlock(&s);
239
240 if (node) {
241 free(node);
242 URCU_TLS(nr_successful_dequeues)++;
243 }
244 URCU_TLS(nr_dequeues)++;
245}
246
247static void do_test_pop_all(enum test_sync sync)
248{
249 struct cds_wfs_head *head;
250 struct cds_wfs_node *node, *n;
251
252 if (sync == TEST_SYNC_MUTEX)
253 cds_wfs_pop_lock(&s);
254 head = __cds_wfs_pop_all(&s);
255 if (sync == TEST_SYNC_MUTEX)
256 cds_wfs_pop_unlock(&s);
257
d05e9936 258 /* Check if empty */
c7ba06ba 259 if (cds_wfs_first(head) == NULL)
d05e9936
MD
260 return;
261
262 URCU_TLS(nr_pop_all)++;
263
69328034
MD
264 cds_wfs_for_each_blocking_safe(head, node, n) {
265 free(node);
266 URCU_TLS(nr_successful_dequeues)++;
267 URCU_TLS(nr_dequeues)++;
268 }
269}
270
98fbc848 271static void *thr_dequeuer(void *_count)
a5eb5ffc
MD
272{
273 unsigned long long *count = _count;
69328034 274 unsigned int counter;
a5eb5ffc
MD
275
276 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
6ff854ac
MD
277 "dequeuer", (unsigned long) pthread_self(),
278 (unsigned long) gettid());
a5eb5ffc
MD
279
280 set_affinity();
281
a5eb5ffc
MD
282 while (!test_go)
283 {
284 }
5481ddb3 285 cmm_smp_mb();
a5eb5ffc 286
69328034 287 assert(test_pop || test_pop_all);
a5eb5ffc 288
69328034
MD
289 for (;;) {
290 if (test_pop && test_pop_all) {
291 if (counter & 1)
292 do_test_pop(test_sync);
293 else
294 do_test_pop_all(test_sync);
295 counter++;
296 } else {
297 if (test_pop)
298 do_test_pop(test_sync);
299 else
300 do_test_pop_all(test_sync);
a5eb5ffc
MD
301 }
302
a0b7f7ea 303 if (caa_unlikely(!test_duration_dequeue()))
a5eb5ffc 304 break;
a0b7f7ea 305 if (caa_unlikely(rduration))
a5eb5ffc
MD
306 loop_sleep(rduration);
307 }
308
a5eb5ffc 309 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
d05e9936
MD
310 "dequeues %llu, successful_dequeues %llu "
311 "pop_all %llu\n",
6ff854ac
MD
312 pthread_self(),
313 (unsigned long) gettid(),
d05e9936
MD
314 URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues),
315 URCU_TLS(nr_pop_all));
bd252a04
MD
316 count[0] = URCU_TLS(nr_dequeues);
317 count[1] = URCU_TLS(nr_successful_dequeues);
d05e9936 318 count[2] = URCU_TLS(nr_pop_all);
a5eb5ffc
MD
319 return ((void*)2);
320}
321
98fbc848 322static void test_end(struct cds_wfs_stack *s, unsigned long long *nr_dequeues)
a5eb5ffc 323{
16aa9ee8 324 struct cds_wfs_node *node;
a5eb5ffc
MD
325
326 do {
16aa9ee8 327 node = cds_wfs_pop_blocking(s);
a5eb5ffc
MD
328 if (node) {
329 free(node);
330 (*nr_dequeues)++;
331 }
332 } while (node);
333}
334
98fbc848 335static void show_usage(int argc, char **argv)
a5eb5ffc
MD
336{
337 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv[0]);
338 printf(" [-d delay] (enqueuer period (in loops))");
339 printf(" [-c duration] (dequeuer period (in loops))");
340 printf(" [-v] (verbose output)");
341 printf(" [-a cpu#] [-a cpu#]... (affinity)");
69328034
MD
342 printf(" [-p] (test pop)");
343 printf(" [-P] (test pop_all, enabled by default)");
344 printf(" [-M] (use mutex external synchronization)");
345 printf(" Note: default: no external synchronization used.");
0ee400bd 346 printf(" [-f] (force user-provided synchronization)");
d05e9936 347 printf(" [-w] Wait for dequeuer to empty stack");
a5eb5ffc
MD
348 printf("\n");
349}
350
351int main(int argc, char **argv)
352{
353 int err;
354 pthread_t *tid_enqueuer, *tid_dequeuer;
355 void *tret;
356 unsigned long long *count_enqueuer, *count_dequeuer;
357 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
358 unsigned long long tot_successful_enqueues = 0,
d05e9936
MD
359 tot_successful_dequeues = 0,
360 tot_empty_dest_enqueues = 0,
361 tot_pop_all = 0;
a5eb5ffc 362 unsigned long long end_dequeues = 0;
d05e9936 363 int i, a, retval = 0;
a5eb5ffc
MD
364
365 if (argc < 4) {
366 show_usage(argc, argv);
367 return -1;
368 }
369
370 err = sscanf(argv[1], "%u", &nr_dequeuers);
371 if (err != 1) {
372 show_usage(argc, argv);
373 return -1;
374 }
375
376 err = sscanf(argv[2], "%u", &nr_enqueuers);
377 if (err != 1) {
378 show_usage(argc, argv);
379 return -1;
380 }
381
382 err = sscanf(argv[3], "%lu", &duration);
383 if (err != 1) {
384 show_usage(argc, argv);
385 return -1;
386 }
387
388 for (i = 4; i < argc; i++) {
389 if (argv[i][0] != '-')
390 continue;
391 switch (argv[i][1]) {
392 case 'a':
393 if (argc < i + 2) {
394 show_usage(argc, argv);
395 return -1;
396 }
397 a = atoi(argv[++i]);
398 cpu_affinities[next_aff++] = a;
399 use_affinity = 1;
400 printf_verbose("Adding CPU %d affinity\n", a);
401 break;
402 case 'c':
403 if (argc < i + 2) {
404 show_usage(argc, argv);
405 return -1;
406 }
407 rduration = atol(argv[++i]);
408 break;
409 case 'd':
410 if (argc < i + 2) {
411 show_usage(argc, argv);
412 return -1;
413 }
414 wdelay = atol(argv[++i]);
415 break;
416 case 'v':
417 verbose_mode = 1;
418 break;
69328034
MD
419 case 'p':
420 test_pop = 1;
421 break;
422 case 'P':
423 test_pop_all = 1;
424 break;
425 case 'M':
426 test_sync = TEST_SYNC_MUTEX;
427 break;
d05e9936
MD
428 case 'w':
429 test_wait_empty = 1;
430 break;
0ee400bd
MD
431 case 'f':
432 test_force_sync = 1;
433 break;
a5eb5ffc
MD
434 }
435 }
436
69328034
MD
437 /* activate pop_all test by default */
438 if (!test_pop && !test_pop_all)
439 test_pop_all = 1;
440
0ee400bd
MD
441 if (test_sync == TEST_SYNC_NONE && nr_dequeuers > 1 && test_pop) {
442 if (test_force_sync) {
443 fprintf(stderr, "[WARNING] Using pop concurrently "
444 "with other pop or pop_all without external "
445 "synchronization. Expect run-time failure.\n");
446 } else {
447 printf("Enforcing mutex synchronization\n");
448 test_sync = TEST_SYNC_MUTEX;
449 }
450 }
451
a5eb5ffc
MD
452 printf_verbose("running test for %lu seconds, %u enqueuers, "
453 "%u dequeuers.\n",
454 duration, nr_enqueuers, nr_dequeuers);
69328034
MD
455 if (test_pop)
456 printf_verbose("pop test activated.\n");
457 if (test_pop_all)
458 printf_verbose("pop_all test activated.\n");
98fbc848
MD
459 if (test_sync == TEST_SYNC_MUTEX)
460 printf_verbose("External sync: mutex.\n");
461 else
462 printf_verbose("External sync: none.\n");
d05e9936
MD
463 if (test_wait_empty)
464 printf_verbose("Wait for dequeuers to empty stack.\n");
a5eb5ffc
MD
465 printf_verbose("Writer delay : %lu loops.\n", rduration);
466 printf_verbose("Reader duration : %lu loops.\n", wdelay);
467 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
6ff854ac
MD
468 "main", (unsigned long) pthread_self(),
469 (unsigned long) gettid());
a5eb5ffc
MD
470
471 tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers);
472 tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers);
d05e9936
MD
473 count_enqueuer = malloc(3 * sizeof(*count_enqueuer) * nr_enqueuers);
474 count_dequeuer = malloc(3 * sizeof(*count_dequeuer) * nr_dequeuers);
16aa9ee8 475 cds_wfs_init(&s);
a5eb5ffc
MD
476
477 next_aff = 0;
478
479 for (i = 0; i < nr_enqueuers; i++) {
480 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
d05e9936 481 &count_enqueuer[3 * i]);
a5eb5ffc
MD
482 if (err != 0)
483 exit(1);
484 }
485 for (i = 0; i < nr_dequeuers; i++) {
486 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
d05e9936 487 &count_dequeuer[3 * i]);
a5eb5ffc
MD
488 if (err != 0)
489 exit(1);
490 }
491
5481ddb3 492 cmm_smp_mb();
a5eb5ffc
MD
493
494 test_go = 1;
495
496 for (i = 0; i < duration; i++) {
497 sleep(1);
498 if (verbose_mode)
499 write (1, ".", 1);
500 }
501
d05e9936
MD
502 test_stop_enqueue = 1;
503
504 if (test_wait_empty) {
505 while (nr_enqueuers != uatomic_read(&test_enqueue_stopped)) {
506 sleep(1);
507 }
508 while (!cds_wfs_empty(&s)) {
509 sleep(1);
510 }
511 }
512
513 test_stop_dequeue = 1;
a5eb5ffc
MD
514
515 for (i = 0; i < nr_enqueuers; i++) {
516 err = pthread_join(tid_enqueuer[i], &tret);
517 if (err != 0)
518 exit(1);
d05e9936
MD
519 tot_enqueues += count_enqueuer[3 * i];
520 tot_successful_enqueues += count_enqueuer[3 * i + 1];
521 tot_empty_dest_enqueues += count_enqueuer[3 * i + 2];
a5eb5ffc
MD
522 }
523 for (i = 0; i < nr_dequeuers; i++) {
524 err = pthread_join(tid_dequeuer[i], &tret);
525 if (err != 0)
526 exit(1);
d05e9936
MD
527 tot_dequeues += count_dequeuer[3 * i];
528 tot_successful_dequeues += count_dequeuer[3 * i + 1];
529 tot_pop_all += count_dequeuer[3 * i + 2];
a5eb5ffc
MD
530 }
531
532 test_end(&s, &end_dequeues);
533
534 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
535 tot_enqueues, tot_dequeues);
536 printf_verbose("total number of successful enqueues : %llu, "
d05e9936
MD
537 "enqueues to empty dest : %llu, "
538 "successful dequeues %llu, "
539 "pop_all : %llu\n",
540 tot_successful_enqueues,
541 tot_empty_dest_enqueues,
542 tot_successful_dequeues,
543 tot_pop_all);
a5eb5ffc
MD
544 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
545 "nr_dequeuers %3u "
546 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
d05e9936
MD
547 "successful enqueues %12llu enqueues to empty dest %12llu "
548 "successful dequeues %12llu pop_all %12llu "
a5eb5ffc
MD
549 "end_dequeues %llu nr_ops %12llu\n",
550 argv[0], duration, nr_enqueuers, wdelay,
551 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
552 tot_successful_enqueues,
d05e9936
MD
553 tot_empty_dest_enqueues,
554 tot_successful_dequeues, tot_pop_all, end_dequeues,
a5eb5ffc 555 tot_enqueues + tot_dequeues);
d05e9936 556 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) {
a5eb5ffc
MD
557 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
558 "succ. dequeues + end dequeues %llu.\n",
559 tot_successful_enqueues,
560 tot_successful_dequeues + end_dequeues);
d05e9936
MD
561 retval = 1;
562 }
563 /*
564 * If only using pop_all to dequeue, the enqueuer should see
565 * exactly as many empty queues than the number of non-empty
566 * stacks dequeued.
567 */
568 if (test_wait_empty && test_pop_all && !test_pop
569 && tot_empty_dest_enqueues != tot_pop_all) {
570 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
571 "number of non-empty pop_all (%llu)\n",
572 tot_empty_dest_enqueues,
573 tot_pop_all);
574 retval = 1;
575 }
a5eb5ffc
MD
576 free(count_enqueuer);
577 free(count_dequeuer);
578 free(tid_enqueuer);
579 free(tid_dequeuer);
d05e9936 580 return retval;
a5eb5ffc 581}
This page took 0.050674 seconds and 4 git commands to generate.