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
);
166 static unsigned int nr_enqueuers
;
167 static unsigned int nr_dequeuers
;
169 static struct cds_wfcq_head
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) head
;
170 static struct cds_wfcq_tail
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) tail
;
172 static void *thr_enqueuer(void *_count
)
174 unsigned long long *count
= _count
;
177 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
178 "enqueuer", (unsigned long) pthread_self(),
179 (unsigned long) gettid());
189 struct cds_wfcq_node
*node
= malloc(sizeof(*node
));
192 cds_wfcq_node_init(node
);
193 was_nonempty
= cds_wfcq_enqueue(&head
, &tail
, node
);
194 URCU_TLS(nr_successful_enqueues
)++;
196 URCU_TLS(nr_empty_dest_enqueues
)++;
198 if (caa_unlikely(wdelay
))
201 URCU_TLS(nr_enqueues
)++;
202 if (caa_unlikely(!test_duration_enqueue()))
206 uatomic_inc(&test_enqueue_stopped
);
207 count
[0] = URCU_TLS(nr_enqueues
);
208 count
[1] = URCU_TLS(nr_successful_enqueues
);
209 count
[2] = URCU_TLS(nr_empty_dest_enqueues
);
210 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
211 "enqueues %llu successful_enqueues %llu, "
212 "empty_dest_enqueues %llu\n",
214 (unsigned long) gettid(),
215 URCU_TLS(nr_enqueues
),
216 URCU_TLS(nr_successful_enqueues
),
217 URCU_TLS(nr_empty_dest_enqueues
));
222 static void do_test_dequeue(enum test_sync sync
)
224 struct cds_wfcq_node
*node
;
226 if (sync
== TEST_SYNC_MUTEX
)
227 node
= cds_wfcq_dequeue_blocking(&head
, &tail
);
229 node
= __cds_wfcq_dequeue_blocking(&head
, &tail
);
233 URCU_TLS(nr_successful_dequeues
)++;
235 URCU_TLS(nr_dequeues
)++;
238 static void do_test_splice(enum test_sync sync
)
240 struct cds_wfcq_head tmp_head
;
241 struct cds_wfcq_tail tmp_tail
;
242 struct cds_wfcq_node
*node
, *n
;
243 enum cds_wfcq_ret ret
;
245 cds_wfcq_init(&tmp_head
, &tmp_tail
);
247 if (sync
== TEST_SYNC_MUTEX
)
248 ret
= cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
251 ret
= __cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
255 case CDS_WFCQ_RET_WOULDBLOCK
:
256 assert(0); /* blocking call */
258 case CDS_WFCQ_RET_DEST_EMPTY
:
259 URCU_TLS(nr_splice
)++;
262 case CDS_WFCQ_RET_DEST_NON_EMPTY
:
263 assert(0); /* entirely unexpected */
265 case CDS_WFCQ_RET_SRC_EMPTY
:
266 /* ok, we could even skip iteration on dest if we wanted */
270 __cds_wfcq_for_each_blocking_safe(&tmp_head
, &tmp_tail
, node
, n
) {
272 URCU_TLS(nr_successful_dequeues
)++;
273 URCU_TLS(nr_dequeues
)++;
277 static void *thr_dequeuer(void *_count
)
279 unsigned long long *count
= _count
;
280 unsigned int counter
;
282 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
283 "dequeuer", (unsigned long) pthread_self(),
284 (unsigned long) gettid());
294 if (test_dequeue
&& test_splice
) {
296 do_test_dequeue(test_sync
);
298 do_test_splice(test_sync
);
302 do_test_dequeue(test_sync
);
304 do_test_splice(test_sync
);
306 if (caa_unlikely(!test_duration_dequeue()))
308 if (caa_unlikely(rduration
))
309 loop_sleep(rduration
);
312 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
313 "dequeues %llu, successful_dequeues %llu, "
316 (unsigned long) gettid(),
317 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
),
318 URCU_TLS(nr_splice
));
319 count
[0] = URCU_TLS(nr_dequeues
);
320 count
[1] = URCU_TLS(nr_successful_dequeues
);
321 count
[2] = URCU_TLS(nr_splice
);
325 static void test_end(unsigned long long *nr_dequeues
)
327 struct cds_wfcq_node
*node
;
330 node
= cds_wfcq_dequeue_blocking(&head
, &tail
);
338 static void show_usage(int argc
, char **argv
)
340 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv
[0]);
341 printf(" [-d delay] (enqueuer period (in loops))");
342 printf(" [-c duration] (dequeuer period (in loops))");
343 printf(" [-v] (verbose output)");
344 printf(" [-a cpu#] [-a cpu#]... (affinity)");
345 printf(" [-q] (test dequeue)");
346 printf(" [-s] (test splice, enabled by default)");
347 printf(" [-M] (use mutex external synchronization)");
348 printf(" Note: default: no external synchronization used.");
349 printf(" [-f] (force user-provided synchronization)");
350 printf(" [-w] Wait for dequeuer to empty queue");
354 int main(int argc
, char **argv
)
357 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
359 unsigned long long *count_enqueuer
, *count_dequeuer
;
360 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
361 unsigned long long tot_successful_enqueues
= 0,
362 tot_successful_dequeues
= 0,
363 tot_empty_dest_enqueues
= 0,
365 unsigned long long end_dequeues
= 0;
366 int i
, a
, retval
= 0;
369 show_usage(argc
, argv
);
373 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
375 show_usage(argc
, argv
);
379 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
381 show_usage(argc
, argv
);
385 err
= sscanf(argv
[3], "%lu", &duration
);
387 show_usage(argc
, argv
);
391 for (i
= 4; i
< argc
; i
++) {
392 if (argv
[i
][0] != '-')
394 switch (argv
[i
][1]) {
397 show_usage(argc
, argv
);
401 cpu_affinities
[next_aff
++] = a
;
403 printf_verbose("Adding CPU %d affinity\n", a
);
407 show_usage(argc
, argv
);
410 rduration
= atol(argv
[++i
]);
414 show_usage(argc
, argv
);
417 wdelay
= atol(argv
[++i
]);
429 test_sync
= TEST_SYNC_MUTEX
;
440 /* activate splice test by default */
441 if (!test_dequeue
&& !test_splice
)
444 if (test_sync
== TEST_SYNC_NONE
&& nr_dequeuers
> 1 && test_dequeue
) {
445 if (test_force_sync
) {
446 fprintf(stderr
, "[WARNING] Using dequeue concurrently "
447 "with other dequeue or splice without external "
448 "synchronization. Expect run-time failure.\n");
450 printf("Enforcing mutex synchronization\n");
451 test_sync
= TEST_SYNC_MUTEX
;
455 printf_verbose("running test for %lu seconds, %u enqueuers, "
457 duration
, nr_enqueuers
, nr_dequeuers
);
459 printf_verbose("dequeue test activated.\n");
461 printf_verbose("splice test activated.\n");
462 if (test_sync
== TEST_SYNC_MUTEX
)
463 printf_verbose("External sync: mutex.\n");
465 printf_verbose("External sync: none.\n");
467 printf_verbose("Wait for dequeuers to empty queue.\n");
468 printf_verbose("Writer delay : %lu loops.\n", rduration
);
469 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
470 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
471 "main", (unsigned long) pthread_self(),
472 (unsigned long) gettid());
474 tid_enqueuer
= calloc(nr_enqueuers
, sizeof(*tid_enqueuer
));
475 tid_dequeuer
= calloc(nr_dequeuers
, sizeof(*tid_dequeuer
));
476 count_enqueuer
= calloc(nr_enqueuers
, 3 * sizeof(*count_enqueuer
));
477 count_dequeuer
= calloc(nr_dequeuers
, 3 * sizeof(*count_dequeuer
));
479 cds_wfcq_init(&head
, &tail
);
483 for (i
= 0; i
< nr_enqueuers
; i
++) {
484 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
485 &count_enqueuer
[3 * i
]);
489 for (i
= 0; i
< nr_dequeuers
; i
++) {
490 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
491 &count_dequeuer
[3 * i
]);
500 for (i
= 0; i
< duration
; i
++) {
506 test_stop_enqueue
= 1;
508 if (test_wait_empty
) {
509 while (nr_enqueuers
!= uatomic_read(&test_enqueue_stopped
)) {
512 while (!cds_wfcq_empty(&head
, &tail
)) {
517 test_stop_dequeue
= 1;
519 for (i
= 0; i
< nr_enqueuers
; i
++) {
520 err
= pthread_join(tid_enqueuer
[i
], &tret
);
523 tot_enqueues
+= count_enqueuer
[3 * i
];
524 tot_successful_enqueues
+= count_enqueuer
[3 * i
+ 1];
525 tot_empty_dest_enqueues
+= count_enqueuer
[3 * i
+ 2];
527 for (i
= 0; i
< nr_dequeuers
; i
++) {
528 err
= pthread_join(tid_dequeuer
[i
], &tret
);
531 tot_dequeues
+= count_dequeuer
[3 * i
];
532 tot_successful_dequeues
+= count_dequeuer
[3 * i
+ 1];
533 tot_splice
+= count_dequeuer
[3 * i
+ 2];
536 test_end(&end_dequeues
);
538 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
539 tot_enqueues
, tot_dequeues
);
540 printf_verbose("total number of successful enqueues : %llu, "
541 "enqueues to empty dest : %llu, "
542 "successful dequeues %llu, "
544 tot_successful_enqueues
,
545 tot_empty_dest_enqueues
,
546 tot_successful_dequeues
,
548 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
550 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
551 "successful enqueues %12llu enqueues to empty dest %12llu "
552 "successful dequeues %12llu splice %12llu "
553 "end_dequeues %llu nr_ops %12llu\n",
554 argv
[0], duration
, nr_enqueuers
, wdelay
,
555 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
556 tot_successful_enqueues
,
557 tot_empty_dest_enqueues
,
558 tot_successful_dequeues
, tot_splice
, end_dequeues
,
559 tot_enqueues
+ tot_dequeues
);
561 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
) {
562 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
563 "succ. dequeues + end dequeues %llu.\n",
564 tot_successful_enqueues
,
565 tot_successful_dequeues
+ end_dequeues
);
570 * If only using splice to dequeue, the enqueuer should see
571 * exactly as many empty queues than the number of non-empty
574 if (test_wait_empty
&& test_splice
&& !test_dequeue
575 && tot_empty_dest_enqueues
!= tot_splice
) {
576 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
577 "number of non-empty splice (%llu)\n",
578 tot_empty_dest_enqueues
,
582 free(count_enqueuer
);
583 free(count_dequeuer
);