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>
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/wfstack.h>
72 * External synchronization used.
79 static enum test_sync test_sync
;
81 static int test_force_sync
;
83 static volatile int test_go
, test_stop
;
85 static unsigned long rduration
;
87 static unsigned long duration
;
89 /* read-side C.S. duration, in loops */
90 static unsigned long wdelay
;
92 static inline void loop_sleep(unsigned long loops
)
98 static int verbose_mode
;
100 static int test_pop
, test_pop_all
;
102 #define printf_verbose(fmt, args...) \
105 printf(fmt, ## args); \
108 static unsigned int cpu_affinities
[NR_CPUS
];
109 static unsigned int next_aff
= 0;
110 static int use_affinity
= 0;
112 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
114 #ifndef HAVE_CPU_SET_T
115 typedef unsigned long cpu_set_t
;
116 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
117 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
120 static void set_affinity(void)
122 #if HAVE_SCHED_SETAFFINITY
125 #endif /* HAVE_SCHED_SETAFFINITY */
130 #if HAVE_SCHED_SETAFFINITY
131 ret
= pthread_mutex_lock(&affinity_mutex
);
133 perror("Error in pthread mutex lock");
136 cpu
= cpu_affinities
[next_aff
++];
137 ret
= pthread_mutex_unlock(&affinity_mutex
);
139 perror("Error in pthread mutex unlock");
145 #if SCHED_SETAFFINITY_ARGS == 2
146 sched_setaffinity(0, &mask
);
148 sched_setaffinity(0, sizeof(mask
), &mask
);
150 #endif /* HAVE_SCHED_SETAFFINITY */
154 * returns 0 if test should end.
156 static int test_duration_dequeue(void)
161 static int test_duration_enqueue(void)
166 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
167 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
169 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
170 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
172 static unsigned int nr_enqueuers
;
173 static unsigned int nr_dequeuers
;
175 static struct cds_wfs_stack s
;
177 static void *thr_enqueuer(void *_count
)
179 unsigned long long *count
= _count
;
181 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
182 "enqueuer", (unsigned long) pthread_self(),
183 (unsigned long) gettid());
193 struct cds_wfs_node
*node
= malloc(sizeof(*node
));
196 cds_wfs_node_init(node
);
197 cds_wfs_push(&s
, node
);
198 URCU_TLS(nr_successful_enqueues
)++;
200 if (caa_unlikely(wdelay
))
203 URCU_TLS(nr_enqueues
)++;
204 if (caa_unlikely(!test_duration_enqueue()))
208 count
[0] = URCU_TLS(nr_enqueues
);
209 count
[1] = URCU_TLS(nr_successful_enqueues
);
210 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
211 "enqueues %llu successful_enqueues %llu\n",
213 (unsigned long) gettid(),
214 URCU_TLS(nr_enqueues
), URCU_TLS(nr_successful_enqueues
));
219 static void do_test_pop(enum test_sync sync
)
221 struct cds_wfs_node
*node
;
223 if (sync
== TEST_SYNC_MUTEX
)
224 cds_wfs_pop_lock(&s
);
225 node
= __cds_wfs_pop_blocking(&s
);
226 if (sync
== TEST_SYNC_MUTEX
)
227 cds_wfs_pop_unlock(&s
);
231 URCU_TLS(nr_successful_dequeues
)++;
233 URCU_TLS(nr_dequeues
)++;
236 static void do_test_pop_all(enum test_sync sync
)
238 struct cds_wfs_head
*head
;
239 struct cds_wfs_node
*node
, *n
;
241 if (sync
== TEST_SYNC_MUTEX
)
242 cds_wfs_pop_lock(&s
);
243 head
= __cds_wfs_pop_all(&s
);
244 if (sync
== TEST_SYNC_MUTEX
)
245 cds_wfs_pop_unlock(&s
);
247 cds_wfs_for_each_blocking_safe(head
, node
, n
) {
249 URCU_TLS(nr_successful_dequeues
)++;
250 URCU_TLS(nr_dequeues
)++;
254 static void *thr_dequeuer(void *_count
)
256 unsigned long long *count
= _count
;
257 unsigned int counter
;
259 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
260 "dequeuer", (unsigned long) pthread_self(),
261 (unsigned long) gettid());
270 assert(test_pop
|| test_pop_all
);
273 if (test_pop
&& test_pop_all
) {
275 do_test_pop(test_sync
);
277 do_test_pop_all(test_sync
);
281 do_test_pop(test_sync
);
283 do_test_pop_all(test_sync
);
286 if (caa_unlikely(!test_duration_dequeue()))
288 if (caa_unlikely(rduration
))
289 loop_sleep(rduration
);
292 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
293 "dequeues %llu, successful_dequeues %llu\n",
295 (unsigned long) gettid(),
296 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
));
297 count
[0] = URCU_TLS(nr_dequeues
);
298 count
[1] = URCU_TLS(nr_successful_dequeues
);
302 static void test_end(struct cds_wfs_stack
*s
, unsigned long long *nr_dequeues
)
304 struct cds_wfs_node
*node
;
307 node
= cds_wfs_pop_blocking(s
);
315 static void show_usage(int argc
, char **argv
)
317 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv
[0]);
318 printf(" [-d delay] (enqueuer period (in loops))");
319 printf(" [-c duration] (dequeuer period (in loops))");
320 printf(" [-v] (verbose output)");
321 printf(" [-a cpu#] [-a cpu#]... (affinity)");
322 printf(" [-p] (test pop)");
323 printf(" [-P] (test pop_all, enabled by default)");
324 printf(" [-M] (use mutex external synchronization)");
325 printf(" Note: default: no external synchronization used.");
326 printf(" [-f] (force user-provided synchronization)");
330 int main(int argc
, char **argv
)
333 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
335 unsigned long long *count_enqueuer
, *count_dequeuer
;
336 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
337 unsigned long long tot_successful_enqueues
= 0,
338 tot_successful_dequeues
= 0;
339 unsigned long long end_dequeues
= 0;
343 show_usage(argc
, argv
);
347 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
349 show_usage(argc
, argv
);
353 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
355 show_usage(argc
, argv
);
359 err
= sscanf(argv
[3], "%lu", &duration
);
361 show_usage(argc
, argv
);
365 for (i
= 4; i
< argc
; i
++) {
366 if (argv
[i
][0] != '-')
368 switch (argv
[i
][1]) {
371 show_usage(argc
, argv
);
375 cpu_affinities
[next_aff
++] = a
;
377 printf_verbose("Adding CPU %d affinity\n", a
);
381 show_usage(argc
, argv
);
384 rduration
= atol(argv
[++i
]);
388 show_usage(argc
, argv
);
391 wdelay
= atol(argv
[++i
]);
403 test_sync
= TEST_SYNC_MUTEX
;
411 /* activate pop_all test by default */
412 if (!test_pop
&& !test_pop_all
)
415 if (test_sync
== TEST_SYNC_NONE
&& nr_dequeuers
> 1 && test_pop
) {
416 if (test_force_sync
) {
417 fprintf(stderr
, "[WARNING] Using pop concurrently "
418 "with other pop or pop_all without external "
419 "synchronization. Expect run-time failure.\n");
421 printf("Enforcing mutex synchronization\n");
422 test_sync
= TEST_SYNC_MUTEX
;
426 printf_verbose("running test for %lu seconds, %u enqueuers, "
428 duration
, nr_enqueuers
, nr_dequeuers
);
430 printf_verbose("pop test activated.\n");
432 printf_verbose("pop_all test activated.\n");
433 if (test_sync
== TEST_SYNC_MUTEX
)
434 printf_verbose("External sync: mutex.\n");
436 printf_verbose("External sync: none.\n");
437 printf_verbose("Writer delay : %lu loops.\n", rduration
);
438 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
439 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
440 "main", (unsigned long) pthread_self(),
441 (unsigned long) gettid());
443 tid_enqueuer
= malloc(sizeof(*tid_enqueuer
) * nr_enqueuers
);
444 tid_dequeuer
= malloc(sizeof(*tid_dequeuer
) * nr_dequeuers
);
445 count_enqueuer
= malloc(2 * sizeof(*count_enqueuer
) * nr_enqueuers
);
446 count_dequeuer
= malloc(2 * sizeof(*count_dequeuer
) * nr_dequeuers
);
451 for (i
= 0; i
< nr_enqueuers
; i
++) {
452 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
453 &count_enqueuer
[2 * i
]);
457 for (i
= 0; i
< nr_dequeuers
; i
++) {
458 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
459 &count_dequeuer
[2 * i
]);
468 for (i
= 0; i
< duration
; i
++) {
476 for (i
= 0; i
< nr_enqueuers
; i
++) {
477 err
= pthread_join(tid_enqueuer
[i
], &tret
);
480 tot_enqueues
+= count_enqueuer
[2 * i
];
481 tot_successful_enqueues
+= count_enqueuer
[2 * i
+ 1];
483 for (i
= 0; i
< nr_dequeuers
; i
++) {
484 err
= pthread_join(tid_dequeuer
[i
], &tret
);
487 tot_dequeues
+= count_dequeuer
[2 * i
];
488 tot_successful_dequeues
+= count_dequeuer
[2 * i
+ 1];
491 test_end(&s
, &end_dequeues
);
493 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
494 tot_enqueues
, tot_dequeues
);
495 printf_verbose("total number of successful enqueues : %llu, "
496 "successful dequeues %llu\n",
497 tot_successful_enqueues
, tot_successful_dequeues
);
498 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
500 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
501 "successful enqueues %12llu successful dequeues %12llu "
502 "end_dequeues %llu nr_ops %12llu\n",
503 argv
[0], duration
, nr_enqueuers
, wdelay
,
504 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
505 tot_successful_enqueues
,
506 tot_successful_dequeues
, end_dequeues
,
507 tot_enqueues
+ tot_dequeues
);
508 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
)
509 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
510 "succ. dequeues + end dequeues %llu.\n",
511 tot_successful_enqueues
,
512 tot_successful_dequeues
+ end_dequeues
);
514 free(count_enqueuer
);
515 free(count_dequeuer
);