Use nanoseconds timestamps for TimeTest

Using `clock_gettime(CLOCK_MONOTONIC, ...)` fetches timespecs with nanoseconds
at no extra cost.  This added time precision is useful for time stamping small
numbers of iterations or very fast test cases, particularly when using glbench
as a debugging tool.

Using nanosecond resolution should have minimal effect on existing way scores
are currently computed using microseconds, other than possibly reducing
variance due to quantization effects.

BUG=none
TEST=run glbench; scores are roughly the same before and after change

Change-Id: I252e7892f3d941cb2dc5c9e11a995cba21ac5faa
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/glbench/+/2775409
Reviewed-by: Ilja H. Friedel <ihf@chromium.org>
Commit-Queue: Ilja H. Friedel <ihf@chromium.org>
Tested-by: Ilja H. Friedel <ihf@chromium.org>
diff --git a/src/main.h b/src/main.h
index 9846d03..a3f39a1 100644
--- a/src/main.h
+++ b/src/main.h
@@ -92,6 +92,13 @@
          1000000ULL * static_cast<uint64_t>(tv.tv_sec);
 }
 
+inline uint64_t GetNTime() {
+  struct timespec tv;
+  clock_gettime(CLOCK_MONOTONIC, &tv);
+  return static_cast<uint64_t>(tv.tv_nsec) +
+         1000000000ULL * static_cast<uint64_t>(tv.tv_sec);
+}
+
 extern bool g_verbose;
 extern bool g_hasty;
 extern GLint g_width;
diff --git a/src/testbase.cc b/src/testbase.cc
index 71b5a35..8065235 100644
--- a/src/testbase.cc
+++ b/src/testbase.cc
@@ -27,18 +27,18 @@
 uint64_t TimeTest(TestBase* test, uint64_t iterations) {
   g_main_gl_interface->SwapBuffers();
   glFinish();
-  uint64_t time1 = GetUTime();
+  uint64_t time1 = GetNTime();
   if (!test->TestFunc(iterations))
     return ~0;
   glFinish();
-  uint64_t time2 = GetUTime();
+  uint64_t time2 = GetNTime();
   return time2 - time1;
 }
 
 // Target minimum iteration duration of 0.4s. This means the final/longest
 // iteration is between 0.4s and 0.8s and the machine is active for 0.8s to 1.6s.
 // Notice as of March 2014 the BVT suite has a hard limit per job of 20 minutes.
-#define MIN_ITERATION_DURATION_US 400000
+#define MIN_ITERATION_DURATION_NS 400000000
 
 #define MAX_TESTNAME 46
 
@@ -71,7 +71,7 @@
 
   // We average the times for the last two runs to reduce noise. We could
   // sum up all runs but the initial measurements have high CPU overhead,
-  // while the last two runs are both on the order of MIN_ITERATION_DURATION_US.
+  // while the last two runs are both on the order of MIN_ITERATION_DURATION_NS.
   uint64_t iterations = 1;
   uint64_t iterations_prev = 0;
   uint64_t time = 0;
@@ -84,9 +84,11 @@
     // If we are running in hasty mode we will stop after a fraction of the
     // testing time and return much more noisy performance numbers. The MD5s
     // of the images should stay the same though.
-    if (time > MIN_ITERATION_DURATION_US / (::g_hasty ? 20.0 : 1.0))
+    // We divide by 1000.0 to convert from nanoseconds to microseconds for use
+    // by the caller to compute Mpixel/sec.
+    if (time > MIN_ITERATION_DURATION_NS / (::g_hasty ? 20.0 : 1.0))
       return (static_cast<double>(time + time_prev) /
-              (iterations + iterations_prev));
+              (iterations + iterations_prev)) / 1000.0;
 
     time_prev = time;
     iterations_prev = iterations;