wfstack API: rename cds_wfs_first_blocking to cds_wfs_first
[userspace-rcu.git] / tests / test_urcu_wfcq.c
CommitLineData
5e70d572
MD
1/*
2 * test_urcu_wfcq.c
3 *
4 * Userspace RCU library - example RCU-based lock-free concurrent queue
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>
37#include <sched.h>
38#include <errno.h>
39
40#include <urcu/arch.h>
41#include <urcu/tls-compat.h>
42
43#ifdef __linux__
44#include <syscall.h>
45#endif
46
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>
69#include <urcu/wfcqueue.h>
70
f5d36fed 71enum test_sync {
372b9ad7
MD
72 TEST_SYNC_NONE = 0,
73 TEST_SYNC_MUTEX,
f5d36fed
MD
74};
75
76static enum test_sync test_sync;
77
372b9ad7
MD
78static int test_force_sync;
79
cf9451dc 80static volatile int test_go, test_stop_enqueue, test_stop_dequeue;
5e70d572
MD
81
82static unsigned long rduration;
83
84static unsigned long duration;
85
86/* read-side C.S. duration, in loops */
87static unsigned long wdelay;
88
ab0aacbe 89static inline void loop_sleep(unsigned long loops)
5e70d572 90{
ab0aacbe 91 while (loops-- != 0)
5e70d572
MD
92 caa_cpu_relax();
93}
94
95static int verbose_mode;
96
cf9451dc
MD
97static int test_dequeue, test_splice, test_wait_empty;
98static int test_enqueue_stopped;
f5d36fed 99
5e70d572
MD
100#define printf_verbose(fmt, args...) \
101 do { \
102 if (verbose_mode) \
f5d36fed 103 printf(fmt, ## args); \
5e70d572
MD
104 } while (0)
105
106static unsigned int cpu_affinities[NR_CPUS];
107static unsigned int next_aff = 0;
108static int use_affinity = 0;
109
110pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
111
112#ifndef HAVE_CPU_SET_T
113typedef unsigned long cpu_set_t;
114# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
115# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
116#endif
117
118static void set_affinity(void)
119{
95bc7fb9 120#if HAVE_SCHED_SETAFFINITY
5e70d572 121 cpu_set_t mask;
95bc7fb9
MD
122 int cpu, ret;
123#endif /* HAVE_SCHED_SETAFFINITY */
5e70d572
MD
124
125 if (!use_affinity)
126 return;
127
128#if HAVE_SCHED_SETAFFINITY
129 ret = pthread_mutex_lock(&affinity_mutex);
130 if (ret) {
131 perror("Error in pthread mutex lock");
132 exit(-1);
133 }
134 cpu = cpu_affinities[next_aff++];
135 ret = pthread_mutex_unlock(&affinity_mutex);
136 if (ret) {
137 perror("Error in pthread mutex unlock");
138 exit(-1);
139 }
140
141 CPU_ZERO(&mask);
142 CPU_SET(cpu, &mask);
143#if SCHED_SETAFFINITY_ARGS == 2
144 sched_setaffinity(0, &mask);
145#else
146 sched_setaffinity(0, sizeof(mask), &mask);
147#endif
148#endif /* HAVE_SCHED_SETAFFINITY */
149}
150
151/*
152 * returns 0 if test should end.
153 */
154static int test_duration_dequeue(void)
155{
cf9451dc 156 return !test_stop_dequeue;
5e70d572
MD
157}
158
159static int test_duration_enqueue(void)
160{
cf9451dc 161 return !test_stop_enqueue;
5e70d572
MD
162}
163
164static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
165static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
166
167static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
168static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
cf9451dc
MD
169static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues);
170static DEFINE_URCU_TLS(unsigned long long, nr_splice);
5e70d572
MD
171
172static unsigned int nr_enqueuers;
173static unsigned int nr_dequeuers;
174
175static struct cds_wfcq_head __attribute__((aligned(CAA_CACHE_LINE_SIZE))) head;
176static struct cds_wfcq_tail __attribute__((aligned(CAA_CACHE_LINE_SIZE))) tail;
177
f5d36fed 178static void *thr_enqueuer(void *_count)
5e70d572
MD
179{
180 unsigned long long *count = _count;
cf9451dc 181 bool was_nonempty;
5e70d572
MD
182
183 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
6ff854ac
MD
184 "enqueuer", (unsigned long) pthread_self(),
185 (unsigned long) gettid());
5e70d572
MD
186
187 set_affinity();
188
189 while (!test_go)
190 {
191 }
192 cmm_smp_mb();
193
194 for (;;) {
195 struct cds_wfcq_node *node = malloc(sizeof(*node));
196 if (!node)
197 goto fail;
198 cds_wfcq_node_init(node);
cf9451dc 199 was_nonempty = cds_wfcq_enqueue(&head, &tail, node);
5e70d572 200 URCU_TLS(nr_successful_enqueues)++;
cf9451dc
MD
201 if (!was_nonempty)
202 URCU_TLS(nr_empty_dest_enqueues)++;
5e70d572
MD
203
204 if (caa_unlikely(wdelay))
205 loop_sleep(wdelay);
206fail:
207 URCU_TLS(nr_enqueues)++;
208 if (caa_unlikely(!test_duration_enqueue()))
209 break;
210 }
211
cf9451dc 212 uatomic_inc(&test_enqueue_stopped);
5e70d572
MD
213 count[0] = URCU_TLS(nr_enqueues);
214 count[1] = URCU_TLS(nr_successful_enqueues);
cf9451dc 215 count[2] = URCU_TLS(nr_empty_dest_enqueues);
5e70d572 216 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
cf9451dc
MD
217 "enqueues %llu successful_enqueues %llu, "
218 "empty_dest_enqueues %llu\n",
6ff854ac
MD
219 pthread_self(),
220 (unsigned long) gettid(),
cf9451dc
MD
221 URCU_TLS(nr_enqueues),
222 URCU_TLS(nr_successful_enqueues),
223 URCU_TLS(nr_empty_dest_enqueues));
5e70d572
MD
224 return ((void*)1);
225
226}
227
f5d36fed
MD
228static void do_test_dequeue(enum test_sync sync)
229{
230 struct cds_wfcq_node *node;
231
232 if (sync == TEST_SYNC_MUTEX)
233 node = cds_wfcq_dequeue_blocking(&head, &tail);
234 else
235 node = __cds_wfcq_dequeue_blocking(&head, &tail);
236
237 if (node) {
238 free(node);
239 URCU_TLS(nr_successful_dequeues)++;
240 }
241 URCU_TLS(nr_dequeues)++;
242}
243
244static void do_test_splice(enum test_sync sync)
245{
246 struct cds_wfcq_head tmp_head;
247 struct cds_wfcq_tail tmp_tail;
248 struct cds_wfcq_node *node, *n;
cf9451dc 249 enum cds_wfcq_ret ret;
f5d36fed
MD
250
251 cds_wfcq_init(&tmp_head, &tmp_tail);
252
253 if (sync == TEST_SYNC_MUTEX)
cf9451dc 254 ret = cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
f5d36fed
MD
255 &head, &tail);
256 else
cf9451dc 257 ret = __cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
f5d36fed
MD
258 &head, &tail);
259
cf9451dc
MD
260 switch (ret) {
261 case CDS_WFCQ_RET_WOULDBLOCK:
262 assert(0); /* blocking call */
263 break;
264 case CDS_WFCQ_RET_DEST_EMPTY:
265 URCU_TLS(nr_splice)++;
266 /* ok */
267 break;
268 case CDS_WFCQ_RET_DEST_NON_EMPTY:
269 assert(0); /* entirely unexpected */
270 break;
271 case CDS_WFCQ_RET_SRC_EMPTY:
272 /* ok, we could even skip iteration on dest if we wanted */
273 break;
274 }
275
f5d36fed
MD
276 __cds_wfcq_for_each_blocking_safe(&tmp_head, &tmp_tail, node, n) {
277 free(node);
278 URCU_TLS(nr_successful_dequeues)++;
279 URCU_TLS(nr_dequeues)++;
280 }
281}
282
283static void *thr_dequeuer(void *_count)
5e70d572
MD
284{
285 unsigned long long *count = _count;
f5d36fed 286 unsigned int counter;
5e70d572
MD
287
288 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
6ff854ac
MD
289 "dequeuer", (unsigned long) pthread_self(),
290 (unsigned long) gettid());
5e70d572
MD
291
292 set_affinity();
293
294 while (!test_go)
295 {
296 }
297 cmm_smp_mb();
298
299 for (;;) {
f5d36fed
MD
300 if (test_dequeue && test_splice) {
301 if (counter & 1)
302 do_test_dequeue(test_sync);
303 else
304 do_test_splice(test_sync);
305 counter++;
306 } else {
307 if (test_dequeue)
308 do_test_dequeue(test_sync);
309 else
310 do_test_splice(test_sync);
5e70d572 311 }
5e70d572
MD
312 if (caa_unlikely(!test_duration_dequeue()))
313 break;
314 if (caa_unlikely(rduration))
315 loop_sleep(rduration);
316 }
317
318 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
cf9451dc
MD
319 "dequeues %llu, successful_dequeues %llu, "
320 "nr_splice %llu\n",
6ff854ac
MD
321 pthread_self(),
322 (unsigned long) gettid(),
cf9451dc
MD
323 URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues),
324 URCU_TLS(nr_splice));
5e70d572
MD
325 count[0] = URCU_TLS(nr_dequeues);
326 count[1] = URCU_TLS(nr_successful_dequeues);
cf9451dc 327 count[2] = URCU_TLS(nr_splice);
5e70d572
MD
328 return ((void*)2);
329}
330
f5d36fed 331static void test_end(unsigned long long *nr_dequeues)
5e70d572
MD
332{
333 struct cds_wfcq_node *node;
334
335 do {
336 node = cds_wfcq_dequeue_blocking(&head, &tail);
337 if (node) {
338 free(node);
339 (*nr_dequeues)++;
340 }
341 } while (node);
342}
343
f5d36fed 344static void show_usage(int argc, char **argv)
5e70d572
MD
345{
346 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv[0]);
347 printf(" [-d delay] (enqueuer period (in loops))");
348 printf(" [-c duration] (dequeuer period (in loops))");
349 printf(" [-v] (verbose output)");
350 printf(" [-a cpu#] [-a cpu#]... (affinity)");
f5d36fed
MD
351 printf(" [-q] (test dequeue)");
352 printf(" [-s] (test splice, enabled by default)");
353 printf(" [-M] (use mutex external synchronization)");
372b9ad7
MD
354 printf(" Note: default: no external synchronization used.");
355 printf(" [-f] (force user-provided synchronization)");
cf9451dc 356 printf(" [-w] Wait for dequeuer to empty queue");
5e70d572
MD
357 printf("\n");
358}
359
360int main(int argc, char **argv)
361{
362 int err;
363 pthread_t *tid_enqueuer, *tid_dequeuer;
364 void *tret;
365 unsigned long long *count_enqueuer, *count_dequeuer;
366 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
367 unsigned long long tot_successful_enqueues = 0,
cf9451dc
MD
368 tot_successful_dequeues = 0,
369 tot_empty_dest_enqueues = 0,
370 tot_splice = 0;
5e70d572 371 unsigned long long end_dequeues = 0;
cf9451dc 372 int i, a, retval = 0;
5e70d572
MD
373
374 if (argc < 4) {
375 show_usage(argc, argv);
376 return -1;
377 }
378
379 err = sscanf(argv[1], "%u", &nr_dequeuers);
380 if (err != 1) {
381 show_usage(argc, argv);
382 return -1;
383 }
384
385 err = sscanf(argv[2], "%u", &nr_enqueuers);
386 if (err != 1) {
387 show_usage(argc, argv);
388 return -1;
389 }
390
391 err = sscanf(argv[3], "%lu", &duration);
392 if (err != 1) {
393 show_usage(argc, argv);
394 return -1;
395 }
396
397 for (i = 4; i < argc; i++) {
398 if (argv[i][0] != '-')
399 continue;
400 switch (argv[i][1]) {
401 case 'a':
402 if (argc < i + 2) {
403 show_usage(argc, argv);
404 return -1;
405 }
406 a = atoi(argv[++i]);
407 cpu_affinities[next_aff++] = a;
408 use_affinity = 1;
409 printf_verbose("Adding CPU %d affinity\n", a);
410 break;
411 case 'c':
412 if (argc < i + 2) {
413 show_usage(argc, argv);
414 return -1;
415 }
416 rduration = atol(argv[++i]);
417 break;
418 case 'd':
419 if (argc < i + 2) {
420 show_usage(argc, argv);
421 return -1;
422 }
423 wdelay = atol(argv[++i]);
424 break;
425 case 'v':
426 verbose_mode = 1;
427 break;
f5d36fed
MD
428 case 'q':
429 test_dequeue = 1;
430 break;
431 case 's':
432 test_splice = 1;
433 break;
434 case 'M':
435 test_sync = TEST_SYNC_MUTEX;
436 break;
cf9451dc
MD
437 case 'w':
438 test_wait_empty = 1;
439 break;
372b9ad7
MD
440 case 'f':
441 test_force_sync = 1;
442 break;
5e70d572
MD
443 }
444 }
445
f5d36fed
MD
446 /* activate splice test by default */
447 if (!test_dequeue && !test_splice)
448 test_splice = 1;
449
372b9ad7
MD
450 if (test_sync == TEST_SYNC_NONE && nr_dequeuers > 1 && test_dequeue) {
451 if (test_force_sync) {
452 fprintf(stderr, "[WARNING] Using dequeue concurrently "
453 "with other dequeue or splice without external "
454 "synchronization. Expect run-time failure.\n");
455 } else {
456 printf("Enforcing mutex synchronization\n");
457 test_sync = TEST_SYNC_MUTEX;
458 }
459 }
460
5e70d572
MD
461 printf_verbose("running test for %lu seconds, %u enqueuers, "
462 "%u dequeuers.\n",
463 duration, nr_enqueuers, nr_dequeuers);
f5d36fed
MD
464 if (test_dequeue)
465 printf_verbose("dequeue test activated.\n");
466 else
467 printf_verbose("splice test activated.\n");
468 if (test_sync == TEST_SYNC_MUTEX)
469 printf_verbose("External sync: mutex.\n");
470 else
471 printf_verbose("External sync: none.\n");
cf9451dc
MD
472 if (test_wait_empty)
473 printf_verbose("Wait for dequeuers to empty queue.\n");
5e70d572
MD
474 printf_verbose("Writer delay : %lu loops.\n", rduration);
475 printf_verbose("Reader duration : %lu loops.\n", wdelay);
476 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
6ff854ac
MD
477 "main", (unsigned long) pthread_self(),
478 (unsigned long) gettid());
5e70d572
MD
479
480 tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers);
481 tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers);
cf9451dc
MD
482 count_enqueuer = malloc(3 * sizeof(*count_enqueuer) * nr_enqueuers);
483 count_dequeuer = malloc(3 * sizeof(*count_dequeuer) * nr_dequeuers);
5e70d572
MD
484 cds_wfcq_init(&head, &tail);
485
486 next_aff = 0;
487
488 for (i = 0; i < nr_enqueuers; i++) {
489 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
cf9451dc 490 &count_enqueuer[3 * i]);
5e70d572
MD
491 if (err != 0)
492 exit(1);
493 }
494 for (i = 0; i < nr_dequeuers; i++) {
495 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
cf9451dc 496 &count_dequeuer[3 * i]);
5e70d572
MD
497 if (err != 0)
498 exit(1);
499 }
500
501 cmm_smp_mb();
502
503 test_go = 1;
504
505 for (i = 0; i < duration; i++) {
506 sleep(1);
507 if (verbose_mode)
508 write (1, ".", 1);
509 }
510
cf9451dc
MD
511 test_stop_enqueue = 1;
512
513 if (test_wait_empty) {
514 while (nr_enqueuers != uatomic_read(&test_enqueue_stopped)) {
515 sleep(1);
516 }
517 while (!cds_wfcq_empty(&head, &tail)) {
518 sleep(1);
519 }
520 }
521
522 test_stop_dequeue = 1;
5e70d572
MD
523
524 for (i = 0; i < nr_enqueuers; i++) {
525 err = pthread_join(tid_enqueuer[i], &tret);
526 if (err != 0)
527 exit(1);
cf9451dc
MD
528 tot_enqueues += count_enqueuer[3 * i];
529 tot_successful_enqueues += count_enqueuer[3 * i + 1];
530 tot_empty_dest_enqueues += count_enqueuer[3 * i + 2];
5e70d572
MD
531 }
532 for (i = 0; i < nr_dequeuers; i++) {
533 err = pthread_join(tid_dequeuer[i], &tret);
534 if (err != 0)
535 exit(1);
cf9451dc
MD
536 tot_dequeues += count_dequeuer[3 * i];
537 tot_successful_dequeues += count_dequeuer[3 * i + 1];
538 tot_splice += count_dequeuer[3 * i + 2];
5e70d572
MD
539 }
540
541 test_end(&end_dequeues);
542
543 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
544 tot_enqueues, tot_dequeues);
545 printf_verbose("total number of successful enqueues : %llu, "
cf9451dc
MD
546 "enqueues to empty dest : %llu, "
547 "successful dequeues %llu, ",
548 "splice : %llu\n",
549 tot_successful_enqueues,
550 tot_empty_dest_enqueues,
551 tot_successful_dequeues,
552 tot_splice);
5e70d572
MD
553 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
554 "nr_dequeuers %3u "
555 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
cf9451dc
MD
556 "successful enqueues %12llu enqueues to empty dest %12llu "
557 "successful dequeues %12llu splice %12llu "
5e70d572
MD
558 "end_dequeues %llu nr_ops %12llu\n",
559 argv[0], duration, nr_enqueuers, wdelay,
560 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
561 tot_successful_enqueues,
cf9451dc
MD
562 tot_empty_dest_enqueues,
563 tot_successful_dequeues, tot_splice, end_dequeues,
5e70d572 564 tot_enqueues + tot_dequeues);
cf9451dc
MD
565
566 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) {
5e70d572
MD
567 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
568 "succ. dequeues + end dequeues %llu.\n",
569 tot_successful_enqueues,
570 tot_successful_dequeues + end_dequeues);
cf9451dc
MD
571 retval = 1;
572 }
5e70d572 573
cf9451dc
MD
574 /*
575 * If only using splice to dequeue, the enqueuer should see
576 * exactly as many empty queues than the number of non-empty
577 * src splice.
578 */
579 if (test_wait_empty && test_splice && !test_dequeue
580 && tot_empty_dest_enqueues != tot_splice) {
581 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
582 "number of non-empty splice (%llu)\n",
583 tot_empty_dest_enqueues,
584 tot_splice);
585 retval = 1;
586 }
5e70d572
MD
587 free(count_enqueuer);
588 free(count_dequeuer);
589 free(tid_enqueuer);
590 free(tid_dequeuer);
cf9451dc 591 return retval;
5e70d572 592}
This page took 0.079909 seconds and 4 git commands to generate.