Reviewers: Benedikt Meurer, Sven Panne,

Message:
Use CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE when available for reasons
of being faster than the traditional gettimeofday() and
clock_gettime(CLOCK_MONOTONIC) system calls.

As mentioned in CR 51333007, I can change the guard logic to `#if
defined(CLOCK_REALTIME_COARSE)` and `#if defined(CLOCK_MONOTONIC_COARSE)` so
Android gets it for free if/when they add support for it.

Description:
linux: use CLOCK_{REALTIME,MONOTONIC}_COARSE

Please review this at https://codereview.chromium.org/68203004/

Affected files (+41, -0 lines):
  M src/platform/time.cc


Index: src/platform/time.cc
diff --git a/src/platform/time.cc b/src/platform/time.cc
index de0ca16473f6b5106485508653cfe797f6632c37..e3dae354203f12a591301a7e17770b1b2e684ec5 100644
--- a/src/platform/time.cc
+++ b/src/platform/time.cc
@@ -43,6 +43,15 @@
 #include "win32-headers.h"
 #endif

+#if V8_OS_LINUX && !V8_OS_ANDROID
+#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 && !V8_OS_ANDROID
+
 namespace v8 {
 namespace internal {

@@ -271,11 +280,29 @@ FILETIME Time::ToFiletime() const {
 #elif V8_OS_POSIX

 Time Time::Now() {
+#if V8_OS_LINUX && !V8_OS_ANDROID
+ // 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 && !V8_OS_ANDROID
   struct timeval tv;
   int result = gettimeofday(&tv, NULL);
   ASSERT_EQ(0, result);
   USE(result);
   return FromTimeval(tv);
+#endif  // V8_OS_LINUX && !V8_OS_ANDROID
 }


@@ -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 && !V8_OS_ANDROID
+ // 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  // V8_OS_LINUX && !V8_OS_ANDROID
   int result = clock_gettime(CLOCK_MONOTONIC, &ts);
+#endif  // V8_OS_LINUX && !V8_OS_ANDROID
   ASSERT_EQ(0, result);
   USE(result);
   ticks = (ts.tv_sec * Time::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