Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(655)

Unified Diff: src/platform/time.cc

Issue 97073004: Make Time functions go through the Platform layer. (Closed) Base URL: git://github.com/v8/v8.git@bleeding_edge
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/platform/default-platform-time.cc ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/platform/time.cc
diff --git a/src/platform/time.cc b/src/platform/time.cc
index de0ca16473f6b5106485508653cfe797f6632c37..667ec854745cb0cc8d65d51a30d22f5e4cd7116d 100644
--- a/src/platform/time.cc
+++ b/src/platform/time.cc
@@ -37,8 +37,8 @@
#include <cstring>
#include "checks.h"
-#include "cpu.h"
#include "platform.h"
+#include "v8.h"
#if V8_OS_WIN
#include "win32-headers.h"
#endif
@@ -160,76 +160,16 @@ struct timespec TimeDelta::ToTimespec() const {
#endif // V8_OS_POSIX
-
-#if V8_OS_WIN
-
-// We implement time using the high-resolution timers so that we can get
-// timeouts which are smaller than 10-15ms. To avoid any drift, we
-// periodically resync the internal clock to the system clock.
-class Clock V8_FINAL {
- public:
- Clock() : initial_ticks_(GetSystemTicks()), initial_time_(GetSystemTime()) {}
-
- Time Now() {
- // Time between resampling the un-granular clock for this API (1 minute).
- const TimeDelta kMaxElapsedTime = TimeDelta::FromMinutes(1);
-
- LockGuard<Mutex> lock_guard(&mutex_);
-
- // Determine current time and ticks.
- TimeTicks ticks = GetSystemTicks();
- Time time = GetSystemTime();
-
- // Check if we need to synchronize with the system clock due to a backwards
- // time change or the amount of time elapsed.
- TimeDelta elapsed = ticks - initial_ticks_;
- if (time < initial_time_ || elapsed > kMaxElapsedTime) {
- initial_ticks_ = ticks;
- initial_time_ = time;
- return time;
- }
-
- return initial_time_ + elapsed;
- }
-
- Time NowFromSystemTime() {
- LockGuard<Mutex> lock_guard(&mutex_);
- initial_ticks_ = GetSystemTicks();
- initial_time_ = GetSystemTime();
- return initial_time_;
- }
-
- private:
- static TimeTicks GetSystemTicks() {
- return TimeTicks::Now();
- }
-
- static Time GetSystemTime() {
- FILETIME ft;
- ::GetSystemTimeAsFileTime(&ft);
- return Time::FromFiletime(ft);
- }
-
- TimeTicks initial_ticks_;
- Time initial_time_;
- Mutex mutex_;
-};
-
-
-static LazyStaticInstance<Clock,
- DefaultConstructTrait<Clock>,
- ThreadSafeInitOnceTrait>::type clock = LAZY_STATIC_INSTANCE_INITIALIZER;
-
-
Time Time::Now() {
- return clock.Pointer()->Now();
+ return Time(V8::GetCurrentPlatform()->CurrentTime());
}
Time Time::NowFromSystemTime() {
- return clock.Pointer()->NowFromSystemTime();
+ return Time(V8::GetCurrentPlatform()->CurrentTimeFromSystemTime());
}
+#if V8_OS_WIN
// Time between windows epoch and standard epoch.
static const int64_t kTimeToEpochInMicroseconds = V8_INT64_C(11644473600000000);
@@ -270,20 +210,6 @@ FILETIME Time::ToFiletime() const {
#elif V8_OS_POSIX
-Time Time::Now() {
- struct timeval tv;
- int result = gettimeofday(&tv, NULL);
- ASSERT_EQ(0, result);
- USE(result);
- return FromTimeval(tv);
-}
-
-
-Time Time::NowFromSystemTime() {
- return Now();
-}
-
-
Time Time::FromTimespec(struct timespec ts) {
ASSERT(ts.tv_nsec >= 0);
ASSERT(ts.tv_nsec < static_cast<long>(kNanosecondsPerSecond)); // NOLINT
@@ -375,160 +301,18 @@ double Time::ToJsTime() const {
}
-#if V8_OS_WIN
-
-class TickClock {
- public:
- virtual ~TickClock() {}
- virtual int64_t Now() = 0;
- virtual bool IsHighResolution() = 0;
-};
-
-
-// Overview of time counters:
-// (1) CPU cycle counter. (Retrieved via RDTSC)
-// The CPU counter provides the highest resolution time stamp and is the least
-// expensive to retrieve. However, the CPU counter is unreliable and should not
-// be used in production. Its biggest issue is that it is per processor and it
-// is not synchronized between processors. Also, on some computers, the counters
-// will change frequency due to thermal and power changes, and stop in some
-// states.
-//
-// (2) QueryPerformanceCounter (QPC). The QPC counter provides a high-
-// resolution (100 nanoseconds) time stamp but is comparatively more expensive
-// to retrieve. What QueryPerformanceCounter actually does is up to the HAL.
-// (with some help from ACPI).
-// According to http://blogs.msdn.com/oldnewthing/archive/2005/09/02/459952.aspx
-// in the worst case, it gets the counter from the rollover interrupt on the
-// programmable interrupt timer. In best cases, the HAL may conclude that the
-// RDTSC counter runs at a constant frequency, then it uses that instead. On
-// multiprocessor machines, it will try to verify the values returned from
-// RDTSC on each processor are consistent with each other, and apply a handful
-// of workarounds for known buggy hardware. In other words, QPC is supposed to
-// give consistent result on a multiprocessor computer, but it is unreliable in
-// reality due to bugs in BIOS or HAL on some, especially old computers.
-// With recent updates on HAL and newer BIOS, QPC is getting more reliable but
-// it should be used with caution.
-//
-// (3) System time. The system time provides a low-resolution (typically 10ms
-// to 55 milliseconds) time stamp but is comparatively less expensive to
-// retrieve and more reliable.
-class HighResolutionTickClock V8_FINAL : public TickClock {
- public:
- explicit HighResolutionTickClock(int64_t ticks_per_second)
- : ticks_per_second_(ticks_per_second) {
- ASSERT_LT(0, ticks_per_second);
- }
- virtual ~HighResolutionTickClock() {}
-
- virtual int64_t Now() V8_OVERRIDE {
- LARGE_INTEGER now;
- BOOL result = QueryPerformanceCounter(&now);
- ASSERT(result);
- USE(result);
-
- // Intentionally calculate microseconds in a round about manner to avoid
- // overflow and precision issues. Think twice before simplifying!
- int64_t whole_seconds = now.QuadPart / ticks_per_second_;
- int64_t leftover_ticks = now.QuadPart % ticks_per_second_;
- int64_t ticks = (whole_seconds * Time::kMicrosecondsPerSecond) +
- ((leftover_ticks * Time::kMicrosecondsPerSecond) / ticks_per_second_);
-
- // Make sure we never return 0 here, so that TimeTicks::HighResolutionNow()
- // will never return 0.
- return ticks + 1;
- }
-
- virtual bool IsHighResolution() V8_OVERRIDE {
- return true;
- }
-
- private:
- int64_t ticks_per_second_;
-};
-
-
-class RolloverProtectedTickClock V8_FINAL : public TickClock {
- public:
- // We initialize rollover_ms_ to 1 to ensure that we will never
- // return 0 from TimeTicks::HighResolutionNow() and TimeTicks::Now() below.
- RolloverProtectedTickClock() : last_seen_now_(0), rollover_ms_(1) {}
- virtual ~RolloverProtectedTickClock() {}
-
- virtual int64_t Now() V8_OVERRIDE {
- LockGuard<Mutex> lock_guard(&mutex_);
- // We use timeGetTime() to implement TimeTicks::Now(), which rolls over
- // every ~49.7 days. We try to track rollover ourselves, which works if
- // TimeTicks::Now() is called at least every 49 days.
- // Note that we do not use GetTickCount() here, since timeGetTime() gives
- // more predictable delta values, as described here:
- // http://blogs.msdn.com/b/larryosterman/archive/2009/09/02/what-s-the-difference-between-gettickcount-and-timegettime.aspx
- // timeGetTime() provides 1ms granularity when combined with
- // timeBeginPeriod(). If the host application for V8 wants fast timers, it
- // can use timeBeginPeriod() to increase the resolution.
- DWORD now = timeGetTime();
- if (now < last_seen_now_) {
- rollover_ms_ += V8_INT64_C(0x100000000); // ~49.7 days.
- }
- last_seen_now_ = now;
- return (now + rollover_ms_) * Time::kMicrosecondsPerMillisecond;
- }
-
- virtual bool IsHighResolution() V8_OVERRIDE {
- return false;
- }
-
- private:
- Mutex mutex_;
- DWORD last_seen_now_;
- int64_t rollover_ms_;
-};
-
-
-static LazyStaticInstance<RolloverProtectedTickClock,
- DefaultConstructTrait<RolloverProtectedTickClock>,
- ThreadSafeInitOnceTrait>::type tick_clock =
- LAZY_STATIC_INSTANCE_INITIALIZER;
-
-
-struct CreateHighResTickClockTrait {
- static TickClock* Create() {
- // Check if the installed hardware supports a high-resolution performance
- // counter, and if not fallback to the low-resolution tick clock.
- LARGE_INTEGER ticks_per_second;
- if (!QueryPerformanceFrequency(&ticks_per_second)) {
- return tick_clock.Pointer();
- }
-
- // On Athlon X2 CPUs (e.g. model 15) the QueryPerformanceCounter
- // is unreliable, fallback to the low-resolution tick clock.
- CPU cpu;
- if (strcmp(cpu.vendor(), "AuthenticAMD") == 0 && cpu.family() == 15) {
- return tick_clock.Pointer();
- }
-
- return new HighResolutionTickClock(ticks_per_second.QuadPart);
- }
-};
-
-
-static LazyDynamicInstance<TickClock,
- CreateHighResTickClockTrait,
- ThreadSafeInitOnceTrait>::type high_res_tick_clock =
- LAZY_DYNAMIC_INSTANCE_INITIALIZER;
-
-
TimeTicks TimeTicks::Now() {
+ TimeTicks ticks = TimeTicks(i::V8::GetCurrentPlatform()->TimeTicksNow());
// Make sure we never return 0 here.
- TimeTicks ticks(tick_clock.Pointer()->Now());
ASSERT(!ticks.IsNull());
return ticks;
}
TimeTicks TimeTicks::HighResolutionNow() {
+ TimeTicks ticks = TimeTicks(
+ i::V8::GetCurrentPlatform()->TimeTicksHighResNow());
// Make sure we never return 0 here.
- TimeTicks ticks(high_res_tick_clock.Pointer()->Now());
ASSERT(!ticks.IsNull());
return ticks;
}
@@ -536,56 +320,7 @@ TimeTicks TimeTicks::HighResolutionNow() {
// static
bool TimeTicks::IsHighResolutionClockWorking() {
- return high_res_tick_clock.Pointer()->IsHighResolution();
+ return i::V8::GetCurrentPlatform()->TimeTicksHasHighRes();
}
-#else // V8_OS_WIN
-
-TimeTicks TimeTicks::Now() {
- return HighResolutionNow();
-}
-
-
-TimeTicks TimeTicks::HighResolutionNow() {
- int64_t ticks;
-#if V8_OS_MACOSX
- static struct mach_timebase_info info;
- if (info.denom == 0) {
- kern_return_t result = mach_timebase_info(&info);
- ASSERT_EQ(KERN_SUCCESS, result);
- USE(result);
- }
- ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond *
- info.numer / info.denom);
-#elif V8_OS_SOLARIS
- ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond);
-#elif 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);
- ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec);
-#elif V8_OS_POSIX
- struct timespec ts;
- int result = clock_gettime(CLOCK_MONOTONIC, &ts);
- ASSERT_EQ(0, result);
- USE(result);
- ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond +
- ts.tv_nsec / Time::kNanosecondsPerMicrosecond);
-#endif // V8_OS_MACOSX
- // Make sure we never return 0 here.
- return TimeTicks(ticks + 1);
-}
-
-
-// static
-bool TimeTicks::IsHighResolutionClockWorking() {
- return true;
-}
-
-#endif // V8_OS_WIN
-
} } // namespace v8::internal
« no previous file with comments | « src/platform/default-platform-time.cc ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698