fix: shadowed local variable (-Wshadow)
[urcu.git] / tests / benchmark / test_urcu_wfs.c
... / ...
CommitLineData
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#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>
35#include <errno.h>
36
37#include <urcu/arch.h>
38#include <urcu/tls-compat.h>
39#include <urcu/uatomic.h>
40#include "thread-id.h"
41
42/* hardcoded number of CPUs */
43#define NR_CPUS 16384
44
45#ifndef DYNAMIC_LINK_TEST
46#define _LGPL_SOURCE
47#endif
48#include <urcu/wfstack.h>
49
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
60static int test_force_sync;
61
62static volatile int test_go, test_stop_enqueue, test_stop_dequeue;
63
64static unsigned long rduration;
65
66static unsigned long duration;
67
68/* read-side C.S. duration, in loops */
69static unsigned long wdelay;
70
71static inline void loop_sleep(unsigned long loops)
72{
73 while (loops-- != 0)
74 caa_cpu_relax();
75}
76
77static int verbose_mode;
78
79static int test_pop, test_pop_all, test_wait_empty;
80static unsigned int test_enqueue_stopped;
81
82#define printf_verbose(fmt, args...) \
83 do { \
84 if (verbose_mode) \
85 printf(fmt, ## args); \
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
94static void set_affinity(void)
95{
96#if HAVE_SCHED_SETAFFINITY
97 cpu_set_t mask;
98 int cpu, ret;
99#endif /* HAVE_SCHED_SETAFFINITY */
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 sched_setaffinity(0, sizeof(mask), &mask);
120#endif /* HAVE_SCHED_SETAFFINITY */
121}
122
123/*
124 * returns 0 if test should end.
125 */
126static int test_duration_dequeue(void)
127{
128 return !test_stop_dequeue;
129}
130
131static int test_duration_enqueue(void)
132{
133 return !test_stop_enqueue;
134}
135
136static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
137static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
138
139static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
140static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
141static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues);
142static DEFINE_URCU_TLS(unsigned long long, nr_pop_all);
143static DEFINE_URCU_TLS(unsigned long long, nr_pop_last);
144
145static unsigned int nr_enqueuers;
146static unsigned int nr_dequeuers;
147
148static struct cds_wfs_stack s;
149
150static void *thr_enqueuer(void *_count)
151{
152 unsigned long long *count = _count;
153 bool was_nonempty;
154
155 printf_verbose("thread_begin %s, tid %lu\n",
156 "enqueuer", urcu_get_thread_id());
157
158 set_affinity();
159
160 while (!test_go)
161 {
162 }
163 cmm_smp_mb();
164
165 for (;;) {
166 struct cds_wfs_node *node = malloc(sizeof(*node));
167 if (!node)
168 goto fail;
169 cds_wfs_node_init(node);
170 was_nonempty = cds_wfs_push(&s, node);
171 URCU_TLS(nr_successful_enqueues)++;
172 if (!was_nonempty)
173 URCU_TLS(nr_empty_dest_enqueues)++;
174
175 if (caa_unlikely(wdelay))
176 loop_sleep(wdelay);
177fail:
178 URCU_TLS(nr_enqueues)++;
179 if (caa_unlikely(!test_duration_enqueue()))
180 break;
181 }
182
183 uatomic_inc(&test_enqueue_stopped);
184 count[0] = URCU_TLS(nr_enqueues);
185 count[1] = URCU_TLS(nr_successful_enqueues);
186 count[2] = URCU_TLS(nr_empty_dest_enqueues);
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));
194 return ((void*)1);
195
196}
197
198static void do_test_pop(enum test_sync sync)
199{
200 struct cds_wfs_node *node;
201 int state;
202
203 if (sync == TEST_SYNC_MUTEX)
204 cds_wfs_pop_lock(&s);
205 node = __cds_wfs_pop_with_state_blocking(&s, &state);
206 if (sync == TEST_SYNC_MUTEX)
207 cds_wfs_pop_unlock(&s);
208
209 if (node) {
210 if (state & CDS_WFS_STATE_LAST)
211 URCU_TLS(nr_pop_last)++;
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
229 /* Check if empty */
230 if (cds_wfs_first(head) == NULL)
231 return;
232
233 URCU_TLS(nr_pop_all)++;
234 URCU_TLS(nr_pop_last)++;
235
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
243static void *thr_dequeuer(void *_count)
244{
245 unsigned long long *count = _count;
246 unsigned int counter = 0;
247
248 printf_verbose("thread_begin %s, tid %lu\n",
249 "dequeuer", urcu_get_thread_id());
250
251 set_affinity();
252
253 while (!test_go)
254 {
255 }
256 cmm_smp_mb();
257
258 assert(test_pop || test_pop_all);
259
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);
272 }
273
274 if (caa_unlikely(!test_duration_dequeue()))
275 break;
276 if (caa_unlikely(rduration))
277 loop_sleep(rduration);
278 }
279
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));
287 count[0] = URCU_TLS(nr_dequeues);
288 count[1] = URCU_TLS(nr_successful_dequeues);
289 count[2] = URCU_TLS(nr_pop_all);
290 count[3] = URCU_TLS(nr_pop_last);
291 return ((void*)2);
292}
293
294static void test_end(unsigned long long *nr_dequeues_l,
295 unsigned long long *nr_pop_last_l)
296{
297 struct cds_wfs_node *node;
298 int state;
299
300 do {
301 node = cds_wfs_pop_with_state_blocking(&s, &state);
302 if (node) {
303 if (state & CDS_WFS_STATE_LAST)
304 (*nr_pop_last_l)++;
305 free(node);
306 (*nr_dequeues_l)++;
307 }
308 } while (node);
309}
310
311static void show_usage(int argc, char **argv)
312{
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");
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,
337 tot_successful_dequeues = 0,
338 tot_empty_dest_enqueues = 0,
339 tot_pop_all = 0, tot_pop_last = 0;
340 unsigned long long end_dequeues = 0;
341 int i, a, retval = 0;
342 unsigned int i_thr;
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 }
360
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;
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;
407 case 'w':
408 test_wait_empty = 1;
409 break;
410 case 'f':
411 test_force_sync = 1;
412 break;
413 }
414 }
415
416 /* activate pop_all test by default */
417 if (!test_pop && !test_pop_all)
418 test_pop_all = 1;
419
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
431 printf_verbose("running test for %lu seconds, %u enqueuers, "
432 "%u dequeuers.\n",
433 duration, nr_enqueuers, nr_dequeuers);
434 if (test_pop)
435 printf_verbose("pop test activated.\n");
436 if (test_pop_all)
437 printf_verbose("pop_all test activated.\n");
438 if (test_sync == TEST_SYNC_MUTEX)
439 printf_verbose("External sync: mutex.\n");
440 else
441 printf_verbose("External sync: none.\n");
442 if (test_wait_empty)
443 printf_verbose("Wait for dequeuers to empty stack.\n");
444 printf_verbose("Writer delay : %lu loops.\n", rduration);
445 printf_verbose("Reader duration : %lu loops.\n", wdelay);
446 printf_verbose("thread %-6s, tid %lu\n",
447 "main", urcu_get_thread_id());
448
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));
453 cds_wfs_init(&s);
454
455 next_aff = 0;
456
457 for (i_thr = 0; i_thr < nr_enqueuers; i_thr++) {
458 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
459 &count_enqueuer[3 * i_thr]);
460 if (err != 0)
461 exit(1);
462 }
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]);
466 if (err != 0)
467 exit(1);
468 }
469
470 cmm_smp_mb();
471
472 test_go = 1;
473
474 for (i_thr = 0; i_thr < duration; i_thr++) {
475 sleep(1);
476 if (verbose_mode) {
477 fwrite(".", sizeof(char), 1, stdout);
478 fflush(stdout);
479 }
480 }
481
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;
494
495 for (i_thr = 0; i_thr < nr_enqueuers; i_thr++) {
496 err = pthread_join(tid_enqueuer[i_thr], &tret);
497 if (err != 0)
498 exit(1);
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];
502 }
503 for (i_thr = 0; i_thr < nr_dequeuers; i_thr++) {
504 err = pthread_join(tid_dequeuer[i_thr], &tret);
505 if (err != 0)
506 exit(1);
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];
511 }
512
513 test_end(&end_dequeues, &tot_pop_last);
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, "
518 "enqueues to empty dest : %llu, "
519 "successful dequeues %llu, "
520 "pop_all : %llu, pop_last : %llu\n",
521 tot_successful_enqueues,
522 tot_empty_dest_enqueues,
523 tot_successful_dequeues,
524 tot_pop_all, tot_pop_last);
525 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
526 "nr_dequeuers %3u "
527 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
528 "successful enqueues %12llu enqueues to empty dest %12llu "
529 "successful dequeues %12llu pop_all %12llu "
530 "pop_last %llu end_dequeues %llu nr_ops %12llu\n",
531 argv[0], duration, nr_enqueuers, wdelay,
532 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
533 tot_successful_enqueues,
534 tot_empty_dest_enqueues,
535 tot_successful_dequeues, tot_pop_all, tot_pop_last,
536 end_dequeues,
537 tot_enqueues + tot_dequeues);
538 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) {
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);
543 retval = 1;
544 }
545 /*
546 * The enqueuer should see exactly as many empty queues than the
547 * number of non-empty stacks dequeued.
548 */
549 if (tot_empty_dest_enqueues != tot_pop_last) {
550 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
551 "number of pop last (%llu)\n",
552 tot_empty_dest_enqueues,
553 tot_pop_last);
554 retval = 1;
555 }
556
557 cds_wfs_destroy(&s);
558 free(count_enqueuer);
559 free(count_dequeuer);
560 free(tid_enqueuer);
561 free(tid_dequeuer);
562
563 return retval;
564}
This page took 0.023554 seconds and 4 git commands to generate.