[Intel-gfx] [PATCH i-g-t v3] lib/igt_core: Prefer CLOCK_MONOTONIC_RAW

Joonas Lahtinen joonas.lahtinen at linux.intel.com
Wed Nov 18 04:18:52 PST 2015


CLOCK_MONOTONIC_RAW is not affected by NTP, so it should be THE clock
used for timing execution of tests.

When fetching either the starting or ending time of a test, show the
time as -1.000s.

v3:
- Do not exit directly from handler (Chris)
- Show elapsed time as -1 if it is not calculable

v2:
- Cache the used clock (Chris)
- Do not change the clock during execution
- Spit out and error if monotonic time can not be read

Cc: Thomas Wood <thomas.wood at intel.com>
Cc: Chris Wilson <chris at chris-wilson.co.uk>
Signed-off-by: Joonas Lahtinen <joonas.lahtinen at linux.intel.com>
---
 lib/igt_core.c | 51 +++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 41 insertions(+), 10 deletions(-)

diff --git a/lib/igt_core.c b/lib/igt_core.c
index 04a0ab2..b4cab42 100644
--- a/lib/igt_core.c
+++ b/lib/igt_core.c
@@ -220,6 +220,7 @@ static char *run_single_subtest = NULL;
 static bool run_single_subtest_found = false;
 static const char *in_subtest = NULL;
 static struct timespec subtest_time;
+static clockid_t igt_clock = (clockid_t)-1;
 static bool in_fixture = false;
 static bool test_with_subtests = false;
 static bool in_atexit_handler = false;
@@ -337,14 +338,32 @@ static void kmsg(const char *format, ...)
 	fclose(file);
 }
 
-static void gettime(struct timespec *ts)
+static int gettime(struct timespec *ts)
 {
 	memset(ts, 0, sizeof(*ts));
+	errno = 0;
 
+	// Stay on the same clock for consistency.
+	if (igt_clock != (clockid_t)-1) {
+		if (clock_gettime(igt_clock, ts))
+			goto error;
+		return 0;
+	}
+
+#ifdef CLOCK_MONOTONIC_RAW
+	if (!clock_gettime(igt_clock = CLOCK_MONOTONIC_RAW, ts))
+		return 0;
+#endif
 #ifdef CLOCK_MONOTONIC_COARSE
-	if (clock_gettime(CLOCK_MONOTONIC_COARSE, ts))
+	if (!clock_gettime(igt_clock = CLOCK_MONOTONIC_COARSE, ts))
+		return 0;
 #endif
-		clock_gettime(CLOCK_MONOTONIC, ts);
+	if (!clock_gettime(igt_clock = CLOCK_MONOTONIC, ts))
+		return 0;
+error:
+	igt_warn("Could not read monotonic time: %s\n",
+		 strerror(errno));
+	return -errno;
 }
 
 bool __igt_fixture(void)
@@ -832,10 +851,16 @@ static void exit_subtest(const char *result)
 {
 	struct timespec now;
 	double elapsed;
+	int err;
 
-	gettime(&now);
-	elapsed = now.tv_sec - subtest_time.tv_sec;
-	elapsed += (now.tv_nsec - subtest_time.tv_nsec) * 1e-9;
+	err = gettime(&now);
+	if (!err && subtest_time.tv_sec != 0 &&
+	    subtest_time.tv_nsec != 0) {
+		elapsed = now.tv_sec - subtest_time.tv_sec;
+		elapsed += (now.tv_nsec - subtest_time.tv_nsec) * 1e-9;
+	} else {
+		elapsed = -1.;
+	}
 
 	printf("%sSubtest %s: %s (%.3fs)%s\n",
 	       (!__igt_plain_output) ? "\x1b[1m" : "",
@@ -1090,10 +1115,17 @@ void igt_exit(void)
 		struct timespec now;
 		double elapsed;
 		const char *result;
+		int err;
 
-		gettime(&now);
-		elapsed = now.tv_sec - subtest_time.tv_sec;
-		elapsed += (now.tv_nsec - subtest_time.tv_nsec) * 1e-9;
+		err = gettime(&now);
+
+		if (!err && subtest_time.tv_sec != 0 &&
+		    subtest_time.tv_nsec != 0) {
+			elapsed = now.tv_sec - subtest_time.tv_sec;
+			elapsed += (now.tv_nsec - subtest_time.tv_nsec) * 1e-9;
+		} else {
+			elapsed = -1.;
+		}
 
 		switch (igt_exitcode) {
 			case IGT_EXIT_SUCCESS:
@@ -1109,7 +1141,6 @@ void igt_exit(void)
 				result = "FAIL";
 		}
 
-
 		printf("%s (%.3fs)\n", result, elapsed);
 		exit(igt_exitcode);
 	}
-- 
2.4.3



More information about the Intel-gfx mailing list