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