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