diff mbox

[i-g-t,v3] lib/igt_core: Prefer CLOCK_MONOTONIC_RAW

Message ID 1447849132-10485-1-git-send-email-joonas.lahtinen@linux.intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Joonas Lahtinen Nov. 18, 2015, 12:18 p.m. UTC
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@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Signed-off-by: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
---
 lib/igt_core.c | 51 +++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 41 insertions(+), 10 deletions(-)

Comments

Chris Wilson Nov. 18, 2015, 12:28 p.m. UTC | #1
On Wed, Nov 18, 2015 at 02:18:52PM +0200, Joonas Lahtinen wrote:
> 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

Aye, that's better for the subtest handling.

> @@ -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) {

A little paranoid? If we want the paranoia perhaps move it to gettime
and return an error?
-Chris
Joonas Lahtinen Nov. 18, 2015, 12:54 p.m. UTC | #2
On ke, 2015-11-18 at 12:28 +0000, Chris Wilson wrote:
> On Wed, Nov 18, 2015 at 02:18:52PM +0200, Joonas Lahtinen wrote:
> > 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
> 
> Aye, that's better for the subtest handling.
> 
> > @@ -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) {
> 
> A little paranoid? If we want the paranoia perhaps move it to gettime
> and return an error?
> 

I'm just checking that the starting time which is in the subtest_time
did not fail (which leads to both being zero). I'm not looking at the
now values :)

Regards, Joonas

> -Chris
>
Chris Wilson Nov. 18, 2015, 1:07 p.m. UTC | #3
On Wed, Nov 18, 2015 at 02:54:20PM +0200, Joonas Lahtinen wrote:
> On ke, 2015-11-18 at 12:28 +0000, Chris Wilson wrote:
> > On Wed, Nov 18, 2015 at 02:18:52PM +0200, Joonas Lahtinen wrote:
> > > 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
> > 
> > Aye, that's better for the subtest handling.
> > 
> > > @@ -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) {
> > 
> > A little paranoid? If we want the paranoia perhaps move it to gettime
> > and return an error?
> > 
> 
> I'm just checking that the starting time which is in the subtest_time
> did not fail (which leads to both being zero). I'm not looking at the
> now values :)

Ok, now I see.

#define time_valid(ts) ((ts)->tv_sec || (ts)->tv_nsec)

gettime(&now);
if (time_valid(&now) && time_valid(&subtest_time)) {
	elapsed = ...;
} else {
	elapsed = -1;
}

Perhaps?
-Chris
diff mbox

Patch

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);
 	}