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.
30 #include <sys/types.h>
36 #include <urcu/arch.h>
37 #include <urcu/assert.h>
38 #include <urcu/tls-compat.h>
39 #include <urcu/uatomic.h>
40 #include "thread-id.h"
42 /* hardcoded number of CPUs */
45 #ifndef DYNAMIC_LINK_TEST
48 #include <urcu/wfcqueue.h>
55 static enum test_sync test_sync
;
57 static int test_force_sync
;
59 static volatile int test_go
, test_stop_enqueue
, test_stop_dequeue
;
61 static unsigned long rduration
;
63 static unsigned long duration
;
65 /* read-side C.S. duration, in loops */
66 static unsigned long wdelay
;
68 static inline void loop_sleep(unsigned long loops
)
74 static int verbose_mode
;
76 static int test_dequeue
, test_splice
, test_wait_empty
;
77 static unsigned int test_enqueue_stopped
;
79 #define printf_verbose(fmt, args...) \
82 printf(fmt, ## args); \
85 static unsigned int cpu_affinities
[NR_CPUS
];
86 static unsigned int next_aff
= 0;
87 static int use_affinity
= 0;
89 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
91 static void set_affinity(void)
93 #ifdef HAVE_SCHED_SETAFFINITY
96 #endif /* HAVE_SCHED_SETAFFINITY */
101 #ifdef HAVE_SCHED_SETAFFINITY
102 ret
= pthread_mutex_lock(&affinity_mutex
);
104 perror("Error in pthread mutex lock");
107 cpu
= cpu_affinities
[next_aff
++];
108 ret
= pthread_mutex_unlock(&affinity_mutex
);
110 perror("Error in pthread mutex unlock");
116 sched_setaffinity(0, sizeof(mask
), &mask
);
117 #endif /* HAVE_SCHED_SETAFFINITY */
121 * returns 0 if test should end.
123 static int test_duration_dequeue(void)
125 return !test_stop_dequeue
;
128 static int test_duration_enqueue(void)
130 return !test_stop_enqueue
;
133 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
134 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
136 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
137 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
138 static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues
);
139 static DEFINE_URCU_TLS(unsigned long long, nr_splice
);
140 static DEFINE_URCU_TLS(unsigned long long, nr_dequeue_last
);
142 static unsigned int nr_enqueuers
;
143 static unsigned int nr_dequeuers
;
145 static struct cds_wfcq_head
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) head
;
146 static struct cds_wfcq_tail
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) tail
;
148 static void *thr_enqueuer(void *_count
)
150 unsigned long long *count
= _count
;
153 printf_verbose("thread_begin %s, tid %lu\n",
154 "enqueuer", urcu_get_thread_id());
164 struct cds_wfcq_node
*node
= malloc(sizeof(*node
));
167 cds_wfcq_node_init(node
);
168 was_nonempty
= cds_wfcq_enqueue(&head
, &tail
, node
);
169 URCU_TLS(nr_successful_enqueues
)++;
171 URCU_TLS(nr_empty_dest_enqueues
)++;
173 if (caa_unlikely(wdelay
))
176 URCU_TLS(nr_enqueues
)++;
177 if (caa_unlikely(!test_duration_enqueue()))
181 uatomic_inc(&test_enqueue_stopped
);
182 count
[0] = URCU_TLS(nr_enqueues
);
183 count
[1] = URCU_TLS(nr_successful_enqueues
);
184 count
[2] = URCU_TLS(nr_empty_dest_enqueues
);
185 printf_verbose("enqueuer thread_end, tid %lu, "
186 "enqueues %llu successful_enqueues %llu, "
187 "empty_dest_enqueues %llu\n",
188 urcu_get_thread_id(),
189 URCU_TLS(nr_enqueues
),
190 URCU_TLS(nr_successful_enqueues
),
191 URCU_TLS(nr_empty_dest_enqueues
));
196 static void do_test_dequeue(enum test_sync sync
)
198 struct cds_wfcq_node
*node
;
201 if (sync
== TEST_SYNC_MUTEX
)
202 node
= cds_wfcq_dequeue_with_state_blocking(&head
, &tail
,
205 node
= __cds_wfcq_dequeue_with_state_blocking(&head
, &tail
,
208 if (state
& CDS_WFCQ_STATE_LAST
)
209 URCU_TLS(nr_dequeue_last
)++;
213 URCU_TLS(nr_successful_dequeues
)++;
215 URCU_TLS(nr_dequeues
)++;
218 static void do_test_splice(enum test_sync sync
)
220 struct cds_wfcq_head tmp_head
;
221 struct cds_wfcq_tail tmp_tail
;
222 struct cds_wfcq_node
*node
, *n
;
223 enum cds_wfcq_ret ret
;
225 cds_wfcq_init(&tmp_head
, &tmp_tail
);
227 if (sync
== TEST_SYNC_MUTEX
)
228 ret
= cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
231 ret
= __cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
235 case CDS_WFCQ_RET_WOULDBLOCK
:
236 urcu_posix_assert(0); /* blocking call */
238 case CDS_WFCQ_RET_DEST_EMPTY
:
239 URCU_TLS(nr_splice
)++;
240 URCU_TLS(nr_dequeue_last
)++;
243 case CDS_WFCQ_RET_DEST_NON_EMPTY
:
244 urcu_posix_assert(0); /* entirely unexpected */
246 case CDS_WFCQ_RET_SRC_EMPTY
:
247 /* ok, we could even skip iteration on dest if we wanted */
251 __cds_wfcq_for_each_blocking_safe(&tmp_head
, &tmp_tail
, node
, n
) {
253 URCU_TLS(nr_successful_dequeues
)++;
254 URCU_TLS(nr_dequeues
)++;
256 cds_wfcq_destroy(&tmp_head
, &tmp_tail
);
259 static void *thr_dequeuer(void *_count
)
261 unsigned long long *count
= _count
;
262 unsigned int counter
= 0;
264 printf_verbose("thread_begin %s, tid %lu\n",
265 "dequeuer", urcu_get_thread_id());
275 if (test_dequeue
&& test_splice
) {
277 do_test_dequeue(test_sync
);
279 do_test_splice(test_sync
);
283 do_test_dequeue(test_sync
);
285 do_test_splice(test_sync
);
287 if (caa_unlikely(!test_duration_dequeue()))
289 if (caa_unlikely(rduration
))
290 loop_sleep(rduration
);
293 printf_verbose("dequeuer thread_end, tid %lu, "
294 "dequeues %llu, successful_dequeues %llu, "
296 urcu_get_thread_id(),
297 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
),
298 URCU_TLS(nr_splice
));
299 count
[0] = URCU_TLS(nr_dequeues
);
300 count
[1] = URCU_TLS(nr_successful_dequeues
);
301 count
[2] = URCU_TLS(nr_splice
);
302 count
[3] = URCU_TLS(nr_dequeue_last
);
306 static void test_end(unsigned long long *nr_dequeues_l
,
307 unsigned long long *nr_dequeue_last_l
)
309 struct cds_wfcq_node
*node
;
313 node
= cds_wfcq_dequeue_with_state_blocking(&head
, &tail
,
316 if (state
& CDS_WFCQ_STATE_LAST
)
317 (*nr_dequeue_last_l
)++;
324 static void show_usage(char **argv
)
326 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
328 printf("OPTIONS:\n");
329 printf(" [-d delay] (enqueuer period (in loops))\n");
330 printf(" [-c duration] (dequeuer period (in loops))\n");
331 printf(" [-v] (verbose output)\n");
332 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
333 printf(" [-q] (test dequeue)\n");
334 printf(" [-s] (test splice, enabled by default)\n");
335 printf(" [-M] (use mutex external synchronization)\n");
336 printf(" Note: default: no external synchronization used.\n");
337 printf(" [-f] (force user-provided synchronization)\n");
338 printf(" [-w] Wait for dequeuer to empty queue\n");
342 int main(int argc
, char **argv
)
345 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
347 unsigned long long *count_enqueuer
, *count_dequeuer
;
348 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
349 unsigned long long tot_successful_enqueues
= 0,
350 tot_successful_dequeues
= 0,
351 tot_empty_dest_enqueues
= 0,
352 tot_splice
= 0, tot_dequeue_last
= 0;
353 unsigned long long end_dequeues
= 0;
354 int i
, a
, retval
= 0;
362 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
368 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
374 err
= sscanf(argv
[3], "%lu", &duration
);
380 for (i
= 4; i
< argc
; i
++) {
381 if (argv
[i
][0] != '-')
383 switch (argv
[i
][1]) {
390 cpu_affinities
[next_aff
++] = a
;
392 printf_verbose("Adding CPU %d affinity\n", a
);
399 rduration
= atol(argv
[++i
]);
406 wdelay
= atol(argv
[++i
]);
418 test_sync
= TEST_SYNC_MUTEX
;
429 /* activate splice test by default */
430 if (!test_dequeue
&& !test_splice
)
433 if (test_sync
== TEST_SYNC_NONE
&& nr_dequeuers
> 1 && test_dequeue
) {
434 if (test_force_sync
) {
435 fprintf(stderr
, "[WARNING] Using dequeue concurrently "
436 "with other dequeue or splice without external "
437 "synchronization. Expect run-time failure.\n");
439 printf("Enforcing mutex synchronization\n");
440 test_sync
= TEST_SYNC_MUTEX
;
444 printf_verbose("running test for %lu seconds, %u enqueuers, "
446 duration
, nr_enqueuers
, nr_dequeuers
);
448 printf_verbose("dequeue test activated.\n");
450 printf_verbose("splice test activated.\n");
451 if (test_sync
== TEST_SYNC_MUTEX
)
452 printf_verbose("External sync: mutex.\n");
454 printf_verbose("External sync: none.\n");
456 printf_verbose("Wait for dequeuers to empty queue.\n");
457 printf_verbose("Writer delay : %lu loops.\n", rduration
);
458 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
459 printf_verbose("thread %-6s, tid %lu\n",
460 "main", urcu_get_thread_id());
462 tid_enqueuer
= calloc(nr_enqueuers
, sizeof(*tid_enqueuer
));
463 tid_dequeuer
= calloc(nr_dequeuers
, sizeof(*tid_dequeuer
));
464 count_enqueuer
= calloc(nr_enqueuers
, 3 * sizeof(*count_enqueuer
));
465 count_dequeuer
= calloc(nr_dequeuers
, 4 * sizeof(*count_dequeuer
));
466 cds_wfcq_init(&head
, &tail
);
470 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
471 err
= pthread_create(&tid_enqueuer
[i_thr
], NULL
, thr_enqueuer
,
472 &count_enqueuer
[3 * i_thr
]);
476 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
477 err
= pthread_create(&tid_dequeuer
[i_thr
], NULL
, thr_dequeuer
,
478 &count_dequeuer
[4 * i_thr
]);
487 for (i_thr
= 0; i_thr
< duration
; i_thr
++) {
490 fwrite(".", sizeof(char), 1, stdout
);
495 test_stop_enqueue
= 1;
497 if (test_wait_empty
) {
498 while (nr_enqueuers
!= uatomic_read(&test_enqueue_stopped
)) {
501 while (!cds_wfcq_empty(&head
, &tail
)) {
506 test_stop_dequeue
= 1;
508 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
509 err
= pthread_join(tid_enqueuer
[i_thr
], &tret
);
512 tot_enqueues
+= count_enqueuer
[3 * i_thr
];
513 tot_successful_enqueues
+= count_enqueuer
[3 * i_thr
+ 1];
514 tot_empty_dest_enqueues
+= count_enqueuer
[3 * i_thr
+ 2];
516 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
517 err
= pthread_join(tid_dequeuer
[i_thr
], &tret
);
520 tot_dequeues
+= count_dequeuer
[4 * i_thr
];
521 tot_successful_dequeues
+= count_dequeuer
[4 * i_thr
+ 1];
522 tot_splice
+= count_dequeuer
[4 * i_thr
+ 2];
523 tot_dequeue_last
+= count_dequeuer
[4 * i_thr
+ 3];
526 test_end(&end_dequeues
, &tot_dequeue_last
);
528 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
529 tot_enqueues
, tot_dequeues
);
530 printf_verbose("total number of successful enqueues : %llu, "
531 "enqueues to empty dest : %llu, "
532 "successful dequeues %llu, "
533 "splice : %llu, dequeue_last : %llu\n",
534 tot_successful_enqueues
,
535 tot_empty_dest_enqueues
,
536 tot_successful_dequeues
,
537 tot_splice
, tot_dequeue_last
);
538 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
540 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
541 "successful enqueues %12llu enqueues to empty dest %12llu "
542 "successful dequeues %12llu splice %12llu "
544 "end_dequeues %llu nr_ops %12llu\n",
545 argv
[0], duration
, nr_enqueuers
, wdelay
,
546 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
547 tot_successful_enqueues
,
548 tot_empty_dest_enqueues
,
549 tot_successful_dequeues
, tot_splice
, tot_dequeue_last
,
551 tot_enqueues
+ tot_dequeues
);
553 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
) {
554 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
555 "succ. dequeues + end dequeues %llu.\n",
556 tot_successful_enqueues
,
557 tot_successful_dequeues
+ end_dequeues
);
562 * If only using splice to dequeue, the enqueuer should see
563 * exactly as many empty queues than the number of non-empty
566 if (tot_empty_dest_enqueues
!= tot_dequeue_last
) {
567 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
568 "number of dequeue of last element (%llu)\n",
569 tot_empty_dest_enqueues
,
573 cds_wfcq_destroy(&head
, &tail
);
574 free(count_enqueuer
);
575 free(count_dequeuer
);