Provides a utility for blocking the cpu for a requested time (nanosleep
+ retry on SIGINT for Linux else usleep()) and reports the final
duration to help tests be resilient to over or under runs due to
signalling or scheduling.

Signed-off-by: Robert Bragg <[email protected]>
---
 tests/util/piglit-util.c | 29 +++++++++++++++++++++++++----
 tests/util/piglit-util.h | 15 +++++++++++++++
 2 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/tests/util/piglit-util.c b/tests/util/piglit-util.c
index 0a66a85..b9c4606 100644
--- a/tests/util/piglit-util.c
+++ b/tests/util/piglit-util.c
@@ -32,6 +32,7 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/syscall.h>
+#include <time.h>
 #endif
 
 #include <assert.h>
@@ -42,13 +43,10 @@
 #include <errno.h>
 #include <inttypes.h>
 
-#ifdef PIGLIT_HAS_POSIX_CLOCK_MONOTONIC
-#ifdef PIGLIT_HAS_POSIX_TIMER_NOTIFY_THREAD
+#if defined(PIGLIT_HAS_POSIX_CLOCK_MONOTONIC) && 
defined(PIGLIT_HAS_POSIX_TIMER_NOTIFY_THREAD)
 #include <pthread.h>
 #include <signal.h>
 #endif
-#include <time.h>
-#endif
 
 #include "config.h"
 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H) && 
defined(HAVE_SETRLIMIT)
@@ -624,6 +622,29 @@ piglit_time_get_nano(void)
 #endif
 }
 
+int64_t
+piglit_delay_ns(int64_t time_ns)
+{
+       int64_t start = start = piglit_time_get_nano();
+       int64_t end;
+
+#ifdef __linux__
+       struct timespec ts;
+
+       ts.tv_sec = time_ns / 1000000000LL;
+       ts.tv_nsec = time_ns - ts.tv_sec * 1000000000LL;
+
+       while (nanosleep(&ts, &ts) == -1 && errno == EINTR)
+               ;
+#else
+       usleep(time_ns / 1000);
+#endif
+
+       end = piglit_time_get_nano();
+
+       return end - start;
+}
+
 /**
  * Search for an argument with the given name in the argument list.
  * If it is found, remove it and return true.
diff --git a/tests/util/piglit-util.h b/tests/util/piglit-util.h
index 4328863..a36ffe2 100644
--- a/tests/util/piglit-util.h
+++ b/tests/util/piglit-util.h
@@ -406,6 +406,21 @@ piglit_time_is_monotonic();
 int64_t
 piglit_time_get_nano(void);
 
+/**
+ * \brief Try to delay for a given duration
+ *
+ * \param time_ns      The requested duration to wait for
+ *
+ * Returns the time elapsed which may be cut short or overrun the requested
+ * duration, e.g. due to signal handling or scheduling.
+ *
+ * \sa on Linux nanosleep is used and restarted on SIGINT
+ * \sa usleep() is use on other OSs
+ * \sa the returned duration is timed using piglit_time_get_nano()
+ */
+int64_t
+piglit_delay_ns(int64_t time_ns);
+
 const char**
 piglit_split_string_to_array(const char *string, const char *separators);
 
-- 
2.9.2

_______________________________________________
Piglit mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/piglit

Reply via email to