tests: add missing unsigned long casts to pthread_self()
[urcu.git] / tests / test_urcu_wfcq.c
CommitLineData
5e70d572
MD
1/*
2 * test_urcu_wfcq.c
3 *
4 * Userspace RCU library - example RCU-based lock-free concurrent queue
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>
5e70d572
MD
37#include <errno.h>
38
39#include <urcu/arch.h>
40#include <urcu/tls-compat.h>
985c3168 41#include <urcu/uatomic.h>
2953b501 42#include "cpuset.h"
5e70d572
MD
43
44#ifdef __linux__
45#include <syscall.h>
46#endif
47
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
5e70d572
MD
69#include <urcu/wfcqueue.h>
70
f5d36fed 71enum test_sync {
372b9ad7
MD
72 TEST_SYNC_NONE = 0,
73 TEST_SYNC_MUTEX,
f5d36fed
MD
74};
75
76static enum test_sync test_sync;
77
372b9ad7
MD
78static int test_force_sync;
79
cf9451dc 80static volatile int test_go, test_stop_enqueue, test_stop_dequeue;
5e70d572
MD
81
82static unsigned long rduration;
83
84static unsigned long duration;
85
86/* read-side C.S. duration, in loops */
87static unsigned long wdelay;
88
ab0aacbe 89static inline void loop_sleep(unsigned long loops)
5e70d572 90{
ab0aacbe 91 while (loops-- != 0)
5e70d572
MD
92 caa_cpu_relax();
93}
94
95static int verbose_mode;
96
cf9451dc
MD
97static int test_dequeue, test_splice, test_wait_empty;
98static int test_enqueue_stopped;
f5d36fed 99
5e70d572
MD
100#define printf_verbose(fmt, args...) \
101 do { \
102 if (verbose_mode) \
f5d36fed 103 printf(fmt, ## args); \
5e70d572
MD
104 } while (0)
105
106static unsigned int cpu_affinities[NR_CPUS];
107static unsigned int next_aff = 0;
108static int use_affinity = 0;
109
110pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
111
5e70d572
MD
112static void set_affinity(void)
113{
95bc7fb9 114#if HAVE_SCHED_SETAFFINITY
5e70d572 115 cpu_set_t mask;
95bc7fb9
MD
116 int cpu, ret;
117#endif /* HAVE_SCHED_SETAFFINITY */
5e70d572
MD
118
119 if (!use_affinity)
120 return;
121
122#if HAVE_SCHED_SETAFFINITY
123 ret = pthread_mutex_lock(&affinity_mutex);
124 if (ret) {
125 perror("Error in pthread mutex lock");
126 exit(-1);
127 }
128 cpu = cpu_affinities[next_aff++];
129 ret = pthread_mutex_unlock(&affinity_mutex);
130 if (ret) {
131 perror("Error in pthread mutex unlock");
132 exit(-1);
133 }
134
135 CPU_ZERO(&mask);
136 CPU_SET(cpu, &mask);
137#if SCHED_SETAFFINITY_ARGS == 2
138 sched_setaffinity(0, &mask);
139#else
140 sched_setaffinity(0, sizeof(mask), &mask);
141#endif
142#endif /* HAVE_SCHED_SETAFFINITY */
143}
144
145/*
146 * returns 0 if test should end.
147 */
148static int test_duration_dequeue(void)
149{
cf9451dc 150 return !test_stop_dequeue;
5e70d572
MD
151}
152
153static int test_duration_enqueue(void)
154{
cf9451dc 155 return !test_stop_enqueue;
5e70d572
MD
156}
157
158static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
159static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
160
161static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
162static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
cf9451dc
MD
163static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues);
164static DEFINE_URCU_TLS(unsigned long long, nr_splice);
db182e31 165static DEFINE_URCU_TLS(unsigned long long, nr_dequeue_last);
5e70d572
MD
166
167static unsigned int nr_enqueuers;
168static unsigned int nr_dequeuers;
169
170static struct cds_wfcq_head __attribute__((aligned(CAA_CACHE_LINE_SIZE))) head;
171static struct cds_wfcq_tail __attribute__((aligned(CAA_CACHE_LINE_SIZE))) tail;
172
f5d36fed 173static void *thr_enqueuer(void *_count)
5e70d572
MD
174{
175 unsigned long long *count = _count;
cf9451dc 176 bool was_nonempty;
5e70d572
MD
177
178 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
6ff854ac
MD
179 "enqueuer", (unsigned long) pthread_self(),
180 (unsigned long) gettid());
5e70d572
MD
181
182 set_affinity();
183
184 while (!test_go)
185 {
186 }
187 cmm_smp_mb();
188
189 for (;;) {
190 struct cds_wfcq_node *node = malloc(sizeof(*node));
191 if (!node)
192 goto fail;
193 cds_wfcq_node_init(node);
cf9451dc 194 was_nonempty = cds_wfcq_enqueue(&head, &tail, node);
5e70d572 195 URCU_TLS(nr_successful_enqueues)++;
cf9451dc
MD
196 if (!was_nonempty)
197 URCU_TLS(nr_empty_dest_enqueues)++;
5e70d572
MD
198
199 if (caa_unlikely(wdelay))
200 loop_sleep(wdelay);
201fail:
202 URCU_TLS(nr_enqueues)++;
203 if (caa_unlikely(!test_duration_enqueue()))
204 break;
205 }
206
cf9451dc 207 uatomic_inc(&test_enqueue_stopped);
5e70d572
MD
208 count[0] = URCU_TLS(nr_enqueues);
209 count[1] = URCU_TLS(nr_successful_enqueues);
cf9451dc 210 count[2] = URCU_TLS(nr_empty_dest_enqueues);
5e70d572 211 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
cf9451dc
MD
212 "enqueues %llu successful_enqueues %llu, "
213 "empty_dest_enqueues %llu\n",
6ff854ac
MD
214 pthread_self(),
215 (unsigned long) gettid(),
cf9451dc
MD
216 URCU_TLS(nr_enqueues),
217 URCU_TLS(nr_successful_enqueues),
218 URCU_TLS(nr_empty_dest_enqueues));
5e70d572
MD
219 return ((void*)1);
220
221}
222
f5d36fed
MD
223static void do_test_dequeue(enum test_sync sync)
224{
225 struct cds_wfcq_node *node;
db182e31 226 int state;
f5d36fed
MD
227
228 if (sync == TEST_SYNC_MUTEX)
db182e31
MD
229 node = cds_wfcq_dequeue_with_state_blocking(&head, &tail,
230 &state);
f5d36fed 231 else
db182e31
MD
232 node = __cds_wfcq_dequeue_with_state_blocking(&head, &tail,
233 &state);
234
235 if (state & CDS_WFCQ_STATE_LAST)
236 URCU_TLS(nr_dequeue_last)++;
f5d36fed
MD
237
238 if (node) {
239 free(node);
240 URCU_TLS(nr_successful_dequeues)++;
241 }
242 URCU_TLS(nr_dequeues)++;
243}
244
245static void do_test_splice(enum test_sync sync)
246{
247 struct cds_wfcq_head tmp_head;
248 struct cds_wfcq_tail tmp_tail;
249 struct cds_wfcq_node *node, *n;
cf9451dc 250 enum cds_wfcq_ret ret;
f5d36fed
MD
251
252 cds_wfcq_init(&tmp_head, &tmp_tail);
253
254 if (sync == TEST_SYNC_MUTEX)
cf9451dc 255 ret = cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
f5d36fed
MD
256 &head, &tail);
257 else
cf9451dc 258 ret = __cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
f5d36fed
MD
259 &head, &tail);
260
cf9451dc
MD
261 switch (ret) {
262 case CDS_WFCQ_RET_WOULDBLOCK:
263 assert(0); /* blocking call */
264 break;
265 case CDS_WFCQ_RET_DEST_EMPTY:
266 URCU_TLS(nr_splice)++;
db182e31 267 URCU_TLS(nr_dequeue_last)++;
cf9451dc
MD
268 /* ok */
269 break;
270 case CDS_WFCQ_RET_DEST_NON_EMPTY:
271 assert(0); /* entirely unexpected */
272 break;
273 case CDS_WFCQ_RET_SRC_EMPTY:
274 /* ok, we could even skip iteration on dest if we wanted */
275 break;
276 }
277
f5d36fed
MD
278 __cds_wfcq_for_each_blocking_safe(&tmp_head, &tmp_tail, node, n) {
279 free(node);
280 URCU_TLS(nr_successful_dequeues)++;
281 URCU_TLS(nr_dequeues)++;
282 }
283}
284
285static void *thr_dequeuer(void *_count)
5e70d572
MD
286{
287 unsigned long long *count = _count;
f42bc7ef 288 unsigned int counter = 0;
5e70d572
MD
289
290 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
6ff854ac
MD
291 "dequeuer", (unsigned long) pthread_self(),
292 (unsigned long) gettid());
5e70d572
MD
293
294 set_affinity();
295
296 while (!test_go)
297 {
298 }
299 cmm_smp_mb();
300
301 for (;;) {
f5d36fed
MD
302 if (test_dequeue && test_splice) {
303 if (counter & 1)
304 do_test_dequeue(test_sync);
305 else
306 do_test_splice(test_sync);
307 counter++;
308 } else {
309 if (test_dequeue)
310 do_test_dequeue(test_sync);
311 else
312 do_test_splice(test_sync);
5e70d572 313 }
5e70d572
MD
314 if (caa_unlikely(!test_duration_dequeue()))
315 break;
316 if (caa_unlikely(rduration))
317 loop_sleep(rduration);
318 }
319
320 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
cf9451dc
MD
321 "dequeues %llu, successful_dequeues %llu, "
322 "nr_splice %llu\n",
6ff854ac
MD
323 pthread_self(),
324 (unsigned long) gettid(),
cf9451dc
MD
325 URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues),
326 URCU_TLS(nr_splice));
5e70d572
MD
327 count[0] = URCU_TLS(nr_dequeues);
328 count[1] = URCU_TLS(nr_successful_dequeues);
cf9451dc 329 count[2] = URCU_TLS(nr_splice);
db182e31 330 count[3] = URCU_TLS(nr_dequeue_last);
5e70d572
MD
331 return ((void*)2);
332}
333
db182e31
MD
334static void test_end(unsigned long long *nr_dequeues,
335 unsigned long long *nr_dequeue_last)
5e70d572
MD
336{
337 struct cds_wfcq_node *node;
db182e31 338 int state;
5e70d572
MD
339
340 do {
db182e31
MD
341 node = cds_wfcq_dequeue_with_state_blocking(&head, &tail,
342 &state);
5e70d572 343 if (node) {
db182e31
MD
344 if (state & CDS_WFCQ_STATE_LAST)
345 (*nr_dequeue_last)++;
5e70d572
MD
346 free(node);
347 (*nr_dequeues)++;
348 }
349 } while (node);
350}
351
f5d36fed 352static void show_usage(int argc, char **argv)
5e70d572 353{
06637138
MD
354 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
355 argv[0]);
356 printf("OPTIONS:\n");
357 printf(" [-d delay] (enqueuer period (in loops))\n");
358 printf(" [-c duration] (dequeuer period (in loops))\n");
359 printf(" [-v] (verbose output)\n");
360 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
361 printf(" [-q] (test dequeue)\n");
362 printf(" [-s] (test splice, enabled by default)\n");
363 printf(" [-M] (use mutex external synchronization)\n");
364 printf(" Note: default: no external synchronization used.\n");
365 printf(" [-f] (force user-provided synchronization)\n");
366 printf(" [-w] Wait for dequeuer to empty queue\n");
5e70d572
MD
367 printf("\n");
368}
369
370int main(int argc, char **argv)
371{
372 int err;
373 pthread_t *tid_enqueuer, *tid_dequeuer;
374 void *tret;
375 unsigned long long *count_enqueuer, *count_dequeuer;
376 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
377 unsigned long long tot_successful_enqueues = 0,
cf9451dc
MD
378 tot_successful_dequeues = 0,
379 tot_empty_dest_enqueues = 0,
db182e31 380 tot_splice = 0, tot_dequeue_last = 0;
5e70d572 381 unsigned long long end_dequeues = 0;
cf9451dc 382 int i, a, retval = 0;
5e70d572
MD
383
384 if (argc < 4) {
385 show_usage(argc, argv);
386 return -1;
387 }
388
389 err = sscanf(argv[1], "%u", &nr_dequeuers);
390 if (err != 1) {
391 show_usage(argc, argv);
392 return -1;
393 }
394
395 err = sscanf(argv[2], "%u", &nr_enqueuers);
396 if (err != 1) {
397 show_usage(argc, argv);
398 return -1;
399 }
400
401 err = sscanf(argv[3], "%lu", &duration);
402 if (err != 1) {
403 show_usage(argc, argv);
404 return -1;
405 }
406
407 for (i = 4; i < argc; i++) {
408 if (argv[i][0] != '-')
409 continue;
410 switch (argv[i][1]) {
411 case 'a':
412 if (argc < i + 2) {
413 show_usage(argc, argv);
414 return -1;
415 }
416 a = atoi(argv[++i]);
417 cpu_affinities[next_aff++] = a;
418 use_affinity = 1;
419 printf_verbose("Adding CPU %d affinity\n", a);
420 break;
421 case 'c':
422 if (argc < i + 2) {
423 show_usage(argc, argv);
424 return -1;
425 }
426 rduration = atol(argv[++i]);
427 break;
428 case 'd':
429 if (argc < i + 2) {
430 show_usage(argc, argv);
431 return -1;
432 }
433 wdelay = atol(argv[++i]);
434 break;
435 case 'v':
436 verbose_mode = 1;
437 break;
f5d36fed
MD
438 case 'q':
439 test_dequeue = 1;
440 break;
441 case 's':
442 test_splice = 1;
443 break;
444 case 'M':
445 test_sync = TEST_SYNC_MUTEX;
446 break;
cf9451dc
MD
447 case 'w':
448 test_wait_empty = 1;
449 break;
372b9ad7
MD
450 case 'f':
451 test_force_sync = 1;
452 break;
5e70d572
MD
453 }
454 }
455
f5d36fed
MD
456 /* activate splice test by default */
457 if (!test_dequeue && !test_splice)
458 test_splice = 1;
459
372b9ad7
MD
460 if (test_sync == TEST_SYNC_NONE && nr_dequeuers > 1 && test_dequeue) {
461 if (test_force_sync) {
462 fprintf(stderr, "[WARNING] Using dequeue concurrently "
463 "with other dequeue or splice without external "
464 "synchronization. Expect run-time failure.\n");
465 } else {
466 printf("Enforcing mutex synchronization\n");
467 test_sync = TEST_SYNC_MUTEX;
468 }
469 }
470
5e70d572
MD
471 printf_verbose("running test for %lu seconds, %u enqueuers, "
472 "%u dequeuers.\n",
473 duration, nr_enqueuers, nr_dequeuers);
f5d36fed
MD
474 if (test_dequeue)
475 printf_verbose("dequeue test activated.\n");
476 else
477 printf_verbose("splice test activated.\n");
478 if (test_sync == TEST_SYNC_MUTEX)
479 printf_verbose("External sync: mutex.\n");
480 else
481 printf_verbose("External sync: none.\n");
cf9451dc
MD
482 if (test_wait_empty)
483 printf_verbose("Wait for dequeuers to empty queue.\n");
5e70d572
MD
484 printf_verbose("Writer delay : %lu loops.\n", rduration);
485 printf_verbose("Reader duration : %lu loops.\n", wdelay);
486 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
6ff854ac
MD
487 "main", (unsigned long) pthread_self(),
488 (unsigned long) gettid());
5e70d572
MD
489
490 tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers);
491 tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers);
cf9451dc 492 count_enqueuer = malloc(3 * sizeof(*count_enqueuer) * nr_enqueuers);
db182e31 493 count_dequeuer = malloc(4 * sizeof(*count_dequeuer) * nr_dequeuers);
5e70d572
MD
494 cds_wfcq_init(&head, &tail);
495
496 next_aff = 0;
497
498 for (i = 0; i < nr_enqueuers; i++) {
499 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
cf9451dc 500 &count_enqueuer[3 * i]);
5e70d572
MD
501 if (err != 0)
502 exit(1);
503 }
504 for (i = 0; i < nr_dequeuers; i++) {
505 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
db182e31 506 &count_dequeuer[4 * i]);
5e70d572
MD
507 if (err != 0)
508 exit(1);
509 }
510
511 cmm_smp_mb();
512
513 test_go = 1;
514
515 for (i = 0; i < duration; i++) {
516 sleep(1);
517 if (verbose_mode)
518 write (1, ".", 1);
519 }
520
cf9451dc
MD
521 test_stop_enqueue = 1;
522
523 if (test_wait_empty) {
524 while (nr_enqueuers != uatomic_read(&test_enqueue_stopped)) {
525 sleep(1);
526 }
527 while (!cds_wfcq_empty(&head, &tail)) {
528 sleep(1);
529 }
530 }
531
532 test_stop_dequeue = 1;
5e70d572
MD
533
534 for (i = 0; i < nr_enqueuers; i++) {
535 err = pthread_join(tid_enqueuer[i], &tret);
536 if (err != 0)
537 exit(1);
cf9451dc
MD
538 tot_enqueues += count_enqueuer[3 * i];
539 tot_successful_enqueues += count_enqueuer[3 * i + 1];
540 tot_empty_dest_enqueues += count_enqueuer[3 * i + 2];
5e70d572
MD
541 }
542 for (i = 0; i < nr_dequeuers; i++) {
543 err = pthread_join(tid_dequeuer[i], &tret);
544 if (err != 0)
545 exit(1);
db182e31
MD
546 tot_dequeues += count_dequeuer[4 * i];
547 tot_successful_dequeues += count_dequeuer[4 * i + 1];
548 tot_splice += count_dequeuer[4 * i + 2];
549 tot_dequeue_last += count_dequeuer[4 * i + 3];
5e70d572
MD
550 }
551
db182e31 552 test_end(&end_dequeues, &tot_dequeue_last);
5e70d572
MD
553
554 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
555 tot_enqueues, tot_dequeues);
556 printf_verbose("total number of successful enqueues : %llu, "
cf9451dc 557 "enqueues to empty dest : %llu, "
23ae48ef 558 "successful dequeues %llu, "
db182e31 559 "splice : %llu, dequeue_last : %llu\n",
cf9451dc
MD
560 tot_successful_enqueues,
561 tot_empty_dest_enqueues,
562 tot_successful_dequeues,
db182e31 563 tot_splice, tot_dequeue_last);
5e70d572
MD
564 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
565 "nr_dequeuers %3u "
566 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
cf9451dc
MD
567 "successful enqueues %12llu enqueues to empty dest %12llu "
568 "successful dequeues %12llu splice %12llu "
db182e31 569 "dequeue_last %llu "
5e70d572
MD
570 "end_dequeues %llu nr_ops %12llu\n",
571 argv[0], duration, nr_enqueuers, wdelay,
572 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
573 tot_successful_enqueues,
cf9451dc 574 tot_empty_dest_enqueues,
db182e31
MD
575 tot_successful_dequeues, tot_splice, tot_dequeue_last,
576 end_dequeues,
5e70d572 577 tot_enqueues + tot_dequeues);
cf9451dc
MD
578
579 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) {
5e70d572
MD
580 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
581 "succ. dequeues + end dequeues %llu.\n",
582 tot_successful_enqueues,
583 tot_successful_dequeues + end_dequeues);
cf9451dc
MD
584 retval = 1;
585 }
5e70d572 586
cf9451dc
MD
587 /*
588 * If only using splice to dequeue, the enqueuer should see
589 * exactly as many empty queues than the number of non-empty
590 * src splice.
591 */
db182e31 592 if (tot_empty_dest_enqueues != tot_dequeue_last) {
cf9451dc 593 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
db182e31 594 "number of dequeue of last element (%llu)\n",
cf9451dc 595 tot_empty_dest_enqueues,
db182e31 596 tot_dequeue_last);
cf9451dc
MD
597 retval = 1;
598 }
5e70d572
MD
599 free(count_enqueuer);
600 free(count_dequeuer);
601 free(tid_enqueuer);
602 free(tid_dequeuer);
cf9451dc 603 return retval;
5e70d572 604}
This page took 0.053932 seconds and 4 git commands to generate.