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
;
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
;
97 #define printf_verbose(fmt, args...) \
100 printf(fmt, ## args); \
103 static unsigned int cpu_affinities
[NR_CPUS
];
104 static unsigned int next_aff
= 0;
105 static int use_affinity
= 0;
107 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
109 #ifndef HAVE_CPU_SET_T
110 typedef unsigned long cpu_set_t
;
111 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
112 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
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)
156 static int test_duration_enqueue(void)
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
);
167 static unsigned int nr_enqueuers
;
168 static unsigned int nr_dequeuers
;
170 static struct cds_wfcq_head
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) head
;
171 static struct cds_wfcq_tail
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) tail
;
173 static void *thr_enqueuer(void *_count
)
175 unsigned long long *count
= _count
;
177 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
178 "enqueuer", pthread_self(), (unsigned long)gettid());
188 struct cds_wfcq_node
*node
= malloc(sizeof(*node
));
191 cds_wfcq_node_init(node
);
192 cds_wfcq_enqueue(&head
, &tail
, node
);
193 URCU_TLS(nr_successful_enqueues
)++;
195 if (caa_unlikely(wdelay
))
198 URCU_TLS(nr_enqueues
)++;
199 if (caa_unlikely(!test_duration_enqueue()))
203 count
[0] = URCU_TLS(nr_enqueues
);
204 count
[1] = URCU_TLS(nr_successful_enqueues
);
205 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
206 "enqueues %llu successful_enqueues %llu\n",
207 pthread_self(), (unsigned long)gettid(),
208 URCU_TLS(nr_enqueues
), URCU_TLS(nr_successful_enqueues
));
213 static void do_test_dequeue(enum test_sync sync
)
215 struct cds_wfcq_node
*node
;
217 if (sync
== TEST_SYNC_MUTEX
)
218 node
= cds_wfcq_dequeue_blocking(&head
, &tail
);
220 node
= __cds_wfcq_dequeue_blocking(&head
, &tail
);
224 URCU_TLS(nr_successful_dequeues
)++;
226 URCU_TLS(nr_dequeues
)++;
229 static void do_test_splice(enum test_sync sync
)
231 struct cds_wfcq_head tmp_head
;
232 struct cds_wfcq_tail tmp_tail
;
233 struct cds_wfcq_node
*node
, *n
;
235 cds_wfcq_init(&tmp_head
, &tmp_tail
);
237 if (sync
== TEST_SYNC_MUTEX
)
238 cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
241 __cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
244 __cds_wfcq_for_each_blocking_safe(&tmp_head
, &tmp_tail
, node
, n
) {
246 URCU_TLS(nr_successful_dequeues
)++;
247 URCU_TLS(nr_dequeues
)++;
251 static void *thr_dequeuer(void *_count
)
253 unsigned long long *count
= _count
;
254 unsigned int counter
;
256 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
257 "dequeuer", pthread_self(), (unsigned long)gettid());
267 if (test_dequeue
&& test_splice
) {
269 do_test_dequeue(test_sync
);
271 do_test_splice(test_sync
);
275 do_test_dequeue(test_sync
);
277 do_test_splice(test_sync
);
279 if (caa_unlikely(!test_duration_dequeue()))
281 if (caa_unlikely(rduration
))
282 loop_sleep(rduration
);
285 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
286 "dequeues %llu, successful_dequeues %llu\n",
287 pthread_self(), (unsigned long)gettid(),
288 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
));
289 count
[0] = URCU_TLS(nr_dequeues
);
290 count
[1] = URCU_TLS(nr_successful_dequeues
);
294 static void test_end(unsigned long long *nr_dequeues
)
296 struct cds_wfcq_node
*node
;
299 node
= cds_wfcq_dequeue_blocking(&head
, &tail
);
307 static void show_usage(int argc
, char **argv
)
309 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv
[0]);
310 printf(" [-d delay] (enqueuer period (in loops))");
311 printf(" [-c duration] (dequeuer period (in loops))");
312 printf(" [-v] (verbose output)");
313 printf(" [-a cpu#] [-a cpu#]... (affinity)");
314 printf(" [-q] (test dequeue)");
315 printf(" [-s] (test splice, enabled by default)");
316 printf(" [-M] (use mutex external synchronization)");
317 printf(" [-0] (use no external synchronization)");
318 printf(" Note: default: mutex external synchronization used.");
322 int main(int argc
, char **argv
)
325 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
327 unsigned long long *count_enqueuer
, *count_dequeuer
;
328 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
329 unsigned long long tot_successful_enqueues
= 0,
330 tot_successful_dequeues
= 0;
331 unsigned long long end_dequeues
= 0;
335 show_usage(argc
, argv
);
339 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
341 show_usage(argc
, argv
);
345 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
347 show_usage(argc
, argv
);
351 err
= sscanf(argv
[3], "%lu", &duration
);
353 show_usage(argc
, argv
);
357 for (i
= 4; i
< argc
; i
++) {
358 if (argv
[i
][0] != '-')
360 switch (argv
[i
][1]) {
363 show_usage(argc
, argv
);
367 cpu_affinities
[next_aff
++] = a
;
369 printf_verbose("Adding CPU %d affinity\n", a
);
373 show_usage(argc
, argv
);
376 rduration
= atol(argv
[++i
]);
380 show_usage(argc
, argv
);
383 wdelay
= atol(argv
[++i
]);
395 test_sync
= TEST_SYNC_MUTEX
;
398 test_sync
= TEST_SYNC_NONE
;
403 /* activate splice test by default */
404 if (!test_dequeue
&& !test_splice
)
407 printf_verbose("running test for %lu seconds, %u enqueuers, "
409 duration
, nr_enqueuers
, nr_dequeuers
);
411 printf_verbose("dequeue test activated.\n");
413 printf_verbose("splice test activated.\n");
414 if (test_sync
== TEST_SYNC_MUTEX
)
415 printf_verbose("External sync: mutex.\n");
417 printf_verbose("External sync: none.\n");
418 printf_verbose("Writer delay : %lu loops.\n", rduration
);
419 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
420 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
421 "main", pthread_self(), (unsigned long)gettid());
423 tid_enqueuer
= malloc(sizeof(*tid_enqueuer
) * nr_enqueuers
);
424 tid_dequeuer
= malloc(sizeof(*tid_dequeuer
) * nr_dequeuers
);
425 count_enqueuer
= malloc(2 * sizeof(*count_enqueuer
) * nr_enqueuers
);
426 count_dequeuer
= malloc(2 * sizeof(*count_dequeuer
) * nr_dequeuers
);
427 cds_wfcq_init(&head
, &tail
);
431 for (i
= 0; i
< nr_enqueuers
; i
++) {
432 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
433 &count_enqueuer
[2 * i
]);
437 for (i
= 0; i
< nr_dequeuers
; i
++) {
438 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
439 &count_dequeuer
[2 * i
]);
448 for (i
= 0; i
< duration
; i
++) {
456 for (i
= 0; i
< nr_enqueuers
; i
++) {
457 err
= pthread_join(tid_enqueuer
[i
], &tret
);
460 tot_enqueues
+= count_enqueuer
[2 * i
];
461 tot_successful_enqueues
+= count_enqueuer
[2 * i
+ 1];
463 for (i
= 0; i
< nr_dequeuers
; i
++) {
464 err
= pthread_join(tid_dequeuer
[i
], &tret
);
467 tot_dequeues
+= count_dequeuer
[2 * i
];
468 tot_successful_dequeues
+= count_dequeuer
[2 * i
+ 1];
471 test_end(&end_dequeues
);
473 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
474 tot_enqueues
, tot_dequeues
);
475 printf_verbose("total number of successful enqueues : %llu, "
476 "successful dequeues %llu\n",
477 tot_successful_enqueues
, tot_successful_dequeues
);
478 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
480 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
481 "successful enqueues %12llu successful dequeues %12llu "
482 "end_dequeues %llu nr_ops %12llu\n",
483 argv
[0], duration
, nr_enqueuers
, wdelay
,
484 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
485 tot_successful_enqueues
,
486 tot_successful_dequeues
, end_dequeues
,
487 tot_enqueues
+ tot_dequeues
);
488 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
)
489 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
490 "succ. dequeues + end dequeues %llu.\n",
491 tot_successful_enqueues
,
492 tot_successful_dequeues
+ end_dequeues
);
494 free(count_enqueuer
);
495 free(count_dequeuer
);