uatomic/x86: Remove redundant memory barriers
[urcu.git] / tests / benchmark / test_urcu_wfs.c
index 358e668b5ec0f84e7234f1344f7251d4af918ccc..39704f4acb5cce5eee13112fa127d78bea31e6d7 100644 (file)
@@ -1,28 +1,12 @@
-/*
- * test_urcu_wfs.c
- *
+// SPDX-FileCopyrightText: 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+// SPDX-FileCopyrightText: 2010 Paolo Bonzini <pbonzini@redhat.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+ /*
  * Userspace RCU library - example RCU-based lock-free stack
- *
- * Copyright February 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- * Copyright February 2010 - Paolo Bonzini <pbonzini@redhat.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#define _GNU_SOURCE
-#include "config.h"
 #include <stdio.h>
 #include <pthread.h>
 #include <stdlib.h>
 #include <sys/wait.h>
 #include <unistd.h>
 #include <stdio.h>
-#include <assert.h>
 #include <errno.h>
 
 #include <urcu/arch.h>
+#include <urcu/assert.h>
 #include <urcu/tls-compat.h>
 #include <urcu/uatomic.h>
-#include "cpuset.h"
 #include "thread-id.h"
 
 /* hardcoded number of CPUs */
@@ -62,7 +45,7 @@ static enum test_sync test_sync;
 
 static int test_force_sync;
 
-static volatile int test_go, test_stop_enqueue, test_stop_dequeue;
+static volatile int test_stop_enqueue, test_stop_dequeue;
 
 static unsigned long rduration;
 
@@ -80,7 +63,7 @@ static inline void loop_sleep(unsigned long loops)
 static int verbose_mode;
 
 static int test_pop, test_pop_all, test_wait_empty;
