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