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 // Time represents an absolute point in coordinated universal time (UTC), | 5 // Time represents an absolute point in coordinated universal time (UTC), |
6 // internally represented as microseconds (s/1,000,000) since the Windows epoch | 6 // internally represented as microseconds (s/1,000,000) since the Windows epoch |
7 // (1601-01-01 00:00:00 UTC) (See http://crbug.com/14734). System-dependent | 7 // (1601-01-01 00:00:00 UTC) (See http://crbug.com/14734). System-dependent |
8 // clock interface routines are defined in time_PLATFORM.cc. | 8 // clock interface routines are defined in time_PLATFORM.cc. |
9 // | 9 // |
10 // TimeDelta represents a duration of time, internally represented in | 10 // TimeDelta represents a duration of time, internally represented in |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 static TimeDelta Max(); | 93 static TimeDelta Max(); |
94 | 94 |
95 // Returns the internal numeric value of the TimeDelta object. Please don't | 95 // Returns the internal numeric value of the TimeDelta object. Please don't |
96 // use this and do arithmetic on it, as it is more error prone than using the | 96 // use this and do arithmetic on it, as it is more error prone than using the |
97 // provided operators. | 97 // provided operators. |
98 // For serializing, use FromInternalValue to reconstitute. | 98 // For serializing, use FromInternalValue to reconstitute. |
99 int64 ToInternalValue() const { | 99 int64 ToInternalValue() const { |
100 return delta_; | 100 return delta_; |
101 } | 101 } |
102 | 102 |
| 103 // Returns the magnitude (absolute value) of this TimeDelta. |
| 104 TimeDelta magnitude() const { |
| 105 // Some toolchains provide an incomplete C++11 implementation and lack an |
| 106 // int64 overload for std::abs(). The following is a simple branchless |
| 107 // implementation: |
| 108 const int64 mask = delta_ >> (sizeof(delta_) * 8 - 1); |
| 109 return TimeDelta((delta_ + mask) ^ mask); |
| 110 } |
| 111 |
103 // Returns true if the time delta is the maximum time delta. | 112 // Returns true if the time delta is the maximum time delta. |
104 bool is_max() const { | 113 bool is_max() const { |
105 return delta_ == std::numeric_limits<int64>::max(); | 114 return delta_ == std::numeric_limits<int64>::max(); |
106 } | 115 } |
107 | 116 |
108 #if defined(OS_POSIX) | 117 #if defined(OS_POSIX) |
109 struct timespec ToTimeSpec() const; | 118 struct timespec ToTimeSpec() const; |
110 #endif | 119 #endif |
111 | 120 |
112 // Returns the time delta in some unit. The F versions return a floating | 121 // Returns the time delta in some unit. The F versions return a floating |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 #if defined(OS_LINUX) | 590 #if defined(OS_LINUX) |
582 // Force definition of the system trace clock; it is a chromeos-only api | 591 // Force definition of the system trace clock; it is a chromeos-only api |
583 // at the moment and surfacing it in the right place requires mucking | 592 // at the moment and surfacing it in the right place requires mucking |
584 // with glibc et al. | 593 // with glibc et al. |
585 static const clockid_t kClockSystemTrace = 11; | 594 static const clockid_t kClockSystemTrace = 11; |
586 #endif | 595 #endif |
587 | 596 |
588 TimeTicks() : ticks_(0) { | 597 TimeTicks() : ticks_(0) { |
589 } | 598 } |
590 | 599 |
591 // Platform-dependent tick count representing "right now." | 600 // Platform-dependent tick count representing "right now." When |
592 // The resolution of this clock is ~1-15ms. Resolution varies depending | 601 // IsHighResolution() returns false, the resolution of the clock could be |
593 // on hardware/operating system configuration. | 602 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one |
| 603 // microsecond. |
594 static TimeTicks Now(); | 604 static TimeTicks Now(); |
595 | 605 |
596 // Returns a platform-dependent high-resolution tick count. Implementation | 606 // DEPRECATED |
597 // is hardware dependent and may or may not return sub-millisecond | 607 // TODO(miu): Remove this function, and all callpoints should call Now(). |
598 // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND | 608 static TimeTicks HighResNow() { return TimeTicks::Now(); } |
599 // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED. | |
600 static TimeTicks HighResNow(); | |
601 | 609 |
602 static bool IsHighResNowFastAndReliable(); | 610 // Returns true if the high resolution clock is working on this system and |
| 611 // Now() will return high resolution values. Note that, on systems where the |
| 612 // high resolution clock works but is deemed inefficient, the low resolution |
| 613 // clock will be used instead. |
| 614 static bool IsHighResolution(); |
| 615 |
| 616 // DEPRECATED |
| 617 // TODO(miu): Remove this function, and all callpoints should call |
| 618 // IsHighResolution(). |
| 619 static bool IsHighResNowFastAndReliable() { return IsHighResolution(); } |
603 | 620 |
604 // Returns true if ThreadNow() is supported on this system. | 621 // Returns true if ThreadNow() is supported on this system. |
605 static bool IsThreadNowSupported() { | 622 static bool IsThreadNowSupported() { |
606 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ | 623 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ |
607 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) | 624 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
608 return true; | 625 return true; |
609 #else | 626 #else |
610 return false; | 627 return false; |
611 #endif | 628 #endif |
612 } | 629 } |
613 | 630 |
614 // Returns thread-specific CPU-time on systems that support this feature. | 631 // Returns thread-specific CPU-time on systems that support this feature. |
615 // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer | 632 // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer |
616 // to (approximately) measure how much time the calling thread spent doing | 633 // to (approximately) measure how much time the calling thread spent doing |
617 // actual work vs. being de-scheduled. May return bogus results if the thread | 634 // actual work vs. being de-scheduled. May return bogus results if the thread |
618 // migrates to another CPU between two calls. | 635 // migrates to another CPU between two calls. |
| 636 // |
| 637 // WARNING: The returned value might NOT have the same origin as Now(). Do not |
| 638 // perform math between TimeTicks values returned by Now() and ThreadNow() and |
| 639 // expect meaningful results. |
| 640 // TODO(miu): Since the timeline of these values is different, the values |
| 641 // should be of a different type. |
619 static TimeTicks ThreadNow(); | 642 static TimeTicks ThreadNow(); |
620 | 643 |
621 // Returns the current system trace time or, if none is defined, the current | 644 // Returns the current system trace time or, if not available on this |
622 // high-res time (i.e. HighResNow()). On systems where a global trace clock | 645 // platform, a high-resolution time value; or a low-resolution time value if |
623 // is defined, timestamping TraceEvents's with this value guarantees | 646 // neither are avalable. On systems where a global trace clock is defined, |
624 // synchronization between events collected inside chrome and events | 647 // timestamping TraceEvents's with this value guarantees synchronization |
625 // collected outside (e.g. kernel, X server). | 648 // between events collected inside chrome and events collected outside |
| 649 // (e.g. kernel, X server). |
| 650 // |
| 651 // WARNING: The returned value might NOT have the same origin as Now(). Do not |
| 652 // perform math between TimeTicks values returned by Now() and |
| 653 // NowFromSystemTraceTime() and expect meaningful results. |
| 654 // TODO(miu): Since the timeline of these values is different, the values |
| 655 // should be of a different type. |
626 static TimeTicks NowFromSystemTraceTime(); | 656 static TimeTicks NowFromSystemTraceTime(); |
627 | 657 |
628 #if defined(OS_WIN) | 658 #if defined(OS_WIN) |
629 // Get the absolute value of QPC time drift. For testing. | 659 // Translates an absolute QPC timestamp into a TimeTicks value. The returned |
630 static int64 GetQPCDriftMicroseconds(); | 660 // value has the same origin as Now(). Do NOT attempt to use this if |
631 | 661 // IsHighResolution() returns false. |
632 static TimeTicks FromQPCValue(LONGLONG qpc_value); | 662 static TimeTicks FromQPCValue(LONGLONG qpc_value); |
633 | |
634 // Returns true if the high resolution clock is working on this system. | |
635 // This is only for testing. | |
636 static bool IsHighResClockWorking(); | |
637 #endif | 663 #endif |
638 | 664 |
639 // Returns true if this object has not been initialized. | 665 // Returns true if this object has not been initialized. |
640 bool is_null() const { | 666 bool is_null() const { |
641 return ticks_ == 0; | 667 return ticks_ == 0; |
642 } | 668 } |
643 | 669 |
644 // Converts an integer value representing TimeTicks to a class. This is used | 670 // Converts an integer value representing TimeTicks to a class. This is used |
645 // when deserializing a |TimeTicks| structure, using a value known to be | 671 // when deserializing a |TimeTicks| structure, using a value known to be |
646 // compatible. It is not provided as a constructor because the integer type | 672 // compatible. It is not provided as a constructor because the integer type |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 inline TimeTicks TimeDelta::operator+(TimeTicks t) const { | 757 inline TimeTicks TimeDelta::operator+(TimeTicks t) const { |
732 return TimeTicks(t.ticks_ + delta_); | 758 return TimeTicks(t.ticks_ + delta_); |
733 } | 759 } |
734 | 760 |
735 // For logging use only. | 761 // For logging use only. |
736 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); | 762 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); |
737 | 763 |
738 } // namespace base | 764 } // namespace base |
739 | 765 |
740 #endif // BASE_TIME_TIME_H_ | 766 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |