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