Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: base/time/time.h

Issue 797893003: [Windows] One TimeTicks clock: efficient/reliable high-res, with low-res fallback. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Resolve lack of std::abs(int64) overload on some toolchains. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/time/time_mac.cc » ('j') | base/time/time_win.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 true if this TimeDelta is negative.
104 bool is_negative() const {
105 return delta_ < 0;
106 }
107
108 // Returns the magnitude (absolute value) of this TimeDelta.
109 TimeDelta magnitude() const {
110 // Some toolchains provide an incomplete C++11 implementation and lack an
111 // int64 overload for std::abs(). The following is a simple branchless
112 // implementation:
113 const int64 mask = delta_ >> (sizeof(delta_) * 8 - 1);
114 return TimeDelta((delta_ + mask) ^ mask);
115 }
116
103 // Returns true if the time delta is the maximum time delta. 117 // Returns true if the time delta is the maximum time delta.
104 bool is_max() const { 118 bool is_max() const {
105 return delta_ == std::numeric_limits<int64>::max(); 119 return delta_ == std::numeric_limits<int64>::max();
106 } 120 }
107 121
108 #if defined(OS_POSIX) 122 #if defined(OS_POSIX)
109 struct timespec ToTimeSpec() const; 123 struct timespec ToTimeSpec() const;
110 #endif 124 #endif
111 125
112 // Returns the time delta in some unit. The F versions return a floating 126 // 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
581 #if defined(OS_LINUX) 595 #if defined(OS_LINUX)
582 // Force definition of the system trace clock; it is a chromeos-only api 596 // 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 597 // at the moment and surfacing it in the right place requires mucking
584 // with glibc et al. 598 // with glibc et al.
585 static const clockid_t kClockSystemTrace = 11; 599 static const clockid_t kClockSystemTrace = 11;
586 #endif 600 #endif
587 601
588 TimeTicks() : ticks_(0) { 602 TimeTicks() : ticks_(0) {
589 } 603 }
590 604
591 // Platform-dependent tick count representing "right now." 605 // Platform-dependent tick count representing "right now." When
592 // The resolution of this clock is ~1-15ms. Resolution varies depending 606 // IsHighResolution() returns false, the resolution of the clock could be
593 // on hardware/operating system configuration. 607 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one
608 // microsecond.
594 static TimeTicks Now(); 609 static TimeTicks Now();
595 610
596 // Returns a platform-dependent high-resolution tick count. Implementation 611 // DEPRECATED
597 // is hardware dependent and may or may not return sub-millisecond 612 // TODO(miu): Remove this function, and all callpoints should call Now().
598 // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND 613 static TimeTicks HighResNow() { return TimeTicks::Now(); }
599 // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
600 static TimeTicks HighResNow();
601 614
602 static bool IsHighResNowFastAndReliable(); 615 // Returns true if the high resolution clock is working on this system and
616 // Now() will return high resolution values. Note that, on systems where the
617 // high resolution clock works but is deemed inefficient, the low resolution
618 // clock will be used instead.
619 static bool IsHighResolution();
620
621 // DEPRECATED
622 // TODO(miu): Remove this function, and all callpoints should call
623 // IsHighResolution().
624 static bool IsHighResNowFastAndReliable() { return IsHighResolution(); }
603 625
604 // Returns true if ThreadNow() is supported on this system. 626 // Returns true if ThreadNow() is supported on this system.
605 static bool IsThreadNowSupported() { 627 static bool IsThreadNowSupported() {
606 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ 628 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
607 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) 629 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
608 return true; 630 return true;
609 #else 631 #else
610 return false; 632 return false;
611 #endif 633 #endif
612 } 634 }
613 635
614 // Returns thread-specific CPU-time on systems that support this feature. 636 // Returns thread-specific CPU-time on systems that support this feature.
615 // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer 637 // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer
616 // to (approximately) measure how much time the calling thread spent doing 638 // 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 639 // actual work vs. being de-scheduled. May return bogus results if the thread
618 // migrates to another CPU between two calls. 640 // migrates to another CPU between two calls.
641 //
642 // WARNING: The returned value might NOT have the same origin as Now(). Do not
643 // perform math with TimeTicks values returned by Now() and expect meaningful
brucedawson 2015/01/07 01:49:26 Improve comment clarity? < perform math with Time
miu 2015/01/07 05:22:45 Done.
644 // results.
645 // TODO(miu): Since the timeline of these values is different, the values
646 // should be of a different type.
619 static TimeTicks ThreadNow(); 647 static TimeTicks ThreadNow();
620 648
621 // Returns the current system trace time or, if none is defined, the current 649 // 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 650 // platform, a high-resolution time value; or a low-resolution time value if
623 // is defined, timestamping TraceEvents's with this value guarantees 651 // neither are avalable. On systems where a global trace clock is defined,
624 // synchronization between events collected inside chrome and events 652 // timestamping TraceEvents's with this value guarantees synchronization
625 // collected outside (e.g. kernel, X server). 653 // between events collected inside chrome and events collected outside
654 // (e.g. kernel, X server).
655 //
656 // WARNING: The returned value might NOT have the same origin as Now(). Do not
657 // perform math with TimeTicks values returned by Now() and expect meaningful
brucedawson 2015/01/07 01:49:26 Same comment clarification applies.
miu 2015/01/07 05:22:45 Done.
658 // results.
659 // TODO(miu): Since the timeline of these values is different, the values
660 // should be of a different type.
626 static TimeTicks NowFromSystemTraceTime(); 661 static TimeTicks NowFromSystemTraceTime();
627 662
628 #if defined(OS_WIN) 663 #if defined(OS_WIN)
629 // Get the absolute value of QPC time drift. For testing. 664 // Translates an absolute QPC timestamp into a TimeTicks value. The returned
630 static int64 GetQPCDriftMicroseconds(); 665 // value has the same origin as Now(). Do NOT attempt to use this if
631 666 // IsHighResolution() returns false.
632 static TimeTicks FromQPCValue(LONGLONG qpc_value); 667 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 668 #endif
638 669
639 // Returns true if this object has not been initialized. 670 // Returns true if this object has not been initialized.
640 bool is_null() const { 671 bool is_null() const {
641 return ticks_ == 0; 672 return ticks_ == 0;
642 } 673 }
643 674
644 // Converts an integer value representing TimeTicks to a class. This is used 675 // Converts an integer value representing TimeTicks to a class. This is used
645 // when deserializing a |TimeTicks| structure, using a value known to be 676 // when deserializing a |TimeTicks| structure, using a value known to be
646 // compatible. It is not provided as a constructor because the integer type 677 // compatible. It is not provided as a constructor because the integer type
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 inline TimeTicks TimeDelta::operator+(TimeTicks t) const { 762 inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
732 return TimeTicks(t.ticks_ + delta_); 763 return TimeTicks(t.ticks_ + delta_);
733 } 764 }
734 765
735 // For logging use only. 766 // For logging use only.
736 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); 767 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks);
737 768
738 } // namespace base 769 } // namespace base
739 770
740 #endif // BASE_TIME_TIME_H_ 771 #endif // BASE_TIME_TIME_H_
OLDNEW
« no previous file with comments | « no previous file | base/time/time_mac.cc » ('j') | base/time/time_win.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698