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>
37 #include <urcu/arch.h>
38 #include <urcu/tls-compat.h>
39 #include <urcu/uatomic.h>
41 #include "thread-id.h"
43 /* hardcoded number of CPUs */
46 #ifndef DYNAMIC_LINK_TEST
49 #include <urcu/wfstack.h>
52 * External synchronization used.
59 static enum test_sync test_sync
;
61 static int test_force_sync
;
63 static volatile int test_go
, test_stop_enqueue
, test_stop_dequeue
;
65 static unsigned long rduration
;
67 static unsigned long duration
;
69 /* read-side C.S. duration, in loops */
70 static unsigned long wdelay
;
72 static inline void loop_sleep(unsigned long loops
)
78 static int verbose_mode
;
80 static int test_pop
, test_pop_all
, test_wait_empty
;
81 static unsigned int test_enqueue_stopped
;
83 #define printf_verbose(fmt, args...) \
86 printf(fmt, ## args); \
89 static unsigned int cpu_affinities
[NR_CPUS
];
90 static unsigned int next_aff
= 0;
91 static int use_affinity
= 0;
93 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
95 static void set_affinity(void)
97 #ifdef HAVE_SCHED_SETAFFINITY
100 #endif /* HAVE_SCHED_SETAFFINITY */
105 #ifdef HAVE_SCHED_SETAFFINITY
106 ret
= pthread_mutex_lock(&affinity_mutex
);
108 perror("Error in pthread mutex lock");
111 cpu
= cpu_affinities
[next_aff
++];
112 ret
= pthread_mutex_unlock(&affinity_mutex
);
114 perror("Error in pthread mutex unlock");
120 #if SCHED_SETAFFINITY_ARGS == 2
121 sched_setaffinity(0, &mask
);
123 sched_setaffinity(0, sizeof(mask
), &mask
);
125 #endif /* HAVE_SCHED_SETAFFINITY */
129 * returns 0 if test should end.
131 static int test_duration_dequeue(void)
133 return !test_stop_dequeue
;
136 static int test_duration_enqueue(void)
138 return !test_stop_enqueue
;
141 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
142 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
144 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
145 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
146 static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues
);
147 static DEFINE_URCU_TLS(unsigned long long, nr_pop_all
);
148 static DEFINE_URCU_TLS(unsigned long long, nr_pop_last
);
150 static unsigned int nr_enqueuers
;
151 static unsigned int nr_dequeuers
;
153 static struct cds_wfs_stack s
;
155 static void *thr_enqueuer(void *_count
)
157 unsigned long long *count
= _count
;
160 printf_verbose("thread_begin %s, tid %lu\n",
161 "enqueuer", urcu_get_thread_id());
171 struct cds_wfs_node
*node
= malloc(sizeof(*node
));
174 cds_wfs_node_init(node
);
175 was_nonempty
= cds_wfs_push(&s
, node
);
176 URCU_TLS(nr_successful_enqueues
)++;
178 URCU_TLS(nr_empty_dest_enqueues
)++;
180 if (caa_unlikely(wdelay
))
183 URCU_TLS(nr_enqueues
)++;
184 if (caa_unlikely(!test_duration_enqueue()))
188 uatomic_inc(&test_enqueue_stopped
);
189 count
[0] = URCU_TLS(nr_enqueues
);
190 count
[1] = URCU_TLS(nr_successful_enqueues
);
191 count
[2] = URCU_TLS(nr_empty_dest_enqueues
);
192 printf_verbose("enqueuer thread_end, tid %lu, "
193 "enqueues %llu successful_enqueues %llu, "
194 "empty_dest_enqueues %llu\n",
195 urcu_get_thread_id(),
196 URCU_TLS(nr_enqueues
),
197 URCU_TLS(nr_successful_enqueues
),
198 URCU_TLS(nr_empty_dest_enqueues
));
203 static void do_test_pop(enum test_sync sync
)
205 struct cds_wfs_node
*node
;
208 if (sync
== TEST_SYNC_MUTEX
)
209 cds_wfs_pop_lock(&s
);
210 node
= __cds_wfs_pop_with_state_blocking(&s
, &state
);
211 if (sync
== TEST_SYNC_MUTEX
)
212 cds_wfs_pop_unlock(&s
);
215 if (state
& CDS_WFS_STATE_LAST
)
216 URCU_TLS(nr_pop_last
)++;
218 URCU_TLS(nr_successful_dequeues
)++;
220 URCU_TLS(nr_dequeues
)++;
223 static void do_test_pop_all(enum test_sync sync
)
225 struct cds_wfs_head
*head
;
226 struct cds_wfs_node
*node
, *n
;
228 if (sync
== TEST_SYNC_MUTEX
)
229 cds_wfs_pop_lock(&s
);
230 head
= __cds_wfs_pop_all(&s
);
231 if (sync
== TEST_SYNC_MUTEX
)
232 cds_wfs_pop_unlock(&s
);
235 if (cds_wfs_first(head
) == NULL
)
238 URCU_TLS(nr_pop_all
)++;
239 URCU_TLS(nr_pop_last
)++;
241 cds_wfs_for_each_blocking_safe(head
, node
, n
) {
243 URCU_TLS(nr_successful_dequeues
)++;
244 URCU_TLS(nr_dequeues
)++;
248 static void *thr_dequeuer(void *_count
)
250 unsigned long long *count
= _count
;
251 unsigned int counter
= 0;
253 printf_verbose("thread_begin %s, tid %lu\n",
254 "dequeuer", urcu_get_thread_id());
263 assert(test_pop
|| test_pop_all
);
266 if (test_pop
&& test_pop_all
) {
268 do_test_pop(test_sync
);
270 do_test_pop_all(test_sync
);
274 do_test_pop(test_sync
);
276 do_test_pop_all(test_sync
);
279 if (caa_unlikely(!test_duration_dequeue()))
281 if (caa_unlikely(rduration
))
282 loop_sleep(rduration
);
285 printf_verbose("dequeuer thread_end, tid %lu, "
286 "dequeues %llu, successful_dequeues %llu "
287 "pop_all %llu pop_last %llu\n",
288 urcu_get_thread_id(),
289 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
),
290 URCU_TLS(nr_pop_all
),
291 URCU_TLS(nr_pop_last
));
292 count
[0] = URCU_TLS(nr_dequeues
);
293 count
[1] = URCU_TLS(nr_successful_dequeues
);
294 count
[2] = URCU_TLS(nr_pop_all
);
295 count
[3] = URCU_TLS(nr_pop_last
);
299 static void test_end(unsigned long long *nr_dequeues_l
,
300 unsigned long long *nr_pop_last_l
)
302 struct cds_wfs_node
*node
;
306 node
= cds_wfs_pop_with_state_blocking(&s
, &state
);
308 if (state
& CDS_WFS_STATE_LAST
)
316 static void show_usage(char **argv
)
318 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
320 printf("OPTIONS:\n");
321 printf(" [-d delay] (enqueuer period (in loops))\n");
322 printf(" [-c duration] (dequeuer period (in loops))\n");
323 printf(" [-v] (verbose output)\n");
324 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
325 printf(" [-p] (test pop)\n");
326 printf(" [-P] (test pop_all, enabled by default)\n");
327 printf(" [-M] (use mutex external synchronization)\n");
328 printf(" Note: default: no external synchronization used.\n");
329 printf(" [-f] (force user-provided synchronization)\n");
330 printf(" [-w] Wait for dequeuer to empty stack\n");
334 int main(int argc
, char **argv
)
337 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
339 unsigned long long *count_enqueuer
, *count_dequeuer
;
340 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
341 unsigned long long tot_successful_enqueues
= 0,
342 tot_successful_dequeues
= 0,
343 tot_empty_dest_enqueues
= 0,
344 tot_pop_all
= 0, tot_pop_last
= 0;
345 unsigned long long end_dequeues
= 0;
346 int i
, a
, retval
= 0;
354 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
360 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
366 err
= sscanf(argv
[3], "%lu", &duration
);
372 for (i
= 4; i
< argc
; i
++) {
373 if (argv
[i
][0] != '-')
375 switch (argv
[i
][1]) {
382 cpu_affinities
[next_aff
++] = a
;
384 printf_verbose("Adding CPU %d affinity\n", a
);
391 rduration
= atol(argv
[++i
]);
398 wdelay
= atol(argv
[++i
]);
410 test_sync
= TEST_SYNC_MUTEX
;
421 /* activate pop_all test by default */
422 if (!test_pop
&& !test_pop_all
)
425 if (test_sync
== TEST_SYNC_NONE
&& nr_dequeuers
> 1 && test_pop
) {
426 if (test_force_sync
) {
427 fprintf(stderr
, "[WARNING] Using pop concurrently "
428 "with other pop or pop_all without external "
429 "synchronization. Expect run-time failure.\n");
431 printf("Enforcing mutex synchronization\n");
432 test_sync
= TEST_SYNC_MUTEX
;
436 printf_verbose("running test for %lu seconds, %u enqueuers, "
438 duration
, nr_enqueuers
, nr_dequeuers
);
440 printf_verbose("pop test activated.\n");
442 printf_verbose("pop_all test activated.\n");
443 if (test_sync
== TEST_SYNC_MUTEX
)
444 printf_verbose("External sync: mutex.\n");
446 printf_verbose("External sync: none.\n");
448 printf_verbose("Wait for dequeuers to empty stack.\n");
449 printf_verbose("Writer delay : %lu loops.\n", rduration
);
450 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
451 printf_verbose("thread %-6s, tid %lu\n",
452 "main", urcu_get_thread_id());
454 tid_enqueuer
= calloc(nr_enqueuers
, sizeof(*tid_enqueuer
));
455 tid_dequeuer
= calloc(nr_dequeuers
, sizeof(*tid_dequeuer
));
456 count_enqueuer
= calloc(nr_enqueuers
, 3 * sizeof(*count_enqueuer
));
457 count_dequeuer
= calloc(nr_dequeuers
, 4 * sizeof(*count_dequeuer
));
462 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
463 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
464 &count_enqueuer
[3 * i_thr
]);
468 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
469 err
= pthread_create(&tid_dequeuer
[i_thr
], NULL
, thr_dequeuer
,
470 &count_dequeuer
[4 * i_thr
]);
479 for (i_thr
= 0; i_thr
< duration
; i_thr
++) {
482 fwrite(".", sizeof(char), 1, stdout
);
487 test_stop_enqueue
= 1;
489 if (test_wait_empty
) {
490 while (nr_enqueuers
!= uatomic_read(&test_enqueue_stopped
)) {
493 while (!cds_wfs_empty(&s
)) {
498 test_stop_dequeue
= 1;
500 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
501 err
= pthread_join(tid_enqueuer
[i_thr
], &tret
);
504 tot_enqueues
+= count_enqueuer
[3 * i_thr
];
505 tot_successful_enqueues
+= count_enqueuer
[3 * i_thr
+ 1];
506 tot_empty_dest_enqueues
+= count_enqueuer
[3 * i_thr
+ 2];
508 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
509 err
= pthread_join(tid_dequeuer
[i_thr
], &tret
);
512 tot_dequeues
+= count_dequeuer
[4 * i_thr
];
513 tot_successful_dequeues
+= count_dequeuer
[4 * i_thr
+ 1];
514 tot_pop_all
+= count_dequeuer
[4 * i_thr
+ 2];
515 tot_pop_last
+= count_dequeuer
[4 * i_thr
+ 3];
518 test_end(&end_dequeues
, &tot_pop_last
);
520 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
521 tot_enqueues
, tot_dequeues
);
522 printf_verbose("total number of successful enqueues : %llu, "
523 "enqueues to empty dest : %llu, "
524 "successful dequeues %llu, "
525 "pop_all : %llu, pop_last : %llu\n",
526 tot_successful_enqueues
,
527 tot_empty_dest_enqueues
,
528 tot_successful_dequeues
,
529 tot_pop_all
, tot_pop_last
);
530 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
532 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
533 "successful enqueues %12llu enqueues to empty dest %12llu "
534 "successful dequeues %12llu pop_all %12llu "
535 "pop_last %llu end_dequeues %llu nr_ops %12llu\n",
536 argv
[0], duration
, nr_enqueuers
, wdelay
,
537 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
538 tot_successful_enqueues
,
539 tot_empty_dest_enqueues
,
540 tot_successful_dequeues
, tot_pop_all
, tot_pop_last
,
542 tot_enqueues
+ tot_dequeues
);
543 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
) {
544 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
545 "succ. dequeues + end dequeues %llu.\n",
546 tot_successful_enqueues
,
547 tot_successful_dequeues
+ end_dequeues
);
551 * The enqueuer should see exactly as many empty queues than the
552 * number of non-empty stacks dequeued.
554 if (tot_empty_dest_enqueues
!= tot_pop_last
) {
555 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
556 "number of pop last (%llu)\n",
557 tot_empty_dest_enqueues
,
563 free(count_enqueuer
);
564 free(count_dequeuer
);