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>
43 #include "thread-id.h"
45 /* hardcoded number of CPUs */
48 #ifndef DYNAMIC_LINK_TEST
51 #include <urcu/wfstack.h>
54 * External synchronization used.
61 static enum test_sync test_sync
;
63 static int test_force_sync
;
65 static volatile int test_go
, test_stop_enqueue
, test_stop_dequeue
;
67 static unsigned long rduration
;
69 static unsigned long duration
;
71 /* read-side C.S. duration, in loops */
72 static unsigned long wdelay
;
74 static inline void loop_sleep(unsigned long loops
)
80 static int verbose_mode
;
82 static int test_pop
, test_pop_all
, test_wait_empty
;
83 static int test_enqueue_stopped
;
85 #define printf_verbose(fmt, args...) \
88 printf(fmt, ## args); \
91 static unsigned int cpu_affinities
[NR_CPUS
];
92 static unsigned int next_aff
= 0;
93 static int use_affinity
= 0;
95 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
97 static void set_affinity(void)
99 #if HAVE_SCHED_SETAFFINITY
102 #endif /* HAVE_SCHED_SETAFFINITY */
107 #if HAVE_SCHED_SETAFFINITY
108 ret
= pthread_mutex_lock(&affinity_mutex
);
110 perror("Error in pthread mutex lock");
113 cpu
= cpu_affinities
[next_aff
++];
114 ret
= pthread_mutex_unlock(&affinity_mutex
);
116 perror("Error in pthread mutex unlock");
122 #if SCHED_SETAFFINITY_ARGS == 2
123 sched_setaffinity(0, &mask
);
125 sched_setaffinity(0, sizeof(mask
), &mask
);
127 #endif /* HAVE_SCHED_SETAFFINITY */
131 * returns 0 if test should end.
133 static int test_duration_dequeue(void)
135 return !test_stop_dequeue
;
138 static int test_duration_enqueue(void)
140 return !test_stop_enqueue
;
143 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
144 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
146 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
147 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
148 static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues
);
149 static DEFINE_URCU_TLS(unsigned long long, nr_pop_all
);
150 static DEFINE_URCU_TLS(unsigned long long, nr_pop_last
);
152 static unsigned int nr_enqueuers
;
153 static unsigned int nr_dequeuers
;
155 static struct cds_wfs_stack s
;
157 static void *thr_enqueuer(void *_count
)
159 unsigned long long *count
= _count
;
162 printf_verbose("thread_begin %s, tid %lu\n",
163 "enqueuer", urcu_get_thread_id());
173 struct cds_wfs_node
*node
= malloc(sizeof(*node
));
176 cds_wfs_node_init(node
);
177 was_nonempty
= cds_wfs_push(&s
, node
);
178 URCU_TLS(nr_successful_enqueues
)++;
180 URCU_TLS(nr_empty_dest_enqueues
)++;
182 if (caa_unlikely(wdelay
))
185 URCU_TLS(nr_enqueues
)++;
186 if (caa_unlikely(!test_duration_enqueue()))
190 uatomic_inc(&test_enqueue_stopped
);
191 count
[0] = URCU_TLS(nr_enqueues
);
192 count
[1] = URCU_TLS(nr_successful_enqueues
);
193 count
[2] = URCU_TLS(nr_empty_dest_enqueues
);
194 printf_verbose("enqueuer thread_end, tid %lu, "
195 "enqueues %llu successful_enqueues %llu, "
196 "empty_dest_enqueues %llu\n",
197 urcu_get_thread_id(),
198 URCU_TLS(nr_enqueues
),
199 URCU_TLS(nr_successful_enqueues
),
200 URCU_TLS(nr_empty_dest_enqueues
));
205 static void do_test_pop(enum test_sync sync
)
207 struct cds_wfs_node
*node
;
210 if (sync
== TEST_SYNC_MUTEX
)
211 cds_wfs_pop_lock(&s
);
212 node
= __cds_wfs_pop_with_state_blocking(&s
, &state
);
213 if (sync
== TEST_SYNC_MUTEX
)
214 cds_wfs_pop_unlock(&s
);
217 if (state
& CDS_WFS_STATE_LAST
)
218 URCU_TLS(nr_pop_last
)++;
220 URCU_TLS(nr_successful_dequeues
)++;
222 URCU_TLS(nr_dequeues
)++;
225 static void do_test_pop_all(enum test_sync sync
)
227 struct cds_wfs_head
*head
;
228 struct cds_wfs_node
*node
, *n
;
230 if (sync
== TEST_SYNC_MUTEX
)
231 cds_wfs_pop_lock(&s
);
232 head
= __cds_wfs_pop_all(&s
);
233 if (sync
== TEST_SYNC_MUTEX
)
234 cds_wfs_pop_unlock(&s
);
237 if (cds_wfs_first(head
) == NULL
)
240 URCU_TLS(nr_pop_all
)++;
241 URCU_TLS(nr_pop_last
)++;
243 cds_wfs_for_each_blocking_safe(head
, node
, n
) {
245 URCU_TLS(nr_successful_dequeues
)++;
246 URCU_TLS(nr_dequeues
)++;
250 static void *thr_dequeuer(void *_count
)
252 unsigned long long *count
= _count
;
253 unsigned int counter
= 0;
255 printf_verbose("thread_begin %s, tid %lu\n",
256 "dequeuer", urcu_get_thread_id());
265 assert(test_pop
|| test_pop_all
);
268 if (test_pop
&& test_pop_all
) {
270 do_test_pop(test_sync
);
272 do_test_pop_all(test_sync
);
276 do_test_pop(test_sync
);
278 do_test_pop_all(test_sync
);
281 if (caa_unlikely(!test_duration_dequeue()))
283 if (caa_unlikely(rduration
))
284 loop_sleep(rduration
);
287 printf_verbose("dequeuer thread_end, tid %lu, "
288 "dequeues %llu, successful_dequeues %llu "
289 "pop_all %llu pop_last %llu\n",
290 urcu_get_thread_id(),
291 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
),
292 URCU_TLS(nr_pop_all
),
293 URCU_TLS(nr_pop_last
));
294 count
[0] = URCU_TLS(nr_dequeues
);
295 count
[1] = URCU_TLS(nr_successful_dequeues
);
296 count
[2] = URCU_TLS(nr_pop_all
);
297 count
[3] = URCU_TLS(nr_pop_last
);
301 static void test_end(struct cds_wfs_stack
*s
, unsigned long long *nr_dequeues
,
302 unsigned long long *nr_pop_last
)
304 struct cds_wfs_node
*node
;
308 node
= cds_wfs_pop_with_state_blocking(s
, &state
);
310 if (state
& CDS_WFS_STATE_LAST
)
318 static void show_usage(int argc
, char **argv
)
320 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
322 printf("OPTIONS:\n");
323 printf(" [-d delay] (enqueuer period (in loops))\n");
324 printf(" [-c duration] (dequeuer period (in loops))\n");
325 printf(" [-v] (verbose output)\n");
326 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
327 printf(" [-p] (test pop)\n");
328 printf(" [-P] (test pop_all, enabled by default)\n");
329 printf(" [-M] (use mutex external synchronization)\n");
330 printf(" Note: default: no external synchronization used.\n");
331 printf(" [-f] (force user-provided synchronization)\n");
332 printf(" [-w] Wait for dequeuer to empty stack\n");
336 int main(int argc
, char **argv
)
339 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
341 unsigned long long *count_enqueuer
, *count_dequeuer
;
342 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
343 unsigned long long tot_successful_enqueues
= 0,
344 tot_successful_dequeues
= 0,
345 tot_empty_dest_enqueues
= 0,
346 tot_pop_all
= 0, tot_pop_last
= 0;
347 unsigned long long end_dequeues
= 0;
348 int i
, a
, retval
= 0;
351 show_usage(argc
, argv
);
355 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
357 show_usage(argc
, argv
);
361 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
363 show_usage(argc
, argv
);
367 err
= sscanf(argv
[3], "%lu", &duration
);
369 show_usage(argc
, argv
);
373 for (i
= 4; i
< argc
; i
++) {
374 if (argv
[i
][0] != '-')
376 switch (argv
[i
][1]) {
379 show_usage(argc
, argv
);
383 cpu_affinities
[next_aff
++] = a
;
385 printf_verbose("Adding CPU %d affinity\n", a
);
389 show_usage(argc
, argv
);
392 rduration
= atol(argv
[++i
]);
396 show_usage(argc
, argv
);
399 wdelay
= atol(argv
[++i
]);
411 test_sync
= TEST_SYNC_MUTEX
;
422 /* activate pop_all test by default */
423 if (!test_pop
&& !test_pop_all
)
426 if (test_sync
== TEST_SYNC_NONE
&& nr_dequeuers
> 1 && test_pop
) {
427 if (test_force_sync
) {
428 fprintf(stderr
, "[WARNING] Using pop concurrently "
429 "with other pop or pop_all without external "
430 "synchronization. Expect run-time failure.\n");
432 printf("Enforcing mutex synchronization\n");
433 test_sync
= TEST_SYNC_MUTEX
;
437 printf_verbose("running test for %lu seconds, %u enqueuers, "
439 duration
, nr_enqueuers
, nr_dequeuers
);
441 printf_verbose("pop test activated.\n");
443 printf_verbose("pop_all test activated.\n");
444 if (test_sync
== TEST_SYNC_MUTEX
)
445 printf_verbose("External sync: mutex.\n");
447 printf_verbose("External sync: none.\n");
449 printf_verbose("Wait for dequeuers to empty stack.\n");
450 printf_verbose("Writer delay : %lu loops.\n", rduration
);
451 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
452 printf_verbose("thread %-6s, tid %lu\n",
453 "main", urcu_get_thread_id());
455 tid_enqueuer
= calloc(nr_enqueuers
, sizeof(*tid_enqueuer
));
456 tid_dequeuer
= calloc(nr_dequeuers
, sizeof(*tid_dequeuer
));
457 count_enqueuer
= calloc(nr_enqueuers
, 3 * sizeof(*count_enqueuer
));
458 count_dequeuer
= calloc(nr_dequeuers
, 4 * sizeof(*count_dequeuer
));
463 for (i
= 0; i
< nr_enqueuers
; i
++) {
464 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
465 &count_enqueuer
[3 * i
]);
469 for (i
= 0; i
< nr_dequeuers
; i
++) {
470 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
471 &count_dequeuer
[4 * i
]);
480 for (i
= 0; i
< duration
; i
++) {
486 test_stop_enqueue
= 1;
488 if (test_wait_empty
) {
489 while (nr_enqueuers
!= uatomic_read(&test_enqueue_stopped
)) {
492 while (!cds_wfs_empty(&s
)) {
497 test_stop_dequeue
= 1;
499 for (i
= 0; i
< nr_enqueuers
; i
++) {
500 err
= pthread_join(tid_enqueuer
[i
], &tret
);
503 tot_enqueues
+= count_enqueuer
[3 * i
];
504 tot_successful_enqueues
+= count_enqueuer
[3 * i
+ 1];
505 tot_empty_dest_enqueues
+= count_enqueuer
[3 * i
+ 2];
507 for (i
= 0; i
< nr_dequeuers
; i
++) {
508 err
= pthread_join(tid_dequeuer
[i
], &tret
);
511 tot_dequeues
+= count_dequeuer
[4 * i
];
512 tot_successful_dequeues
+= count_dequeuer
[4 * i
+ 1];
513 tot_pop_all
+= count_dequeuer
[4 * i
+ 2];
514 tot_pop_last
+= count_dequeuer
[4 * i
+ 3];
517 test_end(&s
, &end_dequeues
, &tot_pop_last
);
519 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
520 tot_enqueues
, tot_dequeues
);
521 printf_verbose("total number of successful enqueues : %llu, "
522 "enqueues to empty dest : %llu, "
523 "successful dequeues %llu, "
524 "pop_all : %llu, pop_last : %llu\n",
525 tot_successful_enqueues
,
526 tot_empty_dest_enqueues
,
527 tot_successful_dequeues
,
528 tot_pop_all
, tot_pop_last
);
529 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
531 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
532 "successful enqueues %12llu enqueues to empty dest %12llu "
533 "successful dequeues %12llu pop_all %12llu "
534 "pop_last %llu end_dequeues %llu nr_ops %12llu\n",
535 argv
[0], duration
, nr_enqueuers
, wdelay
,
536 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
537 tot_successful_enqueues
,
538 tot_empty_dest_enqueues
,
539 tot_successful_dequeues
, tot_pop_all
, tot_pop_last
,
541 tot_enqueues
+ tot_dequeues
);
542 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
) {
543 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
544 "succ. dequeues + end dequeues %llu.\n",
545 tot_successful_enqueues
,
546 tot_successful_dequeues
+ end_dequeues
);
550 * The enqueuer should see exactly as many empty queues than the
551 * number of non-empty stacks dequeued.
553 if (tot_empty_dest_enqueues
!= tot_pop_last
) {
554 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
555 "number of pop last (%llu)\n",
556 tot_empty_dest_enqueues
,
560 free(count_enqueuer
);
561 free(count_dequeuer
);