Fix tests: finer-grained use of CPU_SET, CPU_ZERO and cpu_set_t
[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 <errno.h>
38
39#include <urcu/arch.h>
bd252a04 40#include <urcu/tls-compat.h>
96a0c0cd 41#include <urcu/uatomic.h>
2953b501 42#include "cpuset.h"
a5eb5ffc 43
65fcc7e9
MD
44#ifdef __linux__
45#include <syscall.h>
46#endif
47
a5eb5ffc
MD
48/* hardcoded number of CPUs */
49#define NR_CPUS 16384
50
51#if defined(_syscall0)
52_syscall0(pid_t, gettid)
53#elif defined(__NR_gettid)
54static inline pid_t gettid(void)
55{
56 return syscall(__NR_gettid);
57}
58#else
59#warning "use pid as tid"
60static inline pid_t gettid(void)
61{
62 return getpid();
63}
64#endif
65
66#ifndef DYNAMIC_LINK_TEST
67#define _LGPL_SOURCE
68#endif
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
d05e9936 83static volatile int test_go, test_stop_enqueue, test_stop_dequeue;
a5eb5ffc
MD
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
d05e9936
MD
100static int test_pop, test_pop_all, test_wait_empty;
101static int test_enqueue_stopped;
69328034 102
a5eb5ffc
MD
103#define printf_verbose(fmt, args...) \
104 do { \
105 if (verbose_mode) \
69328034 106 printf(fmt, ## args); \
a5eb5ffc
MD
107 } while (0)
108
109static unsigned int cpu_affinities[NR_CPUS];
110static unsigned int next_aff = 0;
111static int use_affinity = 0;
112
113pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
114
a5eb5ffc
MD
115static void set_affinity(void)
116{
95bc7fb9 117#if HAVE_SCHED_SETAFFINITY
a5eb5ffc 118 cpu_set_t mask;
95bc7fb9
MD
119 int cpu, ret;
120#endif /* HAVE_SCHED_SETAFFINITY */
a5eb5ffc
MD
121
122 if (!use_affinity)
123 return;
124
125#if HAVE_SCHED_SETAFFINITY
126 ret = pthread_mutex_lock(&affinity_mutex);
127 if (ret) {
128 perror("Error in pthread mutex lock");
129 exit(-1);
130 }
131 cpu = cpu_affinities[next_aff++];
132 ret = pthread_mutex_unlock(&affinity_mutex);
133 if (ret) {
134 perror("Error in pthread mutex unlock");
135 exit(-1);
136 }
137
138 CPU_ZERO(&mask);
139 CPU_SET(cpu, &mask);
140#if SCHED_SETAFFINITY_ARGS == 2
141 sched_setaffinity(0, &mask);
142#else
143 sched_setaffinity(0, sizeof(mask), &mask);
144#endif
145#endif /* HAVE_SCHED_SETAFFINITY */
146}
147
148/*
149 * returns 0 if test should end.
150 */
151static int test_duration_dequeue(void)
152{
d05e9936 153 return !test_stop_dequeue;
a5eb5ffc
MD
154}
155
156static int test_duration_enqueue(void)
157{
d05e9936 158 return !test_stop_enqueue;
a5eb5ffc
MD
159}
160
bd252a04
MD
161static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
162static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
a5eb5ffc 163
bd252a04
MD
164static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
165static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
d05e9936
MD
166static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues);
167static DEFINE_URCU_TLS(unsigned long long, nr_pop_all);
a5eb5ffc
MD
168
169static unsigned int nr_enqueuers;
170static unsigned int nr_dequeuers;
171
16aa9ee8 172static struct cds_wfs_stack s;
a5eb5ffc 173
98fbc848 174static void *thr_enqueuer(void *_count)
a5eb5ffc
MD
175{
176 unsigned long long *count = _count;
d05e9936 177 bool was_nonempty;
a5eb5ffc
MD
178
179 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
6ff854ac
MD
180 "enqueuer", (unsigned long) pthread_self(),
181 (unsigned long) gettid());
a5eb5ffc
MD
182
183 set_affinity();
184
a5eb5ffc
MD
185 while (!test_go)
186 {
187 }
5481ddb3 188 cmm_smp_mb();
a5eb5ffc
MD
189
190 for (;;) {
16aa9ee8 191 struct cds_wfs_node *node = malloc(sizeof(*node));
a5eb5ffc
MD
192 if (!node)
193 goto fail;
16aa9ee8 194 cds_wfs_node_init(node);
d05e9936 195 was_nonempty = cds_wfs_push(&s, node);
bd252a04 196 URCU_TLS(nr_successful_enqueues)++;
d05e9936
MD
197 if (!was_nonempty)
198 URCU_TLS(nr_empty_dest_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
d05e9936 208 uatomic_inc(&test_enqueue_stopped);
bd252a04
MD
209 count[0] = URCU_TLS(nr_enqueues);
210 count[1] = URCU_TLS(nr_successful_enqueues);
d05e9936 211 count[2] = URCU_TLS(nr_empty_dest_enqueues);
a5eb5ffc 212 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
d05e9936
MD
213 "enqueues %llu successful_enqueues %llu, "
214 "empty_dest_enqueues %llu\n",
6ff854ac
MD
215 pthread_self(),
216 (unsigned long) gettid(),
d05e9936
MD
217 URCU_TLS(nr_enqueues),
218 URCU_TLS(nr_successful_enqueues),
219 URCU_TLS(nr_empty_dest_enqueues));
a5eb5ffc
MD
220 return ((void*)1);
221
222}
223
69328034
MD
224static void do_test_pop(enum test_sync sync)
225{
226 struct cds_wfs_node *node;
227
228 if (sync == TEST_SYNC_MUTEX)
229 cds_wfs_pop_lock(&s);
230 node = __cds_wfs_pop_blocking(&s);
231 if (sync == TEST_SYNC_MUTEX)
232 cds_wfs_pop_unlock(&s);
233
234 if (node) {
235 free(node);
236 URCU_TLS(nr_successful_dequeues)++;
237 }
238 URCU_TLS(nr_dequeues)++;
239}
240
241static void do_test_pop_all(enum test_sync sync)
242{
243 struct cds_wfs_head *head;
244 struct cds_wfs_node *node, *n;
245
246 if (sync == TEST_SYNC_MUTEX)
247 cds_wfs_pop_lock(&s);
248 head = __cds_wfs_pop_all(&s);
249 if (sync == TEST_SYNC_MUTEX)
250 cds_wfs_pop_unlock(&s);
251
d05e9936 252 /* Check if empty */
c7ba06ba 253 if (cds_wfs_first(head) == NULL)
d05e9936
MD
254 return;
255
256 URCU_TLS(nr_pop_all)++;
257
69328034
MD
258 cds_wfs_for_each_blocking_safe(head, node, n) {
259 free(node);
260 URCU_TLS(nr_successful_dequeues)++;
261 URCU_TLS(nr_dequeues)++;
262 }
263}
264
98fbc848 265static void *thr_dequeuer(void *_count)
a5eb5ffc
MD
266{
267 unsigned long long *count = _count;
69328034 268 unsigned int counter;
a5eb5ffc
MD
269
270 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
6ff854ac
MD
271 "dequeuer", (unsigned long) pthread_self(),
272 (unsigned long) gettid());
a5eb5ffc
MD
273
274 set_affinity();
275
a5eb5ffc
MD
276 while (!test_go)
277 {
278 }
5481ddb3 279 cmm_smp_mb();
a5eb5ffc 280
69328034 281 assert(test_pop || test_pop_all);
a5eb5ffc 282
69328034
MD
283 for (;;) {
284 if (test_pop && test_pop_all) {
285 if (counter & 1)
286 do_test_pop(test_sync);
287 else
288 do_test_pop_all(test_sync);
289 counter++;
290 } else {
291 if (test_pop)
292 do_test_pop(test_sync);
293 else
294 do_test_pop_all(test_sync);
a5eb5ffc
MD
295 }
296
a0b7f7ea 297 if (caa_unlikely(!test_duration_dequeue()))
a5eb5ffc 298 break;
a0b7f7ea 299 if (caa_unlikely(rduration))
a5eb5ffc
MD
300 loop_sleep(rduration);
301 }
302
a5eb5ffc 303 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
d05e9936
MD
304 "dequeues %llu, successful_dequeues %llu "
305 "pop_all %llu\n",
6ff854ac
MD
306 pthread_self(),
307 (unsigned long) gettid(),
d05e9936
MD
308 URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues),
309 URCU_TLS(nr_pop_all));
bd252a04
MD
310 count[0] = URCU_TLS(nr_dequeues);
311 count[1] = URCU_TLS(nr_successful_dequeues);
d05e9936 312 count[2] = URCU_TLS(nr_pop_all);
a5eb5ffc
MD
313 return ((void*)2);
314}
315
98fbc848 316static void test_end(struct cds_wfs_stack *s, unsigned long long *nr_dequeues)
a5eb5ffc 317{
16aa9ee8 318 struct cds_wfs_node *node;
a5eb5ffc
MD
319
320 do {
16aa9ee8 321 node = cds_wfs_pop_blocking(s);
a5eb5ffc
MD
322 if (node) {
323 free(node);
324 (*nr_dequeues)++;
325 }
326 } while (node);
327}
328
98fbc848 329static void show_usage(int argc, char **argv)
a5eb5ffc
MD
330{
331 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv[0]);
332 printf(" [-d delay] (enqueuer period (in loops))");
333 printf(" [-c duration] (dequeuer period (in loops))");
334 printf(" [-v] (verbose output)");
335 printf(" [-a cpu#] [-a cpu#]... (affinity)");
69328034
MD
336 printf(" [-p] (test pop)");
337 printf(" [-P] (test pop_all, enabled by default)");
338 printf(" [-M] (use mutex external synchronization)");
339 printf(" Note: default: no external synchronization used.");
0ee400bd 340 printf(" [-f] (force user-provided synchronization)");
d05e9936 341 printf(" [-w] Wait for dequeuer to empty stack");
a5eb5ffc
MD
342 printf("\n");
343}
344
345int main(int argc, char **argv)
346{
347 int err;
348 pthread_t *tid_enqueuer, *tid_dequeuer;
349 void *tret;
350 unsigned long long *count_enqueuer, *count_dequeuer;
351 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
352 unsigned long long tot_successful_enqueues = 0,
d05e9936
MD
353 tot_successful_dequeues = 0,
354 tot_empty_dest_enqueues = 0,
355 tot_pop_all = 0;
a5eb5ffc 356 unsigned long long end_dequeues = 0;
d05e9936 357 int i, a, retval = 0;
a5eb5ffc
MD
358
359 if (argc < 4) {
360 show_usage(argc, argv);
361 return -1;
362 }
363
364 err = sscanf(argv[1], "%u", &nr_dequeuers);
365 if (err != 1) {
366 show_usage(argc, argv);
367 return -1;
368 }
369
370 err = sscanf(argv[2], "%u", &nr_enqueuers);
371 if (err != 1) {
372 show_usage(argc, argv);
373 return -1;
374 }
375
376 err = sscanf(argv[3], "%lu", &duration);
377 if (err != 1) {
378 show_usage(argc, argv);
379 return -1;
380 }
381
382 for (i = 4; i < argc; i++) {
383 if (argv[i][0] != '-')
384 continue;
385 switch (argv[i][1]) {
386 case 'a':
387 if (argc < i + 2) {
388 show_usage(argc, argv);
389 return -1;
390 }
391 a = atoi(argv[++i]);
392 cpu_affinities[next_aff++] = a;
393 use_affinity = 1;
394 printf_verbose("Adding CPU %d affinity\n", a);
395 break;
396 case 'c':
397 if (argc < i + 2) {
398 show_usage(argc, argv);
399 return -1;
400 }
401 rduration = atol(argv[++i]);
402 break;
403 case 'd':
404 if (argc < i + 2) {
405 show_usage(argc, argv);
406 return -1;
407 }
408 wdelay = atol(argv[++i]);
409 break;
410 case 'v':
411 verbose_mode = 1;
412 break;
69328034
MD
413 case 'p':
414 test_pop = 1;
415 break;
416 case 'P':
417 test_pop_all = 1;
418 break;
419 case 'M':
420 test_sync = TEST_SYNC_MUTEX;
421 break;
d05e9936
MD
422 case 'w':
423 test_wait_empty = 1;
424 break;
0ee400bd
MD
425 case 'f':
426 test_force_sync = 1;
427 break;
a5eb5ffc
MD
428 }
429 }
430
69328034
MD
431 /* activate pop_all test by default */
432 if (!test_pop && !test_pop_all)
433 test_pop_all = 1;
434
0ee400bd
MD
435 if (test_sync == TEST_SYNC_NONE && nr_dequeuers > 1 && test_pop) {
436 if (test_force_sync) {
437 fprintf(stderr, "[WARNING] Using pop concurrently "
438 "with other pop or pop_all without external "
439 "synchronization. Expect run-time failure.\n");
440 } else {
441 printf("Enforcing mutex synchronization\n");
442 test_sync = TEST_SYNC_MUTEX;
443 }
444 }
445
a5eb5ffc
MD
446 printf_verbose("running test for %lu seconds, %u enqueuers, "
447 "%u dequeuers.\n",
448 duration, nr_enqueuers, nr_dequeuers);
69328034
MD
449 if (test_pop)
450 printf_verbose("pop test activated.\n");
451 if (test_pop_all)
452 printf_verbose("pop_all test activated.\n");
98fbc848
MD
453 if (test_sync == TEST_SYNC_MUTEX)
454 printf_verbose("External sync: mutex.\n");
455 else
456 printf_verbose("External sync: none.\n");
d05e9936
MD
457 if (test_wait_empty)
458 printf_verbose("Wait for dequeuers to empty stack.\n");
a5eb5ffc
MD
459 printf_verbose("Writer delay : %lu loops.\n", rduration);
460 printf_verbose("Reader duration : %lu loops.\n", wdelay);
461 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
6ff854ac
MD
462 "main", (unsigned long) pthread_self(),
463 (unsigned long) gettid());
a5eb5ffc
MD
464
465 tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers);
466 tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers);
d05e9936
MD
467 count_enqueuer = malloc(3 * sizeof(*count_enqueuer) * nr_enqueuers);
468 count_dequeuer = malloc(3 * sizeof(*count_dequeuer) * nr_dequeuers);
16aa9ee8 469 cds_wfs_init(&s);
a5eb5ffc
MD
470
471 next_aff = 0;
472
473 for (i = 0; i < nr_enqueuers; i++) {
474 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
d05e9936 475 &count_enqueuer[3 * i]);
a5eb5ffc
MD
476 if (err != 0)
477 exit(1);
478 }
479 for (i = 0; i < nr_dequeuers; i++) {
480 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
d05e9936 481 &count_dequeuer[3 * i]);
a5eb5ffc
MD
482 if (err != 0)
483 exit(1);
484 }
485
5481ddb3 486 cmm_smp_mb();
a5eb5ffc
MD
487
488 test_go = 1;
489
490 for (i = 0; i < duration; i++) {
491 sleep(1);
492 if (verbose_mode)
493 write (1, ".", 1);
494 }
495
d05e9936
MD
496 test_stop_enqueue = 1;
497
498 if (test_wait_empty) {
499 while (nr_enqueuers != uatomic_read(&test_enqueue_stopped)) {
500 sleep(1);
501 }
502 while (!cds_wfs_empty(&s)) {
503 sleep(1);
504 }
505 }
506
507 test_stop_dequeue = 1;
a5eb5ffc
MD
508
509 for (i = 0; i < nr_enqueuers; i++) {
510 err = pthread_join(tid_enqueuer[i], &tret);
511 if (err != 0)
512 exit(1);
d05e9936
MD
513 tot_enqueues += count_enqueuer[3 * i];
514 tot_successful_enqueues += count_enqueuer[3 * i + 1];
515 tot_empty_dest_enqueues += count_enqueuer[3 * i + 2];
a5eb5ffc
MD
516 }
517 for (i = 0; i < nr_dequeuers; i++) {
518 err = pthread_join(tid_dequeuer[i], &tret);
519 if (err != 0)
520 exit(1);
d05e9936
MD
521 tot_dequeues += count_dequeuer[3 * i];
522 tot_successful_dequeues += count_dequeuer[3 * i + 1];
523 tot_pop_all += count_dequeuer[3 * i + 2];
a5eb5ffc
MD
524 }
525
526 test_end(&s, &end_dequeues);
527
528 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
529 tot_enqueues, tot_dequeues);
530 printf_verbose("total number of successful enqueues : %llu, "
d05e9936
MD
531 "enqueues to empty dest : %llu, "
532 "successful dequeues %llu, "
533 "pop_all : %llu\n",
534 tot_successful_enqueues,
535 tot_empty_dest_enqueues,
536 tot_successful_dequeues,
537 tot_pop_all);
a5eb5ffc
MD
538 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
539 "nr_dequeuers %3u "
540 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
d05e9936
MD
541 "successful enqueues %12llu enqueues to empty dest %12llu "
542 "successful dequeues %12llu pop_all %12llu "
a5eb5ffc
MD
543 "end_dequeues %llu nr_ops %12llu\n",
544 argv[0], duration, nr_enqueuers, wdelay,
545 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
546 tot_successful_enqueues,
d05e9936
MD
547 tot_empty_dest_enqueues,
548 tot_successful_dequeues, tot_pop_all, end_dequeues,
a5eb5ffc 549 tot_enqueues + tot_dequeues);
d05e9936 550 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) {
a5eb5ffc
MD
551 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
552 "succ. dequeues + end dequeues %llu.\n",
553 tot_successful_enqueues,
554 tot_successful_dequeues + end_dequeues);
d05e9936
MD
555 retval = 1;
556 }
557 /*
558 * If only using pop_all to dequeue, the enqueuer should see
559 * exactly as many empty queues than the number of non-empty
560 * stacks dequeued.
561 */
562 if (test_wait_empty && test_pop_all && !test_pop
563 && tot_empty_dest_enqueues != tot_pop_all) {
564 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
565 "number of non-empty pop_all (%llu)\n",
566 tot_empty_dest_enqueues,
567 tot_pop_all);
568 retval = 1;
569 }
a5eb5ffc
MD
570 free(count_enqueuer);
571 free(count_dequeuer);
572 free(tid_enqueuer);
573 free(tid_dequeuer);
d05e9936 574 return retval;
a5eb5ffc 575}
This page took 0.051419 seconds and 4 git commands to generate.