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
3 // found in the LICENSE file.
5 // Time represents an absolute point in coordinated universal time (UTC),
6 // internally represented as microseconds (s/1,000,000) since the Windows epoch
7 // (1601-01-01 00:00:00 UTC). System-dependent clock interface routines are
8 // defined in time_PLATFORM.cc. Note that values for Time may skew and jump
9 // around as the operating system makes adjustments to synchronize (e.g., with
10 // NTP servers). Thus, client code that uses the Time class must account for
13 // TimeDelta represents a duration of time, internally represented in
16 // TimeTicks, ThreadTicks, and TraceTicks represent an abstract time that is
17 // most of the time incrementing, for use in measuring time durations.
18 // Internally, they are represented in microseconds. They can not be converted
19 // to a human-readable time, but are guaranteed not to decrease (unlike the Time
20 // class). Note that TimeTicks may "stand still" (e.g., if the computer is
21 // suspended), and ThreadTicks will "stand still" whenever the thread has been
22 // de-scheduled by the operating system.
24 // All time classes are copyable, assignable, and occupy 64-bits per
25 // instance. Thus, they can be efficiently passed by-value (as opposed to
28 // Definitions of operator<< are provided to make these types work with
29 // DCHECK_EQ() and other log macros. For human-readable formatting, see
30 // "base/i18n/time_formatting.h".
32 // So many choices! Which time class should you use? Examples:
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.
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
46 // ThreadTicks: Benchmarking how long the current thread has been doing actual
49 // TraceTicks: This is only meant to be used by the event tracing
50 // infrastructure, and by outside code modules in special
51 // circumstances. Please be sure to consult a
52 // base/trace_event/OWNER before committing any new code that
55 #ifndef BASE_TIME_TIME_H_
56 #define BASE_TIME_TIME_H_
62 #include "base/base_export.h"
63 #include "base/basictypes.h"
64 #include "base/numerics/safe_math.h"
65 #include "build/build_config.h"
67 #if defined(OS_MACOSX)
68 #include <CoreFoundation/CoreFoundation.h>
69 // Avoid Mac system header macro leak.
79 // For FILETIME in FromFileTime, until it moves to a new converter class.
80 // See TODO(iyengar) below.
90 // The functions in the time_internal namespace are meant to be used only by the
91 // time classes and functions. Please use the math operators defined in the
92 // time classes instead.
93 namespace time_internal
{
95 // Add or subtract |value| from a TimeDelta. The int64 argument and return value
96 // are in terms of a microsecond timebase.
97 BASE_EXPORT int64
SaturatedAdd(TimeDelta delta
, int64 value
);
98 BASE_EXPORT int64
SaturatedSub(TimeDelta delta
, int64 value
);
100 // Clamp |value| on overflow and underflow conditions. The int64 argument and
101 // return value are in terms of a microsecond timebase.
102 BASE_EXPORT int64
FromCheckedNumeric(const CheckedNumeric
<int64
> value
);
104 } // namespace time_internal
106 // TimeDelta ------------------------------------------------------------------
108 class BASE_EXPORT TimeDelta
{
110 TimeDelta() : delta_(0) {
113 // Converts units of time to TimeDeltas.
114 static TimeDelta
FromDays(int days
);
115 static TimeDelta
FromHours(int hours
);
116 static TimeDelta
FromMinutes(int minutes
);
117 static TimeDelta
FromSeconds(int64 secs
);
118 static TimeDelta
FromMilliseconds(int64 ms
);
119 static TimeDelta
FromSecondsD(double secs
);
120 static TimeDelta
FromMillisecondsD(double ms
);
121 static TimeDelta
FromMicroseconds(int64 us
);
123 static TimeDelta
FromQPCValue(LONGLONG qpc_value
);
126 // Converts an integer value representing TimeDelta to a class. This is used
127 // when deserializing a |TimeDelta| structure, using a value known to be
128 // compatible. It is not provided as a constructor because the integer type
129 // may be unclear from the perspective of a caller.
130 static TimeDelta
FromInternalValue(int64 delta
) {
131 return TimeDelta(delta
);
134 // Returns the maximum time delta, which should be greater than any reasonable
135 // time delta we might compare it to. Adding or subtracting the maximum time
136 // delta to a time or another time delta has an undefined result.
137 static TimeDelta
Max();
139 // Returns the internal numeric value of the TimeDelta object. Please don't
140 // use this and do arithmetic on it, as it is more error prone than using the
141 // provided operators.
142 // For serializing, use FromInternalValue to reconstitute.
143 int64
ToInternalValue() const {
147 // Returns the magnitude (absolute value) of this TimeDelta.
148 TimeDelta
magnitude() const {
149 // Some toolchains provide an incomplete C++11 implementation and lack an
150 // int64 overload for std::abs(). The following is a simple branchless
152 const int64 mask
= delta_
>> (sizeof(delta_
) * 8 - 1);
153 return TimeDelta((delta_
+ mask
) ^ mask
);
156 // Returns true if the time delta is zero.
157 bool is_zero() const {
161 // Returns true if the time delta is the maximum time delta.
162 bool is_max() const {
163 return delta_
== std::numeric_limits
<int64
>::max();
166 #if defined(OS_POSIX)
167 struct timespec
ToTimeSpec() const;
170 // Returns the time delta in some unit. The F versions return a floating
171 // point value, the "regular" versions return a rounded-down value.
173 // InMillisecondsRoundedUp() instead returns an integer that is rounded up
174 // to the next full millisecond.
177 int InMinutes() const;
178 double InSecondsF() const;
179 int64
InSeconds() const;
180 double InMillisecondsF() const;
181 int64
InMilliseconds() const;
182 int64
InMillisecondsRoundedUp() const;
183 int64
InMicroseconds() const;
185 TimeDelta
& operator=(TimeDelta other
) {
186 delta_
= other
.delta_
;
190 // Computations with other deltas.
191 TimeDelta
operator+(TimeDelta other
) const {
192 return TimeDelta(time_internal::SaturatedAdd(*this, other
.delta_
));
194 TimeDelta
operator-(TimeDelta other
) const {
195 return TimeDelta(time_internal::SaturatedSub(*this, other
.delta_
));
198 TimeDelta
& operator+=(TimeDelta other
) {
199 return *this = (*this + other
);
201 TimeDelta
& operator-=(TimeDelta other
) {
202 return *this = (*this - other
);
204 TimeDelta
operator-() const {
205 return TimeDelta(-delta_
);
208 // Computations with numeric types.
210 TimeDelta
operator*(T a
) const {
211 CheckedNumeric
<int64
> rv(delta_
);
213 return TimeDelta(time_internal::FromCheckedNumeric(rv
));
216 TimeDelta
operator/(T a
) const {
217 CheckedNumeric
<int64
> rv(delta_
);
219 return TimeDelta(time_internal::FromCheckedNumeric(rv
));
222 TimeDelta
& operator*=(T a
) {
223 return *this = (*this * a
);
226 TimeDelta
& operator/=(T a
) {
227 return *this = (*this / a
);
230 int64
operator/(TimeDelta a
) const {
231 return delta_
/ a
.delta_
;
233 TimeDelta
operator%(TimeDelta a
) const {
234 return TimeDelta(delta_
% a
.delta_
);
237 // Comparison operators.
238 bool operator==(TimeDelta other
) const {
239 return delta_
== other
.delta_
;
241 bool operator!=(TimeDelta other
) const {
242 return delta_
!= other
.delta_
;
244 bool operator<(TimeDelta other
) const {
245 return delta_
< other
.delta_
;
247 bool operator<=(TimeDelta other
) const {
248 return delta_
<= other
.delta_
;
250 bool operator>(TimeDelta other
) const {
251 return delta_
> other
.delta_
;
253 bool operator>=(TimeDelta other
) const {
254 return delta_
>= other
.delta_
;
258 friend int64
time_internal::SaturatedAdd(TimeDelta delta
, int64 value
);
259 friend int64
time_internal::SaturatedSub(TimeDelta delta
, int64 value
);
261 // Constructs a delta given the duration in microseconds. This is private
262 // to avoid confusion by callers with an integer constructor. Use
263 // FromSeconds, FromMilliseconds, etc. instead.
264 explicit TimeDelta(int64 delta_us
) : delta_(delta_us
) {
267 // Delta in microseconds.
272 inline TimeDelta
operator*(T a
, TimeDelta td
) {
276 // For logging use only.
277 BASE_EXPORT
std::ostream
& operator<<(std::ostream
& os
, TimeDelta time_delta
);
279 // Do not reference the time_internal::TimeBase template class directly. Please
280 // use one of the time subclasses instead, and only reference the public
281 // TimeBase members via those classes.
282 namespace time_internal
{
284 // TimeBase--------------------------------------------------------------------
286 // Provides value storage and comparison/math operations common to all time
287 // classes. Each subclass provides for strong type-checking to ensure
288 // semantically meaningful comparison/math of time values from the same clock
289 // source or timeline.
290 template<class TimeClass
>
293 static const int64 kHoursPerDay
= 24;
294 static const int64 kMillisecondsPerSecond
= 1000;
295 static const int64 kMillisecondsPerDay
= kMillisecondsPerSecond
* 60 * 60 *
297 static const int64 kMicrosecondsPerMillisecond
= 1000;
298 static const int64 kMicrosecondsPerSecond
= kMicrosecondsPerMillisecond
*
299 kMillisecondsPerSecond
;
300 static const int64 kMicrosecondsPerMinute
= kMicrosecondsPerSecond
* 60;
301 static const int64 kMicrosecondsPerHour
= kMicrosecondsPerMinute
* 60;
302 static const int64 kMicrosecondsPerDay
= kMicrosecondsPerHour
* kHoursPerDay
;
303 static const int64 kMicrosecondsPerWeek
= kMicrosecondsPerDay
* 7;
304 static const int64 kNanosecondsPerMicrosecond
= 1000;
305 static const int64 kNanosecondsPerSecond
= kNanosecondsPerMicrosecond
*
306 kMicrosecondsPerSecond
;
308 // Returns true if this object has not been initialized.
310 // Warning: Be careful when writing code that performs math on time values,
311 // since it's possible to produce a valid "zero" result that should not be
312 // interpreted as a "null" value.
313 bool is_null() const {
317 // Returns true if this object represents the maximum time.
318 bool is_max() const {
319 return us_
== std::numeric_limits
<int64
>::max();
322 // For serializing only. Use FromInternalValue() to reconstitute. Please don't
323 // use this and do arithmetic on it, as it is more error prone than using the
324 // provided operators.
325 int64
ToInternalValue() const {
329 TimeClass
& operator=(TimeClass other
) {
331 return *(static_cast<TimeClass
*>(this));
334 // Compute the difference between two times.
335 TimeDelta
operator-(TimeClass other
) const {
336 return TimeDelta::FromMicroseconds(us_
- other
.us_
);
339 // Return a new time modified by some delta.
340 TimeClass
operator+(TimeDelta delta
) const {
341 return TimeClass(time_internal::SaturatedAdd(delta
, us_
));
343 TimeClass
operator-(TimeDelta delta
) const {
344 return TimeClass(-time_internal::SaturatedSub(delta
, us_
));
347 // Modify by some time delta.
348 TimeClass
& operator+=(TimeDelta delta
) {
349 return static_cast<TimeClass
&>(*this = (*this + delta
));
351 TimeClass
& operator-=(TimeDelta delta
) {
352 return static_cast<TimeClass
&>(*this = (*this - delta
));
355 // Comparison operators
356 bool operator==(TimeClass other
) const {
357 return us_
== other
.us_
;
359 bool operator!=(TimeClass other
) const {
360 return us_
!= other
.us_
;
362 bool operator<(TimeClass other
) const {
363 return us_
< other
.us_
;
365 bool operator<=(TimeClass other
) const {
366 return us_
<= other
.us_
;
368 bool operator>(TimeClass other
) const {
369 return us_
> other
.us_
;
371 bool operator>=(TimeClass other
) const {
372 return us_
>= other
.us_
;
375 // Converts an integer value representing TimeClass to a class. This is used
376 // when deserializing a |TimeClass| structure, using a value known to be
377 // compatible. It is not provided as a constructor because the integer type
378 // may be unclear from the perspective of a caller.
379 static TimeClass
FromInternalValue(int64 us
) {
380 return TimeClass(us
);
384 explicit TimeBase(int64 us
) : us_(us
) {
387 // Time value in a microsecond timebase.
391 } // namespace time_internal
393 template<class TimeClass
>
394 inline TimeClass
operator+(TimeDelta delta
, TimeClass t
) {
398 // Time -----------------------------------------------------------------------
400 // Represents a wall clock time in UTC. Values are not guaranteed to be
401 // monotonically non-decreasing and are subject to large amounts of skew.
402 class BASE_EXPORT Time
: public time_internal::TimeBase
<Time
> {
404 // The representation of Jan 1, 1970 UTC in microseconds since the
405 // platform-dependent epoch.
406 static const int64 kTimeTToMicrosecondsOffset
;
409 // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to
410 // the Posix delta of 1970. This is used for migrating between the old
411 // 1970-based epochs to the new 1601-based ones. It should be removed from
412 // this global header and put in the platform-specific ones when we remove the
414 static const int64 kWindowsEpochDeltaMicroseconds
;
416 // To avoid overflow in QPC to Microseconds calculations, since we multiply
417 // by kMicrosecondsPerSecond, then the QPC value should not exceed
418 // (2^63 - 1) / 1E6. If it exceeds that threshold, we divide then multiply.
419 static const int64 kQPCOverflowThreshold
= 0x8637BD05AF7;
422 // Represents an exploded time that can be formatted nicely. This is kind of
423 // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few
424 // additions and changes to prevent errors.
425 struct BASE_EXPORT Exploded
{
426 int year
; // Four digit year "2007"
427 int month
; // 1-based month (values 1 = January, etc.)
428 int day_of_week
; // 0-based day of week (0 = Sunday, etc.)
429 int day_of_month
; // 1-based day of month (1-31)
430 int hour
; // Hour within the current day (0-23)
431 int minute
; // Minute within the current hour (0-59)
432 int second
; // Second within the current minute (0-59 plus leap
433 // seconds which may take it up to 60).
434 int millisecond
; // Milliseconds within the current second (0-999)
436 // A cursory test for whether the data members are within their
437 // respective ranges. A 'true' return value does not guarantee the
438 // Exploded value can be successfully converted to a Time value.
439 bool HasValidValues() const;
442 // Contains the NULL time. Use Time::Now() to get the current time.
443 Time() : TimeBase(0) {
446 // Returns the time for epoch in Unix-like system (Jan 1, 1970).
447 static Time
UnixEpoch();
449 // Returns the current time. Watch out, the system might adjust its clock
450 // in which case time will actually go backwards. We don't guarantee that
451 // times are increasing, or that two calls to Now() won't be the same.
454 // Returns the maximum time, which should be greater than any reasonable time
455 // with which we might compare it.
458 // Returns the current time. Same as Now() except that this function always
459 // uses system time so that there are no discrepancies between the returned
460 // time and system time even on virtual environments including our test bot.
461 // For timing sensitive unittests, this function should be used.
462 static Time
NowFromSystemTime();
464 // Converts to/from time_t in UTC and a Time class.
465 // TODO(brettw) this should be removed once everybody starts using the |Time|
467 static Time
FromTimeT(time_t tt
);
468 time_t ToTimeT() const;
470 // Converts time to/from a double which is the number of seconds since epoch
471 // (Jan 1, 1970). Webkit uses this format to represent time.
472 // Because WebKit initializes double time value to 0 to indicate "not
473 // initialized", we map it to empty Time object that also means "not
475 static Time
FromDoubleT(double dt
);
476 double ToDoubleT() const;
478 #if defined(OS_POSIX)
479 // Converts the timespec structure to time. MacOS X 10.8.3 (and tentatively,
480 // earlier versions) will have the |ts|'s tv_nsec component zeroed out,
481 // having a 1 second resolution, which agrees with
482 // https://developer.apple.com/legacy/library/#technotes/tn/tn1150.html#HFSPlusDates.
483 static Time
FromTimeSpec(const timespec
& ts
);
486 // Converts to/from the Javascript convention for times, a number of
487 // milliseconds since the epoch:
488 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime.
489 static Time
FromJsTime(double ms_since_epoch
);
490 double ToJsTime() const;
492 // Converts to Java convention for times, a number of
493 // milliseconds since the epoch.
494 int64
ToJavaTime() const;
496 #if defined(OS_POSIX)
497 static Time
FromTimeVal(struct timeval t
);
498 struct timeval
ToTimeVal() const;
501 #if defined(OS_MACOSX)
502 static Time
FromCFAbsoluteTime(CFAbsoluteTime t
);
503 CFAbsoluteTime
ToCFAbsoluteTime() const;
507 static Time
FromFileTime(FILETIME ft
);
508 FILETIME
ToFileTime() const;
510 // The minimum time of a low resolution timer. This is basically a windows
511 // constant of ~15.6ms. While it does vary on some older OS versions, we'll
512 // treat it as static across all windows versions.
513 static const int kMinLowResolutionThresholdMs
= 16;
515 // Enable or disable Windows high resolution timer.
516 static void EnableHighResolutionTimer(bool enable
);
518 // Activates or deactivates the high resolution timer based on the |activate|
519 // flag. If the HighResolutionTimer is not Enabled (see
520 // EnableHighResolutionTimer), this function will return false. Otherwise
521 // returns true. Each successful activate call must be paired with a
522 // subsequent deactivate call.
523 // All callers to activate the high resolution timer must eventually call
524 // this function to deactivate the high resolution timer.
525 static bool ActivateHighResolutionTimer(bool activate
);
527 // Returns true if the high resolution timer is both enabled and activated.
528 // This is provided for testing only, and is not tracked in a thread-safe
530 static bool IsHighResolutionTimerInUse();
533 // Converts an exploded structure representing either the local time or UTC
534 // into a Time class.
535 static Time
FromUTCExploded(const Exploded
& exploded
) {
536 return FromExploded(false, exploded
);
538 static Time
FromLocalExploded(const Exploded
& exploded
) {
539 return FromExploded(true, exploded
);
542 // Converts a string representation of time to a Time object.
543 // An example of a time string which is converted is as below:-
544 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
545 // in the input string, FromString assumes local time and FromUTCString
546 // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not
547 // specified in RFC822) is treated as if the timezone is not specified.
548 // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to
549 // a new time converter class.
550 static bool FromString(const char* time_string
, Time
* parsed_time
) {
551 return FromStringInternal(time_string
, true, parsed_time
);
553 static bool FromUTCString(const char* time_string
, Time
* parsed_time
) {
554 return FromStringInternal(time_string
, false, parsed_time
);
557 // Fills the given exploded structure with either the local time or UTC from
558 // this time structure (containing UTC).
559 void UTCExplode(Exploded
* exploded
) const {
560 return Explode(false, exploded
);
562 void LocalExplode(Exploded
* exploded
) const {
563 return Explode(true, exploded
);
566 // Rounds this time down to the nearest day in local time. It will represent
567 // midnight on that day.
568 Time
LocalMidnight() const;
571 friend class time_internal::TimeBase
<Time
>;
573 explicit Time(int64 us
) : TimeBase(us
) {
576 // Explodes the given time to either local time |is_local = true| or UTC
577 // |is_local = false|.
578 void Explode(bool is_local
, Exploded
* exploded
) const;
580 // Unexplodes a given time assuming the source is either local time
581 // |is_local = true| or UTC |is_local = false|.
582 static Time
FromExploded(bool is_local
, const Exploded
& exploded
);
584 // Converts a string representation of time to a Time object.
585 // An example of a time string which is converted is as below:-
586 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
587 // in the input string, local time |is_local = true| or
588 // UTC |is_local = false| is assumed. A timezone that cannot be parsed
589 // (e.g. "UTC" which is not specified in RFC822) is treated as if the
590 // timezone is not specified.
591 static bool FromStringInternal(const char* time_string
,
596 // Inline the TimeDelta factory methods, for fast TimeDelta construction.
599 inline TimeDelta
TimeDelta::FromDays(int days
) {
600 // Preserve max to prevent overflow.
601 if (days
== std::numeric_limits
<int>::max())
603 return TimeDelta(days
* Time::kMicrosecondsPerDay
);
607 inline TimeDelta
TimeDelta::FromHours(int hours
) {
608 // Preserve max to prevent overflow.
609 if (hours
== std::numeric_limits
<int>::max())
611 return TimeDelta(hours
* Time::kMicrosecondsPerHour
);
615 inline TimeDelta
TimeDelta::FromMinutes(int minutes
) {
616 // Preserve max to prevent overflow.
617 if (minutes
== std::numeric_limits
<int>::max())
619 return TimeDelta(minutes
* Time::kMicrosecondsPerMinute
);
623 inline TimeDelta
TimeDelta::FromSeconds(int64 secs
) {
624 // Preserve max to prevent overflow.
625 if (secs
== std::numeric_limits
<int64
>::max())
627 return TimeDelta(secs
* Time::kMicrosecondsPerSecond
);
631 inline TimeDelta
TimeDelta::FromMilliseconds(int64 ms
) {
632 // Preserve max to prevent overflow.
633 if (ms
== std::numeric_limits
<int64
>::max())
635 return TimeDelta(ms
* Time::kMicrosecondsPerMillisecond
);
639 inline TimeDelta
TimeDelta::FromSecondsD(double secs
) {
640 // Preserve max to prevent overflow.
641 if (secs
== std::numeric_limits
<double>::infinity())
643 return TimeDelta(static_cast<int64
>(secs
* Time::kMicrosecondsPerSecond
));
647 inline TimeDelta
TimeDelta::FromMillisecondsD(double ms
) {
648 // Preserve max to prevent overflow.
649 if (ms
== std::numeric_limits
<double>::infinity())
651 return TimeDelta(static_cast<int64
>(ms
* Time::kMicrosecondsPerMillisecond
));
655 inline TimeDelta
TimeDelta::FromMicroseconds(int64 us
) {
656 // Preserve max to prevent overflow.
657 if (us
== std::numeric_limits
<int64
>::max())
659 return TimeDelta(us
);
662 // For logging use only.
663 BASE_EXPORT
std::ostream
& operator<<(std::ostream
& os
, Time time
);
665 // TimeTicks ------------------------------------------------------------------
667 // Represents monotonically non-decreasing clock time.
668 class BASE_EXPORT TimeTicks
: public time_internal::TimeBase
<TimeTicks
> {
670 TimeTicks() : TimeBase(0) {
673 // Platform-dependent tick count representing "right now." When
674 // IsHighResolution() returns false, the resolution of the clock could be
675 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one
677 static TimeTicks
Now();
679 // Returns true if the high resolution clock is working on this system and
680 // Now() will return high resolution values. Note that, on systems where the
681 // high resolution clock works but is deemed inefficient, the low resolution
682 // clock will be used instead.
683 static bool IsHighResolution();
686 // Translates an absolute QPC timestamp into a TimeTicks value. The returned
687 // value has the same origin as Now(). Do NOT attempt to use this if
688 // IsHighResolution() returns false.
689 static TimeTicks
FromQPCValue(LONGLONG qpc_value
);
692 // Get the TimeTick value at the time of the UnixEpoch. This is useful when
693 // you need to relate the value of TimeTicks to a real time and date.
694 // Note: Upon first invocation, this function takes a snapshot of the realtime
695 // clock to establish a reference point. This function will return the same
696 // value for the duration of the application, but will be different in future
698 static TimeTicks
UnixEpoch();
700 // Returns |this| snapped to the next tick, given a |tick_phase| and
701 // repeating |tick_interval| in both directions. |this| may be before,
702 // after, or equal to the |tick_phase|.
703 TimeTicks
SnappedToNextTick(TimeTicks tick_phase
,
704 TimeDelta tick_interval
) const;
708 typedef DWORD (*TickFunctionType
)(void);
709 static TickFunctionType
SetMockTickFunction(TickFunctionType ticker
);
713 friend class time_internal::TimeBase
<TimeTicks
>;
715 // Please use Now() to create a new object. This is for internal use
717 explicit TimeTicks(int64 us
) : TimeBase(us
) {
721 // For logging use only.
722 BASE_EXPORT
std::ostream
& operator<<(std::ostream
& os
, TimeTicks time_ticks
);
724 // ThreadTicks ----------------------------------------------------------------
726 // Represents a clock, specific to a particular thread, than runs only while the
727 // thread is running.
728 class BASE_EXPORT ThreadTicks
: public time_internal::TimeBase
<ThreadTicks
> {
730 ThreadTicks() : TimeBase(0) {
733 // Returns true if ThreadTicks::Now() is supported on this system.
734 static bool IsSupported() {
735 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
736 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
743 // Returns thread-specific CPU-time on systems that support this feature.
744 // Needs to be guarded with a call to IsSupported(). Use this timer
745 // to (approximately) measure how much time the calling thread spent doing
746 // actual work vs. being de-scheduled. May return bogus results if the thread
747 // migrates to another CPU between two calls.
748 static ThreadTicks
Now();
751 friend class time_internal::TimeBase
<ThreadTicks
>;
753 // Please use Now() to create a new object. This is for internal use
755 explicit ThreadTicks(int64 us
) : TimeBase(us
) {
759 // For logging use only.
760 BASE_EXPORT
std::ostream
& operator<<(std::ostream
& os
, ThreadTicks time_ticks
);
762 // TraceTicks ----------------------------------------------------------------
764 // Represents high-resolution system trace clock time.
765 class BASE_EXPORT TraceTicks
: public time_internal::TimeBase
<TraceTicks
> {
767 // We define this even without OS_CHROMEOS for seccomp sandbox testing.
768 #if defined(OS_LINUX)
769 // Force definition of the system trace clock; it is a chromeos-only api
770 // at the moment and surfacing it in the right place requires mucking
772 static const clockid_t kClockSystemTrace
= 11;
775 TraceTicks() : TimeBase(0) {
778 // Returns the current system trace time or, if not available on this
779 // platform, a high-resolution time value; or a low-resolution time value if
780 // neither are avalable. On systems where a global trace clock is defined,
781 // timestamping TraceEvents's with this value guarantees synchronization
782 // between events collected inside chrome and events collected outside
783 // (e.g. kernel, X server).
785 // On some platforms, the clock source used for tracing can vary depending on
786 // hardware and/or kernel support. Do not make any assumptions without
787 // consulting the documentation for this functionality in the time_win.cc,
788 // time_posix.cc, etc. files.
790 // NOTE: This is only meant to be used by the event tracing infrastructure,
791 // and by outside code modules in special circumstances. Please be sure to
792 // consult a base/trace_event/OWNER before committing any new code that uses
794 static TraceTicks
Now();
797 friend class time_internal::TimeBase
<TraceTicks
>;
799 // Please use Now() to create a new object. This is for internal use
801 explicit TraceTicks(int64 us
) : TimeBase(us
) {
805 // For logging use only.
806 BASE_EXPORT
std::ostream
& operator<<(std::ostream
& os
, TraceTicks time_ticks
);
810 #endif // BASE_TIME_TIME_H_