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 30 matching lines...) Expand all Loading... |
41 #include "base/basictypes.h" | 41 #include "base/basictypes.h" |
42 #include "base/cpu.h" | 42 #include "base/cpu.h" |
43 #include "base/lazy_instance.h" | 43 #include "base/lazy_instance.h" |
44 #include "base/logging.h" | 44 #include "base/logging.h" |
45 #include "base/synchronization/lock.h" | 45 #include "base/synchronization/lock.h" |
46 | 46 |
47 using base::ThreadTicks; | 47 using base::ThreadTicks; |
48 using base::Time; | 48 using base::Time; |
49 using base::TimeDelta; | 49 using base::TimeDelta; |
50 using base::TimeTicks; | 50 using base::TimeTicks; |
51 using base::TraceTicks; | |
52 | 51 |
53 namespace { | 52 namespace { |
54 | 53 |
55 // From MSDN, FILETIME "Contains a 64-bit value representing the number of | 54 // From MSDN, FILETIME "Contains a 64-bit value representing the number of |
56 // 100-nanosecond intervals since January 1, 1601 (UTC)." | 55 // 100-nanosecond intervals since January 1, 1601 (UTC)." |
57 int64 FileTimeToMicroseconds(const FILETIME& ft) { | 56 int64 FileTimeToMicroseconds(const FILETIME& ft) { |
58 // Need to bit_cast to fix alignment, then divide by 10 to convert | 57 // Need to bit_cast to fix alignment, then divide by 10 to convert |
59 // 100-nanoseconds to microseconds. This only works on little-endian | 58 // 100-nanoseconds to microseconds. This only works on little-endian |
60 // machines. | 59 // machines. |
61 return bit_cast<int64, FILETIME>(ft) / 10; | 60 return bit_cast<int64, FILETIME>(ft) / 10; |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 // the low-resolution clock. | 451 // the low-resolution clock. |
453 // | 452 // |
454 // If the QPC implementation is expensive and/or unreliable, TimeTicks::Now() | 453 // If the QPC implementation is expensive and/or unreliable, TimeTicks::Now() |
455 // will still use the low-resolution clock. A CPU lacking a non-stop time | 454 // will still use the low-resolution clock. A CPU lacking a non-stop time |
456 // counter will cause Windows to provide an alternate QPC implementation that | 455 // counter will cause Windows to provide an alternate QPC implementation that |
457 // works, but is expensive to use. Certain Athlon CPUs are known to make the | 456 // works, but is expensive to use. Certain Athlon CPUs are known to make the |
458 // QPC implementation unreliable. | 457 // QPC implementation unreliable. |
459 // | 458 // |
460 // Otherwise, Now uses the high-resolution QPC clock. As of 21 August 2015, | 459 // Otherwise, Now uses the high-resolution QPC clock. As of 21 August 2015, |
461 // ~72% of users fall within this category. | 460 // ~72% of users fall within this category. |
462 // | |
463 // TraceTicks::Now() always uses the same clock as TimeTicks::Now(), even | |
464 // when the QPC exists but is expensive or unreliable. This is because we'd | |
465 // eventually like to merge TraceTicks and TimeTicks and have one type of | |
466 // timestamp that is reliable, monotonic, and comparable. Also, while we could | |
467 // use the high-resolution timer for TraceTicks even when it's unreliable or | |
468 // slow, it's easier to make tracing tools accommodate a coarse timer than | |
469 // one that's unreliable or slow. | |
470 NowFunction now_function; | 461 NowFunction now_function; |
471 base::CPU cpu; | 462 base::CPU cpu; |
472 if (ticks_per_sec.QuadPart <= 0 || | 463 if (ticks_per_sec.QuadPart <= 0 || |
473 !cpu.has_non_stop_time_stamp_counter() || IsBuggyAthlon(cpu)) { | 464 !cpu.has_non_stop_time_stamp_counter() || IsBuggyAthlon(cpu)) { |
474 now_function = &RolloverProtectedNow; | 465 now_function = &RolloverProtectedNow; |
475 } else { | 466 } else { |
476 now_function = &QPCNow; | 467 now_function = &QPCNow; |
477 } | 468 } |
478 | 469 |
479 // Threading note 1: In an unlikely race condition, it's possible for two or | 470 // Threading note 1: In an unlikely race condition, it's possible for two or |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 | 597 |
607 // Compute the frequency of the TSC. | 598 // Compute the frequency of the TSC. |
608 DCHECK_GE(tsc_now, tsc_initial); | 599 DCHECK_GE(tsc_now, tsc_initial); |
609 uint64 tsc_ticks = tsc_now - tsc_initial; | 600 uint64 tsc_ticks = tsc_now - tsc_initial; |
610 tsc_ticks_per_second = tsc_ticks / elapsed_time_seconds; | 601 tsc_ticks_per_second = tsc_ticks / elapsed_time_seconds; |
611 | 602 |
612 return tsc_ticks_per_second; | 603 return tsc_ticks_per_second; |
613 } | 604 } |
614 | 605 |
615 // static | 606 // static |
616 TraceTicks TraceTicks::Now() { | |
617 return TraceTicks() + g_now_function(); | |
618 } | |
619 | |
620 // static | |
621 TimeTicks TimeTicks::FromQPCValue(LONGLONG qpc_value) { | 607 TimeTicks TimeTicks::FromQPCValue(LONGLONG qpc_value) { |
622 return TimeTicks() + QPCValueToTimeDelta(qpc_value); | 608 return TimeTicks() + QPCValueToTimeDelta(qpc_value); |
623 } | 609 } |
624 | 610 |
625 // TimeDelta ------------------------------------------------------------------ | 611 // TimeDelta ------------------------------------------------------------------ |
626 | 612 |
627 // static | 613 // static |
628 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { | 614 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { |
629 return QPCValueToTimeDelta(qpc_value); | 615 return QPCValueToTimeDelta(qpc_value); |
630 } | 616 } |
OLD | NEW |