Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src membar_ops(3): Add some automatic tests.



details:   https://anonhg.NetBSD.org/src/rev/c35cf111231e
branches:  trunk
changeset: 364718:c35cf111231e
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Fri Apr 08 23:35:51 2022 +0000

description:
membar_ops(3): Add some automatic tests.

These tests run two threads for five seconds each to try to trigger
races in the event of broken memory barriers.  They run only on
machines with at least two CPUs; on uniprocessor systems there's no
point -- the membars can correctly just be (instruction barrier)
no-ops.

diffstat:

 distrib/sets/lists/debug/mi        |    5 +-
 distrib/sets/lists/tests/mi        |    9 +-
 etc/mtree/NetBSD.dist.tests        |    4 +-
 tests/lib/libc/Makefile            |   25 ++++-
 tests/lib/libc/membar/Makefile     |   15 ++
 tests/lib/libc/membar/t_dekker.c   |  172 ++++++++++++++++++++++++++++++++
 tests/lib/libc/membar/t_seqlock.c  |  197 +++++++++++++++++++++++++++++++++++++
 tests/lib/libc/membar/t_spinlock.c |  164 ++++++++++++++++++++++++++++++
 8 files changed, 585 insertions(+), 6 deletions(-)

diffs (truncated from 677 to 300 lines):

diff -r b53138da3470 -r c35cf111231e distrib/sets/lists/debug/mi
--- a/distrib/sets/lists/debug/mi       Fri Apr 08 23:14:21 2022 +0000
+++ b/distrib/sets/lists/debug/mi       Fri Apr 08 23:35:51 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.375 2022/04/06 14:28:44 reinoud Exp $
+# $NetBSD: mi,v 1.376 2022/04/08 23:35:51 riastradh Exp $
 ./etc/mtree/set.debug                           comp-sys-root
 ./usr/lib                                      comp-sys-usr            compatdir
 ./usr/lib/i18n/libBIG5_g.a                     comp-c-debuglib         debuglib,compatfile
@@ -2062,6 +2062,9 @@
 ./usr/libdata/debug/usr/tests/lib/libc/locale/t_wcstod.debug           tests-lib-debug         debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/lib/libc/locale/t_wctomb.debug           tests-lib-debug         debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/lib/libc/locale/t_wctype.debug           tests-lib-debug         debug,atf,compattestfile
+./usr/libdata/debug/usr/tests/lib/libc/membar/t_dekker.debug           tests-lib-debug         debug,atf,compattestfile
+./usr/libdata/debug/usr/tests/lib/libc/membar/t_seqlock.debug          tests-lib-debug         debug,atf,compattestfile
+./usr/libdata/debug/usr/tests/lib/libc/membar/t_spinlock.debug         tests-lib-debug         debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/lib/libc/misc/t_ubsan.debug              tests-lib-debug         debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/lib/libc/misc/t_ubsanxx.debug            tests-lib-debug         debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/lib/libc/net/getaddrinfo/h_gai.debug     tests-lib-debug         debug,atf,compattestfile
diff -r b53138da3470 -r c35cf111231e distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Fri Apr 08 23:14:21 2022 +0000
+++ b/distrib/sets/lists/tests/mi       Fri Apr 08 23:35:51 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1191 2022/04/08 21:29:29 rillig Exp $
+# $NetBSD: mi,v 1.1192 2022/04/08 23:35:52 riastradh Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -104,6 +104,7 @@
 ./usr/libdata/debug/usr/tests/lib/libc/ieeefp          tests-obsolete          obsolete
 ./usr/libdata/debug/usr/tests/lib/libc/inet            tests-lib-debug         compattestfile,atf
 ./usr/libdata/debug/usr/tests/lib/libc/locale          tests-lib-debug         compattestfile,atf
+./usr/libdata/debug/usr/tests/lib/libc/membar          tests-lib-debug         compattestfile,atf
 ./usr/libdata/debug/usr/tests/lib/libc/misc            tests-lib-debug         compattestfile,atf
 ./usr/libdata/debug/usr/tests/lib/libc/net             tests-lib-debug         compattestfile,atf
 ./usr/libdata/debug/usr/tests/lib/libc/net/getaddrinfo tests-lib-debug         compattestfile,atf
@@ -2955,6 +2956,12 @@
 ./usr/tests/lib/libc/locale/t_wcstod                   tests-lib-tests         compattestfile,atf
 ./usr/tests/lib/libc/locale/t_wctomb                   tests-lib-tests         compattestfile,atf
 ./usr/tests/lib/libc/locale/t_wctype                   tests-lib-tests         compattestfile,atf
