Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / base / time / time_win.cc
blob4543680741fffbd46f866bf14fab64440b3535f7
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();
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));
417 TimeDelta QPCNow() {
418 LARGE_INTEGER now;
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;
453 base::CPU cpu;
454 if (ticks_per_sec.QuadPart <= 0 ||
455 !cpu.has_non_stop_time_stamp_counter() || IsBuggyAthlon(cpu)) {
456 now_function = &RolloverProtectedNow;
457 } else {
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
469 // are changed.
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();
480 } // namespace
482 // static
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;
488 g_rollover_ms = 0;
489 g_last_seen_now = 0;
490 return old;
493 // static
494 TimeTicks TimeTicks::Now() {
495 return TimeTicks() + g_now_function();
498 // static
499 bool TimeTicks::IsHighResolution() {
500 if (g_now_function == &InitialNowFunction)
501 InitializeNowFunctionPointer();
502 return g_now_function == &QPCNow;
505 // static
506 ThreadTicks ThreadTicks::Now() {
507 NOTREACHED();
508 return ThreadTicks();
511 // static
512 TraceTicks TraceTicks::Now() {
513 return TraceTicks() + g_now_function();
516 // static
517 TimeTicks TimeTicks::FromQPCValue(LONGLONG qpc_value) {
518 return TimeTicks() + QPCValueToTimeDelta(qpc_value);
521 // TimeDelta ------------------------------------------------------------------
523 // static
524 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) {
525 return QPCValueToTimeDelta(qpc_value);