Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / time / time_win.cc
blobe9044603e9aedf7a7841d6f2e8406ad68289f291
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
7 //
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
32 // power.
34 #include "base/time/time.h"
36 #pragma comment(lib, "winmm.lib")
37 #include <windows.h>
38 #include <mmsystem.h>
39 #include <stdint.h>
41 #include "base/basictypes.h"
42 #include "base/cpu.h"
43 #include "base/lazy_instance.h"
44 #include "base/logging.h"
45 #include "base/synchronization/lock.h"
47 using base::ThreadTicks;
48 using base::Time;
49 using base::TimeDelta;
50 using base::TimeTicks;
51 using base::TraceTicks;
53 namespace {
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
60 // machines.
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() {
74 FILETIME ft;
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;
103 } // namespace
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.
111 // static
112 const int64 Time::kTimeTToMicrosecondsOffset = INT64_C(11644473600000000);
114 // static
115 Time Time::Now() {
116 if (initial_time == 0)
117 InitializeClock();
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
128 // clock.
129 while (true) {
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) {
137 InitializeClock();
138 continue;
141 return Time(elapsed + Time(initial_time));
145 // static
146 Time Time::NowFromSystemTime() {
147 // Force resync.
148 InitializeClock();
149 return Time(initial_time);
152 // static
153 Time Time::FromFileTime(FILETIME ft) {
154 if (bit_cast<int64, FILETIME>(ft) == 0)
155 return Time();
156 if (ft.dwHighDateTime == std::numeric_limits<DWORD>::max() &&
157 ft.dwLowDateTime == std::numeric_limits<DWORD>::max())
158 return Max();
159 return Time(FileTimeToMicroseconds(ft));
162 FILETIME Time::ToFileTime() const {
163 if (is_null())
164 return bit_cast<FILETIME, int64>(0);
165 if (is_max()) {
166 FILETIME result;
167 result.dwHighDateTime = std::numeric_limits<DWORD>::max();
168 result.dwLowDateTime = std::numeric_limits<DWORD>::max();
169 return result;
171 FILETIME utc_ft;
172 MicrosecondsToFileTime(us_, &utc_ft);
173 return utc_ft;
176 // static
177 void Time::EnableHighResolutionTimer(bool enable) {
178 base::AutoLock lock(g_high_res_lock.Get());
179 if (g_high_res_timer_enabled == enable)
180 return;
181 g_high_res_timer_enabled = enable;
182 if (!g_high_res_timer_count)
183 return;
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.
189 if (enable) {
190 timeEndPeriod(kMinTimerIntervalLowResMs);
191 timeBeginPeriod(kMinTimerIntervalHighResMs);
192 } else {
193 timeEndPeriod(kMinTimerIntervalHighResMs);
194 timeBeginPeriod(kMinTimerIntervalLowResMs);
198 // static
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
202 // called.
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;
208 if (activating) {
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);
213 } else {
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);
222 // static
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;
228 // static
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.
232 SYSTEMTIME st;
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);
242 FILETIME ft;
243 bool success = true;
244 // Ensure that it's in UTC.
245 if (is_local) {
246 SYSTEMTIME utc_st;
247 success = TzSpecificLocalTimeToSystemTime(NULL, &st, &utc_st) &&
248 SystemTimeToFileTime(&utc_st, &ft);
249 } else {
250 success = !!SystemTimeToFileTime(&st, &ft);
253 if (!success) {
254 NOTREACHED() << "Unable to convert time";
255 return Time(0);
257 return Time(FileTimeToMicroseconds(ft));
260 void Time::Explode(bool is_local, Exploded* exploded) const {
261 if (us_ < 0LL) {
262 // We are not able to convert it to FILETIME.
263 ZeroMemory(exploded, sizeof(*exploded));
264 return;
267 // FILETIME in UTC.
268 FILETIME utc_ft;
269 MicrosecondsToFileTime(us_, &utc_ft);
271 // FILETIME in local time if necessary.
272 bool success = true;
273 // FILETIME in SYSTEMTIME (exploded).
274 SYSTEMTIME st = {0};
275 if (is_local) {
276 SYSTEMTIME utc_st;
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);
283 } else {
284 success = !!FileTimeToSystemTime(&utc_ft, &st);
287 if (!success) {
288 NOTREACHED() << "Unable to convert time, don't know why";
289 ZeroMemory(exploded, sizeof(*exploded));
290 return;
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 ------------------------------------------------------------------
304 namespace {
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
333 // 49 days.
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 += 0x100000000I64; // ~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
378 // one.
380 using NowFunction = TimeDelta (*)(void);
382 TimeDelta InitialNowFunction();
383 TimeDelta InitialSystemTraceNowFunction();
385 // See "threading notes" in InitializeNowFunctionPointers() for details on how
386 // concurrent reads/writes to these globals has been made safe.
387 NowFunction g_now_function = &InitialNowFunction;
388 NowFunction g_system_trace_now_function = &InitialSystemTraceNowFunction;
389 int64 g_qpc_ticks_per_second = 0;
391 // As of January 2015, use of <atomic> is forbidden in Chromium code. This is
392 // what std::atomic_thread_fence does on Windows on all Intel architectures when
393 // the memory_order argument is anything but std::memory_order_seq_cst:
394 #define ATOMIC_THREAD_FENCE(memory_order) _ReadWriteBarrier();
396 TimeDelta QPCValueToTimeDelta(LONGLONG qpc_value) {
397 // Ensure that the assignment to |g_qpc_ticks_per_second|, made in
398 // InitializeNowFunctionPointers(), has happened by this point.
399 ATOMIC_THREAD_FENCE(memory_order_acquire);
401 DCHECK_GT(g_qpc_ticks_per_second, 0);
403 // If the QPC Value is below the overflow threshold, we proceed with
404 // simple multiply and divide.
405 if (qpc_value < Time::kQPCOverflowThreshold) {
406 return TimeDelta::FromMicroseconds(
407 qpc_value * Time::kMicrosecondsPerSecond / g_qpc_ticks_per_second);
409 // Otherwise, calculate microseconds in a round about manner to avoid
410 // overflow and precision issues.
411 int64 whole_seconds = qpc_value / g_qpc_ticks_per_second;
412 int64 leftover_ticks = qpc_value - (whole_seconds * g_qpc_ticks_per_second);
413 return TimeDelta::FromMicroseconds(
414 (whole_seconds * Time::kMicrosecondsPerSecond) +
415 ((leftover_ticks * Time::kMicrosecondsPerSecond) /
416 g_qpc_ticks_per_second));
419 TimeDelta QPCNow() {
420 LARGE_INTEGER now;
421 QueryPerformanceCounter(&now);
422 return QPCValueToTimeDelta(now.QuadPart);
425 bool IsBuggyAthlon(const base::CPU& cpu) {
426 // On Athlon X2 CPUs (e.g. model 15) QueryPerformanceCounter is unreliable.
427 return cpu.vendor_name() == "AuthenticAMD" && cpu.family() == 15;
430 void InitializeNowFunctionPointers() {
431 LARGE_INTEGER ticks_per_sec = {};
432 if (!QueryPerformanceFrequency(&ticks_per_sec))
433 ticks_per_sec.QuadPart = 0;
435 // If Windows cannot provide a QPC implementation, both TimeTicks::Now() and
436 // TraceTicks::Now() must use the low-resolution clock.
438 // If the QPC implementation is expensive and/or unreliable, TimeTicks::Now()
439 // will use the low-resolution clock, but TraceTicks::Now() will use the QPC
440 // (in the hope that it is still useful for tracing purposes). A CPU lacking a
441 // non-stop time counter will cause Windows to provide an alternate QPC
442 // implementation that works, but is expensive to use. Certain Athlon CPUs are
443 // known to make the QPC implementation unreliable.
445 // Otherwise, both Now functions can use the high-resolution QPC clock. As of
446 // 4 January 2015, ~68% of users fall within this category.
447 NowFunction now_function;
448 NowFunction system_trace_now_function;
449 base::CPU cpu;
450 if (ticks_per_sec.QuadPart <= 0) {
451 now_function = system_trace_now_function = &RolloverProtectedNow;
452 } else if (!cpu.has_non_stop_time_stamp_counter() || IsBuggyAthlon(cpu)) {
453 now_function = &RolloverProtectedNow;
454 system_trace_now_function = &QPCNow;
455 } else {
456 now_function = system_trace_now_function = &QPCNow;
459 // Threading note 1: In an unlikely race condition, it's possible for two or
460 // more threads to enter InitializeNowFunctionPointers() in parallel. This is
461 // not a problem since all threads should end up writing out the same values
462 // to the global variables.
464 // Threading note 2: A release fence is placed here to ensure, from the
465 // perspective of other threads using the function pointers, that the
466 // assignment to |g_qpc_ticks_per_second| happens before the function pointers
467 // are changed.
468 g_qpc_ticks_per_second = ticks_per_sec.QuadPart;
469 ATOMIC_THREAD_FENCE(memory_order_release);
470 g_now_function = now_function;
471 g_system_trace_now_function = system_trace_now_function;
474 TimeDelta InitialNowFunction() {
475 InitializeNowFunctionPointers();
476 return g_now_function();
479 TimeDelta InitialSystemTraceNowFunction() {
480 InitializeNowFunctionPointers();
481 return g_system_trace_now_function();
484 } // namespace
486 // static
487 TimeTicks::TickFunctionType TimeTicks::SetMockTickFunction(
488 TickFunctionType ticker) {
489 base::AutoLock locked(g_rollover_lock);
490 TickFunctionType old = g_tick_function;
491 g_tick_function = ticker;
492 g_rollover_ms = 0;
493 g_last_seen_now = 0;
494 return old;
497 // static
498 TimeTicks TimeTicks::Now() {
499 return TimeTicks() + g_now_function();
502 // static
503 bool TimeTicks::IsHighResolution() {
504 if (g_now_function == &InitialNowFunction)
505 InitializeNowFunctionPointers();
506 return g_now_function == &QPCNow;
509 // static
510 ThreadTicks ThreadTicks::Now() {
511 NOTREACHED();
512 return ThreadTicks();
515 // static
516 TraceTicks TraceTicks::Now() {
517 return TraceTicks() + g_system_trace_now_function();
520 // static
521 TimeTicks TimeTicks::FromQPCValue(LONGLONG qpc_value) {
522 return TimeTicks() + QPCValueToTimeDelta(qpc_value);
525 // TimeDelta ------------------------------------------------------------------
527 // static
528 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) {
529 return QPCValueToTimeDelta(qpc_value);