Source-Changes-HG archive

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

[src/trunk]: src Add new test t_timedmutex



details:   https://anonhg.NetBSD.org/src/rev/777aaadb2d16
branches:  trunk
changeset: 818756:777aaadb2d16
user:      kamil <kamil%NetBSD.org@localhost>
date:      Sun Oct 30 16:17:16 2016 +0000

description:
Add new test t_timedmutex

This test is a clone on t_mutex with additional two tests for timed-mutex
specific block.

All simple-mutex (not with the timed property according to the C11 wording)
specific tests are covered by pthread_mutex_timedlock(3) with parameter
ts_lengthy of sufficiently large tv_sec value (right now UINT16_MAX). If,
a test will hang, it won't wait UINT16_MAX seconds, but will be terminated
within the default timeout for ATF tests (right now 300 [sec] in my
NetBSD/amd64 setup).

This test was inspired by a classic selflock test failure of
pthread_mutex_timedlock(3) of the following form:

#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <time.h>

int main(int argc, char **argv)
{
        pthread_mutex_t mtx;
        struct timespec ts;

        ts.tv_sec = 0;
        ts.tv_nsec = 1000;
        printf("ts{.tv_sec = %d, .tv_nsec=%ld}\n", ts.tv_sec, ts.tv_nsec);
        fflush(stdout);

        printf("mtx_init\n");
        assert(pthread_mutex_init(&mtx, NULL) == 0);

        printf("mtx_lock\n");
        assert(pthread_mutex_lock(&mtx) == 0);

        printf("mtx_timedlock\n");
        assert(pthread_mutex_timedlock(&mtx, &ts) == ETIMEDOUT);

        printf("mtx_unlock\n");
        assert(pthread_mutex_unlock(&mtx) == 0);

        printf("mtx_destroy\n");
        assert(pthread_mutex_destroy(&mtx) == 0);

        return 0;
}

Current NetBSD implementation wrongly hangs on this test.

The issue was detected during development of the C11 portable threads.

My local tests in chroot presents that the are further issues:

t_timedmutex (21/25): 10 test cases
    mutex1: [0.001142s] Failed: /usr/src/tests/lib/libpthread/t_timedmutex.c:75: *param != 20
    mutex2: [0.261499s] Passed.
    mutex3: [0.261496s] Passed.
    mutex4: [0.001204s] Failed: /usr/src/tests/lib/libpthread/t_timedmutex.c:265: pthread_mutex_timedlock(&mutex, &ts_lengthy): Connection timed out
    mutex5: [0.001235s] Failed: /usr/src/tests/lib/libpthread/t_timedmutex.c:337: pthread_mutex_timedlock(&mutex5, &ts_lengthy): Connection timed out
    mutex6: [21.218497s] Failed: /usr/src/tests/lib/libpthread/t_timedmutex.c:512: start != 1
    mutexattr1: [0.001328s] Passed.
    mutexattr2: [0.001175s] Passed.
    timedmutex1: [301.119397s] Failed: Test case timed out after 300 seconds
    timedmutex2: [301.123081s] Failed: Test case timed out after 300 seconds
[623.990659s]

I'm also receiveing the same failure in the mutex6 test in t_mutex, so
there might be a false positives due to local chroot(8) issues.

Commit approved by <christos>.

diffstat:

 distrib/sets/lists/debug/mi         |    3 +-
 distrib/sets/lists/tests/mi         |    3 +-
 tests/lib/libpthread/Makefile       |    3 +-
 tests/lib/libpthread/h_common.h     |    6 +
 tests/lib/libpthread/t_mutex.c      |    6 +-
 tests/lib/libpthread/t_timedmutex.c |  634 ++++++++++++++++++++++++++++++++++++
 6 files changed, 650 insertions(+), 5 deletions(-)

diffs (truncated from 726 to 300 lines):

diff -r 5353b2f73110 -r 777aaadb2d16 distrib/sets/lists/debug/mi
--- a/distrib/sets/lists/debug/mi       Sun Oct 30 15:47:06 2016 +0000
+++ b/distrib/sets/lists/debug/mi       Sun Oct 30 16:17:16 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.167 2016/09/05 02:25:38 ozaki-r Exp $
+# $NetBSD: mi,v 1.168 2016/10/30 16:17:16 kamil 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
@@ -2159,6 +2159,7 @@
 ./usr/libdata/debug/usr/tests/lib/libpthread/t_sleep.debug             tests-lib-tests         debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/lib/libpthread/t_status.debug            tests-obsolete          obsolete,compattestfile
 ./usr/libdata/debug/usr/tests/lib/libpthread/t_swapcontext.debug       tests-lib-tests         debug,atf,compattestfile
