Chromium Code Reviews| 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) { |
|
Sven Panne
2013/10/30 12:54:11
http://refactoring.com/catalog/removeDoubleNegativ
bnoordhuis
2013/10/30 13:38:56
Agree on the double negative but I wrote it this w
Sven Panne
2013/10/30 14:26:50
OK, I was a bit confused. What I meant was the fac
|
| + if (clock_gettime(6 /* CLOCK_MONOTONIC_COARSE */, ts) == 0) { |
|
Benedikt Meurer
2013/10/30 12:49:15
I don't like hardcoding the constant here. Better
|
| + 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() { |
|
Benedikt Meurer
2013/10/30 12:49:15
Time::Now() has to use gettimeofday() on POSIX sys
bnoordhuis
2013/10/30 13:38:56
You mean CLOCK_REALTIME_COARSE is not allowed? If
Benedikt Meurer
2013/10/30 13:50:04
CLOCK_MONOTONIC != CLOCK_REALTIME. Time values hav
bnoordhuis
2013/10/30 14:18:40
Trying to deepen my understanding here. Is that b
|
| + 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); |
|
Benedikt Meurer
2013/10/30 12:49:15
Please take a look at the Xorg GetTimeInMillis() f
bnoordhuis
2013/10/30 13:38:56
What in particular do you want to steal^Wcopy? Th
Benedikt Meurer
2013/10/30 13:50:04
static clockid_t clockid;
if (!clockid) {
#ifd
bnoordhuis
2013/10/30 14:18:40
I'm a man of many qualities but mind reading is no
|
| ASSERT_EQ(0, result); |
| USE(result); |
| ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + |