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>
40 #include "thread-id.h"
42 /* hardcoded number of CPUs */
45 #ifndef DYNAMIC_LINK_TEST
51 #define POISON_PTR ((void *) 0x42UL)
54 * External synchronization used.
61 static enum test_sync test_sync
;
63 static volatile int test_go
, test_stop
;
65 static unsigned long rduration
;
67 static unsigned long duration
;
69 /* read-side C.S. duration, in loops */
70 static unsigned long wdelay
;
72 static inline void loop_sleep(unsigned long loops
)
78 static int verbose_mode
;
80 static int test_pop
, test_pop_all
;
82 #define printf_verbose(fmt, args...) \
85 printf(fmt, ## args); \
88 static unsigned int cpu_affinities
[NR_CPUS
];
89 static unsigned int next_aff
= 0;
90 static int use_affinity
= 0;
92 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
94 static void set_affinity(void)
96 #if HAVE_SCHED_SETAFFINITY
99 #endif /* HAVE_SCHED_SETAFFINITY */
104 #if HAVE_SCHED_SETAFFINITY
105 ret
= pthread_mutex_lock(&affinity_mutex
);
107 perror("Error in pthread mutex lock");
110 cpu
= cpu_affinities
[next_aff
++];
111 ret
= pthread_mutex_unlock(&affinity_mutex
);
113 perror("Error in pthread mutex unlock");
119 #if SCHED_SETAFFINITY_ARGS == 2
120 sched_setaffinity(0, &mask
);
122 sched_setaffinity(0, sizeof(mask
), &mask
);
124 #endif /* HAVE_SCHED_SETAFFINITY */
128 * returns 0 if test should end.
130 static int test_duration_dequeue(void)
135 static int test_duration_enqueue(void)
140 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
141 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
143 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
144 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
146 static unsigned int nr_enqueuers
;
147 static unsigned int nr_dequeuers
;
150 struct cds_lfs_node list
;
154 static struct cds_lfs_stack s
;
156 static void *thr_enqueuer(void *_count
)
158 unsigned long long *count
= _count
;
160 printf_verbose("thread_begin %s, tid %lu\n",
161 "enqueuer", urcu_get_thread_id());
165 rcu_register_thread();
173 struct test
*node
= malloc(sizeof(*node
));
176 cds_lfs_node_init(&node
->list
);
177 cds_lfs_push(&s
, &node
->list
);
178 URCU_TLS(nr_successful_enqueues
)++;
180 if (caa_unlikely(wdelay
))
183 URCU_TLS(nr_enqueues
)++;
184 if (caa_unlikely(!test_duration_enqueue()))
188 rcu_unregister_thread();
190 count
[0] = URCU_TLS(nr_enqueues
);
191 count
[1] = URCU_TLS(nr_successful_enqueues
);
192 printf_verbose("enqueuer thread_end, tid %lu, "
193 "enqueues %llu successful_enqueues %llu\n",
194 urcu_get_thread_id(),
195 URCU_TLS(nr_enqueues
),
196 URCU_TLS(nr_successful_enqueues
));
202 void free_node_cb(struct rcu_head
*head
)
205 caa_container_of(head
, struct test
, rcu
);
210 void do_test_pop(enum test_sync sync
)
212 struct cds_lfs_node
*snode
;
214 if (sync
== TEST_SYNC_RCU
)
216 snode
= __cds_lfs_pop(&s
);
217 if (sync
== TEST_SYNC_RCU
)
222 snode
->next
= POISON_PTR
;
223 node
= caa_container_of(snode
,
225 if (sync
== TEST_SYNC_RCU
)
226 call_rcu(&node
->rcu
, free_node_cb
);
229 URCU_TLS(nr_successful_dequeues
)++;
231 URCU_TLS(nr_dequeues
)++;
235 void do_test_pop_all(enum test_sync sync
)
237 struct cds_lfs_node
*snode
;
238 struct cds_lfs_head
*head
;
239 struct cds_lfs_node
*n
;
241 head
= __cds_lfs_pop_all(&s
);
242 cds_lfs_for_each_safe(head
, snode
, n
) {
245 snode
->next
= POISON_PTR
;
246 node
= caa_container_of(snode
, struct test
, list
);
247 if (sync
== TEST_SYNC_RCU
)
248 call_rcu(&node
->rcu
, free_node_cb
);
251 URCU_TLS(nr_successful_dequeues
)++;
252 URCU_TLS(nr_dequeues
)++;
257 static void *thr_dequeuer(void *_count
)
259 unsigned long long *count
= _count
;
260 unsigned int counter
= 0;
262 printf_verbose("thread_begin %s, tid %lu\n",
263 "dequeuer", urcu_get_thread_id());
267 rcu_register_thread();
274 assert(test_pop
|| test_pop_all
);
277 if (test_pop
&& test_pop_all
) {
278 /* both pop and pop all */
280 do_test_pop(test_sync
);
282 do_test_pop_all(test_sync
);
286 do_test_pop(test_sync
);
288 do_test_pop_all(test_sync
);
291 if (caa_unlikely(!test_duration_dequeue()))
293 if (caa_unlikely(rduration
))
294 loop_sleep(rduration
);
297 rcu_unregister_thread();
299 printf_verbose("dequeuer thread_end, tid %lu, "
300 "dequeues %llu, successful_dequeues %llu\n",
301 urcu_get_thread_id(),
302 URCU_TLS(nr_dequeues
),
303 URCU_TLS(nr_successful_dequeues
));
304 count
[0] = URCU_TLS(nr_dequeues
);
305 count
[1] = URCU_TLS(nr_successful_dequeues
);
309 static void test_end(unsigned long long *nr_dequeues_l
)
311 struct cds_lfs_node
*snode
;
314 snode
= __cds_lfs_pop(&s
);
318 node
= caa_container_of(snode
, struct test
, list
);
325 static void show_usage(int argc
, char **argv
)
327 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
329 printf("OPTIONS:\n");
330 printf(" [-d delay] (enqueuer period (in loops))\n");
331 printf(" [-c duration] (dequeuer period (in loops))\n");
332 printf(" [-v] (verbose output)\n");
333 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
334 printf(" [-p] (test pop)\n");
335 printf(" [-P] (test pop_all, enabled by default)\n");
336 printf(" [-R] (use RCU external synchronization)\n");
337 printf(" Note: default: no external synchronization used.\n");
341 int main(int argc
, char **argv
)
344 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
346 unsigned long long *count_enqueuer
, *count_dequeuer
;
347 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
348 unsigned long long tot_successful_enqueues
= 0,
349 tot_successful_dequeues
= 0;
350 unsigned long long end_dequeues
= 0;
355 show_usage(argc
, argv
);
359 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
361 show_usage(argc
, argv
);
365 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
367 show_usage(argc
, argv
);
371 err
= sscanf(argv
[3], "%lu", &duration
);
373 show_usage(argc
, argv
);
377 for (i
= 4; i
< argc
; i
++) {
378 if (argv
[i
][0] != '-')
380 switch (argv
[i
][1]) {
383 show_usage(argc
, argv
);
387 cpu_affinities
[next_aff
++] = a
;
389 printf_verbose("Adding CPU %d affinity\n", a
);
393 show_usage(argc
, argv
);
396 rduration
= atol(argv
[++i
]);
400 show_usage(argc
, argv
);
403 wdelay
= atol(argv
[++i
]);
415 test_sync
= TEST_SYNC_RCU
;
420 /* activate pop_all test by default */
421 if (!test_pop
&& !test_pop_all
)
424 printf_verbose("running test for %lu seconds, %u enqueuers, "
426 duration
, nr_enqueuers
, nr_dequeuers
);
428 printf_verbose("pop test activated.\n");
430 printf_verbose("pop_all test activated.\n");
431 if (test_sync
== TEST_SYNC_RCU
)
432 printf_verbose("External sync: RCU.\n");
434 printf_verbose("External sync: none.\n");
435 printf_verbose("Writer delay : %lu loops.\n", rduration
);
436 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
437 printf_verbose("thread %-6s, tid %lu\n",
438 "main", urcu_get_thread_id());
440 tid_enqueuer
= calloc(nr_enqueuers
, sizeof(*tid_enqueuer
));
441 tid_dequeuer
= calloc(nr_dequeuers
, sizeof(*tid_dequeuer
));
442 count_enqueuer
= calloc(nr_enqueuers
, 2 * sizeof(*count_enqueuer
));
443 count_dequeuer
= calloc(nr_dequeuers
, 2 * sizeof(*count_dequeuer
));
445 err
= create_all_cpu_call_rcu_data(0);
447 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
452 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
453 err
= pthread_create(&tid_enqueuer
[i_thr
], NULL
, thr_enqueuer
,
454 &count_enqueuer
[2 * i_thr
]);
458 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
459 err
= pthread_create(&tid_dequeuer
[i_thr
], NULL
, thr_dequeuer
,
460 &count_dequeuer
[2 * i_thr
]);
469 for (i_thr
= 0; i_thr
< duration
; i_thr
++) {
472 fwrite(".", sizeof(char), 1, stdout
);
479 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
480 err
= pthread_join(tid_enqueuer
[i_thr
], &tret
);
483 tot_enqueues
+= count_enqueuer
[2 * i_thr
];
484 tot_successful_enqueues
+= count_enqueuer
[2 * i_thr
+ 1];
486 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
487 err
= pthread_join(tid_dequeuer
[i_thr
], &tret
);
490 tot_dequeues
+= count_dequeuer
[2 * i_thr
];
491 tot_successful_dequeues
+= count_dequeuer
[2 * i_thr
+ 1];
494 test_end(&end_dequeues
);
496 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
497 tot_enqueues
, tot_dequeues
);
498 printf_verbose("total number of successful enqueues : %llu, "
499 "successful dequeues %llu\n",
500 tot_successful_enqueues
, tot_successful_dequeues
);
501 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
503 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
504 "successful enqueues %12llu successful dequeues %12llu "
505 "end_dequeues %llu nr_ops %12llu\n",
506 argv
[0], duration
, nr_enqueuers
, wdelay
,
507 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
508 tot_successful_enqueues
,
509 tot_successful_dequeues
, end_dequeues
,
510 tot_enqueues
+ tot_dequeues
);
511 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
)
512 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
513 "succ. dequeues + end dequeues %llu.\n",
514 tot_successful_enqueues
,
515 tot_successful_dequeues
+ end_dequeues
);
517 free_all_cpu_call_rcu_data();
519 free(count_enqueuer
);
520 free(count_dequeuer
);