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