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>
39 #include <urcu/arch.h>
40 #include <urcu/tls-compat.h>
41 #include <urcu/uatomic.h>
48 /* hardcoded number of CPUs */
51 #if defined(_syscall0)
52 _syscall0(pid_t
, gettid
)
53 #elif defined(__NR_gettid)
54 static inline pid_t
gettid(void)
56 return syscall(__NR_gettid
);
59 #warning "use pid as tid"
60 static inline pid_t
gettid(void)
66 #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 static void set_affinity(void)
114 #if HAVE_SCHED_SETAFFINITY
117 #endif /* HAVE_SCHED_SETAFFINITY */
122 #if HAVE_SCHED_SETAFFINITY
123 ret
= pthread_mutex_lock(&affinity_mutex
);
125 perror("Error in pthread mutex lock");
128 cpu
= cpu_affinities
[next_aff
++];
129 ret
= pthread_mutex_unlock(&affinity_mutex
);
131 perror("Error in pthread mutex unlock");
137 #if SCHED_SETAFFINITY_ARGS == 2
138 sched_setaffinity(0, &mask
);
140 sched_setaffinity(0, sizeof(mask
), &mask
);
142 #endif /* HAVE_SCHED_SETAFFINITY */
146 * returns 0 if test should end.
148 static int test_duration_dequeue(void)
150 return !test_stop_dequeue
;
153 static int test_duration_enqueue(void)
155 return !test_stop_enqueue
;
158 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
159 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
161 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
162 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
163 static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues
);
164 static DEFINE_URCU_TLS(unsigned long long, nr_splice
);
165 static DEFINE_URCU_TLS(unsigned long long, nr_dequeue_last
);
167 static unsigned int nr_enqueuers
;
168 static unsigned int nr_dequeuers
;
170 static struct cds_wfcq_head
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) head
;
171 static struct cds_wfcq_tail
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) tail
;
173 static void *thr_enqueuer(void *_count
)
175 unsigned long long *count
= _count
;
178 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
179 "enqueuer", (unsigned long) pthread_self(),
180 (unsigned long) gettid());
190 struct cds_wfcq_node
*node
= malloc(sizeof(*node
));
193 cds_wfcq_node_init(node
);
194 was_nonempty
= cds_wfcq_enqueue(&head
, &tail
, node
);
195 URCU_TLS(nr_successful_enqueues
)++;
197 URCU_TLS(nr_empty_dest_enqueues
)++;
199 if (caa_unlikely(wdelay
))
202 URCU_TLS(nr_enqueues
)++;
203 if (caa_unlikely(!test_duration_enqueue()))
207 uatomic_inc(&test_enqueue_stopped
);
208 count
[0] = URCU_TLS(nr_enqueues
);
209 count
[1] = URCU_TLS(nr_successful_enqueues
);
210 count
[2] = URCU_TLS(nr_empty_dest_enqueues
);
211 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
212 "enqueues %llu successful_enqueues %llu, "
213 "empty_dest_enqueues %llu\n",
215 (unsigned long) gettid(),
216 URCU_TLS(nr_enqueues
),
217 URCU_TLS(nr_successful_enqueues
),
218 URCU_TLS(nr_empty_dest_enqueues
));
223 static void do_test_dequeue(enum test_sync sync
)
225 struct cds_wfcq_node
*node
;
228 if (sync
== TEST_SYNC_MUTEX
)
229 node
= cds_wfcq_dequeue_with_state_blocking(&head
, &tail
,
232 node
= __cds_wfcq_dequeue_with_state_blocking(&head
, &tail
,
235 if (state
& CDS_WFCQ_STATE_LAST
)
236 URCU_TLS(nr_dequeue_last
)++;
240 URCU_TLS(nr_successful_dequeues
)++;
242 URCU_TLS(nr_dequeues
)++;
245 static void do_test_splice(enum test_sync sync
)
247 struct cds_wfcq_head tmp_head
;
248 struct cds_wfcq_tail tmp_tail
;
249 struct cds_wfcq_node
*node
, *n
;
250 enum cds_wfcq_ret ret
;
252 cds_wfcq_init(&tmp_head
, &tmp_tail
);
254 if (sync
== TEST_SYNC_MUTEX
)
255 ret
= cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
258 ret
= __cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
262 case CDS_WFCQ_RET_WOULDBLOCK
:
263 assert(0); /* blocking call */
265 case CDS_WFCQ_RET_DEST_EMPTY
:
266 URCU_TLS(nr_splice
)++;
267 URCU_TLS(nr_dequeue_last
)++;
270 case CDS_WFCQ_RET_DEST_NON_EMPTY
:
271 assert(0); /* entirely unexpected */
273 case CDS_WFCQ_RET_SRC_EMPTY
:
274 /* ok, we could even skip iteration on dest if we wanted */
278 __cds_wfcq_for_each_blocking_safe(&tmp_head
, &tmp_tail
, node
, n
) {
280 URCU_TLS(nr_successful_dequeues
)++;
281 URCU_TLS(nr_dequeues
)++;
285 static void *thr_dequeuer(void *_count
)
287 unsigned long long *count
= _count
;
288 unsigned int counter
;
290 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
291 "dequeuer", (unsigned long) pthread_self(),
292 (unsigned long) gettid());
302 if (test_dequeue
&& test_splice
) {
304 do_test_dequeue(test_sync
);
306 do_test_splice(test_sync
);
310 do_test_dequeue(test_sync
);
312 do_test_splice(test_sync
);
314 if (caa_unlikely(!test_duration_dequeue()))
316 if (caa_unlikely(rduration
))
317 loop_sleep(rduration
);
320 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
321 "dequeues %llu, successful_dequeues %llu, "
324 (unsigned long) gettid(),
325 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
),
326 URCU_TLS(nr_splice
));
327 count
[0] = URCU_TLS(nr_dequeues
);
328 count
[1] = URCU_TLS(nr_successful_dequeues
);
329 count
[2] = URCU_TLS(nr_splice
);
330 count
[3] = URCU_TLS(nr_dequeue_last
);
334 static void test_end(unsigned long long *nr_dequeues
,
335 unsigned long long *nr_dequeue_last
)
337 struct cds_wfcq_node
*node
;
341 node
= cds_wfcq_dequeue_with_state_blocking(&head
, &tail
,
344 if (state
& CDS_WFCQ_STATE_LAST
)
345 (*nr_dequeue_last
)++;
352 static void show_usage(int argc
, char **argv
)
354 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
356 printf("OPTIONS:\n");
357 printf(" [-d delay] (enqueuer period (in loops))\n");
358 printf(" [-c duration] (dequeuer period (in loops))\n");
359 printf(" [-v] (verbose output)\n");
360 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
361 printf(" [-q] (test dequeue)\n");
362 printf(" [-s] (test splice, enabled by default)\n");
363 printf(" [-M] (use mutex external synchronization)\n");
364 printf(" Note: default: no external synchronization used.\n");
365 printf(" [-f] (force user-provided synchronization)\n");
366 printf(" [-w] Wait for dequeuer to empty queue\n");
370 int main(int argc
, char **argv
)
373 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
375 unsigned long long *count_enqueuer
, *count_dequeuer
;
376 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
377 unsigned long long tot_successful_enqueues
= 0,
378 tot_successful_dequeues
= 0,
379 tot_empty_dest_enqueues
= 0,
380 tot_splice
= 0, tot_dequeue_last
= 0;
381 unsigned long long end_dequeues
= 0;
382 int i
, a
, retval
= 0;
385 show_usage(argc
, argv
);
389 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
391 show_usage(argc
, argv
);
395 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
397 show_usage(argc
, argv
);
401 err
= sscanf(argv
[3], "%lu", &duration
);
403 show_usage(argc
, argv
);
407 for (i
= 4; i
< argc
; i
++) {
408 if (argv
[i
][0] != '-')
410 switch (argv
[i
][1]) {
413 show_usage(argc
, argv
);
417 cpu_affinities
[next_aff
++] = a
;
419 printf_verbose("Adding CPU %d affinity\n", a
);
423 show_usage(argc
, argv
);
426 rduration
= atol(argv
[++i
]);
430 show_usage(argc
, argv
);
433 wdelay
= atol(argv
[++i
]);
445 test_sync
= TEST_SYNC_MUTEX
;
456 /* activate splice test by default */
457 if (!test_dequeue
&& !test_splice
)
460 if (test_sync
== TEST_SYNC_NONE
&& nr_dequeuers
> 1 && test_dequeue
) {
461 if (test_force_sync
) {
462 fprintf(stderr
, "[WARNING] Using dequeue concurrently "
463 "with other dequeue or splice without external "
464 "synchronization. Expect run-time failure.\n");
466 printf("Enforcing mutex synchronization\n");
467 test_sync
= TEST_SYNC_MUTEX
;
471 printf_verbose("running test for %lu seconds, %u enqueuers, "
473 duration
, nr_enqueuers
, nr_dequeuers
);
475 printf_verbose("dequeue test activated.\n");
477 printf_verbose("splice test activated.\n");
478 if (test_sync
== TEST_SYNC_MUTEX
)
479 printf_verbose("External sync: mutex.\n");
481 printf_verbose("External sync: none.\n");
483 printf_verbose("Wait for dequeuers to empty queue.\n");
484 printf_verbose("Writer delay : %lu loops.\n", rduration
);
485 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
486 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
487 "main", (unsigned long) pthread_self(),
488 (unsigned long) gettid());
490 tid_enqueuer
= malloc(sizeof(*tid_enqueuer
) * nr_enqueuers
);
491 tid_dequeuer
= malloc(sizeof(*tid_dequeuer
) * nr_dequeuers
);
492 count_enqueuer
= malloc(3 * sizeof(*count_enqueuer
) * nr_enqueuers
);
493 count_dequeuer
= malloc(4 * sizeof(*count_dequeuer
) * nr_dequeuers
);
494 cds_wfcq_init(&head
, &tail
);
498 for (i
= 0; i
< nr_enqueuers
; i
++) {
499 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
500 &count_enqueuer
[3 * i
]);
504 for (i
= 0; i
< nr_dequeuers
; i
++) {
505 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
506 &count_dequeuer
[4 * i
]);
515 for (i
= 0; i
< duration
; i
++) {
521 test_stop_enqueue
= 1;
523 if (test_wait_empty
) {
524 while (nr_enqueuers
!= uatomic_read(&test_enqueue_stopped
)) {
527 while (!cds_wfcq_empty(&head
, &tail
)) {
532 test_stop_dequeue
= 1;
534 for (i
= 0; i
< nr_enqueuers
; i
++) {
535 err
= pthread_join(tid_enqueuer
[i
], &tret
);
538 tot_enqueues
+= count_enqueuer
[3 * i
];
539 tot_successful_enqueues
+= count_enqueuer
[3 * i
+ 1];
540 tot_empty_dest_enqueues
+= count_enqueuer
[3 * i
+ 2];
542 for (i
= 0; i
< nr_dequeuers
; i
++) {
543 err
= pthread_join(tid_dequeuer
[i
], &tret
);
546 tot_dequeues
+= count_dequeuer
[4 * i
];
547 tot_successful_dequeues
+= count_dequeuer
[4 * i
+ 1];
548 tot_splice
+= count_dequeuer
[4 * i
+ 2];
549 tot_dequeue_last
+= count_dequeuer
[4 * i
+ 3];
552 test_end(&end_dequeues
, &tot_dequeue_last
);
554 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
555 tot_enqueues
, tot_dequeues
);
556 printf_verbose("total number of successful enqueues : %llu, "
557 "enqueues to empty dest : %llu, "
558 "successful dequeues %llu, "
559 "splice : %llu, dequeue_last : %llu\n",
560 tot_successful_enqueues
,
561 tot_empty_dest_enqueues
,
562 tot_successful_dequeues
,
563 tot_splice
, tot_dequeue_last
);
564 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
566 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
567 "successful enqueues %12llu enqueues to empty dest %12llu "
568 "successful dequeues %12llu splice %12llu "
570 "end_dequeues %llu nr_ops %12llu\n",
571 argv
[0], duration
, nr_enqueuers
, wdelay
,
572 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
573 tot_successful_enqueues
,
574 tot_empty_dest_enqueues
,
575 tot_successful_dequeues
, tot_splice
, tot_dequeue_last
,
577 tot_enqueues
+ tot_dequeues
);
579 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
) {
580 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
581 "succ. dequeues + end dequeues %llu.\n",
582 tot_successful_enqueues
,
583 tot_successful_dequeues
+ end_dequeues
);
588 * If only using splice to dequeue, the enqueuer should see
589 * exactly as many empty queues than the number of non-empty
592 if (tot_empty_dest_enqueues
!= tot_dequeue_last
) {
593 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
594 "number of dequeue of last element (%llu)\n",
595 tot_empty_dest_enqueues
,
599 free(count_enqueuer
);
600 free(count_dequeuer
);