Use VDSO for clock_gettime(2) and gettimeofday(2).

Bug: 15387103

(cherry picked from commit 625993dfbb085a3cde7492eda8ec1cdc1ee39a78)

Change-Id: I0e156d7049ba1495902259071a96936592e74025
diff --git a/tests/sys_time_test.cpp b/tests/sys_time_test.cpp
index 730992f..bb142bc 100644
--- a/tests/sys_time_test.cpp
+++ b/tests/sys_time_test.cpp
@@ -17,6 +17,7 @@
 #include <gtest/gtest.h>
 
 #include <errno.h>
+#include <sys/syscall.h>
 #include <sys/time.h>
 
 #include "TemporaryFile.h"
@@ -46,3 +47,23 @@
   TemporaryFile tf;
   ASSERT_EQ(0, utimes(tf.filename, NULL));
 }
+
+TEST(sys_time, gettimeofday) {
+  // Try to ensure that our vdso gettimeofday is working.
+  timeval tv1;
+  ASSERT_EQ(0, gettimeofday(&tv1, NULL));
+  timeval tv2;
+  ASSERT_EQ(0, syscall(__NR_gettimeofday, &tv2, NULL));
+
+  // What's the difference between the two?
+  tv2.tv_sec -= tv1.tv_sec;
+  tv2.tv_usec -= tv1.tv_usec;
+  if (tv2.tv_usec < 0) {
+    --tv2.tv_sec;
+    tv2.tv_usec += 1000000;
+  }
+
+  // Should be less than (a very generous, to try to avoid flakiness) 1000us.
+  ASSERT_EQ(0, tv2.tv_sec);
+  ASSERT_LT(tv2.tv_usec, 1000);
+}
diff --git a/tests/time_test.cpp b/tests/time_test.cpp
index 58cb374..12b1ea7 100644
--- a/tests/time_test.cpp
+++ b/tests/time_test.cpp
@@ -21,6 +21,7 @@
 #include <gtest/gtest.h>
 #include <pthread.h>
 #include <signal.h>
+#include <sys/syscall.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 
@@ -419,3 +420,23 @@
   ASSERT_EQ(ESRCH, pthread_detach(tdd.thread_id));
 #endif
 }
+
+TEST(time, clock_gettime) {
+  // Try to ensure that our vdso clock_gettime is working.
+  timespec ts1;
+  ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
+  timespec ts2;
+  ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
+
+  // What's the difference between the two?
+  ts2.tv_sec -= ts1.tv_sec;
+  ts2.tv_nsec -= ts1.tv_nsec;
+  if (ts2.tv_nsec < 0) {
+    --ts2.tv_sec;
+    ts2.tv_nsec += 1000000000;
+  }
+
+  // Should be less than (a very generous, to try to avoid flakiness) 1000000ns.
+  ASSERT_EQ(0, ts2.tv_sec);
+  ASSERT_LT(ts2.tv_nsec, 1000000);
+}