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