Reviewers: ,

Message:
Use clock_gettime(CLOCK_MONOTONIC_COARSE) if available, saves the overhead of a
system call on older and virtualized systems.

Description:
linux: use CLOCK_MONOTONIC_COARSE

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

Affected files (+34, -1 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..a6c426f62db48c4e71f18b4b5789744baf1c597a 100644
--- a/src/platform/time.cc
+++ b/src/platform/time.cc
@@ -28,6 +28,7 @@
 #include "platform/time.h"

 #if V8_OS_POSIX
+#include <errno.h>
 #include <sys/time.h>
 #endif
 #if V8_OS_MACOSX
@@ -270,6 +271,8 @@ FILETIME Time::ToFiletime() const {

 #elif V8_OS_POSIX

+// OS X doesn't support POSIX high-res timers and Solaris uses gethrtime().
+#if V8_OS_MACOSX || V8_OS_SOLARIS
 Time Time::Now() {
   struct timeval tv;
   int result = gettimeofday(&tv, NULL);
@@ -277,6 +280,36 @@ Time Time::Now() {
   USE(result);
   return FromTimeval(tv);
 }
+#else
+inline int GetBestMonotonicClock(struct timespec* ts) {
+#if V8_OS_LINUX
+ // Prefer CLOCK_MONOTONIC_COARSE. It's a high-res clock that's available as + // of 2.6.32. It can be fully serviced from the vDSO, even on virtualized + // systems, which saves the overhead of a system call. The coarseness is not
+  // an issue for us because it still has sub-microsecond resolution.
+  static bool no_clock_monotonic_coarse;
+  if (no_clock_monotonic_coarse == false) {
+    if (clock_gettime(6 /* CLOCK_MONOTONIC_COARSE */, ts) == 0) {
+      return 0;
+    }
+    if (errno != EINVAL) {
+      return -1;
+    }
+    no_clock_monotonic_coarse = true;
+  }
+#endif  // V8_OS_LINUX
+  return clock_gettime(CLOCK_MONOTONIC, ts);
+}
+
+
+Time Time::Now() {
+  struct timespec ts;
+  int result = GetBestMonotonicClock(&ts);
+  ASSERT_EQ(0, result);
+  USE(result);
+  return FromTimespec(ts);
+}
+#endif  // V8_OS_MACOSX || V8_OS_SOLARIS


 Time Time::NowFromSystemTime() {
@@ -570,7 +603,7 @@ TimeTicks TimeTicks::HighResolutionNow() {
   ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec);
 #elif V8_OS_POSIX
   struct timespec ts;
-  int result = clock_gettime(CLOCK_MONOTONIC, &ts);
+  int result = GetBestMonotonicClock(&ts);
   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