+./usr/tests/lib/libc/membar                            tests-lib-tests         compattestfile,atf
+./usr/tests/lib/libc/membar/Atffile                    tests-lib-tests         compattestfile,atf
+./usr/tests/lib/libc/membar/Kyuafile                   tests-lib-tests         compattestfile,atf,kyua
+./usr/tests/lib/libc/membar/t_dekker                   tests-lib-tests         compattestfile,atf
+./usr/tests/lib/libc/membar/t_seqlock                  tests-lib-tests         compattestfile,atf
+./usr/tests/lib/libc/membar/t_spinlock                 tests-lib-tests         compattestfile,atf
 ./usr/tests/lib/libc/misc                              tests-lib-tests         compattestfile,atf
 ./usr/tests/lib/libc/misc/Atffile                      tests-lib-tests         compattestfile,atf
 ./usr/tests/lib/libc/misc/Kyuafile                     tests-lib-tests         compattestfile,atf,kyua
diff -r b53138da3470 -r c35cf111231e etc/mtree/NetBSD.dist.tests
--- a/etc/mtree/NetBSD.dist.tests       Fri Apr 08 23:14:21 2022 +0000
+++ b/etc/mtree/NetBSD.dist.tests       Fri Apr 08 23:35:51 2022 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: NetBSD.dist.tests,v 1.189 2022/04/08 23:14:10 riastradh Exp $
+#      $NetBSD: NetBSD.dist.tests,v 1.190 2022/04/08 23:35:52 riastradh Exp $
 
 ./usr/libdata/debug/usr/tests
 ./usr/libdata/debug/usr/tests/atf
@@ -85,6 +85,7 @@
 ./usr/libdata/debug/usr/tests/lib/libc/hash
 ./usr/libdata/debug/usr/tests/lib/libc/inet
 ./usr/libdata/debug/usr/tests/lib/libc/locale
+./usr/libdata/debug/usr/tests/lib/libc/membar
 ./usr/libdata/debug/usr/tests/lib/libc/misc
 ./usr/libdata/debug/usr/tests/lib/libc/net
 ./usr/libdata/debug/usr/tests/lib/libc/net/getaddrinfo
@@ -287,6 +288,7 @@
 ./usr/tests/lib/libc/hash/data
 ./usr/tests/lib/libc/inet
 ./usr/tests/lib/libc/locale
+./usr/tests/lib/libc/membar
 ./usr/tests/lib/libc/misc
 ./usr/tests/lib/libc/net
 ./usr/tests/lib/libc/net/getaddrinfo
diff -r b53138da3470 -r c35cf111231e tests/lib/libc/Makefile
--- a/tests/lib/libc/Makefile   Fri Apr 08 23:14:21 2022 +0000
+++ b/tests/lib/libc/Makefile   Fri Apr 08 23:35:51 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.50 2020/03/08 22:08:46 mgorny Exp $
+# $NetBSD: Makefile,v 1.51 2022/04/08 23:35:52 riastradh Exp $
 
 .include "Makefile.inc"
 .include <bsd.own.mk>
@@ -6,8 +6,27 @@
 SUBDIR+=       tls_dso .WAIT sync
 
 TESTS_SUBDIRS+=        atomic
-TESTS_SUBDIRS+=        c063 db gen hash inet locale misc net nls regex rpc setjmp
-TESTS_SUBDIRS+=        stdlib stdio string sys termios time tls ttyio
+TESTS_SUBDIRS+=        c063
+TESTS_SUBDIRS+=        db
+TESTS_SUBDIRS+=        gen
+TESTS_SUBDIRS+=        hash
+TESTS_SUBDIRS+=        inet
+TESTS_SUBDIRS+=        locale
+TESTS_SUBDIRS+=        membar
+TESTS_SUBDIRS+=        misc
+TESTS_SUBDIRS+=        net
+TESTS_SUBDIRS+=        nls
+TESTS_SUBDIRS+=        regex
+TESTS_SUBDIRS+=        rpc
+TESTS_SUBDIRS+=        setjmp
+TESTS_SUBDIRS+=        stdio
+TESTS_SUBDIRS+=        stdlib
+TESTS_SUBDIRS+=        string
+TESTS_SUBDIRS+=        sys
+TESTS_SUBDIRS+=        termios
+TESTS_SUBDIRS+=        time
+TESTS_SUBDIRS+=        tls
+TESTS_SUBDIRS+=        ttyio
 
 .if ${HAVE_SSP} == "yes"
 TESTS_SUBDIRS+=        ssp
