| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 | 386 |
| 387 int64 GetQPCDriftMicroseconds() { | 387 int64 GetQPCDriftMicroseconds() { |
| 388 if (!IsUsingHighResClock()) | 388 if (!IsUsingHighResClock()) |
| 389 return 0; | 389 return 0; |
| 390 return abs((UnreliableNow() - ReliableNow()) - skew_); | 390 return abs((UnreliableNow() - ReliableNow()) - skew_); |
| 391 } | 391 } |
| 392 | 392 |
| 393 int64 QPCValueToMicroseconds(LONGLONG qpc_value) { | 393 int64 QPCValueToMicroseconds(LONGLONG qpc_value) { |
| 394 if (!ticks_per_second_) | 394 if (!ticks_per_second_) |
| 395 return 0; | 395 return 0; |
| 396 | 396 // If the QPC Value is below the overflow threshold, we proceed with |
| 397 // Intentionally calculate microseconds in a round about manner to avoid | 397 // simple multiply and divide. |
| 398 // overflow and precision issues. Think twice before simplifying! | 398 if (qpc_value < Time::kQPCOverflowThreshold) |
| 399 return qpc_value * Time::kMicrosecondsPerSecond / ticks_per_second_; |
| 400 // Otherwise, calculate microseconds in a round about manner to avoid |
| 401 // overflow and precision issues. |
| 399 int64 whole_seconds = qpc_value / ticks_per_second_; | 402 int64 whole_seconds = qpc_value / ticks_per_second_; |
| 400 int64 leftover_ticks = qpc_value % ticks_per_second_; | 403 int64 leftover_ticks = qpc_value - (whole_seconds * ticks_per_second_); |
| 401 int64 microseconds = (whole_seconds * Time::kMicrosecondsPerSecond) + | 404 int64 microseconds = (whole_seconds * Time::kMicrosecondsPerSecond) + |
| 402 ((leftover_ticks * Time::kMicrosecondsPerSecond) / | 405 ((leftover_ticks * Time::kMicrosecondsPerSecond) / |
| 403 ticks_per_second_); | 406 ticks_per_second_); |
| 404 return microseconds; | 407 return microseconds; |
| 405 } | 408 } |
| 406 | 409 |
| 407 private: | 410 private: |
| 408 // Synchronize the QPC clock with GetSystemTimeAsFileTime. | 411 // Synchronize the QPC clock with GetSystemTimeAsFileTime. |
| 409 void InitializeClock() { | 412 void InitializeClock() { |
| 410 LARGE_INTEGER ticks_per_sec = {0}; | 413 LARGE_INTEGER ticks_per_sec = {0}; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 440 | 443 |
| 441 TimeDelta HighResNowWrapper() { | 444 TimeDelta HighResNowWrapper() { |
| 442 return GetHighResNowSingleton()->Now(); | 445 return GetHighResNowSingleton()->Now(); |
| 443 } | 446 } |
| 444 | 447 |
| 445 typedef TimeDelta (*NowFunction)(void); | 448 typedef TimeDelta (*NowFunction)(void); |
| 446 NowFunction now_function = RolloverProtectedNow; | 449 NowFunction now_function = RolloverProtectedNow; |
| 447 | 450 |
| 448 bool CPUReliablySupportsHighResTime() { | 451 bool CPUReliablySupportsHighResTime() { |
| 449 base::CPU cpu; | 452 base::CPU cpu; |
| 450 if (!cpu.has_non_stop_time_stamp_counter()) | 453 if (!cpu.has_non_stop_time_stamp_counter() || |
| 454 !GetHighResNowSingleton()->IsUsingHighResClock()) |
| 451 return false; | 455 return false; |
| 452 | 456 |
| 453 if (IsBuggyAthlon(cpu)) | 457 if (IsBuggyAthlon(cpu)) |
| 454 return false; | 458 return false; |
| 455 | 459 |
| 456 return true; | 460 return true; |
| 457 } | 461 } |
| 458 | 462 |
| 459 } // namespace | 463 } // namespace |
| 460 | 464 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime()); | 531 return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime()); |
| 528 } | 532 } |
| 529 } | 533 } |
| 530 | 534 |
| 531 // TimeDelta ------------------------------------------------------------------ | 535 // TimeDelta ------------------------------------------------------------------ |
| 532 | 536 |
| 533 // static | 537 // static |
| 534 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { | 538 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { |
| 535 return TimeDelta(GetHighResNowSingleton()->QPCValueToMicroseconds(qpc_value)); | 539 return TimeDelta(GetHighResNowSingleton()->QPCValueToMicroseconds(qpc_value)); |
| 536 } | 540 } |
| OLD | NEW |