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 |
cpu_(ooo_6.6-7.5)
2015/05/19 19:12:02
that bug has been fixed
miu
2015/05/20 21:58:46
Done.
| |
8 // clock interface routines are defined in time_PLATFORM.cc. | 8 // clock interface routines are defined in time_PLATFORM.cc. Note that values |
9 // for Time may skew and jump around as the operating system makes adjustments | |
10 // to synchronize (e.g., with NTP servers). Thus, client code that uses the Time | |
11 // class must account for this. | |
9 // | 12 // |
10 // TimeDelta represents a duration of time, internally represented in | 13 // TimeDelta represents a duration of time, internally represented in |
11 // microseconds. | 14 // microseconds. |
12 // | 15 // |
13 // TimeTicks represents an abstract time that is most of the time incrementing | 16 // TimeTicks, ThreadTicks, and TraceTicks represent an abstract time that is |
14 // for use in measuring time durations. It is internally represented in | 17 // most of the time incrementing, for use in measuring time |
15 // microseconds. It can not be converted to a human-readable time, but is | 18 // durations. Internally, they are represented in microseconds. They can not be |
16 // guaranteed not to decrease (if the user changes the computer clock, | 19 // converted to a human-readable time, but are guaranteed not to decrease |
17 // Time::Now() may actually decrease or jump). But note that TimeTicks may | 20 // (unlike the Time class). Note that TimeTicks may "stand still" (e.g., if the |
18 // "stand still", for example if the computer suspended. | 21 // computer is suspended), and ThreadTicks will "stand still" whenever the |
22 // thread has been de-scheduled by the operating system. | |
19 // | 23 // |
20 // These classes are represented as only a 64-bit value, so they can be | 24 // All time classes are copyable, assignable, and occupy 64-bits per |
21 // efficiently passed by value. | 25 // instance. Thus, they can be efficiently passed by-value (as opposed to |
cpu_(ooo_6.6-7.5)
2015/05/21 00:47:34
all signed 64 ints?
| |
26 // by-reference). | |
22 // | 27 // |
23 // Definitions of operator<< are provided to make these types work with | 28 // Definitions of operator<< are provided to make these types work with |
24 // DCHECK_EQ() and other log macros. For human-readable formatting, see | 29 // DCHECK_EQ() and other log macros. For human-readable formatting, see |
25 // "base/i18n/time_formatting.h". | 30 // "base/i18n/time_formatting.h". |
31 // | |
32 // So many choices! Which time class should you use? Examples: | |
cpu_(ooo_6.6-7.5)
2015/05/19 19:12:01
also a table of OS support and resolution would be
miu
2015/05/20 21:58:46
I'm not sure how to build such a table. There's a
cpu_(ooo_6.6-7.5)
2015/05/21 00:47:34
Exactly my point. If you split 1 class in 3 and th
miu
2015/05/22 23:01:03
Okay, so, I think we have different perspectives o
| |
33 // | |
34 // Time: Interpreting the wall-clock time provided by a remote | |
35 // system. Detecting whether cached resources have | |
36 // expired. Providing the user with a display of the current date | |
37 // and time. Determining the amount of time between events across | |
38 // re-boots of the machine. | |
39 // | |
40 // TimeTicks: Tracking the amount of time a task runs. Executing delayed | |
41 // tasks at the right time. Computing presentation timestamps. | |
42 // Synchronizing audio and video using TimeTicks as a common | |
43 // reference clock (lip-sync). Measuring network round-trip | |
44 // latency. | |
45 // | |
46 // ThreadTicks: Benchmarking how long the current thread has been doing actual | |
47 // work. | |
48 // | |
49 // TraceTicks: Measuring the real-world passage of time, but also allowing | |
50 // events to be correlated with those from other applications and | |
51 // services on the same system. Generally, this is only used | |
52 // directly by the event tracing infrastructure. | |
cpu_(ooo_6.6-7.5)
2015/05/19 19:12:02
Why expose TraceTicks here then? lets make it par
miu
2015/05/20 21:58:46
There are some code modules (mostly in GPU and com
cpu_(ooo_6.6-7.5)
2015/05/21 00:47:34
I follow on CL is fine with me but I will pest you
miu
2015/05/22 23:01:03
Acknowledged.
| |
26 | 53 |
27 #ifndef BASE_TIME_TIME_H_ | 54 #ifndef BASE_TIME_TIME_H_ |
28 #define BASE_TIME_TIME_H_ | 55 #define BASE_TIME_TIME_H_ |
29 | 56 |
30 #include <time.h> | 57 #include <time.h> |
31 | 58 |
32 #include <iosfwd> | 59 #include <iosfwd> |
33 | 60 |
34 #include "base/base_export.h" | 61 #include "base/base_export.h" |
35 #include "base/basictypes.h" | 62 #include "base/basictypes.h" |
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
632 } | 659 } |
633 | 660 |
634 // For logging use only. | 661 // For logging use only. |
635 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); | 662 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); |
636 | 663 |
637 // TimeTicks ------------------------------------------------------------------ | 664 // TimeTicks ------------------------------------------------------------------ |
638 | 665 |
639 // Represents monotonically non-decreasing clock time. | 666 // Represents monotonically non-decreasing clock time. |
640 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { | 667 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { |
641 public: | 668 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) { | 669 TimeTicks() : TimeBase(0) { |
651 } | 670 } |
652 | 671 |
653 // Platform-dependent tick count representing "right now." When | 672 // Platform-dependent tick count representing "right now." When |
654 // IsHighResolution() returns false, the resolution of the clock could be | 673 // 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 | 674 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one |
656 // microsecond. | 675 // microsecond. |
657 static TimeTicks Now(); | 676 static TimeTicks Now(); |
658 | 677 |
659 // Returns true if the high resolution clock is working on this system and | 678 // 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 | 679 // Now() will return high resolution values. Note that, on systems where the |
661 // high resolution clock works but is deemed inefficient, the low resolution | 680 // high resolution clock works but is deemed inefficient, the low resolution |
662 // clock will be used instead. | 681 // clock will be used instead. |
663 static bool IsHighResolution(); | 682 static bool IsHighResolution(); |
664 | 683 |
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) | 684 #if defined(OS_WIN) |
703 // Translates an absolute QPC timestamp into a TimeTicks value. The returned | 685 // 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 | 686 // value has the same origin as Now(). Do NOT attempt to use this if |
705 // IsHighResolution() returns false. | 687 // IsHighResolution() returns false. |
706 static TimeTicks FromQPCValue(LONGLONG qpc_value); | 688 static TimeTicks FromQPCValue(LONGLONG qpc_value); |
707 #endif | 689 #endif |
708 | 690 |
709 // Get the TimeTick value at the time of the UnixEpoch. This is useful when | 691 // 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. | 692 // 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 | 693 // Note: Upon first invocation, this function takes a snapshot of the realtime |
(...skipping 19 matching lines...) Expand all Loading... | |
731 | 713 |
732 // Please use Now() to create a new object. This is for internal use | 714 // Please use Now() to create a new object. This is for internal use |
733 // and testing. | 715 // and testing. |
734 explicit TimeTicks(int64 us) : TimeBase(us) { | 716 explicit TimeTicks(int64 us) : TimeBase(us) { |
735 } | 717 } |
736 }; | 718 }; |
737 | 719 |
738 // For logging use only. | 720 // For logging use only. |
739 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); | 721 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); |
740 | 722 |
723 // ThreadTicks ---------------------------------------------------------------- | |
724 | |
725 // Represents a clock, specific to a particular thread, than runs only while the | |
726 // thread is running. | |
727 class BASE_EXPORT ThreadTicks : public time_internal::TimeBase<ThreadTicks> { | |
728 public: | |
729 ThreadTicks() : TimeBase(0) { | |
730 } | |
731 | |
732 // Returns true if ThreadTicks::Now() is supported on this system. | |
733 static bool IsSupported() { | |
734 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ | |
735 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) | |
736 return true; | |
737 #else | |
738 return false; | |
739 #endif | |
740 } | |
741 | |
742 // Returns thread-specific CPU-time on systems that support this feature. | |
743 // Needs to be guarded with a call to IsSupported(). Use this timer | |
744 // to (approximately) measure how much time the calling thread spent doing | |
745 // actual work vs. being de-scheduled. May return bogus results if the thread | |
746 // migrates to another CPU between two calls. | |
747 static ThreadTicks Now(); | |
748 | |
749 private: | |
750 friend class time_internal::TimeBase<ThreadTicks>; | |
751 | |
752 // Please use Now() to create a new object. This is for internal use | |
753 // and testing. | |
754 explicit ThreadTicks(int64 us) : TimeBase(us) { | |
755 } | |
756 }; | |
757 | |
758 // For logging use only. | |
759 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); | |
760 | |
761 // TraceTicks ---------------------------------------------------------------- | |
762 | |
763 // Represents high-resolution system trace clock time. | |
764 class BASE_EXPORT TraceTicks : public time_internal::TimeBase<TraceTicks> { | |
765 public: | |
766 // We define this even without OS_CHROMEOS for seccomp sandbox testing. | |
767 #if defined(OS_LINUX) | |
768 // Force definition of the system trace clock; it is a chromeos-only api | |
769 // at the moment and surfacing it in the right place requires mucking | |
770 // with glibc et al. | |
771 static const clockid_t kClockSystemTrace = 11; | |
772 #endif | |
773 | |
774 TraceTicks() : TimeBase(0) { | |
775 } | |
776 | |
777 // Returns the current system trace time or, if not available on this | |
778 // platform, a high-resolution time value; or a low-resolution time value if | |
779 // neither are avalable. On systems where a global trace clock is defined, | |
cpu_(ooo_6.6-7.5)
2015/05/19 19:12:01
on windows for example it is not clear, it seems i
miu
2015/05/20 21:58:46
That is correct. BTW--I didn't modify this commen
cpu_(ooo_6.6-7.5)
2015/05/21 00:47:34
Just basically say that: On windows things are com
miu
2015/05/22 23:01:03
Ah. Okay. Done! :)
| |
780 // timestamping TraceEvents's with this value guarantees synchronization | |
781 // between events collected inside chrome and events collected outside | |
782 // (e.g. kernel, X server). | |
783 static TraceTicks Now(); | |
784 | |
785 private: | |
786 friend class time_internal::TimeBase<TraceTicks>; | |
787 | |
788 // Please use Now() to create a new object. This is for internal use | |
789 // and testing. | |
790 explicit TraceTicks(int64 us) : TimeBase(us) { | |
791 } | |
792 }; | |
793 | |
794 // For logging use only. | |
795 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TraceTicks time_ticks); | |
796 | |
741 } // namespace base | 797 } // namespace base |
742 | 798 |
743 #endif // BASE_TIME_TIME_H_ | 799 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |