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