4 * Userspace RCU library - example lock-free stack
6 * Copyright 2010-2012 - 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>
39 #include <urcu/arch.h>
40 #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
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 static void set_affinity(void)
114 #if HAVE_SCHED_SETAFFINITY
117 #endif /* HAVE_SCHED_SETAFFINITY */
122 #if HAVE_SCHED_SETAFFINITY
123 ret
= pthread_mutex_lock(&affinity_mutex
);
125 perror("Error in pthread mutex lock");
128 cpu
= cpu_affinities
[next_aff
++];
129 ret
= pthread_mutex_unlock(&affinity_mutex
);
131 perror("Error in pthread mutex unlock");
137 #if SCHED_SETAFFINITY_ARGS == 2
138 sched_setaffinity(0, &mask
);
140 sched_setaffinity(0, sizeof(mask
), &mask
);
142 #endif /* HAVE_SCHED_SETAFFINITY */
146 * returns 0 if test should end.
148 static int test_duration_dequeue(void)
153 static int test_duration_enqueue(void)
158 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
159 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
161 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
162 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
164 static unsigned int nr_enqueuers
;
165 static unsigned int nr_dequeuers
;
168 struct cds_lfs_node list
;
172 static struct cds_lfs_stack s
;
174 static void *thr_enqueuer(void *_count
)
176 unsigned long long *count
= _count
;
178 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
179 "enqueuer", (unsigned long) pthread_self(),
180 (unsigned long) gettid());
184 rcu_register_thread();
192 struct test
*node
= malloc(sizeof(*node
));
195 cds_lfs_node_init(&node
->list
);
196 cds_lfs_push(&s
, &node
->list
);
197 URCU_TLS(nr_successful_enqueues
)++;
199 if (caa_unlikely(wdelay
))
202 URCU_TLS(nr_enqueues
)++;
203 if (caa_unlikely(!test_duration_enqueue()))
207 rcu_unregister_thread();
209 count
[0] = URCU_TLS(nr_enqueues
);
210 count
[1] = URCU_TLS(nr_successful_enqueues
);
211 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
212 "enqueues %llu successful_enqueues %llu\n",
214 (unsigned long) gettid(),
215 URCU_TLS(nr_enqueues
), URCU_TLS(nr_successful_enqueues
));
221 void free_node_cb(struct rcu_head
*head
)
224 caa_container_of(head
, struct test
, rcu
);
229 void do_test_pop(enum test_sync sync
)
231 struct cds_lfs_node
*snode
;
233 if (sync
== TEST_SYNC_RCU
)
235 snode
= __cds_lfs_pop(&s
);
236 if (sync
== TEST_SYNC_RCU
)
241 node
= caa_container_of(snode
,
243 if (sync
== TEST_SYNC_RCU
)
244 call_rcu(&node
->rcu
, free_node_cb
);
247 URCU_TLS(nr_successful_dequeues
)++;
249 URCU_TLS(nr_dequeues
)++;
253 void do_test_pop_all(enum test_sync sync
)
255 struct cds_lfs_node
*snode
;
256 struct cds_lfs_head
*head
;
257 struct cds_lfs_node
*n
;
259 head
= __cds_lfs_pop_all(&s
);
260 cds_lfs_for_each_safe(head
, snode
, n
) {
263 node
= caa_container_of(snode
, struct test
, list
);
264 if (sync
== TEST_SYNC_RCU
)
265 call_rcu(&node
->rcu
, free_node_cb
);
268 URCU_TLS(nr_successful_dequeues
)++;
269 URCU_TLS(nr_dequeues
)++;
274 static void *thr_dequeuer(void *_count
)
276 unsigned long long *count
= _count
;
278 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
279 "dequeuer", (unsigned long) pthread_self(),
280 (unsigned long) gettid());
284 rcu_register_thread();
291 assert(test_pop
|| test_pop_all
);
294 unsigned int counter
= 0;
296 if (test_pop
&& test_pop_all
) {
297 /* both pop and pop all */
299 do_test_pop(test_sync
);
301 do_test_pop_all(test_sync
);
305 do_test_pop(test_sync
);
307 do_test_pop_all(test_sync
);
310 if (caa_unlikely(!test_duration_dequeue()))
312 if (caa_unlikely(rduration
))
313 loop_sleep(rduration
);
316 rcu_unregister_thread();
318 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
319 "dequeues %llu, successful_dequeues %llu\n",
321 (unsigned long) gettid(),
322 URCU_TLS(nr_dequeues
), URCU_TLS(nr_successful_dequeues
));
323 count
[0] = URCU_TLS(nr_dequeues
);
324 count
[1] = URCU_TLS(nr_successful_dequeues
);
328 static void test_end(struct cds_lfs_stack
*s
, unsigned long long *nr_dequeues
)
330 struct cds_lfs_node
*snode
;
333 snode
= __cds_lfs_pop(s
);
337 node
= caa_container_of(snode
, struct test
, list
);
344 static void show_usage(int argc
, char **argv
)
346 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
348 printf("OPTIONS:\n");
349 printf(" [-d delay] (enqueuer period (in loops))\n");
350 printf(" [-c duration] (dequeuer period (in loops))\n");
351 printf(" [-v] (verbose output)\n");
352 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
353 printf(" [-p] (test pop)\n");
354 printf(" [-P] (test pop_all, enabled by default)\n");
355 printf(" [-R] (use RCU external synchronization)\n");
356 printf(" Note: default: no external synchronization used.\n");
360 int main(int argc
, char **argv
)
363 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
365 unsigned long long *count_enqueuer
, *count_dequeuer
;
366 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
367 unsigned long long tot_successful_enqueues
= 0,
368 tot_successful_dequeues
= 0;
369 unsigned long long end_dequeues
= 0;
373 show_usage(argc
, argv
);
377 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
379 show_usage(argc
, argv
);
383 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
385 show_usage(argc
, argv
);
389 err
= sscanf(argv
[3], "%lu", &duration
);
391 show_usage(argc
, argv
);
395 for (i
= 4; i
< argc
; i
++) {
396 if (argv
[i
][0] != '-')
398 switch (argv
[i
][1]) {
401 show_usage(argc
, argv
);
405 cpu_affinities
[next_aff
++] = a
;
407 printf_verbose("Adding CPU %d affinity\n", a
);
411 show_usage(argc
, argv
);
414 rduration
= atol(argv
[++i
]);
418 show_usage(argc
, argv
);
421 wdelay
= atol(argv
[++i
]);
433 test_sync
= TEST_SYNC_RCU
;
438 /* activate pop_all test by default */
439 if (!test_pop
&& !test_pop_all
)
442 printf_verbose("running test for %lu seconds, %u enqueuers, "
444 duration
, nr_enqueuers
, nr_dequeuers
);
446 printf_verbose("pop test activated.\n");
448 printf_verbose("pop_all test activated.\n");
449 if (test_sync
== TEST_SYNC_RCU
)
450 printf_verbose("External sync: RCU.\n");
452 printf_verbose("External sync: none.\n");
453 printf_verbose("Writer delay : %lu loops.\n", rduration
);
454 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
455 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
456 "main", (unsigned long) pthread_self(),
457 (unsigned long) gettid());
459 tid_enqueuer
= malloc(sizeof(*tid_enqueuer
) * nr_enqueuers
);
460 tid_dequeuer
= malloc(sizeof(*tid_dequeuer
) * nr_dequeuers
);
461 count_enqueuer
= malloc(2 * sizeof(*count_enqueuer
) * nr_enqueuers
);
462 count_dequeuer
= malloc(2 * sizeof(*count_dequeuer
) * nr_dequeuers
);
464 err
= create_all_cpu_call_rcu_data(0);
466 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
471 for (i
= 0; i
< nr_enqueuers
; i
++) {
472 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
473 &count_enqueuer
[2 * i
]);
477 for (i
= 0; i
< nr_dequeuers
; i
++) {
478 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
479 &count_dequeuer
[2 * i
]);
488 for (i
= 0; i
< duration
; i
++) {
496 for (i
= 0; i
< nr_enqueuers
; i
++) {
497 err
= pthread_join(tid_enqueuer
[i
], &tret
);
500 tot_enqueues
+= count_enqueuer
[2 * i
];
501 tot_successful_enqueues
+= count_enqueuer
[2 * i
+ 1];
503 for (i
= 0; i
< nr_dequeuers
; i
++) {
504 err
= pthread_join(tid_dequeuer
[i
], &tret
);
507 tot_dequeues
+= count_dequeuer
[2 * i
];
508 tot_successful_dequeues
+= count_dequeuer
[2 * i
+ 1];
511 test_end(&s
, &end_dequeues
);
513 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
514 tot_enqueues
, tot_dequeues
);
515 printf_verbose("total number of successful enqueues : %llu, "
516 "successful dequeues %llu\n",
517 tot_successful_enqueues
, tot_successful_dequeues
);
518 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
520 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
521 "successful enqueues %12llu successful dequeues %12llu "
522 "end_dequeues %llu nr_ops %12llu\n",
523 argv
[0], duration
, nr_enqueuers
, wdelay
,
524 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
525 tot_successful_enqueues
,
526 tot_successful_dequeues
, end_dequeues
,
527 tot_enqueues
+ tot_dequeues
);
528 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
)
529 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
530 "succ. dequeues + end dequeues %llu.\n",
531 tot_successful_enqueues
,
532 tot_successful_dequeues
+ end_dequeues
);
534 free_all_cpu_call_rcu_data();
535 free(count_enqueuer
);
536 free(count_dequeuer
);