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.
30 #include <sys/types.h>
37 #include <urcu/arch.h>
38 #include <urcu/tls-compat.h>
39 #include "thread-id.h"
41 /* hardcoded number of CPUs */
44 #ifndef DYNAMIC_LINK_TEST
50 #define POISON_PTR ((void *) 0x42UL)
53 * External synchronization used.
60 static enum test_sync test_sync
;
62 static volatile int test_go
, test_stop
;
64 static unsigned long rduration
;
66 static unsigned long duration
;
68 /* read-side C.S. duration, in loops */
69 static unsigned long wdelay
;
71 static inline void loop_sleep(unsigned long loops
)
77 static int verbose_mode
;
79 static int test_pop
, test_pop_all
;
81 #define printf_verbose(fmt, args...) \
84 printf(fmt, ## args); \
87 static unsigned int cpu_affinities
[NR_CPUS
];
88 static unsigned int next_aff
= 0;
89 static int use_affinity
= 0;
91 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
93 static void set_affinity(void)
95 #if HAVE_SCHED_SETAFFINITY
98 #endif /* HAVE_SCHED_SETAFFINITY */
103 #if HAVE_SCHED_SETAFFINITY
104 ret
= pthread_mutex_lock(&affinity_mutex
);
106 perror("Error in pthread mutex lock");
109 cpu
= cpu_affinities
[next_aff
++];
110 ret
= pthread_mutex_unlock(&affinity_mutex
);
112 perror("Error in pthread mutex unlock");
118 sched_setaffinity(0, sizeof(mask
), &mask
);
119 #endif /* HAVE_SCHED_SETAFFINITY */
123 * returns 0 if test should end.
125 static int test_duration_dequeue(void)
130 static int test_duration_enqueue(void)
135 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
136 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
138 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
139 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
141 static unsigned int nr_enqueuers
;
142 static unsigned int nr_dequeuers
;
145 struct cds_lfs_node list
;
149 static struct cds_lfs_stack s
;
151 static void *thr_enqueuer(void *_count
)
153 unsigned long long *count
= _count
;
155 printf_verbose("thread_begin %s, tid %lu\n",
156 "enqueuer", urcu_get_thread_id());
160 rcu_register_thread();
168 struct test
*node
= malloc(sizeof(*node
));
171 cds_lfs_node_init(&node
->list
);
172 cds_lfs_push(&s
, &node
->list
);
173 URCU_TLS(nr_successful_enqueues
)++;
175 if (caa_unlikely(wdelay
))
178 URCU_TLS(nr_enqueues
)++;
179 if (caa_unlikely(!test_duration_enqueue()))
183 rcu_unregister_thread();
185 count
[0] = URCU_TLS(nr_enqueues
);
186 count
[1] = URCU_TLS(nr_successful_enqueues
);
187 printf_verbose("enqueuer thread_end, tid %lu, "
188 "enqueues %llu successful_enqueues %llu\n",
189 urcu_get_thread_id(),
190 URCU_TLS(nr_enqueues
),
191 URCU_TLS(nr_successful_enqueues
));
197 void free_node_cb(struct rcu_head
*head
)
200 caa_container_of(head
, struct test
, rcu
);
205 void do_test_pop(enum test_sync sync
)
207 struct cds_lfs_node
*snode
;
209 if (sync
== TEST_SYNC_RCU
)
211 snode
= __cds_lfs_pop(&s
);
212 if (sync
== TEST_SYNC_RCU
)
217 snode
->next
= POISON_PTR
;
218 node
= caa_container_of(snode
,
220 if (sync
== TEST_SYNC_RCU
)
221 call_rcu(&node
->rcu
, free_node_cb
);
224 URCU_TLS(nr_successful_dequeues
)++;
226 URCU_TLS(nr_dequeues
)++;
230 void do_test_pop_all(enum test_sync sync
)
232 struct cds_lfs_node
*snode
;
233 struct cds_lfs_head
*head
;
234 struct cds_lfs_node
*n
;
236 head
= __cds_lfs_pop_all(&s
);
237 cds_lfs_for_each_safe(head
, snode
, n
) {
240 snode
->next
= POISON_PTR
;
241 node
= caa_container_of(snode
, struct test
, list
);
242 if (sync
== TEST_SYNC_RCU
)
243 call_rcu(&node
->rcu
, free_node_cb
);
246 URCU_TLS(nr_successful_dequeues
)++;
247 URCU_TLS(nr_dequeues
)++;
252 static void *thr_dequeuer(void *_count
)
254 unsigned long long *count
= _count
;
255 unsigned int counter
= 0;
257 printf_verbose("thread_begin %s, tid %lu\n",
258 "dequeuer", urcu_get_thread_id());
262 rcu_register_thread();
269 assert(test_pop
|| test_pop_all
);
272 if (test_pop
&& test_pop_all
) {
273 /* both pop and 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 rcu_unregister_thread();
294 printf_verbose("dequeuer thread_end, tid %lu, "
295 "dequeues %llu, successful_dequeues %llu\n",
296 urcu_get_thread_id(),
297 URCU_TLS(nr_dequeues
),
298 URCU_TLS(nr_successful_dequeues
));
299 count
[0] = URCU_TLS(nr_dequeues
);
300 count
[1] = URCU_TLS(nr_successful_dequeues
);
304 static void test_end(struct cds_lfs_stack
*s
, unsigned long long *nr_dequeues
)
306 struct cds_lfs_node
*snode
;
309 snode
= __cds_lfs_pop(s
);
313 node
= caa_container_of(snode
, struct test
, list
);
320 static void show_usage(int argc
, char **argv
)
322 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
324 printf("OPTIONS:\n");
325 printf(" [-d delay] (enqueuer period (in loops))\n");
326 printf(" [-c duration] (dequeuer period (in loops))\n");
327 printf(" [-v] (verbose output)\n");
328 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
329 printf(" [-p] (test pop)\n");
330 printf(" [-P] (test pop_all, enabled by default)\n");
331 printf(" [-R] (use RCU external synchronization)\n");
332 printf(" Note: default: no external synchronization used.\n");
336 int main(int argc
, char **argv
)
339 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
341 unsigned long long *count_enqueuer
, *count_dequeuer
;
342 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
343 unsigned long long tot_successful_enqueues
= 0,
344 tot_successful_dequeues
= 0;
345 unsigned long long end_dequeues
= 0;
350 show_usage(argc
, argv
);
354 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
356 show_usage(argc
, argv
);
360 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
362 show_usage(argc
, argv
);
366 err
= sscanf(argv
[3], "%lu", &duration
);
368 show_usage(argc
, argv
);
372 for (i
= 4; i
< argc
; i
++) {
373 if (argv
[i
][0] != '-')
375 switch (argv
[i
][1]) {
378 show_usage(argc
, argv
);
382 cpu_affinities
[next_aff
++] = a
;
384 printf_verbose("Adding CPU %d affinity\n", a
);
388 show_usage(argc
, argv
);
391 rduration
= atol(argv
[++i
]);
395 show_usage(argc
, argv
);
398 wdelay
= atol(argv
[++i
]);
410 test_sync
= TEST_SYNC_RCU
;
415 /* activate pop_all test by default */
416 if (!test_pop
&& !test_pop_all
)
419 printf_verbose("running test for %lu seconds, %u enqueuers, "
421 duration
, nr_enqueuers
, nr_dequeuers
);
423 printf_verbose("pop test activated.\n");
425 printf_verbose("pop_all test activated.\n");
426 if (test_sync
== TEST_SYNC_RCU
)
427 printf_verbose("External sync: RCU.\n");
429 printf_verbose("External sync: none.\n");
430 printf_verbose("Writer delay : %lu loops.\n", rduration
);
431 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
432 printf_verbose("thread %-6s, tid %lu\n",
433 "main", urcu_get_thread_id());
435 tid_enqueuer
= calloc(nr_enqueuers
, sizeof(*tid_enqueuer
));
436 tid_dequeuer
= calloc(nr_dequeuers
, sizeof(*tid_dequeuer
));
437 count_enqueuer
= calloc(nr_enqueuers
, 2 * sizeof(*count_enqueuer
));
438 count_dequeuer
= calloc(nr_dequeuers
, 2 * sizeof(*count_dequeuer
));
440 err
= create_all_cpu_call_rcu_data(0);
442 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
447 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
448 err
= pthread_create(&tid_enqueuer
[i_thr
], NULL
, thr_enqueuer
,
449 &count_enqueuer
[2 * i_thr
]);
453 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
454 err
= pthread_create(&tid_dequeuer
[i_thr
], NULL
, thr_dequeuer
,
455 &count_dequeuer
[2 * i_thr
]);
464 for (i_thr
= 0; i_thr
< duration
; i_thr
++) {
467 fwrite(".", sizeof(char), 1, stdout
);
474 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
475 err
= pthread_join(tid_enqueuer
[i_thr
], &tret
);
478 tot_enqueues
+= count_enqueuer
[2 * i_thr
];
479 tot_successful_enqueues
+= count_enqueuer
[2 * i_thr
+ 1];
481 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
482 err
= pthread_join(tid_dequeuer
[i_thr
], &tret
);
485 tot_dequeues
+= count_dequeuer
[2 * i_thr
];
486 tot_successful_dequeues
+= count_dequeuer
[2 * i_thr
+ 1];
489 test_end(&s
, &end_dequeues
);
491 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
492 tot_enqueues
, tot_dequeues
);
493 printf_verbose("total number of successful enqueues : %llu, "
494 "successful dequeues %llu\n",
495 tot_successful_enqueues
, tot_successful_dequeues
);
496 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
498 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
499 "successful enqueues %12llu successful dequeues %12llu "
500 "end_dequeues %llu nr_ops %12llu\n",
501 argv
[0], duration
, nr_enqueuers
, wdelay
,
502 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
503 tot_successful_enqueues
,
504 tot_successful_dequeues
, end_dequeues
,
505 tot_enqueues
+ tot_dequeues
);
506 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
)
507 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
508 "succ. dequeues + end dequeues %llu.\n",
509 tot_successful_enqueues
,
510 tot_successful_dequeues
+ end_dequeues
);
512 free_all_cpu_call_rcu_data();
514 free(count_enqueuer
);
515 free(count_dequeuer
);