Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 // Copyright (c) 2006-2008 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
10 //
11 // The default windows timer, GetSystemTimeAsFileTime is not very precise.
12 // It is only good to ~15.5ms.
13 //
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.
20 //
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
29 //
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. Using timeBeginPeriod(1) is a requirement in order to make our
33 // message loop waits have the same resolution that our time measurements
34 // do. Otherwise, WaitForSingleObject(..., 1) will no less than 15ms when
35 // there is nothing else to waken the Wait.
37 #include "base/time.h"
39 #pragma comment(lib, "winmm.lib")
40 #include <windows.h>
41 #include <mmsystem.h>
43 #include "base/basictypes.h"
44 #include "base/lock.h"
45 #include "base/logging.h"
46 #include "base/cpu.h"
47 #include "base/singleton.h"
48 #include "base/system_monitor.h"
49 #include "mozilla/Casting.h"
51 using base::Time;
52 using base::TimeDelta;
53 using base::TimeTicks;
54 using mozilla::BitwiseCast;
56 namespace {
58 // From MSDN, FILETIME "Contains a 64-bit value representing the number of
59 // 100-nanosecond intervals since January 1, 1601 (UTC)."
60 int64_t FileTimeToMicroseconds(const FILETIME& ft) {
61 // Need to BitwiseCast to fix alignment, then divide by 10 to convert
62 // 100-nanoseconds to milliseconds. This only works on little-endian
63 // machines.
64 return BitwiseCast<int64_t>(ft) / 10;
65 }
67 void MicrosecondsToFileTime(int64_t us, FILETIME* ft) {
68 DCHECK(us >= 0) << "Time is less than 0, negative values are not "
69 "representable in FILETIME";
71 // Multiply by 10 to convert milliseconds to 100-nanoseconds. BitwiseCast will
72 // handle alignment problems. This only works on little-endian machines.
73 *ft = BitwiseCast<FILETIME>(us * 10);
74 }
76 int64_t CurrentWallclockMicroseconds() {
77 FILETIME ft;
78 ::GetSystemTimeAsFileTime(&ft);
79 return FileTimeToMicroseconds(ft);
80 }
82 // Time between resampling the un-granular clock for this API. 60 seconds.
83 const int kMaxMillisecondsToAvoidDrift = 60 * Time::kMillisecondsPerSecond;
85 int64_t initial_time = 0;
86 TimeTicks initial_ticks;
88 void InitializeClock() {
89 initial_ticks = TimeTicks::Now();
90 initial_time = CurrentWallclockMicroseconds();
91 }
93 } // namespace
95 // Time -----------------------------------------------------------------------
97 // The internal representation of Time uses FILETIME, whose epoch is 1601-01-01
98 // 00:00:00 UTC. ((1970-1601)*365+89)*24*60*60*1000*1000, where 89 is the
99 // number of leap year days between 1601 and 1970: (1970-1601)/4 excluding
100 // 1700, 1800, and 1900.
101 // static
102 const int64_t Time::kTimeTToMicrosecondsOffset = GG_INT64_C(11644473600000000);
104 // static
105 Time Time::Now() {
106 if (initial_time == 0)
107 InitializeClock();
109 // We implement time using the high-resolution timers so that we can get
110 // timeouts which are smaller than 10-15ms. If we just used
111 // CurrentWallclockMicroseconds(), we'd have the less-granular timer.
112 //
113 // To make this work, we initialize the clock (initial_time) and the
114 // counter (initial_ctr). To compute the initial time, we can check
115 // the number of ticks that have elapsed, and compute the delta.
116 //
117 // To avoid any drift, we periodically resync the counters to the system
118 // clock.
119 while(true) {
120 TimeTicks ticks = TimeTicks::Now();
122 // Calculate the time elapsed since we started our timer
123 TimeDelta elapsed = ticks - initial_ticks;
125 // Check if enough time has elapsed that we need to resync the clock.
126 if (elapsed.InMilliseconds() > kMaxMillisecondsToAvoidDrift) {
127 InitializeClock();
128 continue;
129 }
131 return Time(elapsed + initial_time);
132 }
133 }
135 // static
136 Time Time::NowFromSystemTime() {
137 // Force resync.
138 InitializeClock();
139 return Time(initial_time);
140 }
142 // static
143 Time Time::FromFileTime(FILETIME ft) {
144 return Time(FileTimeToMicroseconds(ft));
145 }
147 FILETIME Time::ToFileTime() const {
148 FILETIME utc_ft;
149 MicrosecondsToFileTime(us_, &utc_ft);
150 return utc_ft;
151 }
153 // static
154 Time Time::FromExploded(bool is_local, const Exploded& exploded) {
155 // Create the system struct representing our exploded time. It will either be
156 // in local time or UTC.
157 SYSTEMTIME st;
158 st.wYear = exploded.year;
159 st.wMonth = exploded.month;
160 st.wDayOfWeek = exploded.day_of_week;
161 st.wDay = exploded.day_of_month;
162 st.wHour = exploded.hour;
163 st.wMinute = exploded.minute;
164 st.wSecond = exploded.second;
165 st.wMilliseconds = exploded.millisecond;
167 // Convert to FILETIME.
168 FILETIME ft;
169 if (!SystemTimeToFileTime(&st, &ft)) {
170 NOTREACHED() << "Unable to convert time";
171 return Time(0);
172 }
174 // Ensure that it's in UTC.
175 if (is_local) {
176 FILETIME utc_ft;
177 LocalFileTimeToFileTime(&ft, &utc_ft);
178 return Time(FileTimeToMicroseconds(utc_ft));
179 }
180 return Time(FileTimeToMicroseconds(ft));
181 }
183 void Time::Explode(bool is_local, Exploded* exploded) const {
184 // FILETIME in UTC.
185 FILETIME utc_ft;
186 MicrosecondsToFileTime(us_, &utc_ft);
188 // FILETIME in local time if necessary.
189 BOOL success = TRUE;
190 FILETIME ft;
191 if (is_local)
192 success = FileTimeToLocalFileTime(&utc_ft, &ft);
193 else
194 ft = utc_ft;
196 // FILETIME in SYSTEMTIME (exploded).
197 SYSTEMTIME st;
198 if (!success || !FileTimeToSystemTime(&ft, &st)) {
199 NOTREACHED() << "Unable to convert time, don't know why";
200 ZeroMemory(exploded, sizeof(*exploded));
201 return;
202 }
204 exploded->year = st.wYear;
205 exploded->month = st.wMonth;
206 exploded->day_of_week = st.wDayOfWeek;
207 exploded->day_of_month = st.wDay;
208 exploded->hour = st.wHour;
209 exploded->minute = st.wMinute;
210 exploded->second = st.wSecond;
211 exploded->millisecond = st.wMilliseconds;
212 }
214 // TimeTicks ------------------------------------------------------------------
215 namespace {
217 // We define a wrapper to adapt between the __stdcall and __cdecl call of the
218 // mock function, and to avoid a static constructor. Assigning an import to a
219 // function pointer directly would require setup code to fetch from the IAT.
220 DWORD timeGetTimeWrapper() {
221 return timeGetTime();
222 }
225 DWORD (*tick_function)(void) = &timeGetTimeWrapper;
227 // We use timeGetTime() to implement TimeTicks::Now(). This can be problematic
228 // because it returns the number of milliseconds since Windows has started,
229 // which will roll over the 32-bit value every ~49 days. We try to track
230 // rollover ourselves, which works if TimeTicks::Now() is called at least every
231 // 49 days.
232 class NowSingleton {
233 public:
234 NowSingleton()
235 : rollover_(TimeDelta::FromMilliseconds(0)),
236 last_seen_(0) {
237 }
239 TimeDelta Now() {
240 AutoLock locked(lock_);
241 // We should hold the lock while calling tick_function to make sure that
242 // we keep our last_seen_ stay correctly in sync.
243 DWORD now = tick_function();
244 if (now < last_seen_)
245 rollover_ += TimeDelta::FromMilliseconds(GG_LONGLONG(0x100000000)); // ~49.7 days.
246 last_seen_ = now;
247 return TimeDelta::FromMilliseconds(now) + rollover_;
248 }
250 private:
251 Lock lock_; // To protected last_seen_ and rollover_.
252 TimeDelta rollover_; // Accumulation of time lost due to rollover.
253 DWORD last_seen_; // The last timeGetTime value we saw, to detect rollover.
255 DISALLOW_COPY_AND_ASSIGN(NowSingleton);
256 };
258 // Overview of time counters:
259 // (1) CPU cycle counter. (Retrieved via RDTSC)
260 // The CPU counter provides the highest resolution time stamp and is the least
261 // expensive to retrieve. However, the CPU counter is unreliable and should not
262 // be used in production. Its biggest issue is that it is per processor and it
263 // is not synchronized between processors. Also, on some computers, the counters
264 // will change frequency due to thermal and power changes, and stop in some
265 // states.
266 //
267 // (2) QueryPerformanceCounter (QPC). The QPC counter provides a high-
268 // resolution (100 nanoseconds) time stamp but is comparatively more expensive
269 // to retrieve. What QueryPerformanceCounter actually does is up to the HAL.
270 // (with some help from ACPI).
271 // According to http://blogs.msdn.com/oldnewthing/archive/2005/09/02/459952.aspx
272 // in the worst case, it gets the counter from the rollover interrupt on the
273 // programmable interrupt timer. In best cases, the HAL may conclude that the
274 // RDTSC counter runs at a constant frequency, then it uses that instead. On
275 // multiprocessor machines, it will try to verify the values returned from
276 // RDTSC on each processor are consistent with each other, and apply a handful
277 // of workarounds for known buggy hardware. In other words, QPC is supposed to
278 // give consistent result on a multiprocessor computer, but it is unreliable in
279 // reality due to bugs in BIOS or HAL on some, especially old computers.
280 // With recent updates on HAL and newer BIOS, QPC is getting more reliable but
281 // it should be used with caution.
282 //
283 // (3) System time. The system time provides a low-resolution (typically 10ms
284 // to 55 milliseconds) time stamp but is comparatively less expensive to
285 // retrieve and more reliable.
286 class HighResNowSingleton {
287 public:
288 HighResNowSingleton()
289 : ticks_per_microsecond_(0.0),
290 skew_(0) {
291 InitializeClock();
293 // On Athlon X2 CPUs (e.g. model 15) QueryPerformanceCounter is
294 // unreliable. Fallback to low-res clock.
295 base::CPU cpu;
296 if (cpu.vendor_name() == "AuthenticAMD" && cpu.family() == 15)
297 DisableHighResClock();
298 }
300 bool IsUsingHighResClock() {
301 return ticks_per_microsecond_ != 0.0;
302 }
304 void DisableHighResClock() {
305 ticks_per_microsecond_ = 0.0;
306 }
308 TimeDelta Now() {
309 // Our maximum tolerance for QPC drifting.
310 const int kMaxTimeDrift = 50 * Time::kMicrosecondsPerMillisecond;
312 if (IsUsingHighResClock()) {
313 int64_t now = UnreliableNow();
315 // Verify that QPC does not seem to drift.
316 DCHECK(now - ReliableNow() - skew_ < kMaxTimeDrift);
318 return TimeDelta::FromMicroseconds(now);
319 }
321 // Just fallback to the slower clock.
322 return Singleton<NowSingleton>::get()->Now();
323 }
325 private:
326 // Synchronize the QPC clock with GetSystemTimeAsFileTime.
327 void InitializeClock() {
328 LARGE_INTEGER ticks_per_sec = {0};
329 if (!QueryPerformanceFrequency(&ticks_per_sec))
330 return; // Broken, we don't guarantee this function works.
331 ticks_per_microsecond_ = static_cast<float>(ticks_per_sec.QuadPart) /
332 static_cast<float>(Time::kMicrosecondsPerSecond);
334 skew_ = UnreliableNow() - ReliableNow();
335 }
337 // Get the number of microseconds since boot in a reliable fashion
338 int64_t UnreliableNow() {
339 LARGE_INTEGER now;
340 QueryPerformanceCounter(&now);
341 return static_cast<int64_t>(now.QuadPart / ticks_per_microsecond_);
342 }
344 // Get the number of microseconds since boot in a reliable fashion
345 int64_t ReliableNow() {
346 return Singleton<NowSingleton>::get()->Now().InMicroseconds();
347 }
349 // Cached clock frequency -> microseconds. This assumes that the clock
350 // frequency is faster than one microsecond (which is 1MHz, should be OK).
351 float ticks_per_microsecond_; // 0 indicates QPF failed and we're broken.
352 int64_t skew_; // Skew between lo-res and hi-res clocks (for debugging).
354 DISALLOW_COPY_AND_ASSIGN(HighResNowSingleton);
355 };
357 } // namespace
359 // static
360 TimeTicks::TickFunctionType TimeTicks::SetMockTickFunction(
361 TickFunctionType ticker) {
362 TickFunctionType old = tick_function;
363 tick_function = ticker;
364 return old;
365 }
367 // static
368 TimeTicks TimeTicks::Now() {
369 return TimeTicks() + Singleton<NowSingleton>::get()->Now();
370 }
372 // static
373 TimeTicks TimeTicks::HighResNow() {
374 return TimeTicks() + Singleton<HighResNowSingleton>::get()->Now();
375 }