4 * Userspace RCU library - example RCU-based lock-free concurrent queue
6 * Copyright February 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright February 2010 - Paolo Bonzini <pbonzini@redhat.com>
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.
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.
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.
25 #include "../config.h"
32 #include <sys/types.h>
40 #include <urcu/arch.h>
41 #include <urcu/tls-compat.h>
47 /* hardcoded number of CPUs */
50 #if defined(_syscall0)
51 _syscall0(pid_t
, gettid
)
52 #elif defined(__NR_gettid)
53 static inline pid_t
gettid(void)
55 return syscall(__NR_gettid
);
58 #warning "use pid as tid"
59 static inline pid_t
gettid(void)
65 #ifndef DYNAMIC_LINK_TEST
69 #include <urcu/wfcqueue.h>
76 static enum test_sync test_sync
;
78 static int test_force_sync
;
80 static volatile int test_go
, test_stop_enqueue
, test_stop_dequeue
;
82 static unsigned long rduration
;
84 static unsigned long duration
;
86 /* read-side C.S. duration, in loops */
87 static unsigned long wdelay
;
89 static inline void loop_sleep(unsigned long loops
)
95 static int verbose_mode
;
97 static int test_dequeue
, test_splice
, test_wait_empty
;
98 static int test_enqueue_stopped
;
100 #define printf_verbose(fmt, args...) \
103 printf(fmt, ## args); \
106 static unsigned int cpu_affinities
[NR_CPUS
];
107 static unsigned int next_aff
= 0;
108 static int use_affinity
= 0;
110 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
112 #ifndef HAVE_CPU_SET_T
113 typedef 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)
118 static void set_affinity(void)
120 #if HAVE_SCHED_SETAFFINITY
123 #endif /* HAVE_SCHED_SETAFFINITY */
128 #if HAVE_SCHED_SETAFFINITY
129 ret
= pthread_mutex_lock(&affinity_mutex
);
131 perror("Error in pthread mutex lock");
134 cpu
= cpu_affinities
[next_aff
++];
135 ret
= pthread_mutex_unlock(&affinity_mutex
);
137 perror("Error in pthread mutex unlock");
143 #if SCHED_SETAFFINITY_ARGS == 2
144 sched_setaffinity(0, &mask
);
146 sched_setaffinity(0, sizeof(mask
), &mask
);
148 #endif /* HAVE_SCHED_SETAFFINITY */
152 * returns 0 if test should end.
154 static int test_duration_dequeue(void)
156 return !test_stop_dequeue
;
159 static int test_duration_enqueue(void)
161 return !test_stop_enqueue
;
164 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
165 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
167 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
168 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
169 static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues
);
170 static DEFINE_URCU_TLS(unsigned long long, nr_splice
);
172 static unsigned int nr_enqueuers
;
173 static unsigned int nr_dequeuers
;
175 static struct cds_wfcq_head
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) head
;
176 static struct cds_wfcq_tail
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) tail
;
178 static void *thr_enqueuer(void *_count
)
180 unsigned long long *count
= _count
;
183 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
184 "enqueuer", (unsigned long) pthread_self(),
185 (unsigned long) gettid());
195 struct cds_wfcq_node
*node
= malloc(sizeof(*node
));
198 cds_wfcq_node_init(node
);
199 was_nonempty
= cds_wfcq_enqueue(&head
, &tail
, node
);
200 URCU_TLS(nr_successful_enqueues
)++;
202 URCU_TLS(nr_empty_dest_enqueues
)++;
204 if (caa_unlikely(wdelay
))
207 URCU_TLS(nr_enqueues
)++;
208 if (caa_unlikely(!test_duration_enqueue()))
212 uatomic_inc(&test_enqueue_stopped
);
213 count
[0] = URCU_TLS(nr_enqueues
);
214 count
[1] = URCU_TLS(nr_successful_enqueues
);
215 count
[2] = URCU_TLS(nr_empty_dest_enqueues
);
216 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
217 "enqueues %llu successful_enqueues %llu, "
218 "empty_dest_enqueues %llu\n",
220 (unsigned long) gettid(),
221 URCU_TLS(nr_enqueues
),
222 URCU_TLS(nr_successful_enqueues
),
223 URCU_TLS(nr_empty_dest_enqueues
));
228 static void do_test_dequeue(enum test_sync sync
)
230 struct cds_wfcq_node
*node
;
232 if (sync
== TEST_SYNC_MUTEX
)
233 node
= cds_wfcq_dequeue_blocking(&head
, &tail
);
235 node
= __cds_wfcq_dequeue_blocking(&head
, &tail
);
239 URCU_TLS(nr_successful_dequeues
)++;
241 URCU_TLS(nr_dequeues
)++;
244 static void do_test_splice(enum test_sync sync
)
246 struct cds_wfcq_head tmp_head
;
247 struct cds_wfcq_tail tmp_tail
;
248 struct cds_wfcq_node
*node
, *n
;
249 enum cds_wfcq_ret ret
;
251 cds_wfcq_init(&tmp_head
, &tmp_tail
);
253 if (sync
== TEST_SYNC_MUTEX
)
254 ret
= cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
257 ret
= __cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
261 case CDS_WFCQ_RET_WOULDBLOCK
:
262 assert(0); /* blocking call */
264 case CDS_WFCQ_RET_DEST_EMPTY
:
265 URCU_TLS(nr_splice
)++;
268 case CDS_WFCQ_RET_DEST_NON_EMPTY
:
269 assert(0); /* entirely unexpected */
271 case CDS_WFCQ_RET_SRC_EMPTY
:
272 /* ok, we could even skip iteration on dest if we wanted */
276 __cds_wfcq_for_each_blocking_safe(&tmp_head
, &tmp_tail
, node
, n
) {
278 URCU_TLS(nr_successful_dequeues
)++;
279 URCU_TLS(nr_dequeues
)++;
283 static void *thr_dequeuer(void *_count
)
285 unsigned long long *count
= _count
;
286 unsigned int counter
;
288 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
289 "dequeuer", (unsigned long) pthread_self(),
290 (unsigned long) gettid());
300 if (test_dequeue
&& test_splice
) {
302 do_test_dequeue(test_sync
);
304 do_test_splice(test_sync
);
308 do_test_dequeue(test_sync
);
310 do_test_splice(test_sync
);
312 if (caa_unlikely(!test_duration_dequeue()))
314 if (caa_unlikely(rduration
))
315 loop_sleep(rduration
);
318 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
319 "dequeues %llu, successful_dequeues %llu, "
322 (unsigned long) gettid(),
323 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
),
324 URCU_TLS(nr_splice
));
325 count
[0] = URCU_TLS(nr_dequeues
);
326 count
[1] = URCU_TLS(nr_successful_dequeues
);
327 count
[2] = URCU_TLS(nr_splice
);
331 static void test_end(unsigned long long *nr_dequeues
)
333 struct cds_wfcq_node
*node
;
336 node
= cds_wfcq_dequeue_blocking(&head
, &tail
);
344 static void show_usage(int argc
, char **argv
)
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)");
351 printf(" [-q] (test dequeue)");
352 printf(" [-s] (test splice, enabled by default)");
353 printf(" [-M] (use mutex external synchronization)");
354 printf(" Note: default: no external synchronization used.");
355 printf(" [-f] (force user-provided synchronization)");
356 printf(" [-w] Wait for dequeuer to empty queue");
360 int main(int argc
, char **argv
)
363 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
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,
368 tot_successful_dequeues
= 0,
369 tot_empty_dest_enqueues
= 0,
371 unsigned long long end_dequeues
= 0;
372 int i
, a
, retval
= 0;
375 show_usage(argc
, argv
);
379 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
381 show_usage(argc
, argv
);
385 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
387 show_usage(argc
, argv
);
391 err
= sscanf(argv
[3], "%lu", &duration
);
393 show_usage(argc
, argv
);
397 for (i
= 4; i
< argc
; i
++) {
398 if (argv
[i
][0] != '-')
400 switch (argv
[i
][1]) {
403 show_usage(argc
, argv
);
407 cpu_affinities
[next_aff
++] = a
;
409 printf_verbose("Adding CPU %d affinity\n", a
);
413 show_usage(argc
, argv
);
416 rduration
= atol(argv
[++i
]);
420 show_usage(argc
, argv
);
423 wdelay
= atol(argv
[++i
]);
435 test_sync
= TEST_SYNC_MUTEX
;
446 /* activate splice test by default */
447 if (!test_dequeue
&& !test_splice
)
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");
456 printf("Enforcing mutex synchronization\n");
457 test_sync
= TEST_SYNC_MUTEX
;
461 printf_verbose("running test for %lu seconds, %u enqueuers, "
463 duration
, nr_enqueuers
, nr_dequeuers
);
465 printf_verbose("dequeue test activated.\n");
467 printf_verbose("splice test activated.\n");
468 if (test_sync
== TEST_SYNC_MUTEX
)
469 printf_verbose("External sync: mutex.\n");
471 printf_verbose("External sync: none.\n");
473 printf_verbose("Wait for dequeuers to empty queue.\n");
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",
477 "main", (unsigned long) pthread_self(),
478 (unsigned long) gettid());
480 tid_enqueuer
= malloc(sizeof(*tid_enqueuer
) * nr_enqueuers
);
481 tid_dequeuer
= malloc(sizeof(*tid_dequeuer
) * nr_dequeuers
);
482 count_enqueuer
= malloc(3 * sizeof(*count_enqueuer
) * nr_enqueuers
);
483 count_dequeuer
= malloc(3 * sizeof(*count_dequeuer
) * nr_dequeuers
);
484 cds_wfcq_init(&head
, &tail
);
488 for (i
= 0; i
< nr_enqueuers
; i
++) {
489 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
490 &count_enqueuer
[3 * i
]);
494 for (i
= 0; i
< nr_dequeuers
; i
++) {
495 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
496 &count_dequeuer
[3 * i
]);
505 for (i
= 0; i
< duration
; i
++) {
511 test_stop_enqueue
= 1;
513 if (test_wait_empty
) {
514 while (nr_enqueuers
!= uatomic_read(&test_enqueue_stopped
)) {
517 while (!cds_wfcq_empty(&head
, &tail
)) {
522 test_stop_dequeue
= 1;
524 for (i
= 0; i
< nr_enqueuers
; i
++) {
525 err
= pthread_join(tid_enqueuer
[i
], &tret
);
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];
532 for (i
= 0; i
< nr_dequeuers
; i
++) {
533 err
= pthread_join(tid_dequeuer
[i
], &tret
);
536 tot_dequeues
+= count_dequeuer
[3 * i
];
537 tot_successful_dequeues
+= count_dequeuer
[3 * i
+ 1];
538 tot_splice
+= count_dequeuer
[3 * i
+ 2];
541 test_end(&end_dequeues
);
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, "
546 "enqueues to empty dest : %llu, "
547 "successful dequeues %llu, "
549 tot_successful_enqueues
,
550 tot_empty_dest_enqueues
,
551 tot_successful_dequeues
,
553 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
555 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
556 "successful enqueues %12llu enqueues to empty dest %12llu "
557 "successful dequeues %12llu splice %12llu "
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
,
562 tot_empty_dest_enqueues
,
563 tot_successful_dequeues
, tot_splice
, end_dequeues
,
564 tot_enqueues
+ tot_dequeues
);
566 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
) {
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
);
575 * If only using splice to dequeue, the enqueuer should see
576 * exactly as many empty queues than the number of non-empty
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
,
587 free(count_enqueuer
);
588 free(count_dequeuer
);