+./usr/libdata/debug/usr/tests/lib/libpthread/t_timedmutex.debug                tests-lib-tests         debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/lib/librt/t_sched.debug                  tests-lib-debug         debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/lib/librt/t_sem.debug                    tests-lib-debug         debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/lib/librumpclient/h_exec.debug                   tests-lib-debug         debug,atf,rump
diff -r 5353b2f73110 -r 777aaadb2d16 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Sun Oct 30 15:47:06 2016 +0000
+++ b/distrib/sets/lists/tests/mi       Sun Oct 30 16:17:16 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.688 2016/10/22 14:13:39 abhinav Exp $
+# $NetBSD: mi,v 1.689 2016/10/30 16:17:16 kamil Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -3000,6 +3000,7 @@
 ./usr/tests/lib/libpthread/t_sleep             tests-lib-tests         compattestfile,atf
 ./usr/tests/lib/libpthread/t_status            tests-obsolete          obsolete
 ./usr/tests/lib/libpthread/t_swapcontext       tests-lib-tests         compattestfile,atf
+./usr/tests/lib/libpthread/t_timedmutex                tests-lib-tests         compattestfile,atf
 ./usr/tests/lib/librt                          tests-lib-tests         compattestfile,atf
 ./usr/tests/lib/librt/Atffile                  tests-lib-tests         compattestfile,atf
 ./usr/tests/lib/librt/Kyuafile                 tests-lib-tests         compattestfile,atf,kyua
diff -r 5353b2f73110 -r 777aaadb2d16 tests/lib/libpthread/Makefile
--- a/tests/lib/libpthread/Makefile     Sun Oct 30 15:47:06 2016 +0000
+++ b/tests/lib/libpthread/Makefile     Sun Oct 30 16:17:16 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.11 2013/04/12 17:18:11 christos Exp $
+# $NetBSD: Makefile,v 1.12 2016/10/30 16:17:16 kamil Exp $
 
 NOMAN=         # defined
 
@@ -38,6 +38,7 @@
 TESTS_C+=      t_siglongjmp
 TESTS_C+=      t_sleep
 TESTS_C+=      t_swapcontext
+TESTS_C+=      t_timedmutex
 
 LDADD.t_sem+=  -lrt
 
diff -r 5353b2f73110 -r 777aaadb2d16 tests/lib/libpthread/h_common.h
--- a/tests/lib/libpthread/h_common.h   Sun Oct 30 15:47:06 2016 +0000
+++ b/tests/lib/libpthread/h_common.h   Sun Oct 30 16:17:16 2016 +0000
@@ -9,4 +9,10 @@
         ATF_REQUIRE_MSG(ret == 0, "%s: %s", #x, strerror(ret)); \
     } while (0)
 
