wfstack: document first/next return values
[urcu.git] / tests / test_urcu_wfs.c
CommitLineData
a5eb5ffc
MD
1/*
2 * test_urcu_wfs.c
3 *
4 * Userspace RCU library - example RCU-based lock-free stack
5 *
6 * Copyright February 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
24#define _GNU_SOURCE
25#include "../config.h"
26#include <stdio.h>
27#include <pthread.h>
28#include <stdlib.h>
29#include <stdint.h>
30#include <stdbool.h>
31#include <string.h>
32#include <sys/types.h>
33#include <sys/wait.h>
34#include <unistd.h>
35#include <stdio.h>
36#include <assert.h>
a5eb5ffc
MD
37#include <sched.h>
38#include <errno.h>
39
40#include <urcu/arch.h>
bd252a04 41#include <urcu/tls-compat.h>
a5eb5ffc 42
65fcc7e9
MD
43#ifdef __linux__
44#include <syscall.h>
45#endif
46
a5eb5ffc
MD
47/* hardcoded number of CPUs */
48#define NR_CPUS 16384
49
50#if defined(_syscall0)
51_syscall0(pid_t, gettid)
52#elif defined(__NR_gettid)
53static inline pid_t gettid(void)
54{
55 return syscall(__NR_gettid);
56}
57#else
58#warning "use pid as tid"
59static inline pid_t gettid(void)
60{
61 return getpid();
62}
63#endif
64
65#ifndef DYNAMIC_LINK_TEST
66#define _LGPL_SOURCE
67#endif
68#include <urcu.h>
cb3f3d6b 69#include <urcu/wfstack.h>
a5eb5ffc 70
69328034
MD
71/*
72 * External synchronization used.
73 */
74enum test_sync {
75 TEST_SYNC_NONE = 0,
76 TEST_SYNC_MUTEX,
77};
78
79static enum test_sync test_sync;
80
0ee400bd
MD
81static int test_force_sync;
82
a5eb5ffc
MD
83static volatile int test_go, test_stop;
84
85static unsigned long rduration;
86
87static unsigned long duration;
88
89/* read-side C.S. duration, in loops */
90static unsigned long wdelay;
91
ab0aacbe 92static inline void loop_sleep(unsigned long loops)
a5eb5ffc 93{
ab0aacbe 94 while (loops-- != 0)
06f22bdb 95 caa_cpu_relax();
a5eb5ffc
MD
96}
97
98static int verbose_mode;
99
69328034
MD
100static int test_pop, test_pop_all;
101
a5eb5ffc
MD
102#define printf_verbose(fmt, args...) \
103 do { \
104 if (verbose_mode) \
69328034 105 printf(fmt, ## args); \
a5eb5ffc
MD
106 } while (0)
107
108static unsigned int cpu_affinities[NR_CPUS];
109static unsigned int next_aff = 0;
110static int use_affinity = 0;
111
112pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
113
114#ifndef HAVE_CPU_SET_T
115typedef unsigned long cpu_set_t;
116# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
117# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
118#endif
119
120static void set_affinity(void)
121{
95bc7fb9 122#if HAVE_SCHED_SETAFFINITY
a5eb5ffc 123 cpu_set_t mask;
95bc7fb9
MD
124 int cpu, ret;
125#endif /* HAVE_SCHED_SETAFFINITY */
a5eb5ffc
MD
126
127 if (!use_affinity)
128 return;
129
130#if HAVE_SCHED_SETAFFINITY
131 ret = pthread_mutex_lock(&affinity_mutex);
132 if (ret) {
133 perror("Error in pthread mutex lock");
134 exit(-1);
135 }
136 cpu = cpu_affinities[next_aff++];
137 ret = pthread_mutex_unlock(&affinity_mutex);
138 if (ret) {
139 perror("Error in pthread mutex unlock");
140 exit(-1);
141 }
142
143 CPU_ZERO(&mask);
144 CPU_SET(cpu, &mask);
145#if SCHED_SETAFFINITY_ARGS == 2
146 sched_setaffinity(0, &mask);
147#else
148 sched_setaffinity(0, sizeof(mask), &mask);
149#endif
150#endif /* HAVE_SCHED_SETAFFINITY */
151}
152
153/*
154 * returns 0 if test should end.
155 */
156static int test_duration_dequeue(void)
157{
158 return !test_stop;
159}
160
161static int test_duration_enqueue(void)
162{
163 return !test_stop;
164}
165
bd252a04
MD
166static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
167static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
a5eb5ffc 168
bd252a04
MD
169static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
170static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
a5eb5ffc
MD
171
172static unsigned int nr_enqueuers;
173static unsigned int nr_dequeuers;
174
16aa9ee8 175static struct cds_wfs_stack s;
a5eb5ffc 176
98fbc848 177static void *thr_enqueuer(void *_count)
a5eb5ffc
MD
178{
179 unsigned long long *count = _count;
180
181 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
6ff854ac
MD
182 "enqueuer", (unsigned long) pthread_self(),
183 (unsigned long) gettid());
a5eb5ffc
MD
184
185 set_affinity();
186
a5eb5ffc
MD
187 while (!test_go)
188 {
189 }
5481ddb3 190 cmm_smp_mb();
a5eb5ffc
MD
191
192 for (;;) {
16aa9ee8 193 struct cds_wfs_node *node = malloc(sizeof(*node));
a5eb5ffc
MD
194 if (!node)
195 goto fail;
16aa9ee8
DG
196 cds_wfs_node_init(node);
197 cds_wfs_push(&s, node);
bd252a04 198 URCU_TLS(nr_successful_enqueues)++;
a5eb5ffc 199
a0b7f7ea 200 if (caa_unlikely(wdelay))
a5eb5ffc
MD
201 loop_sleep(wdelay);
202fail:
bd252a04 203 URCU_TLS(nr_enqueues)++;
a0b7f7ea 204 if (caa_unlikely(!test_duration_enqueue()))
a5eb5ffc
MD
205 break;
206 }
207
bd252a04
MD
208 count[0] = URCU_TLS(nr_enqueues);
209 count[1] = URCU_TLS(nr_successful_enqueues);
a5eb5ffc
MD
210 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
211 "enqueues %llu successful_enqueues %llu\n",
6ff854ac
MD
212 pthread_self(),
213 (unsigned long) gettid(),
bd252a04 214 URCU_TLS(nr_enqueues), URCU_TLS(nr_successful_enqueues));
a5eb5ffc
MD
215 return ((void*)1);
216
217}
218
69328034
MD
219static void do_test_pop(enum test_sync sync)
220{
221 struct cds_wfs_node *node;
222
223 if (sync == TEST_SYNC_MUTEX)
224 cds_wfs_pop_lock(&s);
225 node = __cds_wfs_pop_blocking(&s);
226 if (sync == TEST_SYNC_MUTEX)
227 cds_wfs_pop_unlock(&s);
228
229 if (node) {
230 free(node);
231 URCU_TLS(nr_successful_dequeues)++;
232 }
233 URCU_TLS(nr_dequeues)++;
234}
235
236static void do_test_pop_all(enum test_sync sync)
237{
238 struct cds_wfs_head *head;
239 struct cds_wfs_node *node, *n;
240
241 if (sync == TEST_SYNC_MUTEX)
242 cds_wfs_pop_lock(&s);
243 head = __cds_wfs_pop_all(&s);
244 if (sync == TEST_SYNC_MUTEX)
245 cds_wfs_pop_unlock(&s);
246
247 cds_wfs_for_each_blocking_safe(head, node, n) {
248 free(node);
249 URCU_TLS(nr_successful_dequeues)++;
250 URCU_TLS(nr_dequeues)++;
251 }
252}
253
98fbc848 254static void *thr_dequeuer(void *_count)
a5eb5ffc
MD
255{
256 unsigned long long *count = _count;
69328034 257 unsigned int counter;
a5eb5ffc
MD
258
259 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
6ff854ac
MD
260 "dequeuer", (unsigned long) pthread_self(),
261 (unsigned long) gettid());
a5eb5ffc
MD
262
263 set_affinity();
264
a5eb5ffc
MD
265 while (!test_go)
266 {
267 }
5481ddb3 268 cmm_smp_mb();
a5eb5ffc 269
69328034 270 assert(test_pop || test_pop_all);
a5eb5ffc 271
69328034
MD
272 for (;;) {
273 if (test_pop && test_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);
a5eb5ffc
MD
284 }
285
a0b7f7ea 286 if (caa_unlikely(!test_duration_dequeue()))
a5eb5ffc 287 break;
a0b7f7ea 288 if (caa_unlikely(rduration))
a5eb5ffc
MD
289 loop_sleep(rduration);
290 }
291
a5eb5ffc
MD
292 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
293 "dequeues %llu, successful_dequeues %llu\n",
6ff854ac
MD
294 pthread_self(),
295 (unsigned long) gettid(),
bd252a04
MD
296 URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues));
297 count[0] = URCU_TLS(nr_dequeues);
298 count[1] = URCU_TLS(nr_successful_dequeues);
a5eb5ffc
MD
299 return ((void*)2);
300}
301
98fbc848 302static void test_end(struct cds_wfs_stack *s, unsigned long long *nr_dequeues)
a5eb5ffc 303{
16aa9ee8 304 struct cds_wfs_node *node;
a5eb5ffc
MD
305
306 do {
16aa9ee8 307 node = cds_wfs_pop_blocking(s);
a5eb5ffc
MD
308 if (node) {
309 free(node);
310 (*nr_dequeues)++;
311 }
312 } while (node);
313}
314
98fbc848 315static void show_usage(int argc, char **argv)
a5eb5ffc
MD
316{
317 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv[0]);
318 printf(" [-d delay] (enqueuer period (in loops))");
319 printf(" [-c duration] (dequeuer period (in loops))");
320 printf(" [-v] (verbose output)");
321 printf(" [-a cpu#] [-a cpu#]... (affinity)");
69328034
MD
322 printf(" [-p] (test pop)");
323 printf(" [-P] (test pop_all, enabled by default)");
324 printf(" [-M] (use mutex external synchronization)");
325 printf(" Note: default: no external synchronization used.");
0ee400bd 326 printf(" [-f] (force user-provided synchronization)");
a5eb5ffc
MD
327 printf("\n");
328}
329
330int main(int argc, char **argv)
331{
332 int err;
333 pthread_t *tid_enqueuer, *tid_dequeuer;
334 void *tret;
335 unsigned long long *count_enqueuer, *count_dequeuer;
336 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
337 unsigned long long tot_successful_enqueues = 0,
338 tot_successful_dequeues = 0;
339 unsigned long long end_dequeues = 0;
340 int i, a;
341
342 if (argc < 4) {
343 show_usage(argc, argv);
344 return -1;
345 }
346
347 err = sscanf(argv[1], "%u", &nr_dequeuers);
348 if (err != 1) {
349 show_usage(argc, argv);
350 return -1;
351 }
352
353 err = sscanf(argv[2], "%u", &nr_enqueuers);
354 if (err != 1) {
355 show_usage(argc, argv);
356 return -1;
357 }
358
359 err = sscanf(argv[3], "%lu", &duration);
360 if (err != 1) {
361 show_usage(argc, argv);
362 return -1;
363 }
364
365 for (i = 4; i < argc; i++) {
366 if (argv[i][0] != '-')
367 continue;
368 switch (argv[i][1]) {
369 case 'a':
370 if (argc < i + 2) {
371 show_usage(argc, argv);
372 return -1;
373 }
374 a = atoi(argv[++i]);
375 cpu_affinities[next_aff++] = a;
376 use_affinity = 1;
377 printf_verbose("Adding CPU %d affinity\n", a);
378 break;
379 case 'c':
380 if (argc < i + 2) {
381 show_usage(argc, argv);
382 return -1;
383 }
384 rduration = atol(argv[++i]);
385 break;
386 case 'd':
387 if (argc < i + 2) {
388 show_usage(argc, argv);
389 return -1;
390 }
391 wdelay = atol(argv[++i]);
392 break;
393 case 'v':
394 verbose_mode = 1;
395 break;
69328034
MD
396 case 'p':
397 test_pop = 1;
398 break;
399 case 'P':
400 test_pop_all = 1;
401 break;
402 case 'M':
403 test_sync = TEST_SYNC_MUTEX;
404 break;
0ee400bd
MD
405 case 'f':
406 test_force_sync = 1;
407 break;
a5eb5ffc
MD
408 }
409 }
410
69328034
MD
411 /* activate pop_all test by default */
412 if (!test_pop && !test_pop_all)
413 test_pop_all = 1;
414
0ee400bd
MD
415 if (test_sync == TEST_SYNC_NONE && nr_dequeuers > 1 && test_pop) {
416 if (test_force_sync) {
417 fprintf(stderr, "[WARNING] Using pop concurrently "
418 "with other pop or pop_all without external "
419 "synchronization. Expect run-time failure.\n");
420 } else {
421 printf("Enforcing mutex synchronization\n");
422 test_sync = TEST_SYNC_MUTEX;
423 }
424 }
425
a5eb5ffc
MD
426 printf_verbose("running test for %lu seconds, %u enqueuers, "
427 "%u dequeuers.\n",
428 duration, nr_enqueuers, nr_dequeuers);
69328034
MD
429 if (test_pop)
430 printf_verbose("pop test activated.\n");
431 if (test_pop_all)
432 printf_verbose("pop_all test activated.\n");
98fbc848
MD
433 if (test_sync == TEST_SYNC_MUTEX)
434 printf_verbose("External sync: mutex.\n");
435 else
436 printf_verbose("External sync: none.\n");
a5eb5ffc
MD
437 printf_verbose("Writer delay : %lu loops.\n", rduration);
438 printf_verbose("Reader duration : %lu loops.\n", wdelay);
439 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
6ff854ac
MD
440 "main", (unsigned long) pthread_self(),
441 (unsigned long) gettid());
a5eb5ffc
MD
442
443 tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers);
444 tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers);
445 count_enqueuer = malloc(2 * sizeof(*count_enqueuer) * nr_enqueuers);
446 count_dequeuer = malloc(2 * sizeof(*count_dequeuer) * nr_dequeuers);
16aa9ee8 447 cds_wfs_init(&s);
a5eb5ffc
MD
448
449 next_aff = 0;
450
451 for (i = 0; i < nr_enqueuers; i++) {
452 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
453 &count_enqueuer[2 * i]);
454 if (err != 0)
455 exit(1);
456 }
457 for (i = 0; i < nr_dequeuers; i++) {
458 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
459 &count_dequeuer[2 * i]);
460 if (err != 0)
461 exit(1);
462 }
463
5481ddb3 464 cmm_smp_mb();
a5eb5ffc
MD
465
466 test_go = 1;
467
468 for (i = 0; i < duration; i++) {
469 sleep(1);
470 if (verbose_mode)
471 write (1, ".", 1);
472 }
473
474 test_stop = 1;
475
476 for (i = 0; i < nr_enqueuers; i++) {
477 err = pthread_join(tid_enqueuer[i], &tret);
478 if (err != 0)
479 exit(1);
480 tot_enqueues += count_enqueuer[2 * i];
481 tot_successful_enqueues += count_enqueuer[2 * i + 1];
482 }
483 for (i = 0; i < nr_dequeuers; i++) {
484 err = pthread_join(tid_dequeuer[i], &tret);
485 if (err != 0)
486 exit(1);
487 tot_dequeues += count_dequeuer[2 * i];
488 tot_successful_dequeues += count_dequeuer[2 * i + 1];
489 }
490
491 test_end(&s, &end_dequeues);
492
493 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
494 tot_enqueues, tot_dequeues);
495 printf_verbose("total number of successful enqueues : %llu, "
496 "successful dequeues %llu\n",
497 tot_successful_enqueues, tot_successful_dequeues);
498 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
499 "nr_dequeuers %3u "
500 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
501 "successful enqueues %12llu successful dequeues %12llu "
502 "end_dequeues %llu nr_ops %12llu\n",
503 argv[0], duration, nr_enqueuers, wdelay,
504 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
505 tot_successful_enqueues,
506 tot_successful_dequeues, end_dequeues,
507 tot_enqueues + tot_dequeues);
508 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues)
509 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
510 "succ. dequeues + end dequeues %llu.\n",
511 tot_successful_enqueues,
512 tot_successful_dequeues + end_dequeues);
513
514 free(count_enqueuer);
515 free(count_dequeuer);
516 free(tid_enqueuer);
517 free(tid_dequeuer);
518 return 0;
519}
This page took 0.0747640000000001 seconds and 4 git commands to generate.