git: 74c02b96ad5d - stable/13 - sem_clockwait_np test: relax time constraint on VMs

From: Eric van Gyzen <vangyzen_at_FreeBSD.org>
Date: Wed, 02 Mar 2022 21:57:22 UTC
The branch stable/13 has been updated by vangyzen:

URL: https://cgit.FreeBSD.org/src/commit/?id=74c02b96ad5d02c80d0e4e6f9c0abd9f1912e61d

commit 74c02b96ad5d02c80d0e4e6f9c0abd9f1912e61d
Author:     Eric van Gyzen <vangyzen@FreeBSD.org>
AuthorDate: 2021-10-01 10:37:17 +0000
Commit:     Eric van Gyzen <vangyzen@FreeBSD.org>
CommitDate: 2022-03-02 21:56:30 +0000

    sem_clockwait_np test: relax time constraint on VMs
    
    In a guest on a busy hypervisor, the time remaining after an
    interrupted sleep could be much lower than other environments.
    Relax the lower bound on VMs.
    
    MFC after:      1 week
    Sponsored by:   Dell EMC Isilon
    
    (cherry picked from commit 31466594cd09d1413dbd8cab516fafc3557c2200)
---
 contrib/netbsd-tests/lib/librt/t_sem.c | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)

diff --git a/contrib/netbsd-tests/lib/librt/t_sem.c b/contrib/netbsd-tests/lib/librt/t_sem.c
index 5e059238f278..55a416083846 100644
--- a/contrib/netbsd-tests/lib/librt/t_sem.c
+++ b/contrib/netbsd-tests/lib/librt/t_sem.c
@@ -60,6 +60,11 @@ __COPYRIGHT("@(#) Copyright (c) 2008, 2010\
  The NetBSD Foundation, inc. All rights reserved.");
 __RCSID("$NetBSD: t_sem.c,v 1.3 2017/01/14 20:58:20 christos Exp $");
 
+#ifdef __FreeBSD__
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#endif
+
 #include <sys/time.h>
 #include <sys/wait.h>
 
@@ -78,6 +83,24 @@ __RCSID("$NetBSD: t_sem.c,v 1.3 2017/01/14 20:58:20 christos Exp $");
 #define SEM_REQUIRE(x) \
 	ATF_REQUIRE_EQ_MSG(x, 0, "%s", strerror(errno))
 
+#ifdef __FreeBSD__
+static bool
+machine_is_virtual(void)
+{
+	char vm_guest[32];
+	int error;
+
+	error = sysctlbyname("kern.vm_guest", vm_guest,
+	    &(size_t){sizeof(vm_guest)}, NULL, 0);
+	ATF_CHECK_EQ_MSG(0, error, "sysctlbyname(kern.vm_guest): %s",
+	    strerror(errno));
+	if (error != 0) {
+		return (false);
+	}
+	return (strcmp(vm_guest, "none") != 0);
+}
+#endif
+
 ATF_TC_WITH_CLEANUP(basic);
 ATF_TC_HEAD(basic, tc)
 {
@@ -306,15 +329,11 @@ ATF_TC_BODY(timedwait, tc)
 	ATF_REQUIRE_ERRNO(EINTR, sem_clockwait_np(&sem, CLOCK_MONOTONIC, 0, &ts,
 	    &remain));
 	ATF_REQUIRE_MSG(got_sigalrm, "did not get SIGALRM");
-	/*
-	 * If this nsec comparison turns out to be unreliable due to timing,
-	 * it could simply check that nsec < 100 ms.
-	 */
 	ATF_REQUIRE_MSG(remain.tv_sec == 0 &&
-	    remain.tv_nsec >= 25*1000*1000 &&
+	    (remain.tv_nsec >= 25*1000*1000 || machine_is_virtual()) &&
 	    remain.tv_nsec <= 75*1000*1000,
 	    "the remaining time was not as expected when a relative clockwait"
-	    " got EINTR" );
+	    " got EINTR: %ld.%09ld", remain.tv_sec, remain.tv_nsec);
 #endif
 }