[Piglit] [PATCH 06/12] util: add non-monotonic/windows version of piglit_time_get_nano

Emil Velikov emil.l.velikov at gmail.com
Sat Nov 22 14:40:28 PST 2014


Fallback to gettimeofday, if clock_gettime supports monotonic yet
fails to get the time.

v2: Fix gettimeofday fallback.

Signed-off-by: Emil Velikov <emil.l.velikov at gmail.com>
---
 tests/util/piglit-util.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/tests/util/piglit-util.c b/tests/util/piglit-util.c
index 5180bb8..b089944 100644
--- a/tests/util/piglit-util.c
+++ b/tests/util/piglit-util.c
@@ -662,15 +662,26 @@ piglit_time_is_monotonic(void)
 int64_t
 piglit_time_get_nano(void)
 {
+#if !defined(_WIN32)
 #ifdef PIGLIT_HAS_POSIX_CLOCK_MONOTONIC
 	struct timespec t;
 	int r = clock_gettime(CLOCK_MONOTONIC, &t);
-	if (r >= 0)
+
+	if (r == 0 || (r == -1 && errno != EINVAL))
 		return (t.tv_sec * INT64_C(1000000000)) + t.tv_nsec;
-	else
-		return -1LL;
+#endif
+	struct timeval tv;
+
+	gettimeofday(&tv, NULL);
+	return tv.tv_usec * INT64_C(1000) + tv.tv_sec * INT64_C(1000000000);
 #else
-	return -1LL;
+	static LARGE_INTEGER frequency;
+	LARGE_INTEGER counter;
+
+	if (!frequency.QuadPart)
+		QueryPerformanceFrequency(&frequency);
+	QueryPerformanceCounter(&counter);
+	return counter.QuadPart * INT64_C(1000000000)/frequency.QuadPart;
 #endif
 }
 
-- 
2.1.3



More information about the Piglit mailing list