fix: handle EINTR correctly in get_cpu_mask_from_sysfs
[urcu.git] / tests / benchmark / test_urcu_hash.c
index d610d05020c6539742895919e622c7f1ff06539d..606851baf37ee1b5a84e63b9e8c3cbe3e2a74e48 100644 (file)
@@ -1,26 +1,11 @@
+// SPDX-FileCopyrightText: 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /*
- * test_urcu_hash.c
- *
  * Userspace RCU library - test program
- *
- * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
- *
- * 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 "test_urcu_hash.h"
 
 enum test_hash {
@@ -57,26 +42,31 @@ struct test_hash_cb test_hash_cb[] = {
 
 static enum test_hash test_choice = TEST_HASH_RW;
 
+static
 void (*get_sigusr1_cb(void))(int)
 {
        return test_hash_cb[test_choice].sigusr1;
 }
 
+static
 void (*get_sigusr2_cb(void))(int)
 {
        return test_hash_cb[test_choice].sigusr2;
 }
 
+static
 void *(*get_thr_reader_cb(void))(void *)
 {
        return test_hash_cb[test_choice].thr_reader;
 }
 
+static
 void *(*get_thr_writer_cb(void))(void *)
 {
        return test_hash_cb[test_choice].thr_writer;
 }
 
+static
 int (*get_populate_hash_cb(void))(void)
 {
        return test_hash_cb[test_choice].populate_hash;
@@ -92,8 +82,6 @@ DEFINE_URCU_TLS(unsigned long, lookup_ok);
 
 struct cds_lfht *test_ht;
 
-volatile int test_go, test_stop;
-
 unsigned long wdelay;
 
 unsigned long duration;
@@ -136,7 +124,7 @@ static pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 void set_affinity(void)
 {
-#if HAVE_SCHED_SETAFFINITY
+#ifdef HAVE_SCHED_SETAFFINITY
        cpu_set_t mask;
        int cpu, ret;
 #endif /* HAVE_SCHED_SETAFFINITY */
@@ -144,7 +132,7 @@ 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");
@@ -158,11 +146,7 @@ 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 */
 }
 
@@ -192,14 +176,15 @@ unsigned long test_compare(const void *key1, size_t key1_len,
 {
        if (caa_unlikely(key1_len != key2_len))
                return -1;
-       assert(key1_len == sizeof(unsigned long));
+       urcu_posix_assert(key1_len == sizeof(unsigned long));
        if (key1 == key2)
                return 0;
        else
                return 1;
 }
 
-void *thr_count(void *arg)
+static
+void *thr_count(void *arg __attribute__((unused)))
 {
        printf_verbose("thread_begin %s, tid %lu\n",
                        "counter", urcu_get_thread_id());
@@ -257,14 +242,15 @@ void test_delete_all_nodes(struct cds_lfht *ht)
                int ret;
 
                ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
-               assert(!ret);
+               urcu_posix_assert(!ret);
                call_rcu(&node->head, free_node_cb);
                count++;
        }
        printf("deleted %lu nodes.\n", count);
 }
 
-void show_usage(int argc, char **argv)
+static
+void show_usage(char **argv)
 {
        printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
                argv[0]);
@@ -310,34 +296,35 @@ int main(int argc, char **argv)
        unsigned long count;
        long approx_before, approx_after;
        int i, a, ret, err, mainret = 0;
+       unsigned int i_thr;
        struct sigaction act;
        unsigned int remain;
        unsigned int nr_readers_created = 0, nr_writers_created = 0;
        long long nr_leaked;
 
        if (argc < 4) {
-               show_usage(argc, argv);
+               show_usage(argv);
                mainret = 1;
                goto end;
        }
 
        err = sscanf(argv[1], "%u", &nr_readers);
        if (err != 1) {
-               show_usage(argc, argv);
+               show_usage(argv);
                mainret = 1;
                goto end;
        }
 
        err = sscanf(argv[2], "%u", &nr_writers);
        if (err != 1) {
-               show_usage(argc, argv);
+               show_usage(argv);
                mainret = 1;
                goto end;
        }
-       
+
        err = sscanf(argv[3], "%lu", &duration);
        if (err != 1) {
-               show_usage(argc, argv);
+               show_usage(argv);
                mainret = 1;
                goto end;
        }
@@ -354,7 +341,7 @@ int main(int argc, char **argv)
                        break;
                case 'a':
                        if (argc < i + 2) {
-                               show_usage(argc, argv);
+                               show_usage(argv);
                                mainret = 1;
                                goto end;
                        }
@@ -365,7 +352,7 @@ int main(int argc, char **argv)
                        break;
                case 'c':
                        if (argc < i + 2) {
-                               show_usage(argc, argv);
+                               show_usage(argv);
                                mainret = 1;
                                goto end;
                        }
@@ -373,7 +360,7 @@ int main(int argc, char **argv)
                        break;
                case 'd':
                        if (argc < i + 2) {
-                               show_usage(argc, argv);
+                               show_usage(argv);
                                mainret = 1;
                                goto end;
                        }
@@ -384,7 +371,7 @@ int main(int argc, char **argv)
                        break;
                case 'h':
                        if (argc < i + 2) {
-                               show_usage(argc, argv);
+                               show_usage(argv);
                                mainret = 1;
                                goto end;
                        }
@@ -392,7 +379,7 @@ int main(int argc, char **argv)
                        break;
                case 'm':
                        if (argc < i + 2) {
-                               show_usage(argc, argv);
+                               show_usage(argv);
                                mainret = 1;
                                goto end;
                        }
@@ -400,7 +387,7 @@ int main(int argc, char **argv)
                        break;
                case 'n':
                        if (argc < i + 2) {
-                               show_usage(argc, argv);
+                               show_usage(argv);
                                mainret = 1;
                                goto end;
                        }
@@ -431,7 +418,7 @@ int main(int argc, char **argv)
                        break;
                case 'B':
                        if (argc < i + 2) {
-                               show_usage(argc, argv);
+                               show_usage(argv);
                                mainret = 1;
                                goto end;
                        }
@@ -596,7 +583,7 @@ int main(int argc, char **argv)
         */
        rcu_register_thread();
        ret = (get_populate_hash_cb())();
-       assert(!ret);
+       urcu_posix_assert(!ret);
 
        rcu_thread_offline();
 
@@ -619,10 +606,10 @@ int main(int argc, char **argv)
                goto end_close_pipe;
        }
 
-       for (i = 0; i < nr_readers; i++) {
-               err = pthread_create(&tid_reader[i],
+       for (i_thr = 0; i_thr < nr_readers; i_thr++) {
+               err = pthread_create(&tid_reader[i_thr],
                                     NULL, get_thr_reader_cb(),
-                                    &count_reader[i]);
+                                    &count_reader[i_thr]);
                if (err != 0) {
                        errno = err;
                        mainret = 1;
@@ -631,10 +618,10 @@ int main(int argc, char **argv)
                }
                nr_readers_created++;
        }
-       for (i = 0; i < nr_writers; i++) {
-               err = pthread_create(&tid_writer[i],
+       for (i_thr = 0; i_thr < nr_writers; i_thr++) {
+               err = pthread_create(&tid_writer[i_thr],
                                     NULL, get_thr_writer_cb(),
-                                    &count_writer[i]);
+                                    &count_writer[i_thr]);
                if (err != 0) {
                        errno = err;
                        mainret = 1;
@@ -646,36 +633,36 @@ int main(int argc, char **argv)
 
        cmm_smp_mb();
 
-       test_go = 1;
+       begin_test();
 
        remain = duration;
        do {
                remain = sleep(remain);
        } while (remain > 0);
 
-       test_stop = 1;
+       end_test();
 
 end_pthread_join:
-       for (i = 0; i < nr_readers_created; i++) {
-               err = pthread_join(tid_reader[i], &tret);
+       for (i_thr = 0; i_thr < nr_readers_created; i_thr++) {
+               err = pthread_join(tid_reader[i_thr], &tret);
                if (err != 0) {
                        errno = err;
                        mainret = 1;
                        perror("pthread_join");
                }
-               tot_reads += count_reader[i];
+               tot_reads += count_reader[i_thr];
        }
-       for (i = 0; i < nr_writers_created; i++) {
-               err = pthread_join(tid_writer[i], &tret);
+       for (i_thr = 0; i_thr < nr_writers_created; i_thr++) {
+               err = pthread_join(tid_writer[i_thr], &tret);
                if (err != 0) {
                        errno = err;
                        mainret = 1;
                        perror("pthread_join");
                }
-               tot_writes += count_writer[i].update_ops;
-               tot_add += count_writer[i].add;
-               tot_add_exist += count_writer[i].add_exist;
-               tot_remove += count_writer[i].remove;
+               tot_writes += count_writer[i_thr].update_ops;
+               tot_add += count_writer[i_thr].add;
+               tot_add_exist += count_writer[i_thr].add_exist;
+               tot_remove += count_writer[i_thr].remove;
        }
 
        /* teardown counter thread */
@@ -688,11 +675,11 @@ end_pthread_join:
        }
        {
                char msg[1] = { 0x42 };
-               ssize_t ret;
+               ssize_t sret;
 
                do {
-                       ret = write(count_pipe[1], msg, 1);     /* wakeup thread */
-               } while (ret == -1L && errno == EINTR);
+                       sret = write(count_pipe[1], msg, 1);    /* wakeup thread */
+               } while (sret == -1L && errno == EINTR);
        }
        err = pthread_join(tid_count, &tret);
        if (err != 0) {
This page took 0.027896 seconds and 4 git commands to generate.