Use CLOCK_MONOTONIC_COARSE and CLOCK_REALTIME_COARSE on Linux if available.
authorbmeurer@chromium.org <bmeurer@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 11 Nov 2013 07:49:18 +0000 (07:49 +0000)
committerbmeurer@chromium.org <bmeurer@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 11 Nov 2013 07:49:18 +0000 (07:49 +0000)
R=bmeurer@chromium.org

Review URL: https://codereview.chromium.org/51333007

Patch from Ben Noordhuis <info@bnoordhuis.nl>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@17611 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/platform/time.cc

index de0ca16..0b58f19 100644 (file)
 #include "win32-headers.h"
 #endif
 
+#if V8_OS_LINUX
+#if !defined(CLOCK_REALTIME_COARSE)
+#define CLOCK_REALTIME_COARSE 5   // 2.6.32 and up.
+#endif  // !defined(CLOCK_REALTIME_COARSE)
+#if !defined(CLOCK_MONOTONIC_COARSE)
+#define CLOCK_MONOTONIC_COARSE 6  // 2.6.32 and up.
+#endif  // !defined(CLOCK_MONOTONIC_COARSE)
+#endif  // V8_OS_LINUX
+
 namespace v8 {
 namespace internal {
 
@@ -271,11 +280,29 @@ FILETIME Time::ToFiletime() const {
 #elif V8_OS_POSIX
 
 Time Time::Now() {
+#if V8_OS_LINUX
+  // Use CLOCK_REALTIME_COARSE if it's available and has a precision of 1 ms
+  // or higher.  It's serviced from the vDSO with no system call overhead.
+  static clock_t clock_id = -1;
+  struct timespec ts;
+  if (clock_id == -1) {
+    if (clock_getres(CLOCK_REALTIME_COARSE, &ts) || ts.tv_nsec > 1000 * 1000) {
+      clock_id = CLOCK_REALTIME;
+    } else {
+      clock_id = CLOCK_REALTIME_COARSE;
+    }
+  }
+  int result = clock_gettime(clock_id, &ts);
+  ASSERT_EQ(0, result);
+  USE(result);
+  return FromTimespec(ts);
+#else  // V8_OS_LINUX
   struct timeval tv;
   int result = gettimeofday(&tv, NULL);
   ASSERT_EQ(0, result);
   USE(result);
   return FromTimeval(tv);
+#endif  // V8_OS_LINUX
 }
 
 
@@ -570,7 +597,21 @@ TimeTicks TimeTicks::HighResolutionNow() {
   ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec);
 #elif V8_OS_POSIX
   struct timespec ts;
+#if V8_OS_LINUX
+  // Use CLOCK_MONOTONIC_COARSE if it's available and has a precision of 1 ms
+  // or higher.  It's serviced from the vDSO with no system call overhead.
+  static clock_t clock_id = -1;
+  if (clock_id == -1) {
+    if (clock_getres(CLOCK_MONOTONIC_COARSE, &ts) || ts.tv_nsec > 1000 * 1000) {
+      clock_id = CLOCK_MONOTONIC;
+    } else {
+      clock_id = CLOCK_MONOTONIC_COARSE;
+    }
+  }
+  int result = clock_gettime(clock_id, &ts);
+#else
   int result = clock_gettime(CLOCK_MONOTONIC, &ts);
+#endif  // V8_OS_LINUX
   ASSERT_EQ(0, result);
   USE(result);
   ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond +