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 volatile int test_go
, test_stop_enqueue
, test_stop_dequeue
;
80 static unsigned long rduration
;
82 static unsigned long duration
;
84 /* read-side C.S. duration, in loops */
85 static unsigned long wdelay
;
87 static inline void loop_sleep(unsigned long loops
)
93 static int verbose_mode
;
95 static int test_dequeue
, test_splice
, test_wait_empty
;
96 static int test_enqueue_stopped
;
98 #define printf_verbose(fmt, args...) \
101 printf(fmt, ## args); \
104 static unsigned int cpu_affinities
[NR_CPUS
];
105 static unsigned int next_aff
= 0;
106 static int use_affinity
= 0;
108 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
110 #ifndef HAVE_CPU_SET_T
111 typedef unsigned long cpu_set_t
;
112 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
113 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
116 static void set_affinity(void)
118 #if HAVE_SCHED_SETAFFINITY
121 #endif /* HAVE_SCHED_SETAFFINITY */
126 #if HAVE_SCHED_SETAFFINITY
127 ret
= pthread_mutex_lock(&affinity_mutex
);
129 perror("Error in pthread mutex lock");
132 cpu
= cpu_affinities
[next_aff
++];
133 ret
= pthread_mutex_unlock(&affinity_mutex
);
135 perror("Error in pthread mutex unlock");
141 #if SCHED_SETAFFINITY_ARGS == 2
142 sched_setaffinity(0, &mask
);
144 sched_setaffinity(0, sizeof(mask
), &mask
);
146 #endif /* HAVE_SCHED_SETAFFINITY */
150 * returns 0 if test should end.
152 static int test_duration_dequeue(void)
154 return !test_stop_dequeue
;
157 static int test_duration_enqueue(void)
159 return !test_stop_enqueue
;
162 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
163 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
165 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
166 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
167 static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues
);
168 static DEFINE_URCU_TLS(unsigned long long, nr_splice
);
170 static unsigned int nr_enqueuers
;
171 static unsigned int nr_dequeuers
;
173 static struct cds_wfcq_head
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) head
;
174 static struct cds_wfcq_tail
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) tail
;
176 static void *thr_enqueuer(void *_count
)
178 unsigned long long *count
= _count
;
181 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
182 "enqueuer", (unsigned long) pthread_self(),
183 (unsigned long) gettid());
193 struct cds_wfcq_node
*node
= malloc(sizeof(*node
));
196 cds_wfcq_node_init(node
);
197 was_nonempty
= cds_wfcq_enqueue(&head
, &tail
, node
);
198 URCU_TLS(nr_successful_enqueues
)++;
200 URCU_TLS(nr_empty_dest_enqueues
)++;
202 if (caa_unlikely(wdelay
))
205 URCU_TLS(nr_enqueues
)++;
206 if (caa_unlikely(!test_duration_enqueue()))
210 uatomic_inc(&test_enqueue_stopped
);
211 count
[0] = URCU_TLS(nr_enqueues
);
212 count
[1] = URCU_TLS(nr_successful_enqueues
);
213 count
[2] = URCU_TLS(nr_empty_dest_enqueues
);
214 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
215 "enqueues %llu successful_enqueues %llu, "
216 "empty_dest_enqueues %llu\n",
218 (unsigned long) gettid(),
219 URCU_TLS(nr_enqueues
),
220 URCU_TLS(nr_successful_enqueues
),
221 URCU_TLS(nr_empty_dest_enqueues
));
226 static void do_test_dequeue(enum test_sync sync
)
228 struct cds_wfcq_node
*node
;
230 if (sync
== TEST_SYNC_MUTEX
)
231 node
= cds_wfcq_dequeue_blocking(&head
, &tail
);
233 node
= __cds_wfcq_dequeue_blocking(&head
, &tail
);
237 URCU_TLS(nr_successful_dequeues
)++;
239 URCU_TLS(nr_dequeues
)++;
242 static void do_test_splice(enum test_sync sync
)
244 struct cds_wfcq_head tmp_head
;
245 struct cds_wfcq_tail tmp_tail
;
246 struct cds_wfcq_node
*node
, *n
;
247 enum cds_wfcq_ret ret
;
249 cds_wfcq_init(&tmp_head
, &tmp_tail
);
251 if (sync
== TEST_SYNC_MUTEX
)
252 ret
= cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
255 ret
= __cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
259 case CDS_WFCQ_RET_WOULDBLOCK
:
260 assert(0); /* blocking call */
262 case CDS_WFCQ_RET_DEST_EMPTY
:
263 URCU_TLS(nr_splice
)++;
266 case CDS_WFCQ_RET_DEST_NON_EMPTY
:
267 assert(0); /* entirely unexpected */
269 case CDS_WFCQ_RET_SRC_EMPTY
:
270 /* ok, we could even skip iteration on dest if we wanted */
274 __cds_wfcq_for_each_blocking_safe(&tmp_head
, &tmp_tail
, node
, n
) {
276 URCU_TLS(nr_successful_dequeues
)++;
277 URCU_TLS(nr_dequeues
)++;
281 static void *thr_dequeuer(void *_count
)
283 unsigned long long *count
= _count
;
284 unsigned int counter
;
286 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
287 "dequeuer", (unsigned long) pthread_self(),
288 (unsigned long) gettid());
298 if (test_dequeue
&& test_splice
) {
300 do_test_dequeue(test_sync
);
302 do_test_splice(test_sync
);
306 do_test_dequeue(test_sync
);
308 do_test_splice(test_sync
);
310 if (caa_unlikely(!test_duration_dequeue()))
312 if (caa_unlikely(rduration
))
313 loop_sleep(rduration
);
316 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
317 "dequeues %llu, successful_dequeues %llu, "
320 (unsigned long) gettid(),
321 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
),
322 URCU_TLS(nr_splice
));
323 count
[0] = URCU_TLS(nr_dequeues
);
324 count
[1] = URCU_TLS(nr_successful_dequeues
);
325 count
[2] = URCU_TLS(nr_splice
);
329 static void test_end(unsigned long long *nr_dequeues
)
331 struct cds_wfcq_node
*node
;
334 node
= cds_wfcq_dequeue_blocking(&head
, &tail
);
342 static void show_usage(int argc
, char **argv
)
344 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv
[0]);
345 printf(" [-d delay] (enqueuer period (in loops))");
346 printf(" [-c duration] (dequeuer period (in loops))");
347 printf(" [-v] (verbose output)");
348 printf(" [-a cpu#] [-a cpu#]... (affinity)");
349 printf(" [-q] (test dequeue)");
350 printf(" [-s] (test splice, enabled by default)");
351 printf(" [-M] (use mutex external synchronization)");
352 printf(" [-0] (use no external synchronization)");
353 printf(" Note: default: mutex external synchronization used.");
354 printf(" [-w] Wait for dequeuer to empty queue");
358 int main(int argc
, char **argv
)
361 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
363 unsigned long long *count_enqueuer
, *count_dequeuer
;
364 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
365 unsigned long long tot_successful_enqueues
= 0,
366 tot_successful_dequeues
= 0,
367 tot_empty_dest_enqueues
= 0,
369 unsigned long long end_dequeues
= 0;
370 int i
, a
, retval
= 0;
373 show_usage(argc
, argv
);
377 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
379 show_usage(argc
, argv
);
383 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
385 show_usage(argc
, argv
);
389 err
= sscanf(argv
[3], "%lu", &duration
);
391 show_usage(argc
, argv
);
395 for (i
= 4; i
< argc
; i
++) {
396 if (argv
[i
][0] != '-')
398 switch (argv
[i
][1]) {
401 show_usage(argc
, argv
);
405 cpu_affinities
[next_aff
++] = a
;
407 printf_verbose("Adding CPU %d affinity\n", a
);
411 show_usage(argc
, argv
);
414 rduration
= atol(argv
[++i
]);
418 show_usage(argc
, argv
);
421 wdelay
= atol(argv
[++i
]);
433 test_sync
= TEST_SYNC_MUTEX
;
436 test_sync
= TEST_SYNC_NONE
;
444 /* activate splice test by default */
445 if (!test_dequeue
&& !test_splice
)
448 printf_verbose("running test for %lu seconds, %u enqueuers, "
450 duration
, nr_enqueuers
, nr_dequeuers
);
452 printf_verbose("dequeue test activated.\n");
454 printf_verbose("splice test activated.\n");
455 if (test_sync
== TEST_SYNC_MUTEX
)
456 printf_verbose("External sync: mutex.\n");
458 printf_verbose("External sync: none.\n");
460 printf_verbose("Wait for dequeuers to empty queue.\n");
461 printf_verbose("Writer delay : %lu loops.\n", rduration
);
462 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
463 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
464 "main", (unsigned long) pthread_self(),
465 (unsigned long) gettid());
467 tid_enqueuer
= malloc(sizeof(*tid_enqueuer
) * nr_enqueuers
);
468 tid_dequeuer
= malloc(sizeof(*tid_dequeuer
) * nr_dequeuers
);
469 count_enqueuer
= malloc(3 * sizeof(*count_enqueuer
) * nr_enqueuers
);
470 count_dequeuer
= malloc(3 * sizeof(*count_dequeuer
) * nr_dequeuers
);
471 cds_wfcq_init(&head
, &tail
);
475 for (i
= 0; i
< nr_enqueuers
; i
++) {
476 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
477 &count_enqueuer
[3 * i
]);
481 for (i
= 0; i
< nr_dequeuers
; i
++) {
482 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
483 &count_dequeuer
[3 * i
]);
492 for (i
= 0; i
< duration
; i
++) {
498 test_stop_enqueue
= 1;
500 if (test_wait_empty
) {
501 while (nr_enqueuers
!= uatomic_read(&test_enqueue_stopped
)) {
504 while (!cds_wfcq_empty(&head
, &tail
)) {
509 test_stop_dequeue
= 1;
511 for (i
= 0; i
< nr_enqueuers
; i
++) {
512 err
= pthread_join(tid_enqueuer
[i
], &tret
);
515 tot_enqueues
+= count_enqueuer
[3 * i
];
516 tot_successful_enqueues
+= count_enqueuer
[3 * i
+ 1];
517 tot_empty_dest_enqueues
+= count_enqueuer
[3 * i
+ 2];
519 for (i
= 0; i
< nr_dequeuers
; i
++) {
520 err
= pthread_join(tid_dequeuer
[i
], &tret
);
523 tot_dequeues
+= count_dequeuer
[3 * i
];
524 tot_successful_dequeues
+= count_dequeuer
[3 * i
+ 1];
525 tot_splice
+= count_dequeuer
[3 * i
+ 2];
528 test_end(&end_dequeues
);
530 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
531 tot_enqueues
, tot_dequeues
);
532 printf_verbose("total number of successful enqueues : %llu, "
533 "enqueues to empty dest : %llu, "
534 "successful dequeues %llu, ",
536 tot_successful_enqueues
,
537 tot_empty_dest_enqueues
,
538 tot_successful_dequeues
,
540 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
542 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
543 "successful enqueues %12llu enqueues to empty dest %12llu "
544 "successful dequeues %12llu splice %12llu "
545 "end_dequeues %llu nr_ops %12llu\n",
546 argv
[0], duration
, nr_enqueuers
, wdelay
,
547 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
548 tot_successful_enqueues
,
549 tot_empty_dest_enqueues
,
550 tot_successful_dequeues
, tot_splice
, end_dequeues
,
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 (test_wait_empty
&& test_splice
&& !test_dequeue
567 && tot_empty_dest_enqueues
!= tot_splice
) {
568 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
569 "number of non-empty splice (%llu)\n",
570 tot_empty_dest_enqueues
,
574 free(count_enqueuer
);
575 free(count_dequeuer
);