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.
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/wfstack.h>
51 * External synchronization used.
58 static enum test_sync test_sync
;
60 static int test_force_sync
;
62 static volatile int test_go
, test_stop_enqueue
, test_stop_dequeue
;
64 static unsigned long rduration
;
66 static unsigned long duration
;
68 /* read-side C.S. duration, in loops */
69 static unsigned long wdelay
;
71 static inline void loop_sleep(unsigned long loops
)
77 static int verbose_mode
;
79 static int test_pop
, test_pop_all
, test_wait_empty
;
80 static unsigned int test_enqueue_stopped
;
82 #define printf_verbose(fmt, args...) \
85 printf(fmt, ## args); \
88 static unsigned int cpu_affinities
[NR_CPUS
];
89 static unsigned int next_aff
= 0;
90 static int use_affinity
= 0;
92 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
94 static void set_affinity(void)
96 #ifdef HAVE_SCHED_SETAFFINITY
99 #endif /* HAVE_SCHED_SETAFFINITY */
104 #ifdef HAVE_SCHED_SETAFFINITY
105 ret
= pthread_mutex_lock(&affinity_mutex
);
107 perror("Error in pthread mutex lock");
110 cpu
= cpu_affinities
[next_aff
++];
111 ret
= pthread_mutex_unlock(&affinity_mutex
);
113 perror("Error in pthread mutex unlock");
119 sched_setaffinity(0, sizeof(mask
), &mask
);
120 #endif /* HAVE_SCHED_SETAFFINITY */
124 * returns 0 if test should end.
126 static int test_duration_dequeue(void)
128 return !test_stop_dequeue
;
131 static int test_duration_enqueue(void)
133 return !test_stop_enqueue
;
136 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
137 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
139 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
140 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
141 static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues
);
142 static DEFINE_URCU_TLS(unsigned long long, nr_pop_all
);
143 static DEFINE_URCU_TLS(unsigned long long, nr_pop_last
);
145 static unsigned int nr_enqueuers
;
146 static unsigned int nr_dequeuers
;
148 static struct cds_wfs_stack s
;
150 static void *thr_enqueuer(void *_count
)
152 unsigned long long *count
= _count
;
155 printf_verbose("thread_begin %s, tid %lu\n",
156 "enqueuer", urcu_get_thread_id());
166 struct cds_wfs_node
*node
= malloc(sizeof(*node
));
169 cds_wfs_node_init(node
);
170 was_nonempty
= cds_wfs_push(&s
, node
);
171 URCU_TLS(nr_successful_enqueues
)++;
173 URCU_TLS(nr_empty_dest_enqueues
)++;
175 if (caa_unlikely(wdelay
))
178 URCU_TLS(nr_enqueues
)++;
179 if (caa_unlikely(!test_duration_enqueue()))
183 uatomic_inc(&test_enqueue_stopped
);
184 count
[0] = URCU_TLS(nr_enqueues
);
185 count
[1] = URCU_TLS(nr_successful_enqueues
);
186 count
[2] = URCU_TLS(nr_empty_dest_enqueues
);
187 printf_verbose("enqueuer thread_end, tid %lu, "
188 "enqueues %llu successful_enqueues %llu, "
189 "empty_dest_enqueues %llu\n",
190 urcu_get_thread_id(),
191 URCU_TLS(nr_enqueues
),
192 URCU_TLS(nr_successful_enqueues
),
193 URCU_TLS(nr_empty_dest_enqueues
));
198 static void do_test_pop(enum test_sync sync
)
200 struct cds_wfs_node
*node
;
203 if (sync
== TEST_SYNC_MUTEX
)
204 cds_wfs_pop_lock(&s
);
205 node
= __cds_wfs_pop_with_state_blocking(&s
, &state
);
206 if (sync
== TEST_SYNC_MUTEX
)
207 cds_wfs_pop_unlock(&s
);
210 if (state
& CDS_WFS_STATE_LAST
)
211 URCU_TLS(nr_pop_last
)++;
213 URCU_TLS(nr_successful_dequeues
)++;
215 URCU_TLS(nr_dequeues
)++;
218 static void do_test_pop_all(enum test_sync sync
)
220 struct cds_wfs_head
*head
;
221 struct cds_wfs_node
*node
, *n
;
223 if (sync
== TEST_SYNC_MUTEX
)
224 cds_wfs_pop_lock(&s
);
225 head
= __cds_wfs_pop_all(&s
);
226 if (sync
== TEST_SYNC_MUTEX
)
227 cds_wfs_pop_unlock(&s
);
230 if (cds_wfs_first(head
) == NULL
)
233 URCU_TLS(nr_pop_all
)++;
234 URCU_TLS(nr_pop_last
)++;
236 cds_wfs_for_each_blocking_safe(head
, node
, n
) {
238 URCU_TLS(nr_successful_dequeues
)++;
239 URCU_TLS(nr_dequeues
)++;
243 static void *thr_dequeuer(void *_count
)
245 unsigned long long *count
= _count
;
246 unsigned int counter
= 0;
248 printf_verbose("thread_begin %s, tid %lu\n",
249 "dequeuer", urcu_get_thread_id());
258 urcu_posix_assert(test_pop
|| test_pop_all
);
261 if (test_pop
&& test_pop_all
) {
263 do_test_pop(test_sync
);
265 do_test_pop_all(test_sync
);
269 do_test_pop(test_sync
);
271 do_test_pop_all(test_sync
);
274 if (caa_unlikely(!test_duration_dequeue()))
276 if (caa_unlikely(rduration
))
277 loop_sleep(rduration
);
280 printf_verbose("dequeuer thread_end, tid %lu, "
281 "dequeues %llu, successful_dequeues %llu "
282 "pop_all %llu pop_last %llu\n",
283 urcu_get_thread_id(),
284 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
),
285 URCU_TLS(nr_pop_all
),
286 URCU_TLS(nr_pop_last
));
287 count
[0] = URCU_TLS(nr_dequeues
);
288 count
[1] = URCU_TLS(nr_successful_dequeues
);
289 count
[2] = URCU_TLS(nr_pop_all
);
290 count
[3] = URCU_TLS(nr_pop_last
);
294 static void test_end(unsigned long long *nr_dequeues_l
,
295 unsigned long long *nr_pop_last_l
)
297 struct cds_wfs_node
*node
;
301 node
= cds_wfs_pop_with_state_blocking(&s
, &state
);
303 if (state
& CDS_WFS_STATE_LAST
)
311 static void show_usage(char **argv
)
313 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
315 printf("OPTIONS:\n");
316 printf(" [-d delay] (enqueuer period (in loops))\n");
317 printf(" [-c duration] (dequeuer period (in loops))\n");
318 printf(" [-v] (verbose output)\n");
319 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
320 printf(" [-p] (test pop)\n");
321 printf(" [-P] (test pop_all, enabled by default)\n");
322 printf(" [-M] (use mutex external synchronization)\n");
323 printf(" Note: default: no external synchronization used.\n");
324 printf(" [-f] (force user-provided synchronization)\n");
325 printf(" [-w] Wait for dequeuer to empty stack\n");
329 int main(int argc
, char **argv
)
332 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
334 unsigned long long *count_enqueuer
, *count_dequeuer
;
335 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
336 unsigned long long tot_successful_enqueues
= 0,
337 tot_successful_dequeues
= 0,
338 tot_empty_dest_enqueues
= 0,
339 tot_pop_all
= 0, tot_pop_last
= 0;
340 unsigned long long end_dequeues
= 0;
341 int i
, a
, retval
= 0;
349 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
355 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
361 err
= sscanf(argv
[3], "%lu", &duration
);
367 for (i
= 4; i
< argc
; i
++) {
368 if (argv
[i
][0] != '-')
370 switch (argv
[i
][1]) {
377 cpu_affinities
[next_aff
++] = a
;
379 printf_verbose("Adding CPU %d affinity\n", a
);
386 rduration
= atol(argv
[++i
]);
393 wdelay
= atol(argv
[++i
]);
405 test_sync
= TEST_SYNC_MUTEX
;
416 /* activate pop_all test by default */
417 if (!test_pop
&& !test_pop_all
)
420 if (test_sync
== TEST_SYNC_NONE
&& nr_dequeuers
> 1 && test_pop
) {
421 if (test_force_sync
) {
422 fprintf(stderr
, "[WARNING] Using pop concurrently "
423 "with other pop or pop_all without external "
424 "synchronization. Expect run-time failure.\n");
426 printf("Enforcing mutex synchronization\n");
427 test_sync
= TEST_SYNC_MUTEX
;
431 printf_verbose("running test for %lu seconds, %u enqueuers, "
433 duration
, nr_enqueuers
, nr_dequeuers
);
435 printf_verbose("pop test activated.\n");
437 printf_verbose("pop_all test activated.\n");
438 if (test_sync
== TEST_SYNC_MUTEX
)
439 printf_verbose("External sync: mutex.\n");
441 printf_verbose("External sync: none.\n");
443 printf_verbose("Wait for dequeuers to empty stack.\n");
444 printf_verbose("Writer delay : %lu loops.\n", rduration
);
445 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
446 printf_verbose("thread %-6s, tid %lu\n",
447 "main", urcu_get_thread_id());
449 tid_enqueuer
= calloc(nr_enqueuers
, sizeof(*tid_enqueuer
));
450 tid_dequeuer
= calloc(nr_dequeuers
, sizeof(*tid_dequeuer
));
451 count_enqueuer
= calloc(nr_enqueuers
, 3 * sizeof(*count_enqueuer
));
452 count_dequeuer
= calloc(nr_dequeuers
, 4 * sizeof(*count_dequeuer
));
457 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
458 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
459 &count_enqueuer
[3 * i_thr
]);
463 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
464 err
= pthread_create(&tid_dequeuer
[i_thr
], NULL
, thr_dequeuer
,
465 &count_dequeuer
[4 * i_thr
]);
474 for (i_thr
= 0; i_thr
< duration
; i_thr
++) {
477 fwrite(".", sizeof(char), 1, stdout
);
482 test_stop_enqueue
= 1;
484 if (test_wait_empty
) {
485 while (nr_enqueuers
!= uatomic_read(&test_enqueue_stopped
)) {
488 while (!cds_wfs_empty(&s
)) {
493 test_stop_dequeue
= 1;
495 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
496 err
= pthread_join(tid_enqueuer
[i_thr
], &tret
);
499 tot_enqueues
+= count_enqueuer
[3 * i_thr
];
500 tot_successful_enqueues
+= count_enqueuer
[3 * i_thr
+ 1];
501 tot_empty_dest_enqueues
+= count_enqueuer
[3 * i_thr
+ 2];
503 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
504 err
= pthread_join(tid_dequeuer
[i_thr
], &tret
);
507 tot_dequeues
+= count_dequeuer
[4 * i_thr
];
508 tot_successful_dequeues
+= count_dequeuer
[4 * i_thr
+ 1];
509 tot_pop_all
+= count_dequeuer
[4 * i_thr
+ 2];
510 tot_pop_last
+= count_dequeuer
[4 * i_thr
+ 3];
513 test_end(&end_dequeues
, &tot_pop_last
);
515 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
516 tot_enqueues
, tot_dequeues
);
517 printf_verbose("total number of successful enqueues : %llu, "
518 "enqueues to empty dest : %llu, "
519 "successful dequeues %llu, "
520 "pop_all : %llu, pop_last : %llu\n",
521 tot_successful_enqueues
,
522 tot_empty_dest_enqueues
,
523 tot_successful_dequeues
,
524 tot_pop_all
, tot_pop_last
);
525 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
527 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
528 "successful enqueues %12llu enqueues to empty dest %12llu "
529 "successful dequeues %12llu pop_all %12llu "
530 "pop_last %llu end_dequeues %llu nr_ops %12llu\n",
531 argv
[0], duration
, nr_enqueuers
, wdelay
,
532 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
533 tot_successful_enqueues
,
534 tot_empty_dest_enqueues
,
535 tot_successful_dequeues
, tot_pop_all
, tot_pop_last
,
537 tot_enqueues
+ tot_dequeues
);
538 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
) {
539 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
540 "succ. dequeues + end dequeues %llu.\n",
541 tot_successful_enqueues
,
542 tot_successful_dequeues
+ end_dequeues
);
546 * The enqueuer should see exactly as many empty queues than the
547 * number of non-empty stacks dequeued.
549 if (tot_empty_dest_enqueues
!= tot_pop_last
) {
550 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
551 "number of pop last (%llu)\n",
552 tot_empty_dest_enqueues
,
558 free(count_enqueuer
);
559 free(count_dequeuer
);