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 |
11 // microseconds. | 11 // microseconds. |
12 // | 12 // |
13 // TimeTicks represents an abstract time that is most of the time incrementing | 13 // TimeTicks represents an abstract time that is most of the time incrementing |
14 // for use in measuring time durations. It is internally represented in | 14 // for use in measuring time durations. It is internally represented in |
15 // microseconds. It can not be converted to a human-readable time, but is | 15 // microseconds. It can not be converted to a human-readable time, but is |
16 // guaranteed not to decrease (if the user changes the computer clock, | 16 // guaranteed not to decrease (if the user changes the computer clock, |
17 // Time::Now() may actually decrease or jump). But note that TimeTicks may | 17 // Time::Now() may actually decrease or jump). But note that TimeTicks may |
18 // "stand still", for example if the computer suspended. | 18 // "stand still", for example if the computer suspended. |
19 // | 19 // |
20 // These classes are represented as only a 64-bit value, so they can be | 20 // These classes are represented as only a 64-bit value, so they can be |
21 // efficiently passed by value. | 21 // efficiently passed by value. |
22 // | 22 // |
Lei Zhang
2015/05/13 23:31:47
It'll be good to write some more comments here to
miu
2015/05/19 03:14:31
Done. I added a "So many choices! / Examples" sec
| |
23 // Definitions of operator<< are provided to make these types work with | 23 // Definitions of operator<< are provided to make these types work with |
24 // DCHECK_EQ() and other log macros. For human-readable formatting, see | 24 // DCHECK_EQ() and other log macros. For human-readable formatting, see |
25 // "base/i18n/time_formatting.h". | 25 // "base/i18n/time_formatting.h". |
26 | 26 |
27 #ifndef BASE_TIME_TIME_H_ | 27 #ifndef BASE_TIME_TIME_H_ |
28 #define BASE_TIME_TIME_H_ | 28 #define BASE_TIME_TIME_H_ |
29 | 29 |
30 #include <time.h> | 30 #include <time.h> |
31 | 31 |
32 #include <iosfwd> | 32 #include <iosfwd> |
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
632 } | 632 } |
633 | 633 |
634 // For logging use only. | 634 // For logging use only. |
635 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); | 635 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); |
636 | 636 |
637 // TimeTicks ------------------------------------------------------------------ | 637 // TimeTicks ------------------------------------------------------------------ |
638 | 638 |
639 // Represents monotonically non-decreasing clock time. | 639 // Represents monotonically non-decreasing clock time. |
640 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { | 640 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { |
641 public: | 641 public: |
642 // We define this even without OS_CHROMEOS for seccomp sandbox testing. | |
643 #if defined(OS_LINUX) | |
644 // Force definition of the system trace clock; it is a chromeos-only api | |
645 // at the moment and surfacing it in the right place requires mucking | |
646 // with glibc et al. | |
647 static const clockid_t kClockSystemTrace = 11; | |
648 #endif | |
649 | |
650 TimeTicks() : TimeBase(0) { | 642 TimeTicks() : TimeBase(0) { |
651 } | 643 } |
652 | 644 |
653 // Platform-dependent tick count representing "right now." When | 645 // Platform-dependent tick count representing "right now." When |
654 // IsHighResolution() returns false, the resolution of the clock could be | 646 // IsHighResolution() returns false, the resolution of the clock could be |
655 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one | 647 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one |
656 // microsecond. | 648 // microsecond. |
657 static TimeTicks Now(); | 649 static TimeTicks Now(); |
658 | 650 |
659 // Returns true if the high resolution clock is working on this system and | 651 // Returns true if the high resolution clock is working on this system and |
660 // Now() will return high resolution values. Note that, on systems where the | 652 // Now() will return high resolution values. Note that, on systems where the |
661 // high resolution clock works but is deemed inefficient, the low resolution | 653 // high resolution clock works but is deemed inefficient, the low resolution |
662 // clock will be used instead. | 654 // clock will be used instead. |
663 static bool IsHighResolution(); | 655 static bool IsHighResolution(); |
664 | 656 |
665 // Returns true if ThreadNow() is supported on this system. | |
666 static bool IsThreadNowSupported() { | |
667 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ | |
668 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) | |
669 return true; | |
670 #else | |
671 return false; | |
672 #endif | |
673 } | |
674 | |
675 // Returns thread-specific CPU-time on systems that support this feature. | |
676 // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer | |
677 // to (approximately) measure how much time the calling thread spent doing | |
678 // actual work vs. being de-scheduled. May return bogus results if the thread | |
679 // migrates to another CPU between two calls. | |
680 // | |
681 // WARNING: The returned value might NOT have the same origin as Now(). Do not | |
682 // perform math between TimeTicks values returned by Now() and ThreadNow() and | |
683 // expect meaningful results. | |
684 // TODO(miu): Since the timeline of these values is different, the values | |
685 // should be of a different type. | |
686 static TimeTicks ThreadNow(); | |
687 | |
688 // Returns the current system trace time or, if not available on this | |
689 // platform, a high-resolution time value; or a low-resolution time value if | |
690 // neither are avalable. On systems where a global trace clock is defined, | |
691 // timestamping TraceEvents's with this value guarantees synchronization | |
692 // between events collected inside chrome and events collected outside | |
693 // (e.g. kernel, X server). | |
694 // | |
695 // WARNING: The returned value might NOT have the same origin as Now(). Do not | |
696 // perform math between TimeTicks values returned by Now() and | |
697 // NowFromSystemTraceTime() and expect meaningful results. | |
698 // TODO(miu): Since the timeline of these values is different, the values | |
699 // should be of a different type. | |
700 static TimeTicks NowFromSystemTraceTime(); | |
701 | |
702 #if defined(OS_WIN) | 657 #if defined(OS_WIN) |
703 // Translates an absolute QPC timestamp into a TimeTicks value. The returned | 658 // Translates an absolute QPC timestamp into a TimeTicks value. The returned |
704 // value has the same origin as Now(). Do NOT attempt to use this if | 659 // value has the same origin as Now(). Do NOT attempt to use this if |
705 // IsHighResolution() returns false. | 660 // IsHighResolution() returns false. |
706 static TimeTicks FromQPCValue(LONGLONG qpc_value); | 661 static TimeTicks FromQPCValue(LONGLONG qpc_value); |
707 #endif | 662 #endif |
708 | 663 |
709 // Get the TimeTick value at the time of the UnixEpoch. This is useful when | 664 // Get the TimeTick value at the time of the UnixEpoch. This is useful when |
710 // you need to relate the value of TimeTicks to a real time and date. | 665 // you need to relate the value of TimeTicks to a real time and date. |
711 // Note: Upon first invocation, this function takes a snapshot of the realtime | 666 // Note: Upon first invocation, this function takes a snapshot of the realtime |
(...skipping 19 matching lines...) Expand all Loading... | |
731 | 686 |
732 // Please use Now() to create a new object. This is for internal use | 687 // Please use Now() to create a new object. This is for internal use |
733 // and testing. | 688 // and testing. |
734 explicit TimeTicks(int64 us) : TimeBase(us) { | 689 explicit TimeTicks(int64 us) : TimeBase(us) { |
735 } | 690 } |
736 }; | 691 }; |
737 | 692 |
738 // For logging use only. | 693 // For logging use only. |
739 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); | 694 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); |
740 | 695 |
696 // ThreadTicks ---------------------------------------------------------------- | |
697 | |
698 // Represents a clock, specific to a particular thread, than runs only while the | |
699 // thread is running. | |
700 class BASE_EXPORT ThreadTicks : public time_internal::TimeBase<ThreadTicks> { | |
701 public: | |
702 ThreadTicks() : TimeBase(0) { | |
703 } | |
704 | |
705 // Returns true if ThreadTicks::Now() is supported on this system. | |
706 static bool IsSupported() { | |
707 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ | |
708 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) | |
709 return true; | |
710 #else | |
711 return false; | |
712 #endif | |
713 } | |
714 | |
715 // Returns thread-specific CPU-time on systems that support this feature. | |
716 // Needs to be guarded with a call to IsSupported(). Use this timer | |
717 // to (approximately) measure how much time the calling thread spent doing | |
718 // actual work vs. being de-scheduled. May return bogus results if the thread | |
719 // migrates to another CPU between two calls. | |
720 static ThreadTicks Now(); | |
721 | |
722 private: | |
723 friend class time_internal::TimeBase<ThreadTicks>; | |
724 | |
725 // Please use Now() to create a new object. This is for internal use | |
726 // and testing. | |
727 explicit ThreadTicks(int64 us) : TimeBase(us) { | |
728 } | |
729 }; | |
730 | |
731 // For logging use only. | |
732 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); | |
733 | |
734 // TraceTicks ---------------------------------------------------------------- | |
735 | |
736 // Represents high-resolution system trace clock time. | |
737 class BASE_EXPORT TraceTicks : public time_internal::TimeBase<TraceTicks> { | |
738 public: | |
739 // We define this even without OS_CHROMEOS for seccomp sandbox testing. | |
740 #if defined(OS_LINUX) | |
741 // Force definition of the system trace clock; it is a chromeos-only api | |
742 // at the moment and surfacing it in the right place requires mucking | |
743 // with glibc et al. | |
744 static const clockid_t kClockSystemTrace = 11; | |
745 #endif | |
746 | |
747 TraceTicks() : TimeBase(0) { | |
748 } | |
749 | |
750 // Returns the current system trace time or, if not available on this | |
751 // platform, a high-resolution time value; or a low-resolution time value if | |
752 // neither are avalable. On systems where a global trace clock is defined, | |
753 // timestamping TraceEvents's with this value guarantees synchronization | |
754 // between events collected inside chrome and events collected outside | |
755 // (e.g. kernel, X server). | |
756 static TraceTicks Now(); | |
757 | |
758 private: | |
759 friend class time_internal::TimeBase<TraceTicks>; | |
760 | |
761 // Please use Now() to create a new object. This is for internal use | |
762 // and testing. | |
763 explicit TraceTicks(int64 us) : TimeBase(us) { | |
764 } | |
765 }; | |
766 | |
767 // For logging use only. | |
768 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TraceTicks time_ticks); | |
769 | |
741 } // namespace base | 770 } // namespace base |
742 | 771 |
743 #endif // BASE_TIME_TIME_H_ | 772 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |