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