michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: michael@0: // Windows Timer Primer michael@0: // michael@0: // A good article: http://www.ddj.com/windows/184416651 michael@0: // A good mozilla bug: http://bugzilla.mozilla.org/show_bug.cgi?id=363258 michael@0: // michael@0: // The default windows timer, GetSystemTimeAsFileTime is not very precise. michael@0: // It is only good to ~15.5ms. michael@0: // michael@0: // QueryPerformanceCounter is the logical choice for a high-precision timer. michael@0: // However, it is known to be buggy on some hardware. Specifically, it can michael@0: // sometimes "jump". On laptops, QPC can also be very expensive to call. michael@0: // It's 3-4x slower than timeGetTime() on desktops, but can be 10x slower michael@0: // on laptops. A unittest exists which will show the relative cost of various michael@0: // timers on any system. michael@0: // michael@0: // The next logical choice is timeGetTime(). timeGetTime has a precision of michael@0: // 1ms, but only if you call APIs (timeBeginPeriod()) which affect all other michael@0: // applications on the system. By default, precision is only 15.5ms. michael@0: // Unfortunately, we don't want to call timeBeginPeriod because we don't michael@0: // want to affect other applications. Further, on mobile platforms, use of michael@0: // faster multimedia timers can hurt battery life. See the intel michael@0: // article about this here: michael@0: // http://softwarecommunity.intel.com/articles/eng/1086.htm michael@0: // michael@0: // To work around all this, we're going to generally use timeGetTime(). We michael@0: // will only increase the system-wide timer if we're not running on battery michael@0: // power. Using timeBeginPeriod(1) is a requirement in order to make our michael@0: // message loop waits have the same resolution that our time measurements michael@0: // do. Otherwise, WaitForSingleObject(..., 1) will no less than 15ms when michael@0: // there is nothing else to waken the Wait. michael@0: michael@0: #include "base/time/time.h" michael@0: michael@0: #pragma comment(lib, "winmm.lib") michael@0: #include michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/cpu.h" michael@0: #include "base/logging.h" michael@0: #include "base/memory/singleton.h" michael@0: #include "base/synchronization/lock.h" michael@0: michael@0: using base::Time; michael@0: using base::TimeDelta; michael@0: using base::TimeTicks; michael@0: michael@0: namespace { michael@0: michael@0: // From MSDN, FILETIME "Contains a 64-bit value representing the number of michael@0: // 100-nanosecond intervals since January 1, 1601 (UTC)." michael@0: int64 FileTimeToMicroseconds(const FILETIME& ft) { michael@0: // Need to bit_cast to fix alignment, then divide by 10 to convert michael@0: // 100-nanoseconds to milliseconds. This only works on little-endian michael@0: // machines. michael@0: return bit_cast(ft) / 10; michael@0: } michael@0: michael@0: void MicrosecondsToFileTime(int64 us, FILETIME* ft) { michael@0: DCHECK_GE(us, 0LL) << "Time is less than 0, negative values are not " michael@0: "representable in FILETIME"; michael@0: michael@0: // Multiply by 10 to convert milliseconds to 100-nanoseconds. Bit_cast will michael@0: // handle alignment problems. This only works on little-endian machines. michael@0: *ft = bit_cast(us * 10); michael@0: } michael@0: michael@0: int64 CurrentWallclockMicroseconds() { michael@0: FILETIME ft; michael@0: ::GetSystemTimeAsFileTime(&ft); michael@0: return FileTimeToMicroseconds(ft); michael@0: } michael@0: michael@0: // Time between resampling the un-granular clock for this API. 60 seconds. michael@0: const int kMaxMillisecondsToAvoidDrift = 60 * Time::kMillisecondsPerSecond; michael@0: michael@0: int64 initial_time = 0; michael@0: TimeTicks initial_ticks; michael@0: michael@0: void InitializeClock() { michael@0: initial_ticks = TimeTicks::Now(); michael@0: initial_time = CurrentWallclockMicroseconds(); michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: // Time ----------------------------------------------------------------------- michael@0: michael@0: // The internal representation of Time uses FILETIME, whose epoch is 1601-01-01 michael@0: // 00:00:00 UTC. ((1970-1601)*365+89)*24*60*60*1000*1000, where 89 is the michael@0: // number of leap year days between 1601 and 1970: (1970-1601)/4 excluding michael@0: // 1700, 1800, and 1900. michael@0: // static michael@0: const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(11644473600000000); michael@0: michael@0: bool Time::high_resolution_timer_enabled_ = false; michael@0: int Time::high_resolution_timer_activated_ = 0; michael@0: michael@0: // static michael@0: Time Time::Now() { michael@0: if (initial_time == 0) michael@0: InitializeClock(); michael@0: michael@0: // We implement time using the high-resolution timers so that we can get michael@0: // timeouts which are smaller than 10-15ms. If we just used michael@0: // CurrentWallclockMicroseconds(), we'd have the less-granular timer. michael@0: // michael@0: // To make this work, we initialize the clock (initial_time) and the michael@0: // counter (initial_ctr). To compute the initial time, we can check michael@0: // the number of ticks that have elapsed, and compute the delta. michael@0: // michael@0: // To avoid any drift, we periodically resync the counters to the system michael@0: // clock. michael@0: while (true) { michael@0: TimeTicks ticks = TimeTicks::Now(); michael@0: michael@0: // Calculate the time elapsed since we started our timer michael@0: TimeDelta elapsed = ticks - initial_ticks; michael@0: michael@0: // Check if enough time has elapsed that we need to resync the clock. michael@0: if (elapsed.InMilliseconds() > kMaxMillisecondsToAvoidDrift) { michael@0: InitializeClock(); michael@0: continue; michael@0: } michael@0: michael@0: return Time(elapsed + Time(initial_time)); michael@0: } michael@0: } michael@0: michael@0: // static michael@0: Time Time::NowFromSystemTime() { michael@0: // Force resync. michael@0: InitializeClock(); michael@0: return Time(initial_time); michael@0: } michael@0: michael@0: // static michael@0: Time Time::FromFileTime(FILETIME ft) { michael@0: if (bit_cast(ft) == 0) michael@0: return Time(); michael@0: if (ft.dwHighDateTime == std::numeric_limits::max() && michael@0: ft.dwLowDateTime == std::numeric_limits::max()) michael@0: return Max(); michael@0: return Time(FileTimeToMicroseconds(ft)); michael@0: } michael@0: michael@0: FILETIME Time::ToFileTime() const { michael@0: if (is_null()) michael@0: return bit_cast(0); michael@0: if (is_max()) { michael@0: FILETIME result; michael@0: result.dwHighDateTime = std::numeric_limits::max(); michael@0: result.dwLowDateTime = std::numeric_limits::max(); michael@0: return result; michael@0: } michael@0: FILETIME utc_ft; michael@0: MicrosecondsToFileTime(us_, &utc_ft); michael@0: return utc_ft; michael@0: } michael@0: michael@0: // static michael@0: void Time::EnableHighResolutionTimer(bool enable) { michael@0: // Test for single-threaded access. michael@0: static PlatformThreadId my_thread = PlatformThread::CurrentId(); michael@0: DCHECK(PlatformThread::CurrentId() == my_thread); michael@0: michael@0: if (high_resolution_timer_enabled_ == enable) michael@0: return; michael@0: michael@0: high_resolution_timer_enabled_ = enable; michael@0: } michael@0: michael@0: // static michael@0: bool Time::ActivateHighResolutionTimer(bool activating) { michael@0: if (!high_resolution_timer_enabled_ && activating) michael@0: return false; michael@0: michael@0: // Using anything other than 1ms makes timers granular michael@0: // to that interval. michael@0: const int kMinTimerIntervalMs = 1; michael@0: MMRESULT result; michael@0: if (activating) { michael@0: result = timeBeginPeriod(kMinTimerIntervalMs); michael@0: high_resolution_timer_activated_++; michael@0: } else { michael@0: result = timeEndPeriod(kMinTimerIntervalMs); michael@0: high_resolution_timer_activated_--; michael@0: } michael@0: return result == TIMERR_NOERROR; michael@0: } michael@0: michael@0: // static michael@0: bool Time::IsHighResolutionTimerInUse() { michael@0: // Note: we should track the high_resolution_timer_activated_ value michael@0: // under a lock if we want it to be accurate in a system with multiple michael@0: // message loops. We don't do that - because we don't want to take the michael@0: // expense of a lock for this. We *only* track this value so that unit michael@0: // tests can see if the high resolution timer is on or off. michael@0: return high_resolution_timer_enabled_ && michael@0: high_resolution_timer_activated_ > 0; michael@0: } michael@0: michael@0: // static michael@0: Time Time::FromExploded(bool is_local, const Exploded& exploded) { michael@0: // Create the system struct representing our exploded time. It will either be michael@0: // in local time or UTC. michael@0: SYSTEMTIME st; michael@0: st.wYear = exploded.year; michael@0: st.wMonth = exploded.month; michael@0: st.wDayOfWeek = exploded.day_of_week; michael@0: st.wDay = exploded.day_of_month; michael@0: st.wHour = exploded.hour; michael@0: st.wMinute = exploded.minute; michael@0: st.wSecond = exploded.second; michael@0: st.wMilliseconds = exploded.millisecond; michael@0: michael@0: FILETIME ft; michael@0: bool success = true; michael@0: // Ensure that it's in UTC. michael@0: if (is_local) { michael@0: SYSTEMTIME utc_st; michael@0: success = TzSpecificLocalTimeToSystemTime(NULL, &st, &utc_st) && michael@0: SystemTimeToFileTime(&utc_st, &ft); michael@0: } else { michael@0: success = !!SystemTimeToFileTime(&st, &ft); michael@0: } michael@0: michael@0: if (!success) { michael@0: NOTREACHED() << "Unable to convert time"; michael@0: return Time(0); michael@0: } michael@0: return Time(FileTimeToMicroseconds(ft)); michael@0: } michael@0: michael@0: void Time::Explode(bool is_local, Exploded* exploded) const { michael@0: if (us_ < 0LL) { michael@0: // We are not able to convert it to FILETIME. michael@0: ZeroMemory(exploded, sizeof(*exploded)); michael@0: return; michael@0: } michael@0: michael@0: // FILETIME in UTC. michael@0: FILETIME utc_ft; michael@0: MicrosecondsToFileTime(us_, &utc_ft); michael@0: michael@0: // FILETIME in local time if necessary. michael@0: bool success = true; michael@0: // FILETIME in SYSTEMTIME (exploded). michael@0: SYSTEMTIME st; michael@0: if (is_local) { michael@0: SYSTEMTIME utc_st; michael@0: // We don't use FileTimeToLocalFileTime here, since it uses the current michael@0: // settings for the time zone and daylight saving time. Therefore, if it is michael@0: // daylight saving time, it will take daylight saving time into account, michael@0: // even if the time you are converting is in standard time. michael@0: success = FileTimeToSystemTime(&utc_ft, &utc_st) && michael@0: SystemTimeToTzSpecificLocalTime(NULL, &utc_st, &st); michael@0: } else { michael@0: success = !!FileTimeToSystemTime(&utc_ft, &st); michael@0: } michael@0: michael@0: if (!success) { michael@0: NOTREACHED() << "Unable to convert time, don't know why"; michael@0: ZeroMemory(exploded, sizeof(*exploded)); michael@0: return; michael@0: } michael@0: michael@0: exploded->year = st.wYear; michael@0: exploded->month = st.wMonth; michael@0: exploded->day_of_week = st.wDayOfWeek; michael@0: exploded->day_of_month = st.wDay; michael@0: exploded->hour = st.wHour; michael@0: exploded->minute = st.wMinute; michael@0: exploded->second = st.wSecond; michael@0: exploded->millisecond = st.wMilliseconds; michael@0: } michael@0: michael@0: // TimeTicks ------------------------------------------------------------------ michael@0: namespace { michael@0: michael@0: // We define a wrapper to adapt between the __stdcall and __cdecl call of the michael@0: // mock function, and to avoid a static constructor. Assigning an import to a michael@0: // function pointer directly would require setup code to fetch from the IAT. michael@0: DWORD timeGetTimeWrapper() { michael@0: return timeGetTime(); michael@0: } michael@0: michael@0: DWORD (*tick_function)(void) = &timeGetTimeWrapper; michael@0: michael@0: // Accumulation of time lost due to rollover (in milliseconds). michael@0: int64 rollover_ms = 0; michael@0: michael@0: // The last timeGetTime value we saw, to detect rollover. michael@0: DWORD last_seen_now = 0; michael@0: michael@0: // Lock protecting rollover_ms and last_seen_now. michael@0: // Note: this is a global object, and we usually avoid these. However, the time michael@0: // code is low-level, and we don't want to use Singletons here (it would be too michael@0: // easy to use a Singleton without even knowing it, and that may lead to many michael@0: // gotchas). Its impact on startup time should be negligible due to low-level michael@0: // nature of time code. michael@0: base::Lock rollover_lock; michael@0: michael@0: // We use timeGetTime() to implement TimeTicks::Now(). This can be problematic michael@0: // because it returns the number of milliseconds since Windows has started, michael@0: // which will roll over the 32-bit value every ~49 days. We try to track michael@0: // rollover ourselves, which works if TimeTicks::Now() is called at least every michael@0: // 49 days. michael@0: TimeDelta RolloverProtectedNow() { michael@0: base::AutoLock locked(rollover_lock); michael@0: // We should hold the lock while calling tick_function to make sure that michael@0: // we keep last_seen_now stay correctly in sync. michael@0: DWORD now = tick_function(); michael@0: if (now < last_seen_now) michael@0: rollover_ms += 0x100000000I64; // ~49.7 days. michael@0: last_seen_now = now; michael@0: return TimeDelta::FromMilliseconds(now + rollover_ms); michael@0: } michael@0: michael@0: bool IsBuggyAthlon(const base::CPU& cpu) { michael@0: // On Athlon X2 CPUs (e.g. model 15) QueryPerformanceCounter is michael@0: // unreliable. Fallback to low-res clock. michael@0: return cpu.vendor_name() == "AuthenticAMD" && cpu.family() == 15; michael@0: } michael@0: michael@0: // Overview of time counters: michael@0: // (1) CPU cycle counter. (Retrieved via RDTSC) michael@0: // The CPU counter provides the highest resolution time stamp and is the least michael@0: // expensive to retrieve. However, the CPU counter is unreliable and should not michael@0: // be used in production. Its biggest issue is that it is per processor and it michael@0: // is not synchronized between processors. Also, on some computers, the counters michael@0: // will change frequency due to thermal and power changes, and stop in some michael@0: // states. michael@0: // michael@0: // (2) QueryPerformanceCounter (QPC). The QPC counter provides a high- michael@0: // resolution (100 nanoseconds) time stamp but is comparatively more expensive michael@0: // to retrieve. What QueryPerformanceCounter actually does is up to the HAL. michael@0: // (with some help from ACPI). michael@0: // According to http://blogs.msdn.com/oldnewthing/archive/2005/09/02/459952.aspx michael@0: // in the worst case, it gets the counter from the rollover interrupt on the michael@0: // programmable interrupt timer. In best cases, the HAL may conclude that the michael@0: // RDTSC counter runs at a constant frequency, then it uses that instead. On michael@0: // multiprocessor machines, it will try to verify the values returned from michael@0: // RDTSC on each processor are consistent with each other, and apply a handful michael@0: // of workarounds for known buggy hardware. In other words, QPC is supposed to michael@0: // give consistent result on a multiprocessor computer, but it is unreliable in michael@0: // reality due to bugs in BIOS or HAL on some, especially old computers. michael@0: // With recent updates on HAL and newer BIOS, QPC is getting more reliable but michael@0: // it should be used with caution. michael@0: // michael@0: // (3) System time. The system time provides a low-resolution (typically 10ms michael@0: // to 55 milliseconds) time stamp but is comparatively less expensive to michael@0: // retrieve and more reliable. michael@0: class HighResNowSingleton { michael@0: public: michael@0: static HighResNowSingleton* GetInstance() { michael@0: return Singleton::get(); michael@0: } michael@0: michael@0: bool IsUsingHighResClock() { michael@0: return ticks_per_second_ != 0.0; michael@0: } michael@0: michael@0: void DisableHighResClock() { michael@0: ticks_per_second_ = 0.0; michael@0: } michael@0: michael@0: TimeDelta Now() { michael@0: if (IsUsingHighResClock()) michael@0: return TimeDelta::FromMicroseconds(UnreliableNow()); michael@0: michael@0: // Just fallback to the slower clock. michael@0: return RolloverProtectedNow(); michael@0: } michael@0: michael@0: int64 GetQPCDriftMicroseconds() { michael@0: if (!IsUsingHighResClock()) michael@0: return 0; michael@0: return abs((UnreliableNow() - ReliableNow()) - skew_); michael@0: } michael@0: michael@0: int64 QPCValueToMicroseconds(LONGLONG qpc_value) { michael@0: if (!ticks_per_second_) michael@0: return 0; michael@0: michael@0: // Intentionally calculate microseconds in a round about manner to avoid michael@0: // overflow and precision issues. Think twice before simplifying! michael@0: int64 whole_seconds = qpc_value / ticks_per_second_; michael@0: int64 leftover_ticks = qpc_value % ticks_per_second_; michael@0: int64 microseconds = (whole_seconds * Time::kMicrosecondsPerSecond) + michael@0: ((leftover_ticks * Time::kMicrosecondsPerSecond) / michael@0: ticks_per_second_); michael@0: return microseconds; michael@0: } michael@0: michael@0: private: michael@0: HighResNowSingleton() michael@0: : ticks_per_second_(0), michael@0: skew_(0) { michael@0: InitializeClock(); michael@0: michael@0: base::CPU cpu; michael@0: if (IsBuggyAthlon(cpu)) michael@0: DisableHighResClock(); michael@0: } michael@0: michael@0: // Synchronize the QPC clock with GetSystemTimeAsFileTime. michael@0: void InitializeClock() { michael@0: LARGE_INTEGER ticks_per_sec = {0}; michael@0: if (!QueryPerformanceFrequency(&ticks_per_sec)) michael@0: return; // Broken, we don't guarantee this function works. michael@0: ticks_per_second_ = ticks_per_sec.QuadPart; michael@0: michael@0: skew_ = UnreliableNow() - ReliableNow(); michael@0: } michael@0: michael@0: // Get the number of microseconds since boot in an unreliable fashion. michael@0: int64 UnreliableNow() { michael@0: LARGE_INTEGER now; michael@0: QueryPerformanceCounter(&now); michael@0: return QPCValueToMicroseconds(now.QuadPart); michael@0: } michael@0: michael@0: // Get the number of microseconds since boot in a reliable fashion. michael@0: int64 ReliableNow() { michael@0: return RolloverProtectedNow().InMicroseconds(); michael@0: } michael@0: michael@0: int64 ticks_per_second_; // 0 indicates QPF failed and we're broken. michael@0: int64 skew_; // Skew between lo-res and hi-res clocks (for debugging). michael@0: michael@0: friend struct DefaultSingletonTraits; michael@0: }; michael@0: michael@0: TimeDelta HighResNowWrapper() { michael@0: return HighResNowSingleton::GetInstance()->Now(); michael@0: } michael@0: michael@0: typedef TimeDelta (*NowFunction)(void); michael@0: NowFunction now_function = RolloverProtectedNow; michael@0: michael@0: bool CPUReliablySupportsHighResTime() { michael@0: base::CPU cpu; michael@0: if (!cpu.has_non_stop_time_stamp_counter()) michael@0: return false; michael@0: michael@0: if (IsBuggyAthlon(cpu)) michael@0: return false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: // static michael@0: TimeTicks::TickFunctionType TimeTicks::SetMockTickFunction( michael@0: TickFunctionType ticker) { michael@0: base::AutoLock locked(rollover_lock); michael@0: TickFunctionType old = tick_function; michael@0: tick_function = ticker; michael@0: rollover_ms = 0; michael@0: last_seen_now = 0; michael@0: return old; michael@0: } michael@0: michael@0: // static michael@0: bool TimeTicks::SetNowIsHighResNowIfSupported() { michael@0: if (!CPUReliablySupportsHighResTime()) { michael@0: return false; michael@0: } michael@0: michael@0: now_function = HighResNowWrapper; michael@0: return true; michael@0: } michael@0: michael@0: // static michael@0: TimeTicks TimeTicks::Now() { michael@0: return TimeTicks() + now_function(); michael@0: } michael@0: michael@0: // static michael@0: TimeTicks TimeTicks::HighResNow() { michael@0: return TimeTicks() + HighResNowSingleton::GetInstance()->Now(); michael@0: } michael@0: michael@0: // static michael@0: TimeTicks TimeTicks::ThreadNow() { michael@0: NOTREACHED(); michael@0: return TimeTicks(); michael@0: } michael@0: michael@0: // static michael@0: TimeTicks TimeTicks::NowFromSystemTraceTime() { michael@0: return HighResNow(); michael@0: } michael@0: michael@0: // static michael@0: int64 TimeTicks::GetQPCDriftMicroseconds() { michael@0: return HighResNowSingleton::GetInstance()->GetQPCDriftMicroseconds(); michael@0: } michael@0: michael@0: // static michael@0: TimeTicks TimeTicks::FromQPCValue(LONGLONG qpc_value) { michael@0: return TimeTicks( michael@0: HighResNowSingleton::GetInstance()->QPCValueToMicroseconds(qpc_value)); michael@0: } michael@0: michael@0: // static michael@0: bool TimeTicks::IsHighResClockWorking() { michael@0: return HighResNowSingleton::GetInstance()->IsUsingHighResClock(); michael@0: } michael@0: michael@0: TimeTicks TimeTicks::UnprotectedNow() { michael@0: if (now_function == HighResNowWrapper) { michael@0: return Now(); michael@0: } else { michael@0: return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime()); michael@0: } michael@0: } michael@0: michael@0: // TimeDelta ------------------------------------------------------------------ michael@0: michael@0: // static michael@0: TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { michael@0: return TimeDelta( michael@0: HighResNowSingleton::GetInstance()->QPCValueToMicroseconds(qpc_value)); michael@0: }