-static int test_enqueue_stopped;
+static unsigned int test_enqueue_stopped;
 
 #define printf_verbose(fmt, args...)           \
        do {                                    \
@@ -96,7 +79,7 @@ pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static void set_affinity(void)
 {
-#if HAVE_SCHED_SETAFFINITY
+#ifdef HAVE_SCHED_SETAFFINITY
        cpu_set_t mask;
        int cpu, ret;
 #endif /* HAVE_SCHED_SETAFFINITY */
@@ -104,7 +87,7 @@ static void set_affinity(void)
        if (!use_affinity)
                return;
 
-#if HAVE_SCHED_SETAFFINITY
+#ifdef HAVE_SCHED_SETAFFINITY
        ret = pthread_mutex_lock(&affinity_mutex);
        if (ret) {
                perror("Error in pthread mutex lock");
@@ -119,11 +102,7 @@ static void set_affinity(void)
 
        CPU_ZERO(&mask);
        CPU_SET(cpu, &mask);
-#if SCHED_SETAFFINITY_ARGS == 2
-       sched_setaffinity(0, &mask);
-#else
        sched_setaffinity(0, sizeof(mask), &mask);
-#endif
 #endif /* HAVE_SCHED_SETAFFINITY */
 }
 
@@ -132,12 +111,12 @@ static void set_affinity(void)
  */
 static int test_duration_dequeue(void)
 {
-       return !test_stop_dequeue;
+       return !uatomic_load(&test_stop_dequeue, CMM_RELAXED);
 }
 
 static int test_duration_enqueue(void)
 {
-       return !test_stop_enqueue;
+       return !uatomic_load(&test_stop_enqueue, CMM_RELAXED);
 }
 
 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
@@ -164,10 +143,7 @@ static void *thr_enqueuer(void *_count)
 
        set_affinity();
 
-       while (!test_go)
-       {
-       }
-       cmm_smp_mb();
+       wait_until_go();
 
        for (;;) {
                struct cds_wfs_node *node = malloc(sizeof(*node));
@@ -257,12 +233,9 @@ static void *thr_dequeuer(void *_count)
 
        set_affinity();
 
-       while (!test_go)
-       {
-       }
-       cmm_smp_mb();
+       wait_until_go();
 
-       assert(test_pop || test_pop_all);
+       urcu_posix_assert(test_pop || test_pop_all);
 
        for (;;) {
                if (test_pop && test_pop_all) {
@@ -298,24 +271,24 @@ static void *thr_dequeuer(void *_count)
        return ((void*)2);
 }
 
-static void test_end(struct cds_wfs_stack *s, unsigned long long *nr_dequeues,
-               unsigned long long *nr_pop_last)
+static void test_end(unsigned long long *nr_dequeues_l,
+               unsigned long long *nr_pop_last_l)
 {
        struct cds_wfs_node *node;
        int state;
 
        do {
-               node = cds_wfs_pop_with_state_blocking(s, &state);
+               node = cds_wfs_pop_with_state_blocking(&s, &state);
                if (node) {
                        if (state & CDS_WFS_STATE_LAST)
-                               (*nr_pop_last)++;
+                               (*nr_pop_last_l)++;
                        free(node);
-                       (*nr_dequeues)++;
+                       (*nr_dequeues_l)++;
                }
        } while (node);
 }
 
-static void show_usage(int argc, char **argv)
+static void show_usage(char **argv)
 {
        printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
                argv[0]);
@@ -346,27 +319,28 @@ int main(int argc, char **argv)
                           tot_pop_all = 0, tot_pop_last = 0;
        unsigned long long end_dequeues = 0;
        int i, a, retval = 0;
+       unsigned int i_thr;
 
        if (argc < 4) {
-               show_usage(argc, argv);
+               show_usage(argv);
                return -1;
        }
 
        err = sscanf(argv[1], "%u", &nr_dequeuers);
        if (err != 1) {
-               show_usage(argc, argv);
+               show_usage(argv);
                return -1;
        }
 
        err = sscanf(argv[2], "%u", &nr_enqueuers);
        if (err != 1) {
-               show_usage(argc, argv);
+               show_usage(argv);
                return -1;
        }
-       
+
        err = sscanf(argv[3], "%lu", &duration);
        if (err != 1) {
-               show_usage(argc, argv);
+               show_usage(argv);
                return -1;
        }
 
@@ -376,7 +350,7 @@ int main(int argc, char **argv)
                switch (argv[i][1]) {
                case 'a':
                        if (argc < i + 2) {
-                               show_usage(argc, argv);
+                               show_usage(argv);
                                return -1;
                        }
                        a = atoi(argv[++i]);
@@ -386,14 +360,14 @@ int main(int argc, char **argv)
                        break;
                case 'c':
                        if (argc < i + 2) {
-                               show_usage(argc, argv);
+                               show_usage(argv);
                                return -1;
                        }
                        rduration = atol(argv[++i]);
                        break;
                case 'd':
                        if (argc < i + 2) {
-                               show_usage(argc, argv);
+                               show_usage(argv);
                                return -1;
                        }
                        wdelay = atol(argv[++i]);
@@ -460,24 +434,24 @@ int main(int argc, char **argv)
 
        next_aff = 0;
 
-       for (i = 0; i < nr_enqueuers; i++) {
+       for (i_thr = 0; i_thr < nr_enqueuers; i_thr++) {
                err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
-                                    &count_enqueuer[3 * i]);
+                                    &count_enqueuer[3 * i_thr]);
                if (err != 0)
                        exit(1);
        }
-       for (i = 0; i < nr_dequeuers; i++) {
-               err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
-                                    &count_dequeuer[4 * i]);
+       for (i_thr = 0; i_thr < nr_dequeuers; i_thr++) {
+               err = pthread_create(&tid_dequeuer[i_thr], NULL, thr_dequeuer,
+                                    &count_dequeuer[4 * i_thr]);
                if (err != 0)
                        exit(1);
        }
 
        cmm_smp_mb();
 
-       test_go = 1;
+       begin_test();
 
-       for (i = 0; i < duration; i++) {
+       for (i_thr = 0; i_thr < duration; i_thr++) {
                sleep(1);
                if (verbose_mode) {
                        fwrite(".", sizeof(char), 1, stdout);
@@ -485,7 +459,7 @@ int main(int argc, char **argv)
                }
        }
 
-       test_stop_enqueue = 1;
+       uatomic_store(&test_stop_enqueue, 1, CMM_RELEASE);
 
        if (test_wait_empty) {
                while (nr_enqueuers != uatomic_read(&test_enqueue_stopped)) {
@@ -496,27 +470,27 @@ int main(int argc, char **argv)
                }
        }
 
-       test_stop_dequeue = 1;
+       uatomic_store(&test_stop_dequeue, 1, CMM_RELAXED);
 
-       for (i = 0; i < nr_enqueuers; i++) {
-               err = pthread_join(tid_enqueuer[i], &tret);
+       for (i_thr = 0; i_thr < nr_enqueuers; i_thr++) {
+               err = pthread_join(tid_enqueuer[i_thr], &tret);
                if (err != 0)
                        exit(1);
-               tot_enqueues += count_enqueuer[3 * i];
-               tot_successful_enqueues += count_enqueuer[3 * i + 1];
-               tot_empty_dest_enqueues += count_enqueuer[3 * i + 2];
+               tot_enqueues += count_enqueuer[3 * i_thr];
+               tot_successful_enqueues += count_enqueuer[3 * i_thr + 1];
+               tot_empty_dest_enqueues += count_enqueuer[3 * i_thr + 2];
        }
-       for (i = 0; i < nr_dequeuers; i++) {
-               err = pthread_join(tid_dequeuer[i], &tret);
+       for (i_thr = 0; i_thr < nr_dequeuers; i_thr++) {
+               err = pthread_join(tid_dequeuer[i_thr], &tret);
                if (err != 0)
                        exit(1);
-               tot_dequeues += count_dequeuer[4 * i];
-               tot_successful_dequeues += count_dequeuer[4 * i + 1];
-               tot_pop_all += count_dequeuer[4 * i + 2];
-               tot_pop_last += count_dequeuer[4 * i + 3];
+               tot_dequeues += count_dequeuer[4 * i_thr];
+               tot_successful_dequeues += count_dequeuer[4 * i_thr + 1];
+               tot_pop_all += count_dequeuer[4 * i_thr + 2];
+               tot_pop_last += count_dequeuer[4 * i_thr + 3];
        }
-       
-       test_end(&s, &end_dequeues, &tot_pop_last);
+
+       test_end(&end_dequeues, &tot_pop_last);
 
        printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
                       tot_enqueues, tot_dequeues);
@@ -559,10 +533,12 @@ int main(int argc, char **argv)
                        tot_pop_last);
                retval = 1;
        }
+
        cds_wfs_destroy(&s);
        free(count_enqueuer);
        free(count_dequeuer);
        free(tid_enqueuer);
        free(tid_dequeuer);
+
        return retval;
 }
This page took 0.027317 seconds and 4 git commands to generate.