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

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: 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_unittest.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 11 matching lines...) Expand all
22 // 22 //
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 <cstdlib>
32 #include <iosfwd> 33 #include <iosfwd>
33 34
34 #include "base/base_export.h" 35 #include "base/base_export.h"
35 #include "base/basictypes.h" 36 #include "base/basictypes.h"
36 #include "build/build_config.h" 37 #include "build/build_config.h"
37 38
38 #if defined(OS_MACOSX) 39 #if defined(OS_MACOSX)
39 #include <CoreFoundation/CoreFoundation.h> 40 #include <CoreFoundation/CoreFoundation.h>
40 // Avoid Mac system header macro leak. 41 // Avoid Mac system header macro leak.
41 #undef TYPE_BOOL 42 #undef TYPE_BOOL
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 static TimeDelta Max(); 94 static TimeDelta Max();
94 95
95 // Returns the internal numeric value of the TimeDelta object. Please don't 96 // 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 97 // use this and do arithmetic on it, as it is more error prone than using the
97 // provided operators. 98 // provided operators.
98 // For serializing, use FromInternalValue to reconstitute. 99 // For serializing, use FromInternalValue to reconstitute.
99 int64 ToInternalValue() const { 100 int64 ToInternalValue() const {
100 return delta_; 101 return delta_;
101 } 102 }
102 103
104 // Returns true if this TimeDelta is negative.
105 bool is_negative() const {
brianderson 2015/01/07 00:53:43 This is not used and appears unrelated. If the int
miu 2015/01/07 05:22:45 Ah. Yes, it was used in an earlier iteration of t
106 return delta_ < 0;
107 }
108
109 // Returns the magnitude (absolute value) of this TimeDelta.
110 TimeDelta magnitude() const {
brianderson 2015/01/07 00:53:43 This is only used for testing. Do you plan to use
miu 2015/01/07 05:22:45 Yes. I've personally written a lot of code in src
111 return TimeDelta(std::abs(delta_));
112 }
113
103 // Returns true if the time delta is the maximum time delta. 114 // Returns true if the time delta is the maximum time delta.
104 bool is_max() const { 115 bool is_max() const {
105 return delta_ == std::numeric_limits<int64>::max(); 116 return delta_ == std::numeric_limits<int64>::max();
106 } 117 }
107 118
108 #if defined(OS_POSIX) 119 #if defined(OS_POSIX)
109 struct timespec ToTimeSpec() const; 120 struct timespec ToTimeSpec() const;
110 #endif 121 #endif
111 122
112 // Returns the time delta in some unit. The F versions return a floating 123 // 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) 592 #if defined(OS_LINUX)
582 // Force definition of the system trace clock; it is a chromeos-only api 593 // 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 594 // at the moment and surfacing it in the right place requires mucking
584 // with glibc et al. 595 // with glibc et al.
585 static const clockid_t kClockSystemTrace = 11; 596 static const clockid_t kClockSystemTrace = 11;
586 #endif 597 #endif
587 598
588 TimeTicks() : ticks_(0) { 599 TimeTicks() : ticks_(0) {
589 } 600 }
590 601
591 // Platform-dependent tick count representing "right now." 602 // Platform-dependent tick count representing "right now." When
592 // The resolution of this clock is ~1-15ms. Resolution varies depending 603 // IsHighResolution() returns false, the resolution of the clock could be
593 // on hardware/operating system configuration. 604 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one
605 // microsecond.
594 static TimeTicks Now(); 606 static TimeTicks Now();
595 607
596 // Returns a platform-dependent high-resolution tick count. Implementation 608 // DEPRECATED
597 // is hardware dependent and may or may not return sub-millisecond 609 // TODO(miu): Remove this function, and all callpoints should call Now().
598 // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND 610 static TimeTicks HighResNow() { return TimeTicks::Now(); }
599 // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
600 static TimeTicks HighResNow();
601 611
602 static bool IsHighResNowFastAndReliable(); 612 // Returns true if the high resolution clock is working on this system and
613 // Now() will return high resolution values. Note that, on systems where the
614 // high resolution clock works but is deemed inefficient, the low resolution
615 // clock will be used instead.
616 static bool IsHighResolution();
617
618 // DEPRECATED
619 // TODO(miu): Remove this function, and all callpoints should call
620 // IsHighResolution().
621 static bool IsHighResNowFastAndReliable() { return IsHighResolution(); }
603 622
604 // Returns true if ThreadNow() is supported on this system. 623 // Returns true if ThreadNow() is supported on this system.
605 static bool IsThreadNowSupported() { 624 static bool IsThreadNowSupported() {
606 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ 625 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
607 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) 626 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
608 return true; 627 return true;
609 #else 628 #else
610 return false; 629 return false;
611 #endif 630 #endif
612 } 631 }
613 632
614 // Returns thread-specific CPU-time on systems that support this feature. 633 // Returns thread-specific CPU-time on systems that support this feature.
615 // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer 634 // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer
616 // to (approximately) measure how much time the calling thread spent doing 635 // 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 636 // actual work vs. being de-scheduled. May return bogus results if the thread
618 // migrates to another CPU between two calls. 637 // migrates to another CPU between two calls.
638 //
639 // WARNING: The returned value might NOT have the same origin as Now(). Do not
640 // perform math with TimeTicks values returned by Now() and expect meaningful
641 // results.
642 // TODO(miu): Since the timeline of these values is different, the values
643 // should be of a different type.
619 static TimeTicks ThreadNow(); 644 static TimeTicks ThreadNow();
620 645
621 // Returns the current system trace time or, if none is defined, the current 646 // 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 647 // platform, a high-resolution time value; or a low-resolution time value if
623 // is defined, timestamping TraceEvents's with this value guarantees 648 // neither are avalable. On systems where a global trace clock is defined,
624 // synchronization between events collected inside chrome and events 649 // timestamping TraceEvents's with this value guarantees synchronization
625 // collected outside (e.g. kernel, X server). 650 // between events collected inside chrome and events collected outside
651 // (e.g. kernel, X server).
652 //
653 // WARNING: The returned value might NOT have the same origin as Now(). Do not
654 // perform math with TimeTicks values returned by Now() and expect meaningful
655 // results.
656 // TODO(miu): Since the timeline of these values is different, the values
657 // should be of a different type.
626 static TimeTicks NowFromSystemTraceTime(); 658 static TimeTicks NowFromSystemTraceTime();
627 659
628 #if defined(OS_WIN) 660 #if defined(OS_WIN)
629 // Get the absolute value of QPC time drift. For testing. 661 // Translates an absolute QPC timestamp into a TimeTicks value. The returned
630 static int64 GetQPCDriftMicroseconds(); 662 // value has the same origin as Now(). Do NOT attempt to use this if
631 663 // IsHighResolution() returns false.
632 static TimeTicks FromQPCValue(LONGLONG qpc_value); 664 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 665 #endif
638 666
639 // Returns true if this object has not been initialized. 667 // Returns true if this object has not been initialized.
640 bool is_null() const { 668 bool is_null() const {
641 return ticks_ == 0; 669 return ticks_ == 0;
642 } 670 }
643 671
644 // Converts an integer value representing TimeTicks to a class. This is used 672 // Converts an integer value representing TimeTicks to a class. This is used
645 // when deserializing a |TimeTicks| structure, using a value known to be 673 // when deserializing a |TimeTicks| structure, using a value known to be
646 // compatible. It is not provided as a constructor because the integer type 674 // 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 { 759 inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
732 return TimeTicks(t.ticks_ + delta_); 760 return TimeTicks(t.ticks_ + delta_);
733 } 761 }
734 762
735 // For logging use only. 763 // For logging use only.
736 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); 764 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks);
737 765
738 } // namespace base 766 } // namespace base
739 767
740 #endif // BASE_TIME_TIME_H_ 768 #endif // BASE_TIME_TIME_H_
OLDNEW
« no previous file with comments | « no previous file | base/time/time_mac.cc » ('j') | base/time/time_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698