1 // SPDX-FileCopyrightText: 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 // SPDX-FileCopyrightText: 2010 Paolo Bonzini <pbonzini@redhat.com>
4 // SPDX-License-Identifier: GPL-2.0-or-later
7 * Userspace RCU library - example lock-free stack
16 #include <sys/types.h>
22 #include <urcu/arch.h>
23 #include <urcu/assert.h>
24 #include <urcu/tls-compat.h>
25 #include "thread-id.h"
27 /* hardcoded number of CPUs */
30 #ifndef DYNAMIC_LINK_TEST
36 #define POISON_PTR ((void *) 0x42UL)
39 * External synchronization used.
46 static enum test_sync test_sync
;
48 static volatile int test_go
, test_stop
;
50 static unsigned long rduration
;
52 static unsigned long duration
;
54 /* read-side C.S. duration, in loops */
55 static unsigned long wdelay
;
57 static inline void loop_sleep(unsigned long loops
)
63 static int verbose_mode
;
65 static int test_pop
, test_pop_all
;
67 #define printf_verbose(fmt, args...) \
70 printf(fmt, ## args); \
73 static unsigned int cpu_affinities
[NR_CPUS
];
74 static unsigned int next_aff
= 0;
75 static int use_affinity
= 0;
77 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
79 static void set_affinity(void)
81 #ifdef HAVE_SCHED_SETAFFINITY
84 #endif /* HAVE_SCHED_SETAFFINITY */
89 #ifdef HAVE_SCHED_SETAFFINITY
90 ret
= pthread_mutex_lock(&affinity_mutex
);
92 perror("Error in pthread mutex lock");
95 cpu
= cpu_affinities
[next_aff
++];
96 ret
= pthread_mutex_unlock(&affinity_mutex
);
98 perror("Error in pthread mutex unlock");
104 sched_setaffinity(0, sizeof(mask
), &mask
);
105 #endif /* HAVE_SCHED_SETAFFINITY */
109 * returns 0 if test should end.
111 static int test_duration_dequeue(void)
116 static int test_duration_enqueue(void)
121 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
122 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
124 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
125 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
127 static unsigned int nr_enqueuers
;
128 static unsigned int nr_dequeuers
;
131 struct cds_lfs_node list
;
135 static struct cds_lfs_stack s
;
137 static void *thr_enqueuer(void *_count
)
139 unsigned long long *count
= _count
;
141 printf_verbose("thread_begin %s, tid %lu\n",
142 "enqueuer", urcu_get_thread_id());
146 rcu_register_thread();
154 struct test
*node
= malloc(sizeof(*node
));
157 cds_lfs_node_init(&node
->list
);
158 cds_lfs_push(&s
, &node
->list
);
159 URCU_TLS(nr_successful_enqueues
)++;
161 if (caa_unlikely(wdelay
))
164 URCU_TLS(nr_enqueues
)++;
165 if (caa_unlikely(!test_duration_enqueue()))
169 rcu_unregister_thread();
171 count
[0] = URCU_TLS(nr_enqueues
);
172 count
[1] = URCU_TLS(nr_successful_enqueues
);
173 printf_verbose("enqueuer thread_end, tid %lu, "
174 "enqueues %llu successful_enqueues %llu\n",
175 urcu_get_thread_id(),
176 URCU_TLS(nr_enqueues
),
177 URCU_TLS(nr_successful_enqueues
));
183 void free_node_cb(struct rcu_head
*head
)
186 caa_container_of(head
, struct test
, rcu
);
191 void do_test_pop(enum test_sync sync
)
193 struct cds_lfs_node
*snode
;
195 if (sync
== TEST_SYNC_RCU
)
197 snode
= __cds_lfs_pop(&s
);
198 if (sync
== TEST_SYNC_RCU
)
203 snode
->next
= POISON_PTR
;
204 node
= caa_container_of(snode
,
206 if (sync
== TEST_SYNC_RCU
)
207 call_rcu(&node
->rcu
, free_node_cb
);
210 URCU_TLS(nr_successful_dequeues
)++;
212 URCU_TLS(nr_dequeues
)++;
216 void do_test_pop_all(enum test_sync sync
)
218 struct cds_lfs_node
*snode
;
219 struct cds_lfs_head
*head
;
220 struct cds_lfs_node
*n
;
222 head
= __cds_lfs_pop_all(&s
);
223 cds_lfs_for_each_safe(head
, snode
, n
) {
226 snode
->next
= POISON_PTR
;
227 node
= caa_container_of(snode
, struct test
, list
);
228 if (sync
== TEST_SYNC_RCU
)
229 call_rcu(&node
->rcu
, free_node_cb
);
232 URCU_TLS(nr_successful_dequeues
)++;
233 URCU_TLS(nr_dequeues
)++;
238 static void *thr_dequeuer(void *_count
)
240 unsigned long long *count
= _count
;
241 unsigned int counter
= 0;
243 printf_verbose("thread_begin %s, tid %lu\n",
244 "dequeuer", urcu_get_thread_id());
248 rcu_register_thread();
255 urcu_posix_assert(test_pop
|| test_pop_all
);
258 if (test_pop
&& test_pop_all
) {
259 /* both pop and pop all */
261 do_test_pop(test_sync
);
263 do_test_pop_all(test_sync
);
267 do_test_pop(test_sync
);
269 do_test_pop_all(test_sync
);
272 if (caa_unlikely(!test_duration_dequeue()))
274 if (caa_unlikely(rduration
))
275 loop_sleep(rduration
);
278 rcu_unregister_thread();
280 printf_verbose("dequeuer thread_end, tid %lu, "
281 "dequeues %llu, successful_dequeues %llu\n",
282 urcu_get_thread_id(),
283 URCU_TLS(nr_dequeues
),
284 URCU_TLS(nr_successful_dequeues
));
285 count
[0] = URCU_TLS(nr_dequeues
);
286 count
[1] = URCU_TLS(nr_successful_dequeues
);
290 static void test_end(unsigned long long *nr_dequeues_l
)
292 struct cds_lfs_node
*snode
;
295 snode
= __cds_lfs_pop(&s
);
299 node
= caa_container_of(snode
, struct test
, list
);
306 static void show_usage(char **argv
)
308 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
310 printf("OPTIONS:\n");
311 printf(" [-d delay] (enqueuer period (in loops))\n");
312 printf(" [-c duration] (dequeuer period (in loops))\n");
313 printf(" [-v] (verbose output)\n");
314 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
315 printf(" [-p] (test pop)\n");
316 printf(" [-P] (test pop_all, enabled by default)\n");
317 printf(" [-R] (use RCU external synchronization)\n");
318 printf(" Note: default: no external synchronization used.\n");
322 int main(int argc
, char **argv
)
325 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
327 unsigned long long *count_enqueuer
, *count_dequeuer
;
328 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
329 unsigned long long tot_successful_enqueues
= 0,
330 tot_successful_dequeues
= 0;
331 unsigned long long end_dequeues
= 0;
340 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
346 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
352 err
= sscanf(argv
[3], "%lu", &duration
);
358 for (i
= 4; i
< argc
; i
++) {
359 if (argv
[i
][0] != '-')
361 switch (argv
[i
][1]) {
368 cpu_affinities
[next_aff
++] = a
;
370 printf_verbose("Adding CPU %d affinity\n", a
);
377 rduration
= atol(argv
[++i
]);
384 wdelay
= atol(argv
[++i
]);
396 test_sync
= TEST_SYNC_RCU
;
401 /* activate pop_all test by default */
402 if (!test_pop
&& !test_pop_all
)
405 printf_verbose("running test for %lu seconds, %u enqueuers, "
407 duration
, nr_enqueuers
, nr_dequeuers
);
409 printf_verbose("pop test activated.\n");
411 printf_verbose("pop_all test activated.\n");
412 if (test_sync
== TEST_SYNC_RCU
)
413 printf_verbose("External sync: RCU.\n");
415 printf_verbose("External sync: none.\n");
416 printf_verbose("Writer delay : %lu loops.\n", rduration
);
417 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
418 printf_verbose("thread %-6s, tid %lu\n",
419 "main", urcu_get_thread_id());
421 tid_enqueuer
= calloc(nr_enqueuers
, sizeof(*tid_enqueuer
));
422 tid_dequeuer
= calloc(nr_dequeuers
, sizeof(*tid_dequeuer
));
423 count_enqueuer
= calloc(nr_enqueuers
, 2 * sizeof(*count_enqueuer
));
424 count_dequeuer
= calloc(nr_dequeuers
, 2 * sizeof(*count_dequeuer
));
426 err
= create_all_cpu_call_rcu_data(0);
428 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
433 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
434 err
= pthread_create(&tid_enqueuer
[i_thr
], NULL
, thr_enqueuer
,
435 &count_enqueuer
[2 * i_thr
]);
439 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
440 err
= pthread_create(&tid_dequeuer
[i_thr
], NULL
, thr_dequeuer
,
441 &count_dequeuer
[2 * i_thr
]);
450 for (i_thr
= 0; i_thr
< duration
; i_thr
++) {
453 fwrite(".", sizeof(char), 1, stdout
);
460 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
461 err
= pthread_join(tid_enqueuer
[i_thr
], &tret
);
464 tot_enqueues
+= count_enqueuer
[2 * i_thr
];
465 tot_successful_enqueues
+= count_enqueuer
[2 * i_thr
+ 1];
467 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
468 err
= pthread_join(tid_dequeuer
[i_thr
], &tret
);
471 tot_dequeues
+= count_dequeuer
[2 * i_thr
];
472 tot_successful_dequeues
+= count_dequeuer
[2 * i_thr
+ 1];
475 test_end(&end_dequeues
);
477 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
478 tot_enqueues
, tot_dequeues
);
479 printf_verbose("total number of successful enqueues : %llu, "
480 "successful dequeues %llu\n",
481 tot_successful_enqueues
, tot_successful_dequeues
);
482 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
484 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
485 "successful enqueues %12llu successful dequeues %12llu "
486 "end_dequeues %llu nr_ops %12llu\n",
487 argv
[0], duration
, nr_enqueuers
, wdelay
,
488 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
489 tot_successful_enqueues
,
490 tot_successful_dequeues
, end_dequeues
,
491 tot_enqueues
+ tot_dequeues
);
492 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
)
493 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
494 "succ. dequeues + end dequeues %llu.\n",
495 tot_successful_enqueues
,
496 tot_successful_dequeues
+ end_dequeues
);
498 free_all_cpu_call_rcu_data();
500 free(count_enqueuer
);
501 free(count_dequeuer
);