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 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
384 return RolloverProtectedNow(); | 384 return RolloverProtectedNow(); |
385 } | 385 } |
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 // Time::kMicrosecondsPerSecond has 20 bits, to avoid overflow we do a |
brianderson
2014/07/29 23:53:29
I remember you had an explanation for why this cou
fmeawad
2014/07/30 00:49:49
I added that check in the CPUReliablySupportsHighR
brianderson
2014/07/30 00:58:19
Thanks for the explanation.
I was thinking about
| |
395 return 0; | 395 // simple multiplication then division if the qpc_value has less than 44 |
396 | 396 // bits. |
397 // Intentionally calculate microseconds in a round about manner to avoid | 397 if (qpc_value & 0xFFFFF00000000000) |
jar (doing other things)
2014/07/30 23:43:09
Please unit test at exactly above and below this b
jar (doing other things)
2014/07/31 00:01:43
I think the largest positive 64 bit number is 2^63
fmeawad
2014/07/31 18:54:21
Done.
fmeawad
2014/07/31 18:54:21
Done.
| |
398 // overflow and precision issues. Think twice before simplifying! | 398 { |
399 int64 whole_seconds = qpc_value / ticks_per_second_; | 399 // Intentionally calculate microseconds in a round about manner to avoid |
400 int64 leftover_ticks = qpc_value % ticks_per_second_; | 400 // overflow and precision issues. Think twice before simplifying! |
401 int64 microseconds = (whole_seconds * Time::kMicrosecondsPerSecond) + | 401 int64 whole_seconds = qpc_value / ticks_per_second_; |
402 ((leftover_ticks * Time::kMicrosecondsPerSecond) / | 402 int64 leftover_ticks = qpc_value % ticks_per_second_; |
403 ticks_per_second_); | 403 int64 microseconds = (whole_seconds * Time::kMicrosecondsPerSecond) + |
404 return microseconds; | 404 ((leftover_ticks * Time::kMicrosecondsPerSecond) / |
405 ticks_per_second_); | |
406 return microseconds; | |
407 } | |
408 return qpc_value * Time::kMicrosecondsPerSecond / ticks_per_second_; | |
brianderson
2014/07/29 23:53:29
Would a single double-precision multiplication be
fmeawad
2014/07/30 00:49:49
I made a quick experiment, and I have found no win
brianderson
2014/07/30 00:58:19
Thanks for testing that.
| |
405 } | 409 } |
406 | 410 |
407 private: | 411 private: |
408 // Synchronize the QPC clock with GetSystemTimeAsFileTime. | 412 // Synchronize the QPC clock with GetSystemTimeAsFileTime. |
409 void InitializeClock() { | 413 void InitializeClock() { |
410 LARGE_INTEGER ticks_per_sec = {0}; | 414 LARGE_INTEGER ticks_per_sec = {0}; |
411 if (!QueryPerformanceFrequency(&ticks_per_sec)) | 415 if (!QueryPerformanceFrequency(&ticks_per_sec)) |
412 return; // Broken, we don't guarantee this function works. | 416 return; // Broken, we don't guarantee this function works. |
413 ticks_per_second_ = ticks_per_sec.QuadPart; | 417 ticks_per_second_ = ticks_per_sec.QuadPart; |
414 | 418 |
(...skipping 25 matching lines...) Expand all Loading... | |
440 | 444 |
441 TimeDelta HighResNowWrapper() { | 445 TimeDelta HighResNowWrapper() { |
442 return GetHighResNowSingleton()->Now(); | 446 return GetHighResNowSingleton()->Now(); |
443 } | 447 } |
444 | 448 |
445 typedef TimeDelta (*NowFunction)(void); | 449 typedef TimeDelta (*NowFunction)(void); |
446 NowFunction now_function = RolloverProtectedNow; | 450 NowFunction now_function = RolloverProtectedNow; |
447 | 451 |
448 bool CPUReliablySupportsHighResTime() { | 452 bool CPUReliablySupportsHighResTime() { |
449 base::CPU cpu; | 453 base::CPU cpu; |
450 if (!cpu.has_non_stop_time_stamp_counter()) | 454 if (!cpu.has_non_stop_time_stamp_counter() || |
455 !GetHighResNowSingleton()->IsUsingHighResClock()) | |
451 return false; | 456 return false; |
452 | 457 |
453 if (IsBuggyAthlon(cpu)) | 458 if (IsBuggyAthlon(cpu)) |
454 return false; | 459 return false; |
455 | 460 |
456 return true; | 461 return true; |
457 } | 462 } |
458 | 463 |
459 } // namespace | 464 } // namespace |
460 | 465 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
527 return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime()); | 532 return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime()); |
528 } | 533 } |
529 } | 534 } |
530 | 535 |
531 // TimeDelta ------------------------------------------------------------------ | 536 // TimeDelta ------------------------------------------------------------------ |
532 | 537 |
533 // static | 538 // static |
534 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { | 539 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { |
535 return TimeDelta(GetHighResNowSingleton()->QPCValueToMicroseconds(qpc_value)); | 540 return TimeDelta(GetHighResNowSingleton()->QPCValueToMicroseconds(qpc_value)); |
536 } | 541 } |
OLD | NEW |