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", (unsigned long) pthread_self(),
179 (unsigned long) gettid());
189 struct cds_wfcq_node
*node
= malloc(sizeof(*node
));
192 cds_wfcq_node_init(node
);
193 cds_wfcq_enqueue(&head
, &tail
, node
);
194 URCU_TLS(nr_successful_enqueues
)++;
196 if (caa_unlikely(wdelay
))
199 URCU_TLS(nr_enqueues
)++;
200 if (caa_unlikely(!test_duration_enqueue()))
204 count
[0] = URCU_TLS(nr_enqueues
);
205 count
[1] = URCU_TLS(nr_successful_enqueues
);
206 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
207 "enqueues %llu successful_enqueues %llu\n",
209 (unsigned long) gettid(),
210 URCU_TLS(nr_enqueues
), URCU_TLS(nr_successful_enqueues
));
215 static void do_test_dequeue(enum test_sync sync
)
217 struct cds_wfcq_node
*node
;
219 if (sync
== TEST_SYNC_MUTEX
)
220 node
= cds_wfcq_dequeue_blocking(&head
, &tail
);
222 node
= __cds_wfcq_dequeue_blocking(&head
, &tail
);
226 URCU_TLS(nr_successful_dequeues
)++;
228 URCU_TLS(nr_dequeues
)++;
231 static void do_test_splice(enum test_sync sync
)
233 struct cds_wfcq_head tmp_head
;
234 struct cds_wfcq_tail tmp_tail
;
235 struct cds_wfcq_node
*node
, *n
;
237 cds_wfcq_init(&tmp_head
, &tmp_tail
);
239 if (sync
== TEST_SYNC_MUTEX
)
240 cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
243 __cds_wfcq_splice_blocking(&tmp_head
, &tmp_tail
,
246 __cds_wfcq_for_each_blocking_safe(&tmp_head
, &tmp_tail
, node
, n
) {
248 URCU_TLS(nr_successful_dequeues
)++;
249 URCU_TLS(nr_dequeues
)++;
253 static void *thr_dequeuer(void *_count
)
255 unsigned long long *count
= _count
;
256 unsigned int counter
;
258 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
259 "dequeuer", (unsigned long) pthread_self(),
260 (unsigned long) gettid());
270 if (test_dequeue
&& test_splice
) {
272 do_test_dequeue(test_sync
);
274 do_test_splice(test_sync
);
278 do_test_dequeue(test_sync
);
280 do_test_splice(test_sync
);
282 if (caa_unlikely(!test_duration_dequeue()))
284 if (caa_unlikely(rduration
))
285 loop_sleep(rduration
);
288 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
289 "dequeues %llu, successful_dequeues %llu\n",
291 (unsigned long) gettid(),
292 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
));
293 count
[0] = URCU_TLS(nr_dequeues
);
294 count
[1] = URCU_TLS(nr_successful_dequeues
);
298 static void test_end(unsigned long long *nr_dequeues
)
300 struct cds_wfcq_node
*node
;
303 node
= cds_wfcq_dequeue_blocking(&head
, &tail
);
311 static void show_usage(int argc
, char **argv
)
313 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv
[0]);
314 printf(" [-d delay] (enqueuer period (in loops))");
315 printf(" [-c duration] (dequeuer period (in loops))");
316 printf(" [-v] (verbose output)");
317 printf(" [-a cpu#] [-a cpu#]... (affinity)");
318 printf(" [-q] (test dequeue)");
319 printf(" [-s] (test splice, enabled by default)");
320 printf(" [-M] (use mutex external synchronization)");
321 printf(" [-0] (use no external synchronization)");
322 printf(" Note: default: mutex external synchronization used.");
326 int main(int argc
, char **argv
)
329 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
331 unsigned long long *count_enqueuer
, *count_dequeuer
;
332 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
333 unsigned long long tot_successful_enqueues
= 0,
334 tot_successful_dequeues
= 0;
335 unsigned long long end_dequeues
= 0;
339 show_usage(argc
, argv
);
343 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
345 show_usage(argc
, argv
);
349 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
351 show_usage(argc
, argv
);
355 err
= sscanf(argv
[3], "%lu", &duration
);
357 show_usage(argc
, argv
);
361 for (i
= 4; i
< argc
; i
++) {
362 if (argv
[i
][0] != '-')
364 switch (argv
[i
][1]) {
367 show_usage(argc
, argv
);
371 cpu_affinities
[next_aff
++] = a
;
373 printf_verbose("Adding CPU %d affinity\n", a
);
377 show_usage(argc
, argv
);
380 rduration
= atol(argv
[++i
]);
384 show_usage(argc
, argv
);
387 wdelay
= atol(argv
[++i
]);
399 test_sync
= TEST_SYNC_MUTEX
;
402 test_sync
= TEST_SYNC_NONE
;
407 /* activate splice test by default */
408 if (!test_dequeue
&& !test_splice
)
411 printf_verbose("running test for %lu seconds, %u enqueuers, "
413 duration
, nr_enqueuers
, nr_dequeuers
);
415 printf_verbose("dequeue test activated.\n");
417 printf_verbose("splice test activated.\n");
418 if (test_sync
== TEST_SYNC_MUTEX
)
419 printf_verbose("External sync: mutex.\n");
421 printf_verbose("External sync: none.\n");
422 printf_verbose("Writer delay : %lu loops.\n", rduration
);
423 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
424 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
425 "main", (unsigned long) pthread_self(),
426 (unsigned long) gettid());
428 tid_enqueuer
= malloc(sizeof(*tid_enqueuer
) * nr_enqueuers
);
429 tid_dequeuer
= malloc(sizeof(*tid_dequeuer
) * nr_dequeuers
);
430 count_enqueuer
= malloc(2 * sizeof(*count_enqueuer
) * nr_enqueuers
);
431 count_dequeuer
= malloc(2 * sizeof(*count_dequeuer
) * nr_dequeuers
);
432 cds_wfcq_init(&head
, &tail
);
436 for (i
= 0; i
< nr_enqueuers
; i
++) {
437 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
438 &count_enqueuer
[2 * i
]);
442 for (i
= 0; i
< nr_dequeuers
; i
++) {
443 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
444 &count_dequeuer
[2 * i
]);
453 for (i
= 0; i
< duration
; i
++) {
461 for (i
= 0; i
< nr_enqueuers
; i
++) {
462 err
= pthread_join(tid_enqueuer
[i
], &tret
);
465 tot_enqueues
+= count_enqueuer
[2 * i
];
466 tot_successful_enqueues
+= count_enqueuer
[2 * i
+ 1];
468 for (i
= 0; i
< nr_dequeuers
; i
++) {
469 err
= pthread_join(tid_dequeuer
[i
], &tret
);
472 tot_dequeues
+= count_dequeuer
[2 * i
];
473 tot_successful_dequeues
+= count_dequeuer
[2 * i
+ 1];
476 test_end(&end_dequeues
);
478 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
479 tot_enqueues
, tot_dequeues
);
480 printf_verbose("total number of successful enqueues : %llu, "
481 "successful dequeues %llu\n",
482 tot_successful_enqueues
, tot_successful_dequeues
);
483 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
485 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
486 "successful enqueues %12llu successful dequeues %12llu "
487 "end_dequeues %llu nr_ops %12llu\n",
488 argv
[0], duration
, nr_enqueuers
, wdelay
,
489 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
490 tot_successful_enqueues
,
491 tot_successful_dequeues
, end_dequeues
,
492 tot_enqueues
+ tot_dequeues
);
493 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
)
494 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
495 "succ. dequeues + end dequeues %llu.\n",
496 tot_successful_enqueues
,
497 tot_successful_dequeues
+ end_dequeues
);
499 free(count_enqueuer
);
500 free(count_dequeuer
);