diff -r b53138da3470 -r c35cf111231e tests/lib/libc/membar/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/membar/Makefile    Fri Apr 08 23:35:51 2022 +0000
@@ -0,0 +1,15 @@
+#      $NetBSD: Makefile,v 1.1 2022/04/08 23:35:52 riastradh Exp $
+
+.include <bsd.own.mk>
+
+TESTSDIR=      ${TESTSBASE}/lib/libc/membar
+
+TESTS_C+=      t_dekker
+TESTS_C+=      t_seqlock
+TESTS_C+=      t_spinlock
+
+LDADD+=                -pthread
+
+WARNS=         6
+
+.include <bsd.test.mk>
diff -r b53138da3470 -r c35cf111231e tests/lib/libc/membar/t_dekker.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/membar/t_dekker.c  Fri Apr 08 23:35:51 2022 +0000
@@ -0,0 +1,172 @@
+/*     $NetBSD: t_dekker.c,v 1.1 2022/04/08 23:35:52 riastradh Exp $   */
+
+/*-
+ * Copyright (c) 2022 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_dekker.c,v 1.1 2022/04/08 23:35:52 riastradh Exp $");
+
+#include <sys/atomic.h>
+#include <sys/param.h>
+#include <sys/sysctl.h>
+
+#include <assert.h>
+#include <atf-c.h>
+#include <err.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <unistd.h>
+
+/* XXX */
+#define        membar_acquire()        membar_enter()
+#define        membar_release()        membar_exit()
+
+#ifdef BROKEN_SYNC
+#undef membar_sync
+#define        membar_sync()   asm volatile("" ::: "memory")
+#endif /* BROKEN_SYNC */
+
+volatile sig_atomic_t times_up;
+
+volatile unsigned turn __aligned(COHERENCY_UNIT);
+volatile struct {
+       unsigned v;
+} __aligned(COHERENCY_UNIT) waiting[2];
+__CTASSERT(sizeof(waiting) == 2*COHERENCY_UNIT);
+
+volatile uint64_t C;
+uint64_t TC[2];
+
+static void
+lock(unsigned me)
+{
+
+top:   waiting[me].v = 1;
+       membar_sync();
+       while (waiting[1 - me].v) {
+               if (turn != me) {
+                       waiting[me].v = 0;
+                       while (turn != me)
+                               continue;
+                       goto top;
+               }
+       }
+       membar_acquire();
+}
+
+static void
+unlock(unsigned me)
+{
+
+       membar_release();
+       turn = 1 - me;
+       waiting[me].v = 0;
+
+       /*
+        * Not needed for correctness, but this helps on Cavium Octeon
+        * cnMIPS CPUs which require issuing a sync plunger to unclog
+        * store buffers which can otherwise stay clogged for hundreds
+        * of thousands of cycles, giving very little concurrency to
+        * this test.
+        */
+       membar_producer();
+}
+
+static void
+alarm_handler(int signo)
+{
+
+       (void)signo;
+       times_up = 1;
+}
+
+static void *
+thread(void *cookie)
+{
+       unsigned me = (unsigned)(uintptr_t)cookie;
+       uint64_t C_local = 0;
+
+       while (!times_up) {
+               C_local++;
+               lock(me);
+               C++;
+               unlock(me);
+       }
+
+       TC[me] = C_local;
+
+       return NULL;
+}
+
+ATF_TC(dekker);
+ATF_TC_HEAD(dekker, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Verify membar_sync works for Dekker's algorithm");
+}
+ATF_TC_BODY(dekker, tc)
+{
+       pthread_t t[2];
+       unsigned i;
+       int ncpu;
+       size_t ncpulen = sizeof(ncpu);
+       int error;
+
+       if (sysctlbyname("hw.ncpu", &ncpu, &ncpulen, NULL, 0) == -1)
+               atf_tc_fail("hw.ncpu: (%d) %s", errno, strerror(errno));
+       assert(ncpulen == sizeof(ncpu));
+       if (ncpu == 1)
+               atf_tc_skip("membar tests are only for multicore systems");
+
+       if (signal(SIGALRM, alarm_handler) == SIG_ERR)
+               err(1, "signal(SIGALRM");
+       alarm(5);
+       for (i = 0; i < 2; i++) {
+               error = pthread_create(&t[i], NULL, &thread,
+                   (void *)(uintptr_t)i);
+               if (error)
+                       errc(1, error, "pthread_create");
+       }
+       for (i = 0; i < 2; i++) {
+               error = pthread_join(t[i], NULL);
+               if (error)
+                       errc(1, error, "pthread_join");
+       }
+       ATF_REQUIRE_MSG(C == TC[0] + TC[1],
+           "%"PRIu64" != %"PRIu64" + %"PRIu64" (off by %"PRIdMAX")",
+           C, TC[0], TC[1], TC[0] + TC[1] - C);



Home | Main Index | Thread Index | Old Index