+#define PTHREAD_REQUIRE_STATUS(x, v) \
+    do { \
+        int ret = (x); \
+        ATF_REQUIRE_MSG(ret == (v), "%s: %s", #x, strerror(ret)); \
+    } while (0)
+
 #endif // H_COMMON_H
diff -r 5353b2f73110 -r 777aaadb2d16 tests/lib/libpthread/t_mutex.c
--- a/tests/lib/libpthread/t_mutex.c    Sun Oct 30 15:47:06 2016 +0000
+++ b/tests/lib/libpthread/t_mutex.c    Sun Oct 30 16:17:16 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: t_mutex.c,v 1.10 2016/07/31 13:01:29 christos Exp $ */
+/* $NetBSD: t_mutex.c,v 1.11 2016/10/30 16:17:16 kamil Exp $ */
 
 /*
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -26,10 +26,12 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+/* Please sync code from this test with t_timedmutex.c */
+
 #include <sys/cdefs.h>
 __COPYRIGHT("@(#) Copyright (c) 2008\
  The NetBSD Foundation, inc. All rights reserved.");
-__RCSID("$NetBSD: t_mutex.c,v 1.10 2016/07/31 13:01:29 christos Exp $");
+__RCSID("$NetBSD: t_mutex.c,v 1.11 2016/10/30 16:17:16 kamil Exp $");
 
 #include <pthread.h>
 #include <stdio.h>
diff -r 5353b2f73110 -r 777aaadb2d16 tests/lib/libpthread/t_timedmutex.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libpthread/t_timedmutex.c       Sun Oct 30 16:17:16 2016 +0000
@@ -0,0 +1,634 @@
+/* $NetBSD: t_timedmutex.c,v 1.1 2016/10/30 16:17:16 kamil Exp $ */
+
+/*
+ * Copyright (c) 2008 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.
+ */
+
+/* Please sync code from this test with t_mutex.c unless it's timed specific */
+
+#include <sys/cdefs.h>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_timedmutex.c,v 1.1 2016/10/30 16:17:16 kamil Exp $");
+
+#include <pthread.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/sched.h>
+#include <sys/param.h>
+
+#include <atf-c.h>
+
+#include "h_common.h"
+
+static pthread_mutex_t mutex;
+static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
+static int global_x;
+
+/* This code is used for verifying non-timed specific code */
+static struct timespec ts_lengthy = {
+       .tv_sec = UINT16_MAX,
+       .tv_nsec = 0
+};
+/* This code is used for verifying timed-only specific code */
+static struct timespec ts_shortlived = {
+       .tv_sec = 0,
+       .tv_nsec = 120
+};
+
+static void *
+mutex1_threadfunc(void *arg)
+{
+       int *param;
+
+       printf("2: Second thread.\n");
+
+       param = arg;
+       printf("2: Locking mutex\n");
+       pthread_mutex_timedlock(&mutex, &ts_lengthy);
+       printf("2: Got mutex. *param = %d\n", *param);
+       ATF_REQUIRE_EQ(*param, 20);
+       (*param)++;
+
+       pthread_mutex_unlock(&mutex);
+
+       return param;
+}
+
+ATF_TC(mutex1);
+ATF_TC_HEAD(mutex1, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Checks mutexes");
+}
+ATF_TC_BODY(mutex1, tc)
+{
+       int x;
+       pthread_t new;
+       void *joinval;
+
+       printf("1: Mutex-test 1\n");
+
+       PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
+       x = 1;
+       PTHREAD_REQUIRE(pthread_mutex_timedlock(&mutex, &ts_lengthy));
+       PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x));
+       printf("1: Before changing the value.\n");
+       sleep(2);
+       x = 20;
+       printf("1: Before releasing the mutex.\n");
+       sleep(2);
+       PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
+       printf("1: After releasing the mutex.\n");
+       PTHREAD_REQUIRE(pthread_join(new, &joinval));
+
+       PTHREAD_REQUIRE(pthread_mutex_timedlock(&mutex, &ts_lengthy));
+       printf("1: Thread joined. X was %d. Return value (int) was %d\n",
+               x, *(int *)joinval);
+       ATF_REQUIRE_EQ(x, 21);
+       ATF_REQUIRE_EQ(*(int *)joinval, 21);
+       PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
+}
+
+static void *
+mutex2_threadfunc(void *arg)
+{
+       long count = *(int *)arg;
+
+       printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
+
+       while (count--) {
+               PTHREAD_REQUIRE(pthread_mutex_timedlock(&mutex, &ts_lengthy));
+               global_x++;
+               PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
+       }
+
+       return (void *)count;
+}
+
+ATF_TC(mutex2);
+ATF_TC_HEAD(mutex2, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Checks mutexes");
+#if defined(__powerpc__)
+       atf_tc_set_md_var(tc, "timeout", "40");
+#endif
+}
+ATF_TC_BODY(mutex2, tc)
+{
+       int count, count2;
+       pthread_t new;
+       void *joinval;
+
+       printf("1: Mutex-test 2\n");
+
+#if defined(__powerpc__)
+       atf_tc_expect_timeout("PR port-powerpc/44387");
+#endif
+
+       PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
+       
+       global_x = 0;
+       count = count2 = 10000000;
+
+       PTHREAD_REQUIRE(pthread_mutex_timedlock(&mutex, &ts_lengthy));
+       PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
+
+       printf("1: Thread %p\n", pthread_self());
+
+       PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
+
+       while (count--) {
+               PTHREAD_REQUIRE(pthread_mutex_timedlock(&mutex, &ts_lengthy));
+               global_x++;
+               PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
+       }
+
+       PTHREAD_REQUIRE(pthread_join(new, &joinval));
+
+       PTHREAD_REQUIRE(pthread_mutex_timedlock(&mutex, &ts_lengthy));
+       printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
+               global_x, (long)joinval);
+       ATF_REQUIRE_EQ(global_x, 20000000);
+
+#if defined(__powerpc__)
+       /* XXX force a timeout in ppc case since an un-triggered race
+          otherwise looks like a "failure" */
+       /* We sleep for longer than the timeout to make ATF not
+          complain about unexpected success */
+       sleep(41);
+#endif
+}
+
+static void *
+mutex3_threadfunc(void *arg)
+{
+       long count = *(int *)arg;
+
+       printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
+
+       while (count--) {
+               PTHREAD_REQUIRE(pthread_mutex_timedlock(&static_mutex, &ts_lengthy));
+               global_x++;
+               PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
+       }
+
+       return (void *)count;
+}
+
+ATF_TC(mutex3);
+ATF_TC_HEAD(mutex3, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
+           "initializer");
+#if defined(__powerpc__)



Home | Main Index | Thread Index | Old Index