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