Chromium Code Reviews| Index: src/base/platform/time.cc |
| diff --git a/src/base/platform/time.cc b/src/base/platform/time.cc |
| index b6a11cff34f313661536eb0d20b43cf7d598e479..025dbf7529578120371a7bc7e425b742784bcd16 100644 |
| --- a/src/base/platform/time.cc |
| +++ b/src/base/platform/time.cc |
| @@ -252,11 +252,44 @@ FILETIME Time::ToFiletime() const { |
| #elif V8_OS_POSIX |
| Time Time::Now() { |
| +#if V8_OS_LINUX |
|
Benedikt Meurer
2015/05/06 11:25:29
How about something like this instead:
#if V8_OS_
|
| + struct timespec ts; |
| + // clockid_t is an int32_t; loads and stores are atomic. |
| + static clockid_t clock_id = -1; |
| + if (clock_id == -1) { |
| + // CLOCK_REALTIME_COARSE is not supported on kernels <= 2.6.31. |
| + // Probe the kernel to see if it's available and has <= 1 ms resolution. |
| + // |
| + // CLOCK_REALTIME_COARSE, unlike CLOCK_REALTIME, can often be serviced |
| + // entirely from the vDSO without the need to make a system call. |
| + // It can have a dramatic impact on applications that frequently |
| + // query the current time. |
| + // |
| + // One caveat is that CLOCK_REALTIME_COARSE is tied to CONFIG_HZ, the |
| + // number of ticks per second that the kernel runs at. Its granularity |
| + // can be as low as one update every 300 ms so we need to make sure that |
| + // it is accurate enough. Fortunately, many if not most kernels are built |
| + // with CONFIG_HZ=1000, giving it a one millisecond precision and that is |
| + // good enough for our purposes. |
| + static const clockid_t kClockRealTimeCoarse = 5; |
| + if (-1 == clock_getres(kClockRealTimeCoarse, &ts) || |
| + ts.tv_nsec > 1000 * 1000 || ts.tv_sec > 0) { |
| + clock_id = CLOCK_REALTIME; // Not available or not suitable. |
| + } else { |
| + clock_id = kClockRealTimeCoarse; |
| + } |
| + } |
| + int result = clock_gettime(clock_id, &ts); |
| + DCHECK_EQ(0, result); |
| + USE(result); |
| + return FromTimespec(ts); |
| +#else |
| struct timeval tv; |
| int result = gettimeofday(&tv, NULL); |
| DCHECK_EQ(0, result); |
| USE(result); |
| return FromTimeval(tv); |
| +#endif |
| } |