OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 // Windows Timer Primer | 6 // Windows Timer Primer |
7 // | 7 // |
8 // A good article: http://www.ddj.com/windows/184416651 | 8 // A good article: http://www.ddj.com/windows/184416651 |
9 // A good mozilla bug: http://bugzilla.mozilla.org/show_bug.cgi?id=363258 | 9 // A good mozilla bug: http://bugzilla.mozilla.org/show_bug.cgi?id=363258 |
10 // | 10 // |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 // give consistent result on a multiprocessor computer, but it is unreliable in | 303 // give consistent result on a multiprocessor computer, but it is unreliable in |
304 // reality due to bugs in BIOS or HAL on some, especially old computers. | 304 // reality due to bugs in BIOS or HAL on some, especially old computers. |
305 // With recent updates on HAL and newer BIOS, QPC is getting more reliable but | 305 // With recent updates on HAL and newer BIOS, QPC is getting more reliable but |
306 // it should be used with caution. | 306 // it should be used with caution. |
307 // | 307 // |
308 // (3) System time. The system time provides a low-resolution (typically 10ms | 308 // (3) System time. The system time provides a low-resolution (typically 10ms |
309 // to 55 milliseconds) time stamp but is comparatively less expensive to | 309 // to 55 milliseconds) time stamp but is comparatively less expensive to |
310 // retrieve and more reliable. | 310 // retrieve and more reliable. |
311 class HighResNowSingleton { | 311 class HighResNowSingleton { |
312 public: | 312 public: |
313 HighResNowSingleton() | 313 static HighResNowSingleton* GetInstance() { |
314 : ticks_per_microsecond_(0.0), | 314 return Singleton<HighResNowSingleton>::get(); |
315 skew_(0) { | |
316 InitializeClock(); | |
317 | |
318 // On Athlon X2 CPUs (e.g. model 15) QueryPerformanceCounter is | |
319 // unreliable. Fallback to low-res clock. | |
320 base::CPU cpu; | |
321 if (cpu.vendor_name() == "AuthenticAMD" && cpu.family() == 15) | |
322 DisableHighResClock(); | |
323 } | 315 } |
324 | 316 |
325 bool IsUsingHighResClock() { | 317 bool IsUsingHighResClock() { |
326 return ticks_per_microsecond_ != 0.0; | 318 return ticks_per_microsecond_ != 0.0; |
327 } | 319 } |
328 | 320 |
329 void DisableHighResClock() { | 321 void DisableHighResClock() { |
330 ticks_per_microsecond_ = 0.0; | 322 ticks_per_microsecond_ = 0.0; |
331 } | 323 } |
332 | 324 |
333 TimeDelta Now() { | 325 TimeDelta Now() { |
334 if (IsUsingHighResClock()) | 326 if (IsUsingHighResClock()) |
335 return TimeDelta::FromMicroseconds(UnreliableNow()); | 327 return TimeDelta::FromMicroseconds(UnreliableNow()); |
336 | 328 |
337 // Just fallback to the slower clock. | 329 // Just fallback to the slower clock. |
338 return RolloverProtectedNow(); | 330 return RolloverProtectedNow(); |
339 } | 331 } |
340 | 332 |
341 int64 GetQPCDriftMicroseconds() { | 333 int64 GetQPCDriftMicroseconds() { |
342 if (!IsUsingHighResClock()) | 334 if (!IsUsingHighResClock()) |
343 return 0; | 335 return 0; |
344 | 336 |
345 return abs((UnreliableNow() - ReliableNow()) - skew_); | 337 return abs((UnreliableNow() - ReliableNow()) - skew_); |
346 } | 338 } |
347 | 339 |
348 private: | 340 private: |
| 341 HighResNowSingleton() |
| 342 : ticks_per_microsecond_(0.0), |
| 343 skew_(0) { |
| 344 InitializeClock(); |
| 345 |
| 346 // On Athlon X2 CPUs (e.g. model 15) QueryPerformanceCounter is |
| 347 // unreliable. Fallback to low-res clock. |
| 348 base::CPU cpu; |
| 349 if (cpu.vendor_name() == "AuthenticAMD" && cpu.family() == 15) |
| 350 DisableHighResClock(); |
| 351 } |
| 352 |
349 // Synchronize the QPC clock with GetSystemTimeAsFileTime. | 353 // Synchronize the QPC clock with GetSystemTimeAsFileTime. |
350 void InitializeClock() { | 354 void InitializeClock() { |
351 LARGE_INTEGER ticks_per_sec = {0}; | 355 LARGE_INTEGER ticks_per_sec = {0}; |
352 if (!QueryPerformanceFrequency(&ticks_per_sec)) | 356 if (!QueryPerformanceFrequency(&ticks_per_sec)) |
353 return; // Broken, we don't guarantee this function works. | 357 return; // Broken, we don't guarantee this function works. |
354 ticks_per_microsecond_ = static_cast<float>(ticks_per_sec.QuadPart) / | 358 ticks_per_microsecond_ = static_cast<float>(ticks_per_sec.QuadPart) / |
355 static_cast<float>(Time::kMicrosecondsPerSecond); | 359 static_cast<float>(Time::kMicrosecondsPerSecond); |
356 | 360 |
357 skew_ = UnreliableNow() - ReliableNow(); | 361 skew_ = UnreliableNow() - ReliableNow(); |
358 } | 362 } |
359 | 363 |
360 // Get the number of microseconds since boot in an unreliable fashion. | 364 // Get the number of microseconds since boot in an unreliable fashion. |
361 int64 UnreliableNow() { | 365 int64 UnreliableNow() { |
362 LARGE_INTEGER now; | 366 LARGE_INTEGER now; |
363 QueryPerformanceCounter(&now); | 367 QueryPerformanceCounter(&now); |
364 return static_cast<int64>(now.QuadPart / ticks_per_microsecond_); | 368 return static_cast<int64>(now.QuadPart / ticks_per_microsecond_); |
365 } | 369 } |
366 | 370 |
367 // Get the number of microseconds since boot in a reliable fashion. | 371 // Get the number of microseconds since boot in a reliable fashion. |
368 int64 ReliableNow() { | 372 int64 ReliableNow() { |
369 return RolloverProtectedNow().InMicroseconds(); | 373 return RolloverProtectedNow().InMicroseconds(); |
370 } | 374 } |
371 | 375 |
372 // Cached clock frequency -> microseconds. This assumes that the clock | 376 // Cached clock frequency -> microseconds. This assumes that the clock |
373 // frequency is faster than one microsecond (which is 1MHz, should be OK). | 377 // frequency is faster than one microsecond (which is 1MHz, should be OK). |
374 float ticks_per_microsecond_; // 0 indicates QPF failed and we're broken. | 378 float ticks_per_microsecond_; // 0 indicates QPF failed and we're broken. |
375 int64 skew_; // Skew between lo-res and hi-res clocks (for debugging). | 379 int64 skew_; // Skew between lo-res and hi-res clocks (for debugging). |
376 | 380 |
377 DISALLOW_COPY_AND_ASSIGN(HighResNowSingleton); | 381 friend struct DefaultSingletonTraits<HighResNowSingleton>; |
378 }; | 382 }; |
379 | 383 |
380 } // namespace | 384 } // namespace |
381 | 385 |
382 // static | 386 // static |
383 TimeTicks::TickFunctionType TimeTicks::SetMockTickFunction( | 387 TimeTicks::TickFunctionType TimeTicks::SetMockTickFunction( |
384 TickFunctionType ticker) { | 388 TickFunctionType ticker) { |
385 TickFunctionType old = tick_function; | 389 TickFunctionType old = tick_function; |
386 tick_function = ticker; | 390 tick_function = ticker; |
387 return old; | 391 return old; |
388 } | 392 } |
389 | 393 |
390 // static | 394 // static |
391 TimeTicks TimeTicks::Now() { | 395 TimeTicks TimeTicks::Now() { |
392 return TimeTicks() + RolloverProtectedNow(); | 396 return TimeTicks() + RolloverProtectedNow(); |
393 } | 397 } |
394 | 398 |
395 // static | 399 // static |
396 TimeTicks TimeTicks::HighResNow() { | 400 TimeTicks TimeTicks::HighResNow() { |
397 return TimeTicks() + Singleton<HighResNowSingleton>::get()->Now(); | 401 return TimeTicks() + HighResNowSingleton::GetInstance()->Now(); |
398 } | 402 } |
399 | 403 |
400 // static | 404 // static |
401 int64 TimeTicks::GetQPCDriftMicroseconds() { | 405 int64 TimeTicks::GetQPCDriftMicroseconds() { |
402 return Singleton<HighResNowSingleton>::get()->GetQPCDriftMicroseconds(); | 406 return HighResNowSingleton::GetInstance()->GetQPCDriftMicroseconds(); |
403 } | 407 } |
404 | 408 |
405 // static | 409 // static |
406 bool TimeTicks::IsHighResClockWorking() { | 410 bool TimeTicks::IsHighResClockWorking() { |
407 return Singleton<HighResNowSingleton>::get()->IsUsingHighResClock(); | 411 return HighResNowSingleton::GetInstance()->IsUsingHighResClock(); |
408 } | 412 } |
OLD | NEW |