Revision: 17642
Author:   [email protected]
Date:     Tue Nov 12 12:18:35 2013 UTC
Log:      linux: use CLOCK_{REALTIME,MONOTONIC}_COARSE

[email protected]

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

Patch from Ben Noordhuis <[email protected]>.
http://code.google.com/p/v8/source/detail?r=17642

Modified:
 /branches/bleeding_edge/src/platform/time.cc
 /branches/bleeding_edge/src/platform/time.h

=======================================
--- /branches/bleeding_edge/src/platform/time.cc Mon Nov 11 08:46:45 2013 UTC +++ /branches/bleeding_edge/src/platform/time.cc Tue Nov 12 12:18:35 2013 UTC
@@ -43,6 +43,17 @@
 #include "win32-headers.h"
 #endif

+// Make sure CLOCK_{MONOTONIC,REALTIME}_COARSE is defined on Linux.
+#if V8_OS_LINUX
+# if !defined(CLOCK_REALTIME_COARSE)
+#  define CLOCK_REALTIME_COARSE 5   // 2.6.32 and up.
+# endif
+# if !defined(CLOCK_MONOTONIC_COARSE)
+#  define CLOCK_MONOTONIC_COARSE 6  // 2.6.32 and up.
+# endif
+#endif  // V8_OS_LINUX
+
+
 namespace v8 {
 namespace internal {

@@ -271,11 +282,38 @@
 #elif V8_OS_POSIX

 Time Time::Now() {
+#if V8_LIBRT_NOT_AVAILABLE
+  // TODO(bmeurer): This is a temporary hack to support cross-compiling
+  // Chrome for Android in AOSP. Remove this once AOSP is fixed, also
+  // cleanup the tools/gyp/v8.gyp file.
   struct timeval tv;
   int result = gettimeofday(&tv, NULL);
   ASSERT_EQ(0, result);
   USE(result);
   return FromTimeval(tv);
+#elif defined(CLOCK_REALTIME_COARSE)
+  struct timespec ts;
+  // Use CLOCK_REALTIME_COARSE if it's available and has a precision of 1ms
+  // or higher.  It's serviced from the vDSO with no system call overhead.
+  static clock_t clock_id = static_cast<clock_t>(0);
+  if (!clock_id) {
+    if (clock_getres(CLOCK_REALTIME_COARSE, &ts) == 0
+        && ts.tv_nsec <= kNanosecondsPerMillisecond)
+      clock_id = CLOCK_REALTIME_COARSE;
+    else
+      clock_id = CLOCK_REALTIME;
+  }
+  int result = clock_gettime(clock_id, &ts);
+  ASSERT_EQ(0, result);
+  USE(result);
+  return FromTimespec(ts);
+#else
+  struct timeval tv;
+  int result = gettimeofday(&tv, NULL);
+  ASSERT_EQ(0, result);
+  USE(result);
+  return FromTimeval(tv);
+#endif  // V8_LIBRT_NOT_AVAILABLE
 }


@@ -570,7 +608,21 @@
   ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec);
 #elif V8_OS_POSIX
   struct timespec ts;
-  int result = clock_gettime(CLOCK_MONOTONIC, &ts);
+#if defined(CLOCK_MONOTONIC_COARSE)
+ // Use CLOCK_MONOTONIC_COARSE if it's available and has a precision of 1ms
+  // or higher.  It's serviced from the vDSO with no system call overhead.
+  static clock_t clock_id = static_cast<clock_t>(0);
+  if (!clock_id) {
+    if (clock_getres(CLOCK_MONOTONIC_COARSE, &ts) == 0
+        && ts.tv_nsec <= Time::kNanosecondsPerMillisecond)
+      clock_id = CLOCK_MONOTONIC_COARSE;
+    else
+      clock_id = CLOCK_MONOTONIC;
+  }
+#else
+  static const clock_t clock_id = CLOCK_MONOTONIC;
+#endif  // defined(CLOCK_MONOTONIC_COARSE)
+  int result = clock_gettime(clock_id, &ts);
   ASSERT_EQ(0, result);
   USE(result);
   ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond +
=======================================
--- /branches/bleeding_edge/src/platform/time.h Fri Oct  4 14:53:16 2013 UTC
+++ /branches/bleeding_edge/src/platform/time.h Tue Nov 12 12:18:35 2013 UTC
@@ -192,6 +192,8 @@
   static const int64_t kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
   static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
   static const int64_t kNanosecondsPerMicrosecond = 1000;
+ static const int64_t kNanosecondsPerMillisecond = kNanosecondsPerMicrosecond * + kMicrosecondsPerMillisecond;
   static const int64_t kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
                                                kMicrosecondsPerSecond;

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to