Commit | Line | Data |
---|---|---|
ce29b371 MJ |
1 | // SPDX-FileCopyrightText: 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2 | // SPDX-FileCopyrightText: 2010 Paolo Bonzini <pbonzini@redhat.com> | |
3 | // | |
4 | // SPDX-License-Identifier: GPL-2.0-or-later | |
5 | ||
453629a9 | 6 | /* |
cb8818f0 | 7 | * Userspace RCU library - example lock-free stack |
453629a9 MD |
8 | */ |
9 | ||
453629a9 MD |
10 | #include <stdio.h> |
11 | #include <pthread.h> | |
12 | #include <stdlib.h> | |
13 | #include <stdint.h> | |
14 | #include <stdbool.h> | |
15 | #include <string.h> | |
16 | #include <sys/types.h> | |
17 | #include <sys/wait.h> | |
18 | #include <unistd.h> | |
19 | #include <stdio.h> | |
453629a9 MD |
20 | #include <errno.h> |
21 | ||
22 | #include <urcu/arch.h> | |
01477510 | 23 | #include <urcu/assert.h> |
bd252a04 | 24 | #include <urcu/tls-compat.h> |
94df6318 | 25 | #include "thread-id.h" |
65fcc7e9 | 26 | |
453629a9 MD |
27 | /* hardcoded number of CPUs */ |
28 | #define NR_CPUS 16384 | |
29 | ||
453629a9 MD |
30 | #ifndef DYNAMIC_LINK_TEST |
31 | #define _LGPL_SOURCE | |
32 | #endif | |
33 | #include <urcu.h> | |
e5a7d709 | 34 | #include <urcu/cds.h> |
453629a9 | 35 | |
f9b5c2b6 MD |
36 | #define POISON_PTR ((void *) 0x42UL) |
37 | ||
111ce0c3 MD |
38 | /* |
39 | * External synchronization used. | |
40 | */ | |
41 | enum test_sync { | |
42 | TEST_SYNC_NONE = 0, | |
43 | TEST_SYNC_RCU, | |
44 | }; | |
45 | ||
46 | static enum test_sync test_sync; | |
47 | ||
453629a9 MD |
48 | static unsigned long rduration; |
49 | ||
50 | static unsigned long duration; | |
51 | ||
52 | /* read-side C.S. duration, in loops */ | |
53 | static unsigned long wdelay; | |
54 | ||
ab0aacbe | 55 | static inline void loop_sleep(unsigned long loops) |
453629a9 | 56 | { |
ab0aacbe | 57 | while (loops-- != 0) |
06f22bdb | 58 | caa_cpu_relax(); |
453629a9 MD |
59 | } |
60 | ||
61 | static int verbose_mode; | |
62 | ||
111ce0c3 MD |
63 | static int test_pop, test_pop_all; |
64 | ||
453629a9 MD |
65 | #define printf_verbose(fmt, args...) \ |
66 | do { \ | |
67 | if (verbose_mode) \ | |
111ce0c3 | 68 | printf(fmt, ## args); \ |
453629a9 MD |
69 | } while (0) |
70 | ||
71 | static unsigned int cpu_affinities[NR_CPUS]; | |
72 | static unsigned int next_aff = 0; | |
73 | static int use_affinity = 0; | |
74 | ||
75 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; | |
76 | ||
453629a9 MD |
77 | static void set_affinity(void) |
78 | { | |
2388c075 | 79 | #ifdef HAVE_SCHED_SETAFFINITY |
453629a9 | 80 | cpu_set_t mask; |
95bc7fb9 MD |
81 | int cpu, ret; |
82 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
453629a9 MD |
83 | |
84 | if (!use_affinity) | |
85 | return; | |
86 | ||
2388c075 | 87 | #ifdef HAVE_SCHED_SETAFFINITY |
453629a9 MD |
88 | ret = pthread_mutex_lock(&affinity_mutex); |
89 | if (ret) { | |
90 | perror("Error in pthread mutex lock"); | |
91 | exit(-1); | |
92 | } | |
93 | cpu = cpu_affinities[next_aff++]; | |
94 | ret = pthread_mutex_unlock(&affinity_mutex); | |
95 | if (ret) { | |
96 | perror("Error in pthread mutex unlock"); | |
97 | exit(-1); | |
98 | } | |
99 | ||
100 | CPU_ZERO(&mask); | |
101 | CPU_SET(cpu, &mask); | |
453629a9 | 102 | sched_setaffinity(0, sizeof(mask), &mask); |
453629a9 MD |
103 | #endif /* HAVE_SCHED_SETAFFINITY */ |
104 | } | |
105 | ||
106 | /* | |
107 | * returns 0 if test should end. | |
108 | */ | |
109 | static int test_duration_dequeue(void) | |
110 | { | |
9e4e7ad1 | 111 | return test_duration_read(); |
453629a9 MD |
112 | } |
113 | ||
114 | static int test_duration_enqueue(void) | |
115 | { | |
9e4e7ad1 | 116 | return test_duration_write(); |
453629a9 MD |
117 | } |
118 | ||
bd252a04 MD |
119 | static DEFINE_URCU_TLS(unsigned long long, nr_dequeues); |
120 | static DEFINE_URCU_TLS(unsigned long long, nr_enqueues); | |
453629a9 | 121 | |
bd252a04 MD |
122 | static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues); |
123 | static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues); | |
453629a9 MD |
124 | |
125 | static unsigned int nr_enqueuers; | |
126 | static unsigned int nr_dequeuers; | |
127 | ||
94aec122 | 128 | struct test { |
cb8818f0 | 129 | struct cds_lfs_node list; |
94aec122 MD |
130 | struct rcu_head rcu; |
131 | }; | |
132 | ||
cb8818f0 | 133 | static struct cds_lfs_stack s; |
453629a9 | 134 | |
eb314468 | 135 | static void *thr_enqueuer(void *_count) |
453629a9 MD |
136 | { |
137 | unsigned long long *count = _count; | |
138 | ||
94df6318 MD |
139 | printf_verbose("thread_begin %s, tid %lu\n", |
140 | "enqueuer", urcu_get_thread_id()); | |
453629a9 MD |
141 | |
142 | set_affinity(); | |
143 | ||
144 | rcu_register_thread(); | |
145 | ||
9e4e7ad1 | 146 | wait_until_go(); |
453629a9 MD |
147 | |
148 | for (;;) { | |
94aec122 | 149 | struct test *node = malloc(sizeof(*node)); |
453629a9 MD |
150 | if (!node) |
151 | goto fail; | |
cb8818f0 MD |
152 | cds_lfs_node_init(&node->list); |
153 | cds_lfs_push(&s, &node->list); | |
bd252a04 | 154 | URCU_TLS(nr_successful_enqueues)++; |
453629a9 | 155 | |
a0b7f7ea | 156 | if (caa_unlikely(wdelay)) |
453629a9 MD |
157 | loop_sleep(wdelay); |
158 | fail: | |
bd252a04 | 159 | URCU_TLS(nr_enqueues)++; |
a0b7f7ea | 160 | if (caa_unlikely(!test_duration_enqueue())) |
453629a9 MD |
161 | break; |
162 | } | |
163 | ||
164 | rcu_unregister_thread(); | |
165 | ||
bd252a04 MD |
166 | count[0] = URCU_TLS(nr_enqueues); |
167 | count[1] = URCU_TLS(nr_successful_enqueues); | |
94df6318 MD |
168 | printf_verbose("enqueuer thread_end, tid %lu, " |
169 | "enqueues %llu successful_enqueues %llu\n", | |
170 | urcu_get_thread_id(), | |
171 | URCU_TLS(nr_enqueues), | |
172 | URCU_TLS(nr_successful_enqueues)); | |
453629a9 MD |
173 | return ((void*)1); |
174 | ||
175 | } | |
176 | ||
94aec122 MD |
177 | static |
178 | void free_node_cb(struct rcu_head *head) | |
179 | { | |
180 | struct test *node = | |
181 | caa_container_of(head, struct test, rcu); | |
182 | free(node); | |
183 | } | |
184 | ||
111ce0c3 MD |
185 | static |
186 | void do_test_pop(enum test_sync sync) | |
187 | { | |
188 | struct cds_lfs_node *snode; | |
189 | ||
190 | if (sync == TEST_SYNC_RCU) | |
191 | rcu_read_lock(); | |
192 | snode = __cds_lfs_pop(&s); | |
193 | if (sync == TEST_SYNC_RCU) | |
194 | rcu_read_unlock(); | |
195 | if (snode) { | |
196 | struct test *node; | |
197 | ||
f9b5c2b6 | 198 | snode->next = POISON_PTR; |
111ce0c3 MD |
199 | node = caa_container_of(snode, |
200 | struct test, list); | |
201 | if (sync == TEST_SYNC_RCU) | |
202 | call_rcu(&node->rcu, free_node_cb); | |
203 | else | |
204 | free(node); | |
205 | URCU_TLS(nr_successful_dequeues)++; | |
206 | } | |
207 | URCU_TLS(nr_dequeues)++; | |
208 | } | |
209 | ||
210 | static | |
211 | void do_test_pop_all(enum test_sync sync) | |
212 | { | |
213 | struct cds_lfs_node *snode; | |
214 | struct cds_lfs_head *head; | |
215 | struct cds_lfs_node *n; | |
216 | ||
217 | head = __cds_lfs_pop_all(&s); | |
218 | cds_lfs_for_each_safe(head, snode, n) { | |
219 | struct test *node; | |
220 | ||
f9b5c2b6 | 221 | snode->next = POISON_PTR; |
111ce0c3 MD |
222 | node = caa_container_of(snode, struct test, list); |
223 | if (sync == TEST_SYNC_RCU) | |
224 | call_rcu(&node->rcu, free_node_cb); | |
225 | else | |
226 | free(node); | |
227 | URCU_TLS(nr_successful_dequeues)++; | |
228 | URCU_TLS(nr_dequeues)++; | |
229 | } | |
230 | ||
231 | } | |
232 | ||
eb314468 | 233 | static void *thr_dequeuer(void *_count) |
453629a9 MD |
234 | { |
235 | unsigned long long *count = _count; | |
f42bc7ef | 236 | unsigned int counter = 0; |
453629a9 | 237 | |
94df6318 MD |
238 | printf_verbose("thread_begin %s, tid %lu\n", |
239 | "dequeuer", urcu_get_thread_id()); | |
453629a9 MD |
240 | |
241 | set_affinity(); | |
242 | ||
453629a9 MD |
243 | rcu_register_thread(); |
244 | ||
9e4e7ad1 | 245 | wait_until_go(); |
453629a9 | 246 | |
01477510 | 247 | urcu_posix_assert(test_pop || test_pop_all); |
f3eb6ef1 | 248 | |
111ce0c3 | 249 | for (;;) { |
111ce0c3 MD |
250 | if (test_pop && test_pop_all) { |
251 | /* both pop and pop all */ | |
252 | if (counter & 1) | |
253 | do_test_pop(test_sync); | |
254 | else | |
255 | do_test_pop_all(test_sync); | |
256 | counter++; | |
257 | } else { | |
258 | if (test_pop) | |
259 | do_test_pop(test_sync); | |
260 | else | |
261 | do_test_pop_all(test_sync); | |
453629a9 | 262 | } |
111ce0c3 | 263 | |
a0b7f7ea | 264 | if (caa_unlikely(!test_duration_dequeue())) |
453629a9 | 265 | break; |
a0b7f7ea | 266 | if (caa_unlikely(rduration)) |
453629a9 MD |
267 | loop_sleep(rduration); |
268 | } | |
269 | ||
270 | rcu_unregister_thread(); | |
453629a9 | 271 | |
94df6318 MD |
272 | printf_verbose("dequeuer thread_end, tid %lu, " |
273 | "dequeues %llu, successful_dequeues %llu\n", | |
274 | urcu_get_thread_id(), | |
275 | URCU_TLS(nr_dequeues), | |
276 | URCU_TLS(nr_successful_dequeues)); | |
bd252a04 MD |
277 | count[0] = URCU_TLS(nr_dequeues); |
278 | count[1] = URCU_TLS(nr_successful_dequeues); | |
453629a9 MD |
279 | return ((void*)2); |
280 | } | |
281 | ||
bd23a6c6 | 282 | static void test_end(unsigned long long *nr_dequeues_l) |
453629a9 | 283 | { |
cb8818f0 | 284 | struct cds_lfs_node *snode; |
453629a9 MD |
285 | |
286 | do { | |
bd23a6c6 | 287 | snode = __cds_lfs_pop(&s); |
94aec122 MD |
288 | if (snode) { |
289 | struct test *node; | |
290 | ||
291 | node = caa_container_of(snode, struct test, list); | |
453629a9 | 292 | free(node); |
bd23a6c6 | 293 | (*nr_dequeues_l)++; |
453629a9 | 294 | } |
94aec122 | 295 | } while (snode); |
453629a9 MD |
296 | } |
297 | ||
70469b43 | 298 | static void show_usage(char **argv) |
453629a9 | 299 | { |
06637138 MD |
300 | printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n", |
301 | argv[0]); | |
302 | printf("OPTIONS:\n"); | |
303 | printf(" [-d delay] (enqueuer period (in loops))\n"); | |
304 | printf(" [-c duration] (dequeuer period (in loops))\n"); | |
305 | printf(" [-v] (verbose output)\n"); | |
306 | printf(" [-a cpu#] [-a cpu#]... (affinity)\n"); | |
307 | printf(" [-p] (test pop)\n"); | |
308 | printf(" [-P] (test pop_all, enabled by default)\n"); | |
309 | printf(" [-R] (use RCU external synchronization)\n"); | |
310 | printf(" Note: default: no external synchronization used.\n"); | |
453629a9 MD |
311 | printf("\n"); |
312 | } | |
313 | ||
314 | int main(int argc, char **argv) | |
315 | { | |
316 | int err; | |
317 | pthread_t *tid_enqueuer, *tid_dequeuer; | |
318 | void *tret; | |
319 | unsigned long long *count_enqueuer, *count_dequeuer; | |
320 | unsigned long long tot_enqueues = 0, tot_dequeues = 0; | |
321 | unsigned long long tot_successful_enqueues = 0, | |
322 | tot_successful_dequeues = 0; | |
323 | unsigned long long end_dequeues = 0; | |
324 | int i, a; | |
83e334d0 | 325 | unsigned int i_thr; |
453629a9 MD |
326 | |
327 | if (argc < 4) { | |
70469b43 | 328 | show_usage(argv); |
453629a9 MD |
329 | return -1; |
330 | } | |
331 | ||
332 | err = sscanf(argv[1], "%u", &nr_dequeuers); | |
333 | if (err != 1) { | |
70469b43 | 334 | show_usage(argv); |
453629a9 MD |
335 | return -1; |
336 | } | |
337 | ||
338 | err = sscanf(argv[2], "%u", &nr_enqueuers); | |
339 | if (err != 1) { | |
70469b43 | 340 | show_usage(argv); |
453629a9 MD |
341 | return -1; |
342 | } | |
83e334d0 | 343 | |
453629a9 MD |
344 | err = sscanf(argv[3], "%lu", &duration); |
345 | if (err != 1) { | |
70469b43 | 346 | show_usage(argv); |
453629a9 MD |
347 | return -1; |
348 | } | |
349 | ||
350 | for (i = 4; i < argc; i++) { | |
351 | if (argv[i][0] != '-') | |
352 | continue; | |
353 | switch (argv[i][1]) { | |
354 | case 'a': | |
355 | if (argc < i + 2) { | |
70469b43 | 356 | show_usage(argv); |
453629a9 MD |
357 | return -1; |
358 | } | |
359 | a = atoi(argv[++i]); | |
360 | cpu_affinities[next_aff++] = a; | |
361 | use_affinity = 1; | |
362 | printf_verbose("Adding CPU %d affinity\n", a); | |
363 | break; | |
364 | case 'c': | |
365 | if (argc < i + 2) { | |
70469b43 | 366 | show_usage(argv); |
453629a9 MD |
367 | return -1; |
368 | } | |
369 | rduration = atol(argv[++i]); | |
370 | break; | |
371 | case 'd': | |
372 | if (argc < i + 2) { | |
70469b43 | 373 | show_usage(argv); |
453629a9 MD |
374 | return -1; |
375 | } | |
376 | wdelay = atol(argv[++i]); | |
377 | break; | |
378 | case 'v': | |
379 | verbose_mode = 1; | |
380 | break; | |
111ce0c3 MD |
381 | case 'p': |
382 | test_pop = 1; | |
383 | break; | |
384 | case 'P': | |
385 | test_pop_all = 1; | |
386 | break; | |
387 | case 'R': | |
388 | test_sync = TEST_SYNC_RCU; | |
389 | break; | |
453629a9 MD |
390 | } |
391 | } | |
392 | ||
111ce0c3 MD |
393 | /* activate pop_all test by default */ |
394 | if (!test_pop && !test_pop_all) | |
395 | test_pop_all = 1; | |
396 | ||
453629a9 MD |
397 | printf_verbose("running test for %lu seconds, %u enqueuers, " |
398 | "%u dequeuers.\n", | |
399 | duration, nr_enqueuers, nr_dequeuers); | |
111ce0c3 MD |
400 | if (test_pop) |
401 | printf_verbose("pop test activated.\n"); | |
402 | if (test_pop_all) | |
403 | printf_verbose("pop_all test activated.\n"); | |
eb314468 MD |
404 | if (test_sync == TEST_SYNC_RCU) |
405 | printf_verbose("External sync: RCU.\n"); | |
406 | else | |
407 | printf_verbose("External sync: none.\n"); | |
453629a9 MD |
408 | printf_verbose("Writer delay : %lu loops.\n", rduration); |
409 | printf_verbose("Reader duration : %lu loops.\n", wdelay); | |
94df6318 MD |
410 | printf_verbose("thread %-6s, tid %lu\n", |
411 | "main", urcu_get_thread_id()); | |
453629a9 | 412 | |
9aa14175 MD |
413 | tid_enqueuer = calloc(nr_enqueuers, sizeof(*tid_enqueuer)); |
414 | tid_dequeuer = calloc(nr_dequeuers, sizeof(*tid_dequeuer)); | |
415 | count_enqueuer = calloc(nr_enqueuers, 2 * sizeof(*count_enqueuer)); | |
416 | count_dequeuer = calloc(nr_dequeuers, 2 * sizeof(*count_dequeuer)); | |
cb8818f0 | 417 | cds_lfs_init(&s); |
94aec122 | 418 | err = create_all_cpu_call_rcu_data(0); |
8bcbd94a MD |
419 | if (err) { |
420 | printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n"); | |
421 | } | |
453629a9 MD |
422 | |
423 | next_aff = 0; | |
424 | ||
83e334d0 MJ |
425 | for (i_thr = 0; i_thr < nr_enqueuers; i_thr++) { |
426 | err = pthread_create(&tid_enqueuer[i_thr], NULL, thr_enqueuer, | |
427 | &count_enqueuer[2 * i_thr]); | |
453629a9 MD |
428 | if (err != 0) |
429 | exit(1); | |
430 | } | |
83e334d0 MJ |
431 | for (i_thr = 0; i_thr < nr_dequeuers; i_thr++) { |
432 | err = pthread_create(&tid_dequeuer[i_thr], NULL, thr_dequeuer, | |
433 | &count_dequeuer[2 * i_thr]); | |
453629a9 MD |
434 | if (err != 0) |
435 | exit(1); | |
436 | } | |
437 | ||
5481ddb3 | 438 | cmm_smp_mb(); |
453629a9 | 439 | |
9e4e7ad1 | 440 | begin_test(); |
453629a9 | 441 | |
83e334d0 | 442 | for (i_thr = 0; i_thr < duration; i_thr++) { |
453629a9 | 443 | sleep(1); |
4b665350 MD |
444 | if (verbose_mode) { |
445 | fwrite(".", sizeof(char), 1, stdout); | |
446 | fflush(stdout); | |
447 | } | |
453629a9 MD |
448 | } |
449 | ||
9e4e7ad1 | 450 | end_test(); |
453629a9 | 451 | |
83e334d0 MJ |
452 | for (i_thr = 0; i_thr < nr_enqueuers; i_thr++) { |
453 | err = pthread_join(tid_enqueuer[i_thr], &tret); | |
453629a9 MD |
454 | if (err != 0) |
455 | exit(1); | |
83e334d0 MJ |
456 | tot_enqueues += count_enqueuer[2 * i_thr]; |
457 | tot_successful_enqueues += count_enqueuer[2 * i_thr + 1]; | |
453629a9 | 458 | } |
83e334d0 MJ |
459 | for (i_thr = 0; i_thr < nr_dequeuers; i_thr++) { |
460 | err = pthread_join(tid_dequeuer[i_thr], &tret); | |
453629a9 MD |
461 | if (err != 0) |
462 | exit(1); | |
83e334d0 MJ |
463 | tot_dequeues += count_dequeuer[2 * i_thr]; |
464 | tot_successful_dequeues += count_dequeuer[2 * i_thr + 1]; | |
453629a9 | 465 | } |
83e334d0 | 466 | |
bd23a6c6 | 467 | test_end(&end_dequeues); |
453629a9 MD |
468 | |
469 | printf_verbose("total number of enqueues : %llu, dequeues %llu\n", | |
470 | tot_enqueues, tot_dequeues); | |
471 | printf_verbose("total number of successful enqueues : %llu, " | |
472 | "successful dequeues %llu\n", | |
473 | tot_successful_enqueues, tot_successful_dequeues); | |
474 | printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu " | |
475 | "nr_dequeuers %3u " | |
476 | "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu " | |
477 | "successful enqueues %12llu successful dequeues %12llu " | |
478 | "end_dequeues %llu nr_ops %12llu\n", | |
479 | argv[0], duration, nr_enqueuers, wdelay, | |
480 | nr_dequeuers, rduration, tot_enqueues, tot_dequeues, | |
481 | tot_successful_enqueues, | |
482 | tot_successful_dequeues, end_dequeues, | |
483 | tot_enqueues + tot_dequeues); | |
484 | if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) | |
485 | printf("WARNING! Discrepancy between nr succ. enqueues %llu vs " | |
486 | "succ. dequeues + end dequeues %llu.\n", | |
487 | tot_successful_enqueues, | |
488 | tot_successful_dequeues + end_dequeues); | |
489 | ||
0938c541 | 490 | free_all_cpu_call_rcu_data(); |
2af1c19e | 491 | cds_lfs_destroy(&s); |
453629a9 MD |
492 | free(count_enqueuer); |
493 | free(count_dequeuer); | |
494 | free(tid_enqueuer); | |
495 | free(tid_dequeuer); | |
83e334d0 | 496 | |
453629a9 MD |
497 | return 0; |
498 | } |