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 volatile int test_go
, test_stop
;
83 static unsigned long rduration
;
85 static unsigned long duration
;
87 /* read-side C.S. duration, in loops */
88 static unsigned long wdelay
;
90 static inline void loop_sleep(unsigned long loops
)
96 static int verbose_mode
;
98 static int test_pop
, test_pop_all
;
100 #define printf_verbose(fmt, args...) \
103 printf(fmt, ## args); \
106 static unsigned int cpu_affinities
[NR_CPUS
];
107 static unsigned int next_aff
= 0;
108 static int use_affinity
= 0;
110 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
112 #ifndef HAVE_CPU_SET_T
113 typedef unsigned long cpu_set_t
;
114 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
115 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
118 static void set_affinity(void)
120 #if HAVE_SCHED_SETAFFINITY
123 #endif /* HAVE_SCHED_SETAFFINITY */
128 #if HAVE_SCHED_SETAFFINITY
129 ret
= pthread_mutex_lock(&affinity_mutex
);
131 perror("Error in pthread mutex lock");
134 cpu
= cpu_affinities
[next_aff
++];
135 ret
= pthread_mutex_unlock(&affinity_mutex
);
137 perror("Error in pthread mutex unlock");
143 #if SCHED_SETAFFINITY_ARGS == 2
144 sched_setaffinity(0, &mask
);
146 sched_setaffinity(0, sizeof(mask
), &mask
);
148 #endif /* HAVE_SCHED_SETAFFINITY */
152 * returns 0 if test should end.
154 static int test_duration_dequeue(void)
159 static int test_duration_enqueue(void)
164 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
165 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
167 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
168 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
170 static unsigned int nr_enqueuers
;
171 static unsigned int nr_dequeuers
;
173 static struct cds_wfs_stack s
;
175 static void *thr_enqueuer(void *_count
)
177 unsigned long long *count
= _count
;
179 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
180 "enqueuer", (unsigned long) pthread_self(),
181 (unsigned long) gettid());
191 struct cds_wfs_node
*node
= malloc(sizeof(*node
));
194 cds_wfs_node_init(node
);
195 cds_wfs_push(&s
, node
);
196 URCU_TLS(nr_successful_enqueues
)++;
198 if (caa_unlikely(wdelay
))
201 URCU_TLS(nr_enqueues
)++;
202 if (caa_unlikely(!test_duration_enqueue()))
206 count
[0] = URCU_TLS(nr_enqueues
);
207 count
[1] = URCU_TLS(nr_successful_enqueues
);
208 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
209 "enqueues %llu successful_enqueues %llu\n",
211 (unsigned long) gettid(),
212 URCU_TLS(nr_enqueues
), URCU_TLS(nr_successful_enqueues
));
217 static void do_test_pop(enum test_sync sync
)
219 struct cds_wfs_node
*node
;
221 if (sync
== TEST_SYNC_MUTEX
)
222 cds_wfs_pop_lock(&s
);
223 node
= __cds_wfs_pop_blocking(&s
);
224 if (sync
== TEST_SYNC_MUTEX
)
225 cds_wfs_pop_unlock(&s
);
229 URCU_TLS(nr_successful_dequeues
)++;
231 URCU_TLS(nr_dequeues
)++;
234 static void do_test_pop_all(enum test_sync sync
)
236 struct cds_wfs_head
*head
;
237 struct cds_wfs_node
*node
, *n
;
239 if (sync
== TEST_SYNC_MUTEX
)
240 cds_wfs_pop_lock(&s
);
241 head
= __cds_wfs_pop_all(&s
);
242 if (sync
== TEST_SYNC_MUTEX
)
243 cds_wfs_pop_unlock(&s
);
245 cds_wfs_for_each_blocking_safe(head
, node
, n
) {
247 URCU_TLS(nr_successful_dequeues
)++;
248 URCU_TLS(nr_dequeues
)++;
252 static void *thr_dequeuer(void *_count
)
254 unsigned long long *count
= _count
;
255 unsigned int counter
;
257 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
258 "dequeuer", (unsigned long) pthread_self(),
259 (unsigned long) gettid());
268 assert(test_pop
|| test_pop_all
);
271 if (test_pop
&& test_pop_all
) {
273 do_test_pop(test_sync
);
275 do_test_pop_all(test_sync
);
279 do_test_pop(test_sync
);
281 do_test_pop_all(test_sync
);
284 if (caa_unlikely(!test_duration_dequeue()))
286 if (caa_unlikely(rduration
))
287 loop_sleep(rduration
);
290 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
291 "dequeues %llu, successful_dequeues %llu\n",
293 (unsigned long) gettid(),
294 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
));
295 count
[0] = URCU_TLS(nr_dequeues
);
296 count
[1] = URCU_TLS(nr_successful_dequeues
);
300 static void test_end(struct cds_wfs_stack
*s
, unsigned long long *nr_dequeues
)
302 struct cds_wfs_node
*node
;
305 node
= cds_wfs_pop_blocking(s
);
313 static void show_usage(int argc
, char **argv
)
315 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv
[0]);
316 printf(" [-d delay] (enqueuer period (in loops))");
317 printf(" [-c duration] (dequeuer period (in loops))");
318 printf(" [-v] (verbose output)");
319 printf(" [-a cpu#] [-a cpu#]... (affinity)");
320 printf(" [-p] (test pop)");
321 printf(" [-P] (test pop_all, enabled by default)");
322 printf(" [-M] (use mutex external synchronization)");
323 printf(" Note: default: no external synchronization used.");
327 int main(int argc
, char **argv
)
330 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
332 unsigned long long *count_enqueuer
, *count_dequeuer
;
333 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
334 unsigned long long tot_successful_enqueues
= 0,
335 tot_successful_dequeues
= 0;
336 unsigned long long end_dequeues
= 0;
340 show_usage(argc
, argv
);
344 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
346 show_usage(argc
, argv
);
350 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
352 show_usage(argc
, argv
);
356 err
= sscanf(argv
[3], "%lu", &duration
);
358 show_usage(argc
, argv
);
362 for (i
= 4; i
< argc
; i
++) {
363 if (argv
[i
][0] != '-')
365 switch (argv
[i
][1]) {
368 show_usage(argc
, argv
);
372 cpu_affinities
[next_aff
++] = a
;
374 printf_verbose("Adding CPU %d affinity\n", a
);
378 show_usage(argc
, argv
);
381 rduration
= atol(argv
[++i
]);
385 show_usage(argc
, argv
);
388 wdelay
= atol(argv
[++i
]);
400 test_sync
= TEST_SYNC_MUTEX
;
405 /* activate pop_all test by default */
406 if (!test_pop
&& !test_pop_all
)
409 printf_verbose("running test for %lu seconds, %u enqueuers, "
411 duration
, nr_enqueuers
, nr_dequeuers
);
413 printf_verbose("pop test activated.\n");
415 printf_verbose("pop_all test activated.\n");
416 if (test_sync
== TEST_SYNC_MUTEX
)
417 printf_verbose("External sync: mutex.\n");
419 printf_verbose("External sync: none.\n");
420 printf_verbose("Writer delay : %lu loops.\n", rduration
);
421 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
422 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
423 "main", (unsigned long) pthread_self(),
424 (unsigned long) gettid());
426 tid_enqueuer
= malloc(sizeof(*tid_enqueuer
) * nr_enqueuers
);
427 tid_dequeuer
= malloc(sizeof(*tid_dequeuer
) * nr_dequeuers
);
428 count_enqueuer
= malloc(2 * sizeof(*count_enqueuer
) * nr_enqueuers
);
429 count_dequeuer
= malloc(2 * sizeof(*count_dequeuer
) * nr_dequeuers
);
434 for (i
= 0; i
< nr_enqueuers
; i
++) {
435 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
436 &count_enqueuer
[2 * i
]);
440 for (i
= 0; i
< nr_dequeuers
; i
++) {
441 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
442 &count_dequeuer
[2 * i
]);
451 for (i
= 0; i
< duration
; i
++) {
459 for (i
= 0; i
< nr_enqueuers
; i
++) {
460 err
= pthread_join(tid_enqueuer
[i
], &tret
);
463 tot_enqueues
+= count_enqueuer
[2 * i
];
464 tot_successful_enqueues
+= count_enqueuer
[2 * i
+ 1];
466 for (i
= 0; i
< nr_dequeuers
; i
++) {
467 err
= pthread_join(tid_dequeuer
[i
], &tret
);
470 tot_dequeues
+= count_dequeuer
[2 * i
];
471 tot_successful_dequeues
+= count_dequeuer
[2 * i
+ 1];
474 test_end(&s
, &end_dequeues
);
476 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
477 tot_enqueues
, tot_dequeues
);
478 printf_verbose("total number of successful enqueues : %llu, "
479 "successful dequeues %llu\n",
480 tot_successful_enqueues
, tot_successful_dequeues
);
481 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
483 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
484 "successful enqueues %12llu successful dequeues %12llu "
485 "end_dequeues %llu nr_ops %12llu\n",
486 argv
[0], duration
, nr_enqueuers
, wdelay
,
487 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
488 tot_successful_enqueues
,
489 tot_successful_dequeues
, end_dequeues
,
490 tot_enqueues
+ tot_dequeues
);
491 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
)
492 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
493 "succ. dequeues + end dequeues %llu.\n",
494 tot_successful_enqueues
,
495 tot_successful_dequeues
+ end_dequeues
);
497 free(count_enqueuer
);
498 free(count_dequeuer
);