1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 // Windows Timer Primer
8 // A good article: http://www.ddj.com/windows/184416651
9 // A good mozilla bug: http://bugzilla.mozilla.org/show_bug.cgi?id=363258
11 // The default windows timer, GetSystemTimeAsFileTime is not very precise.
12 // It is only good to ~15.5ms.
14 // QueryPerformanceCounter is the logical choice for a high-precision timer.
15 // However, it is known to be buggy on some hardware. Specifically, it can
16 // sometimes "jump". On laptops, QPC can also be very expensive to call.
17 // It's 3-4x slower than timeGetTime() on desktops, but can be 10x slower
18 // on laptops. A unittest exists which will show the relative cost of various
19 // timers on any system.
21 // The next logical choice is timeGetTime(). timeGetTime has a precision of
22 // 1ms, but only if you call APIs (timeBeginPeriod()) which affect all other
23 // applications on the system. By default, precision is only 15.5ms.
24 // Unfortunately, we don't want to call timeBeginPeriod because we don't
25 // want to affect other applications. Further, on mobile platforms, use of
26 // faster multimedia timers can hurt battery life. See the intel
27 // article about this here:
28 // http://softwarecommunity.intel.com/articles/eng/1086.htm
30 // To work around all this, we're going to generally use timeGetTime(). We
31 // will only increase the system-wide timer if we're not running on battery
34 #include "base/time/time.h"
36 #pragma comment(lib, "winmm.lib")
41 #include "base/basictypes.h"
43 #include "base/lazy_instance.h"
44 #include "base/logging.h"
45 #include "base/synchronization/lock.h"
47 using base::ThreadTicks
;
49 using base::TimeDelta
;
50 using base::TimeTicks
;
51 using base::TraceTicks
;
55 // From MSDN, FILETIME "Contains a 64-bit value representing the number of
56 // 100-nanosecond intervals since January 1, 1601 (UTC)."
57 int64
FileTimeToMicroseconds(const FILETIME
& ft
) {
58 // Need to bit_cast to fix alignment, then divide by 10 to convert
59 // 100-nanoseconds to milliseconds. This only works on little-endian
61 return bit_cast
<int64
, FILETIME
>(ft
) / 10;
64 void MicrosecondsToFileTime(int64 us
, FILETIME
* ft
) {
65 DCHECK_GE(us
, 0LL) << "Time is less than 0, negative values are not "
66 "representable in FILETIME";
68 // Multiply by 10 to convert milliseconds to 100-nanoseconds. Bit_cast will
69 // handle alignment problems. This only works on little-endian machines.
70 *ft
= bit_cast
<FILETIME
, int64
>(us
* 10);
73 int64
CurrentWallclockMicroseconds() {
75 ::GetSystemTimeAsFileTime(&ft
);
76 return FileTimeToMicroseconds(ft
);
79 // Time between resampling the un-granular clock for this API. 60 seconds.
80 const int kMaxMillisecondsToAvoidDrift
= 60 * Time::kMillisecondsPerSecond
;
82 int64 initial_time
= 0;
83 TimeTicks initial_ticks
;
85 void InitializeClock() {
86 initial_ticks
= TimeTicks::Now();
87 initial_time
= CurrentWallclockMicroseconds();
90 // The two values that ActivateHighResolutionTimer uses to set the systemwide
91 // timer interrupt frequency on Windows. It controls how precise timers are
92 // but also has a big impact on battery life.
93 const int kMinTimerIntervalHighResMs
= 1;
94 const int kMinTimerIntervalLowResMs
= 4;
95 // Track if kMinTimerIntervalHighResMs or kMinTimerIntervalLowResMs is active.
96 bool g_high_res_timer_enabled
= false;
97 // How many times the high resolution timer has been called.
98 uint32_t g_high_res_timer_count
= 0;
99 // The lock to control access to the above two variables.
100 base::LazyInstance
<base::Lock
>::Leaky g_high_res_lock
=
101 LAZY_INSTANCE_INITIALIZER
;
105 // Time -----------------------------------------------------------------------
107 // The internal representation of Time uses FILETIME, whose epoch is 1601-01-01
108 // 00:00:00 UTC. ((1970-1601)*365+89)*24*60*60*1000*1000, where 89 is the
109 // number of leap year days between 1601 and 1970: (1970-1601)/4 excluding
110 // 1700, 1800, and 1900.
112 const int64
Time::kTimeTToMicrosecondsOffset
= INT64_C(11644473600000000);
116 if (initial_time
== 0)
119 // We implement time using the high-resolution timers so that we can get
120 // timeouts which are smaller than 10-15ms. If we just used
121 // CurrentWallclockMicroseconds(), we'd have the less-granular timer.
123 // To make this work, we initialize the clock (initial_time) and the
124 // counter (initial_ctr). To compute the initial time, we can check
125 // the number of ticks that have elapsed, and compute the delta.
127 // To avoid any drift, we periodically resync the counters to the system
130 TimeTicks ticks
= TimeTicks::Now();
132 // Calculate the time elapsed since we started our timer
133 TimeDelta elapsed
= ticks
- initial_ticks
;
135 // Check if enough time has elapsed that we need to resync the clock.
136 if (elapsed
.InMilliseconds() > kMaxMillisecondsToAvoidDrift
) {
141 return Time(elapsed
+ Time(initial_time
));
146 Time
Time::NowFromSystemTime() {
149 return Time(initial_time
);
153 Time
Time::FromFileTime(FILETIME ft
) {
154 if (bit_cast
<int64
, FILETIME
>(ft
) == 0)
156 if (ft
.dwHighDateTime
== std::numeric_limits
<DWORD
>::max() &&
157 ft
.dwLowDateTime
== std::numeric_limits
<DWORD
>::max())
159 return Time(FileTimeToMicroseconds(ft
));
162 FILETIME
Time::ToFileTime() const {
164 return bit_cast
<FILETIME
, int64
>(0);
167 result
.dwHighDateTime
= std::numeric_limits
<DWORD
>::max();
168 result
.dwLowDateTime
= std::numeric_limits
<DWORD
>::max();
172 MicrosecondsToFileTime(us_
, &utc_ft
);
177 void Time::EnableHighResolutionTimer(bool enable
) {
178 base::AutoLock
lock(g_high_res_lock
.Get());
179 if (g_high_res_timer_enabled
== enable
)
181 g_high_res_timer_enabled
= enable
;
182 if (!g_high_res_timer_count
)
184 // Since g_high_res_timer_count != 0, an ActivateHighResolutionTimer(true)
185 // was called which called timeBeginPeriod with g_high_res_timer_enabled
186 // with a value which is the opposite of |enable|. With that information we
187 // call timeEndPeriod with the same value used in timeBeginPeriod and
188 // therefore undo the period effect.
190 timeEndPeriod(kMinTimerIntervalLowResMs
);
191 timeBeginPeriod(kMinTimerIntervalHighResMs
);
193 timeEndPeriod(kMinTimerIntervalHighResMs
);
194 timeBeginPeriod(kMinTimerIntervalLowResMs
);
199 bool Time::ActivateHighResolutionTimer(bool activating
) {
200 // We only do work on the transition from zero to one or one to zero so we
201 // can easily undo the effect (if necessary) when EnableHighResolutionTimer is
203 const uint32_t max
= std::numeric_limits
<uint32_t>::max();
205 base::AutoLock
lock(g_high_res_lock
.Get());
206 UINT period
= g_high_res_timer_enabled
? kMinTimerIntervalHighResMs
207 : kMinTimerIntervalLowResMs
;
209 DCHECK_NE(g_high_res_timer_count
, max
);
210 ++g_high_res_timer_count
;
211 if (g_high_res_timer_count
== 1)
212 timeBeginPeriod(period
);
214 DCHECK_NE(g_high_res_timer_count
, 0u);
215 --g_high_res_timer_count
;
216 if (g_high_res_timer_count
== 0)
217 timeEndPeriod(period
);
219 return (period
== kMinTimerIntervalHighResMs
);
223 bool Time::IsHighResolutionTimerInUse() {
224 base::AutoLock
lock(g_high_res_lock
.Get());
225 return g_high_res_timer_enabled
&& g_high_res_timer_count
> 0;
229 Time
Time::FromExploded(bool is_local
, const Exploded
& exploded
) {
230 // Create the system struct representing our exploded time. It will either be
231 // in local time or UTC.
233 st
.wYear
= static_cast<WORD
>(exploded
.year
);
234 st
.wMonth
= static_cast<WORD
>(exploded
.month
);
235 st
.wDayOfWeek
= static_cast<WORD
>(exploded
.day_of_week
);
236 st
.wDay
= static_cast<WORD
>(exploded
.day_of_month
);
237 st
.wHour
= static_cast<WORD
>(exploded
.hour
);
238 st
.wMinute
= static_cast<WORD
>(exploded
.minute
);
239 st
.wSecond
= static_cast<WORD
>(exploded
.second
);
240 st
.wMilliseconds
= static_cast<WORD
>(exploded
.millisecond
);
244 // Ensure that it's in UTC.
247 success
= TzSpecificLocalTimeToSystemTime(NULL
, &st
, &utc_st
) &&
248 SystemTimeToFileTime(&utc_st
, &ft
);
250 success
= !!SystemTimeToFileTime(&st
, &ft
);
254 NOTREACHED() << "Unable to convert time";
257 return Time(FileTimeToMicroseconds(ft
));
260 void Time::Explode(bool is_local
, Exploded
* exploded
) const {
262 // We are not able to convert it to FILETIME.
263 ZeroMemory(exploded
, sizeof(*exploded
));
269 MicrosecondsToFileTime(us_
, &utc_ft
);
271 // FILETIME in local time if necessary.
273 // FILETIME in SYSTEMTIME (exploded).
277 // We don't use FileTimeToLocalFileTime here, since it uses the current
278 // settings for the time zone and daylight saving time. Therefore, if it is
279 // daylight saving time, it will take daylight saving time into account,
280 // even if the time you are converting is in standard time.
281 success
= FileTimeToSystemTime(&utc_ft
, &utc_st
) &&
282 SystemTimeToTzSpecificLocalTime(NULL
, &utc_st
, &st
);
284 success
= !!FileTimeToSystemTime(&utc_ft
, &st
);
288 NOTREACHED() << "Unable to convert time, don't know why";
289 ZeroMemory(exploded
, sizeof(*exploded
));
293 exploded
->year
= st
.wYear
;
294 exploded
->month
= st
.wMonth
;
295 exploded
->day_of_week
= st
.wDayOfWeek
;
296 exploded
->day_of_month
= st
.wDay
;
297 exploded
->hour
= st
.wHour
;
298 exploded
->minute
= st
.wMinute
;
299 exploded
->second
= st
.wSecond
;
300 exploded
->millisecond
= st
.wMilliseconds
;
303 // TimeTicks ------------------------------------------------------------------
306 // We define a wrapper to adapt between the __stdcall and __cdecl call of the
307 // mock function, and to avoid a static constructor. Assigning an import to a
308 // function pointer directly would require setup code to fetch from the IAT.
309 DWORD
timeGetTimeWrapper() {
310 return timeGetTime();
313 DWORD (*g_tick_function
)(void) = &timeGetTimeWrapper
;
315 // Accumulation of time lost due to rollover (in milliseconds).
316 int64 g_rollover_ms
= 0;
318 // The last timeGetTime value we saw, to detect rollover.
319 DWORD g_last_seen_now
= 0;
321 // Lock protecting rollover_ms and last_seen_now.
322 // Note: this is a global object, and we usually avoid these. However, the time
323 // code is low-level, and we don't want to use Singletons here (it would be too
324 // easy to use a Singleton without even knowing it, and that may lead to many
325 // gotchas). Its impact on startup time should be negligible due to low-level
326 // nature of time code.
327 base::Lock g_rollover_lock
;
329 // We use timeGetTime() to implement TimeTicks::Now(). This can be problematic
330 // because it returns the number of milliseconds since Windows has started,
331 // which will roll over the 32-bit value every ~49 days. We try to track
332 // rollover ourselves, which works if TimeTicks::Now() is called at least every
334 TimeDelta
RolloverProtectedNow() {
335 base::AutoLock
locked(g_rollover_lock
);
336 // We should hold the lock while calling tick_function to make sure that
337 // we keep last_seen_now stay correctly in sync.
338 DWORD now
= g_tick_function();
339 if (now
< g_last_seen_now
)
340 g_rollover_ms
+= 0x100000000I
64; // ~49.7 days.
341 g_last_seen_now
= now
;
342 return TimeDelta::FromMilliseconds(now
+ g_rollover_ms
);
345 // Discussion of tick counter options on Windows:
347 // (1) CPU cycle counter. (Retrieved via RDTSC)
348 // The CPU counter provides the highest resolution time stamp and is the least
349 // expensive to retrieve. However, on older CPUs, two issues can affect its
350 // reliability: First it is maintained per processor and not synchronized
351 // between processors. Also, the counters will change frequency due to thermal
352 // and power changes, and stop in some states.
354 // (2) QueryPerformanceCounter (QPC). The QPC counter provides a high-
355 // resolution (<1 microsecond) time stamp. On most hardware running today, it
356 // auto-detects and uses the constant-rate RDTSC counter to provide extremely
357 // efficient and reliable time stamps.
359 // On older CPUs where RDTSC is unreliable, it falls back to using more
360 // expensive (20X to 40X more costly) alternate clocks, such as HPET or the ACPI
361 // PM timer, and can involve system calls; and all this is up to the HAL (with
362 // some help from ACPI). According to
363 // http://blogs.msdn.com/oldnewthing/archive/2005/09/02/459952.aspx, in the
364 // worst case, it gets the counter from the rollover interrupt on the
365 // programmable interrupt timer. In best cases, the HAL may conclude that the
366 // RDTSC counter runs at a constant frequency, then it uses that instead. On
367 // multiprocessor machines, it will try to verify the values returned from
368 // RDTSC on each processor are consistent with each other, and apply a handful
369 // of workarounds for known buggy hardware. In other words, QPC is supposed to
370 // give consistent results on a multiprocessor computer, but for older CPUs it
371 // can be unreliable due bugs in BIOS or HAL.
373 // (3) System time. The system time provides a low-resolution (from ~1 to ~15.6
374 // milliseconds) time stamp but is comparatively less expensive to retrieve and
375 // more reliable. Time::EnableHighResolutionTimer() and
376 // Time::ActivateHighResolutionTimer() can be called to alter the resolution of
377 // this timer; and also other Windows applications can alter it, affecting this
380 using NowFunction
= TimeDelta (*)(void);
382 TimeDelta
InitialNowFunction();
384 // See "threading notes" in InitializeNowFunctionPointer() for details on how
385 // concurrent reads/writes to these globals has been made safe.
386 NowFunction g_now_function
= &InitialNowFunction
;
387 int64 g_qpc_ticks_per_second
= 0;
389 // As of January 2015, use of <atomic> is forbidden in Chromium code. This is
390 // what std::atomic_thread_fence does on Windows on all Intel architectures when
391 // the memory_order argument is anything but std::memory_order_seq_cst:
392 #define ATOMIC_THREAD_FENCE(memory_order) _ReadWriteBarrier();
394 TimeDelta
QPCValueToTimeDelta(LONGLONG qpc_value
) {
395 // Ensure that the assignment to |g_qpc_ticks_per_second|, made in
396 // InitializeNowFunctionPointer(), has happened by this point.
397 ATOMIC_THREAD_FENCE(memory_order_acquire
);
399 DCHECK_GT(g_qpc_ticks_per_second
, 0);
401 // If the QPC Value is below the overflow threshold, we proceed with
402 // simple multiply and divide.
403 if (qpc_value
< Time::kQPCOverflowThreshold
) {
404 return TimeDelta::FromMicroseconds(
405 qpc_value
* Time::kMicrosecondsPerSecond
/ g_qpc_ticks_per_second
);
407 // Otherwise, calculate microseconds in a round about manner to avoid
408 // overflow and precision issues.
409 int64 whole_seconds
= qpc_value
/ g_qpc_ticks_per_second
;
410 int64 leftover_ticks
= qpc_value
- (whole_seconds
* g_qpc_ticks_per_second
);
411 return TimeDelta::FromMicroseconds(
412 (whole_seconds
* Time::kMicrosecondsPerSecond
) +
413 ((leftover_ticks
* Time::kMicrosecondsPerSecond
) /
414 g_qpc_ticks_per_second
));
419 QueryPerformanceCounter(&now
);
420 return QPCValueToTimeDelta(now
.QuadPart
);
423 bool IsBuggyAthlon(const base::CPU
& cpu
) {
424 // On Athlon X2 CPUs (e.g. model 15) QueryPerformanceCounter is unreliable.
425 return cpu
.vendor_name() == "AuthenticAMD" && cpu
.family() == 15;
428 void InitializeNowFunctionPointer() {
429 LARGE_INTEGER ticks_per_sec
= {};
430 if (!QueryPerformanceFrequency(&ticks_per_sec
))
431 ticks_per_sec
.QuadPart
= 0;
433 // If Windows cannot provide a QPC implementation, TimeTicks::Now() must use
434 // the low-resolution clock.
436 // If the QPC implementation is expensive and/or unreliable, TimeTicks::Now()
437 // will still use the low-resolution clock. A CPU lacking a non-stop time
438 // counter will cause Windows to provide an alternate QPC implementation that
439 // works, but is expensive to use. Certain Athlon CPUs are known to make the
440 // QPC implementation unreliable.
442 // Otherwise, Now uses the high-resolution QPC clock. As of 21 August 2015,
443 // ~72% of users fall within this category.
445 // TraceTicks::Now() always uses the same clock as TimeTicks::Now(), even
446 // when the QPC exists but is expensive or unreliable. This is because we'd
447 // eventually like to merge TraceTicks and TimeTicks and have one type of
448 // timestamp that is reliable, monotonic, and comparable. Also, while we could
449 // use the high-resolution timer for TraceTicks even when it's unreliable or
450 // slow, it's easier to make tracing tools accommodate a coarse timer than
451 // one that's unreliable or slow.
452 NowFunction now_function
;
454 if (ticks_per_sec
.QuadPart
<= 0 ||
455 !cpu
.has_non_stop_time_stamp_counter() || IsBuggyAthlon(cpu
)) {
456 now_function
= &RolloverProtectedNow
;
458 now_function
= &QPCNow
;
461 // Threading note 1: In an unlikely race condition, it's possible for two or
462 // more threads to enter InitializeNowFunctionPointer() in parallel. This is
463 // not a problem since all threads should end up writing out the same values
464 // to the global variables.
466 // Threading note 2: A release fence is placed here to ensure, from the
467 // perspective of other threads using the function pointers, that the
468 // assignment to |g_qpc_ticks_per_second| happens before the function pointers
470 g_qpc_ticks_per_second
= ticks_per_sec
.QuadPart
;
471 ATOMIC_THREAD_FENCE(memory_order_release
);
472 g_now_function
= now_function
;
475 TimeDelta
InitialNowFunction() {
476 InitializeNowFunctionPointer();
477 return g_now_function();
483 TimeTicks::TickFunctionType
TimeTicks::SetMockTickFunction(
484 TickFunctionType ticker
) {
485 base::AutoLock
locked(g_rollover_lock
);
486 TickFunctionType old
= g_tick_function
;
487 g_tick_function
= ticker
;
494 TimeTicks
TimeTicks::Now() {
495 return TimeTicks() + g_now_function();
499 bool TimeTicks::IsHighResolution() {
500 if (g_now_function
== &InitialNowFunction
)
501 InitializeNowFunctionPointer();
502 return g_now_function
== &QPCNow
;
506 ThreadTicks
ThreadTicks::Now() {
508 return ThreadTicks();
512 TraceTicks
TraceTicks::Now() {
513 return TraceTicks() + g_now_function();
517 TimeTicks
TimeTicks::FromQPCValue(LONGLONG qpc_value
) {
518 return TimeTicks() + QPCValueToTimeDelta(qpc_value
);
521 // TimeDelta ------------------------------------------------------------------
524 TimeDelta
TimeDelta::FromQPCValue(LONGLONG qpc_value
) {
525 return QPCValueToTimeDelta(qpc_value
);