4 * Userspace RCU library - example RCU-based lock-free stack
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/wfstack.h>
72 * External synchronization used.
79 static enum test_sync test_sync
;
81 static int test_force_sync
;
83 static volatile int test_go
, test_stop_enqueue
, test_stop_dequeue
;
85 static unsigned long rduration
;
87 static unsigned long duration
;
89 /* read-side C.S. duration, in loops */
90 static unsigned long wdelay
;
92 static inline void loop_sleep(unsigned long loops
)
98 static int verbose_mode
;
100 static int test_pop
, test_pop_all
, test_wait_empty
;
101 static int test_enqueue_stopped
;
103 #define printf_verbose(fmt, args...) \
106 printf(fmt, ## args); \
109 static unsigned int cpu_affinities
[NR_CPUS
];
110 static unsigned int next_aff
= 0;
111 static int use_affinity
= 0;
113 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
115 static void set_affinity(void)
117 #if HAVE_SCHED_SETAFFINITY
120 #endif /* HAVE_SCHED_SETAFFINITY */
125 #if HAVE_SCHED_SETAFFINITY
126 ret
= pthread_mutex_lock(&affinity_mutex
);
128 perror("Error in pthread mutex lock");
131 cpu
= cpu_affinities
[next_aff
++];
132 ret
= pthread_mutex_unlock(&affinity_mutex
);
134 perror("Error in pthread mutex unlock");
140 #if SCHED_SETAFFINITY_ARGS == 2
141 sched_setaffinity(0, &mask
);
143 sched_setaffinity(0, sizeof(mask
), &mask
);
145 #endif /* HAVE_SCHED_SETAFFINITY */
149 * returns 0 if test should end.
151 static int test_duration_dequeue(void)
153 return !test_stop_dequeue
;
156 static int test_duration_enqueue(void)
158 return !test_stop_enqueue
;
161 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
162 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
164 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
165 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
166 static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues
);
167 static DEFINE_URCU_TLS(unsigned long long, nr_pop_all
);
168 static DEFINE_URCU_TLS(unsigned long long, nr_pop_last
);
170 static unsigned int nr_enqueuers
;
171 static unsigned int nr_dequeuers
;
173 static struct cds_wfs_stack s
;
175 static void *thr_enqueuer(void *_count
)
177 unsigned long long *count
= _count
;
180 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
181 "enqueuer", (unsigned long) pthread_self(),
182 (unsigned long) gettid());
192 struct cds_wfs_node
*node
= malloc(sizeof(*node
));
195 cds_wfs_node_init(node
);
196 was_nonempty
= cds_wfs_push(&s
, node
);
197 URCU_TLS(nr_successful_enqueues
)++;
199 URCU_TLS(nr_empty_dest_enqueues
)++;
201 if (caa_unlikely(wdelay
))
204 URCU_TLS(nr_enqueues
)++;
205 if (caa_unlikely(!test_duration_enqueue()))
209 uatomic_inc(&test_enqueue_stopped
);
210 count
[0] = URCU_TLS(nr_enqueues
);
211 count
[1] = URCU_TLS(nr_successful_enqueues
);
212 count
[2] = URCU_TLS(nr_empty_dest_enqueues
);
213 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
214 "enqueues %llu successful_enqueues %llu, "
215 "empty_dest_enqueues %llu\n",
217 (unsigned long) gettid(),
218 URCU_TLS(nr_enqueues
),
219 URCU_TLS(nr_successful_enqueues
),
220 URCU_TLS(nr_empty_dest_enqueues
));
225 static void do_test_pop(enum test_sync sync
)
227 struct cds_wfs_node
*node
;
230 if (sync
== TEST_SYNC_MUTEX
)
231 cds_wfs_pop_lock(&s
);
232 node
= __cds_wfs_pop_with_state_blocking(&s
, &state
);
233 if (sync
== TEST_SYNC_MUTEX
)
234 cds_wfs_pop_unlock(&s
);
237 if (state
& CDS_WFS_STATE_LAST
)
238 URCU_TLS(nr_pop_last
)++;
240 URCU_TLS(nr_successful_dequeues
)++;
242 URCU_TLS(nr_dequeues
)++;
245 static void do_test_pop_all(enum test_sync sync
)
247 struct cds_wfs_head
*head
;
248 struct cds_wfs_node
*node
, *n
;
250 if (sync
== TEST_SYNC_MUTEX
)
251 cds_wfs_pop_lock(&s
);
252 head
= __cds_wfs_pop_all(&s
);
253 if (sync
== TEST_SYNC_MUTEX
)
254 cds_wfs_pop_unlock(&s
);
257 if (cds_wfs_first(head
) == NULL
)
260 URCU_TLS(nr_pop_all
)++;
261 URCU_TLS(nr_pop_last
)++;
263 cds_wfs_for_each_blocking_safe(head
, node
, n
) {
265 URCU_TLS(nr_successful_dequeues
)++;
266 URCU_TLS(nr_dequeues
)++;
270 static void *thr_dequeuer(void *_count
)
272 unsigned long long *count
= _count
;
273 unsigned int counter
;
275 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
276 "dequeuer", (unsigned long) pthread_self(),
277 (unsigned long) gettid());
286 assert(test_pop
|| test_pop_all
);
289 if (test_pop
&& test_pop_all
) {
291 do_test_pop(test_sync
);
293 do_test_pop_all(test_sync
);
297 do_test_pop(test_sync
);
299 do_test_pop_all(test_sync
);
302 if (caa_unlikely(!test_duration_dequeue()))
304 if (caa_unlikely(rduration
))
305 loop_sleep(rduration
);
308 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
309 "dequeues %llu, successful_dequeues %llu "
310 "pop_all %llu pop_last %llu\n",
312 (unsigned long) gettid(),
313 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
),
314 URCU_TLS(nr_pop_all
),
315 URCU_TLS(nr_pop_last
));
316 count
[0] = URCU_TLS(nr_dequeues
);
317 count
[1] = URCU_TLS(nr_successful_dequeues
);
318 count
[2] = URCU_TLS(nr_pop_all
);
319 count
[3] = URCU_TLS(nr_pop_last
);
323 static void test_end(struct cds_wfs_stack
*s
, unsigned long long *nr_dequeues
,
324 unsigned long long *nr_pop_last
)
326 struct cds_wfs_node
*node
;
330 node
= cds_wfs_pop_with_state_blocking(s
, &state
);
332 if (state
& CDS_WFS_STATE_LAST
)
340 static void show_usage(int argc
, char **argv
)
342 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv
[0]);
343 printf(" [-d delay] (enqueuer period (in loops))");
344 printf(" [-c duration] (dequeuer period (in loops))");
345 printf(" [-v] (verbose output)");
346 printf(" [-a cpu#] [-a cpu#]... (affinity)");
347 printf(" [-p] (test pop)");
348 printf(" [-P] (test pop_all, enabled by default)");
349 printf(" [-M] (use mutex external synchronization)");
350 printf(" Note: default: no external synchronization used.");
351 printf(" [-f] (force user-provided synchronization)");
352 printf(" [-w] Wait for dequeuer to empty stack");
356 int main(int argc
, char **argv
)
359 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
361 unsigned long long *count_enqueuer
, *count_dequeuer
;
362 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
363 unsigned long long tot_successful_enqueues
= 0,
364 tot_successful_dequeues
= 0,
365 tot_empty_dest_enqueues
= 0,
366 tot_pop_all
= 0, tot_pop_last
= 0;
367 unsigned long long end_dequeues
= 0;
368 int i
, a
, retval
= 0;
371 show_usage(argc
, argv
);
375 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
377 show_usage(argc
, argv
);
381 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
383 show_usage(argc
, argv
);
387 err
= sscanf(argv
[3], "%lu", &duration
);
389 show_usage(argc
, argv
);
393 for (i
= 4; i
< argc
; i
++) {
394 if (argv
[i
][0] != '-')
396 switch (argv
[i
][1]) {
399 show_usage(argc
, argv
);
403 cpu_affinities
[next_aff
++] = a
;
405 printf_verbose("Adding CPU %d affinity\n", a
);
409 show_usage(argc
, argv
);
412 rduration
= atol(argv
[++i
]);
416 show_usage(argc
, argv
);
419 wdelay
= atol(argv
[++i
]);
431 test_sync
= TEST_SYNC_MUTEX
;
442 /* activate pop_all test by default */
443 if (!test_pop
&& !test_pop_all
)
446 if (test_sync
== TEST_SYNC_NONE
&& nr_dequeuers
> 1 && test_pop
) {
447 if (test_force_sync
) {
448 fprintf(stderr
, "[WARNING] Using pop concurrently "
449 "with other pop or pop_all without external "
450 "synchronization. Expect run-time failure.\n");
452 printf("Enforcing mutex synchronization\n");
453 test_sync
= TEST_SYNC_MUTEX
;
457 printf_verbose("running test for %lu seconds, %u enqueuers, "
459 duration
, nr_enqueuers
, nr_dequeuers
);
461 printf_verbose("pop test activated.\n");
463 printf_verbose("pop_all test activated.\n");
464 if (test_sync
== TEST_SYNC_MUTEX
)
465 printf_verbose("External sync: mutex.\n");
467 printf_verbose("External sync: none.\n");
469 printf_verbose("Wait for dequeuers to empty stack.\n");
470 printf_verbose("Writer delay : %lu loops.\n", rduration
);
471 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
472 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
473 "main", (unsigned long) pthread_self(),
474 (unsigned long) gettid());
476 tid_enqueuer
= malloc(sizeof(*tid_enqueuer
) * nr_enqueuers
);
477 tid_dequeuer
= malloc(sizeof(*tid_dequeuer
) * nr_dequeuers
);
478 count_enqueuer
= malloc(3 * sizeof(*count_enqueuer
) * nr_enqueuers
);
479 count_dequeuer
= malloc(4 * sizeof(*count_dequeuer
) * nr_dequeuers
);
484 for (i
= 0; i
< nr_enqueuers
; i
++) {
485 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
486 &count_enqueuer
[3 * i
]);
490 for (i
= 0; i
< nr_dequeuers
; i
++) {
491 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
492 &count_dequeuer
[4 * i
]);
501 for (i
= 0; i
< duration
; i
++) {
507 test_stop_enqueue
= 1;
509 if (test_wait_empty
) {
510 while (nr_enqueuers
!= uatomic_read(&test_enqueue_stopped
)) {
513 while (!cds_wfs_empty(&s
)) {
518 test_stop_dequeue
= 1;
520 for (i
= 0; i
< nr_enqueuers
; i
++) {
521 err
= pthread_join(tid_enqueuer
[i
], &tret
);
524 tot_enqueues
+= count_enqueuer
[3 * i
];
525 tot_successful_enqueues
+= count_enqueuer
[3 * i
+ 1];
526 tot_empty_dest_enqueues
+= count_enqueuer
[3 * i
+ 2];
528 for (i
= 0; i
< nr_dequeuers
; i
++) {
529 err
= pthread_join(tid_dequeuer
[i
], &tret
);
532 tot_dequeues
+= count_dequeuer
[4 * i
];
533 tot_successful_dequeues
+= count_dequeuer
[4 * i
+ 1];
534 tot_pop_all
+= count_dequeuer
[4 * i
+ 2];
535 tot_pop_last
+= count_dequeuer
[4 * i
+ 3];
538 test_end(&s
, &end_dequeues
, &tot_pop_last
);
540 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
541 tot_enqueues
, tot_dequeues
);
542 printf_verbose("total number of successful enqueues : %llu, "
543 "enqueues to empty dest : %llu, "
544 "successful dequeues %llu, "
545 "pop_all : %llu, pop_last : %llu\n",
546 tot_successful_enqueues
,
547 tot_empty_dest_enqueues
,
548 tot_successful_dequeues
,
549 tot_pop_all
, tot_pop_last
);
550 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
552 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
553 "successful enqueues %12llu enqueues to empty dest %12llu "
554 "successful dequeues %12llu pop_all %12llu "
555 "pop_last %llu end_dequeues %llu nr_ops %12llu\n",
556 argv
[0], duration
, nr_enqueuers
, wdelay
,
557 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
558 tot_successful_enqueues
,
559 tot_empty_dest_enqueues
,
560 tot_successful_dequeues
, tot_pop_all
, tot_pop_last
,
562 tot_enqueues
+ tot_dequeues
);
563 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
) {
564 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
565 "succ. dequeues + end dequeues %llu.\n",
566 tot_successful_enqueues
,
567 tot_successful_dequeues
+ end_dequeues
);
571 * The enqueuer should see exactly as many empty queues than the
572 * number of non-empty stacks dequeued.
574 if (tot_empty_dest_enqueues
!= tot_pop_last
) {
575 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
576 "number of pop last (%llu)\n",
577 tot_empty_dest_enqueues
,
581 free(count_enqueuer
);
582 free(count_dequeuer
);