Home | History | Annotate | Download | only in platform
      1 // Copyright 2013 the V8 project 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.
      4 
      5 #ifndef V8_BASE_PLATFORM_TIME_H_
      6 #define V8_BASE_PLATFORM_TIME_H_
      7 
      8 #include <ctime>
      9 #include <iosfwd>
     10 #include <limits>
     11 
     12 #include "src/base/bits.h"
     13 #include "src/base/macros.h"
     14 #include "src/base/safe_math.h"
     15 #if V8_OS_WIN
     16 #include "src/base/win32-headers.h"
     17 #endif
     18 
     19 // Forward declarations.
     20 extern "C" {
     21 struct _FILETIME;
     22 struct mach_timespec;
     23 struct timespec;
     24 struct timeval;
     25 }
     26 
     27 namespace v8 {
     28 namespace base {
     29 
     30 class Time;
     31 class TimeDelta;
     32 class TimeTicks;
     33 
     34 namespace time_internal {
     35 template<class TimeClass>
     36 class TimeBase;
     37 }
     38 
     39 // -----------------------------------------------------------------------------
     40 // TimeDelta
     41 //
     42 // This class represents a duration of time, internally represented in
     43 // microseonds.
     44 
     45 class TimeDelta final {
     46  public:
     47   TimeDelta() : delta_(0) {}
     48 
     49   // Converts units of time to TimeDeltas.
     50   static TimeDelta FromDays(int days);
     51   static TimeDelta FromHours(int hours);
     52   static TimeDelta FromMinutes(int minutes);
     53   static TimeDelta FromSeconds(int64_t seconds);
     54   static TimeDelta FromMilliseconds(int64_t milliseconds);
     55   static TimeDelta FromMicroseconds(int64_t microseconds) {
     56     return TimeDelta(microseconds);
     57   }
     58   static TimeDelta FromNanoseconds(int64_t nanoseconds);
     59 
     60   // Returns the time delta in some unit. The F versions return a floating
     61   // point value, the "regular" versions return a rounded-down value.
     62   //
     63   // InMillisecondsRoundedUp() instead returns an integer that is rounded up
     64   // to the next full millisecond.
     65   int InDays() const;
     66   int InHours() const;
     67   int InMinutes() const;
     68   double InSecondsF() const;
     69   int64_t InSeconds() const;
     70   double InMillisecondsF() const;
     71   int64_t InMilliseconds() const;
     72   int64_t InMillisecondsRoundedUp() const;
     73   int64_t InMicroseconds() const { return delta_; }
     74   int64_t InNanoseconds() const;
     75 
     76   // Converts to/from Mach time specs.
     77   static TimeDelta FromMachTimespec(struct mach_timespec ts);
     78   struct mach_timespec ToMachTimespec() const;
     79 
     80   // Converts to/from POSIX time specs.
     81   static TimeDelta FromTimespec(struct timespec ts);
     82   struct timespec ToTimespec() const;
     83 
     84   TimeDelta& operator=(const TimeDelta& other) {
     85     delta_ = other.delta_;
     86     return *this;
     87   }
     88 
     89   // Computations with other deltas.
     90   TimeDelta operator+(const TimeDelta& other) const {
     91     return TimeDelta(delta_ + other.delta_);
     92   }
     93   TimeDelta operator-(const TimeDelta& other) const {
     94     return TimeDelta(delta_ - other.delta_);
     95   }
     96 
     97   TimeDelta& operator+=(const TimeDelta& other) {
     98     delta_ += other.delta_;
     99     return *this;
    100   }
    101   TimeDelta& operator-=(const TimeDelta& other) {
    102     delta_ -= other.delta_;
    103     return *this;
    104   }
    105   TimeDelta operator-() const {
    106     return TimeDelta(-delta_);
    107   }
    108 
    109   double TimesOf(const TimeDelta& other) const {
    110     return static_cast<double>(delta_) / static_cast<double>(other.delta_);
    111   }
    112   double PercentOf(const TimeDelta& other) const {
    113     return TimesOf(other) * 100.0;
    114   }
    115 
    116   // Computations with ints, note that we only allow multiplicative operations
    117   // with ints, and additive operations with other deltas.
    118   TimeDelta operator*(int64_t a) const {
    119     return TimeDelta(delta_ * a);
    120   }
    121   TimeDelta operator/(int64_t a) const {
    122     return TimeDelta(delta_ / a);
    123   }
    124   TimeDelta& operator*=(int64_t a) {
    125     delta_ *= a;
    126     return *this;
    127   }
    128   TimeDelta& operator/=(int64_t a) {
    129     delta_ /= a;
    130     return *this;
    131   }
    132   int64_t operator/(const TimeDelta& other) const {
    133     return delta_ / other.delta_;
    134   }
    135 
    136   // Comparison operators.
    137   bool operator==(const TimeDelta& other) const {
    138     return delta_ == other.delta_;
    139   }
    140   bool operator!=(const TimeDelta& other) const {
    141     return delta_ != other.delta_;
    142   }
    143   bool operator<(const TimeDelta& other) const {
    144     return delta_ < other.delta_;
    145   }
    146   bool operator<=(const TimeDelta& other) const {
    147     return delta_ <= other.delta_;
    148   }
    149   bool operator>(const TimeDelta& other) const {
    150     return delta_ > other.delta_;
    151   }
    152   bool operator>=(const TimeDelta& other) const {
    153     return delta_ >= other.delta_;
    154   }
    155 
    156  private:
    157   template<class TimeClass> friend class time_internal::TimeBase;
    158   // Constructs a delta given the duration in microseconds. This is private
    159   // to avoid confusion by callers with an integer constructor. Use
    160   // FromSeconds, FromMilliseconds, etc. instead.
    161   explicit TimeDelta(int64_t delta) : delta_(delta) {}
    162 
    163   // Delta in microseconds.
    164   int64_t delta_;
    165 };
    166 
    167 
    168 namespace time_internal {
    169 
    170 // TimeBase--------------------------------------------------------------------
    171 
    172 // Provides value storage and comparison/math operations common to all time
    173 // classes. Each subclass provides for strong type-checking to ensure
    174 // semantically meaningful comparison/math of time values from the same clock
    175 // source or timeline.
    176 template<class TimeClass>
    177 class TimeBase {
    178  public:
    179   static const int64_t kHoursPerDay = 24;
    180   static const int64_t kMillisecondsPerSecond = 1000;
    181   static const int64_t kMillisecondsPerDay =
    182       kMillisecondsPerSecond * 60 * 60 * kHoursPerDay;
    183   static const int64_t kMicrosecondsPerMillisecond = 1000;
    184   static const int64_t kMicrosecondsPerSecond =
    185       kMicrosecondsPerMillisecond * kMillisecondsPerSecond;
    186   static const int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
    187   static const int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
    188   static const int64_t kMicrosecondsPerDay =
    189       kMicrosecondsPerHour * kHoursPerDay;
    190   static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
    191   static const int64_t kNanosecondsPerMicrosecond = 1000;
    192   static const int64_t kNanosecondsPerSecond =
    193       kNanosecondsPerMicrosecond * kMicrosecondsPerSecond;
    194 
    195   // Returns true if this object has not been initialized.
    196   //
    197   // Warning: Be careful when writing code that performs math on time values,
    198   // since it's possible to produce a valid "zero" result that should not be
    199   // interpreted as a "null" value.
    200   bool IsNull() const {
    201     return us_ == 0;
    202   }
    203 
    204   // Returns true if this object represents the maximum time.
    205   bool IsMax() const { return us_ == std::numeric_limits<int64_t>::max(); }
    206 
    207   // For serializing only. Use FromInternalValue() to reconstitute. Please don't
    208   // use this and do arithmetic on it, as it is more error prone than using the
    209   // provided operators.
    210   int64_t ToInternalValue() const { return us_; }
    211 
    212   TimeClass& operator=(TimeClass other) {
    213     us_ = other.us_;
    214     return *(static_cast<TimeClass*>(this));
    215   }
    216 
    217   // Compute the difference between two times.
    218   TimeDelta operator-(TimeClass other) const {
    219     return TimeDelta::FromMicroseconds(us_ - other.us_);
    220   }
    221 
    222   // Return a new time modified by some delta.
    223   TimeClass operator+(TimeDelta delta) const {
    224     return TimeClass(bits::SignedSaturatedAdd64(delta.delta_, us_));
    225   }
    226   TimeClass operator-(TimeDelta delta) const {
    227     return TimeClass(-bits::SignedSaturatedSub64(delta.delta_, us_));
    228   }
    229 
    230   // Modify by some time delta.
    231   TimeClass& operator+=(TimeDelta delta) {
    232     return static_cast<TimeClass&>(*this = (*this + delta));
    233   }
    234   TimeClass& operator-=(TimeDelta delta) {
    235     return static_cast<TimeClass&>(*this = (*this - delta));
    236   }
    237 
    238   // Comparison operators
    239   bool operator==(TimeClass other) const {
    240     return us_ == other.us_;
    241   }
    242   bool operator!=(TimeClass other) const {
    243     return us_ != other.us_;
    244   }
    245   bool operator<(TimeClass other) const {
    246     return us_ < other.us_;
    247   }
    248   bool operator<=(TimeClass other) const {
    249     return us_ <= other.us_;
    250   }
    251   bool operator>(TimeClass other) const {
    252     return us_ > other.us_;
    253   }
    254   bool operator>=(TimeClass other) const {
    255     return us_ >= other.us_;
    256   }
    257 
    258   // Converts an integer value representing TimeClass to a class. This is used
    259   // when deserializing a |TimeClass| structure, using a value known to be
    260   // compatible. It is not provided as a constructor because the integer type
    261   // may be unclear from the perspective of a caller.
    262   static TimeClass FromInternalValue(int64_t us) { return TimeClass(us); }
    263 
    264  protected:
    265   explicit TimeBase(int64_t us) : us_(us) {}
    266 
    267   // Time value in a microsecond timebase.
    268   int64_t us_;
    269 };
    270 
    271 }  // namespace time_internal
    272 
    273 
    274 // -----------------------------------------------------------------------------
    275 // Time
    276 //
    277 // This class represents an absolute point in time, internally represented as
    278 // microseconds (s/1,000,000) since 00:00:00 UTC, January 1, 1970.
    279 
    280 class Time final : public time_internal::TimeBase<Time> {
    281  public:
    282   // Contains the NULL time. Use Time::Now() to get the current time.
    283   Time() : TimeBase(0) {}
    284 
    285   // Returns the current time. Watch out, the system might adjust its clock
    286   // in which case time will actually go backwards. We don't guarantee that
    287   // times are increasing, or that two calls to Now() won't be the same.
    288   static Time Now();
    289 
    290   // Returns the current time. Same as Now() except that this function always
    291   // uses system time so that there are no discrepancies between the returned
    292   // time and system time even on virtual environments including our test bot.
    293   // For timing sensitive unittests, this function should be used.
    294   static Time NowFromSystemTime();
    295 
    296   // Returns the time for epoch in Unix-like system (Jan 1, 1970).
    297   static Time UnixEpoch() { return Time(0); }
    298 
    299   // Returns the maximum time, which should be greater than any reasonable time
    300   // with which we might compare it.
    301   static Time Max() { return Time(std::numeric_limits<int64_t>::max()); }
    302 
    303   // Converts to/from POSIX time specs.
    304   static Time FromTimespec(struct timespec ts);
    305   struct timespec ToTimespec() const;
    306 
    307   // Converts to/from POSIX time values.
    308   static Time FromTimeval(struct timeval tv);
    309   struct timeval ToTimeval() const;
    310 
    311   // Converts to/from Windows file times.
    312   static Time FromFiletime(struct _FILETIME ft);
    313   struct _FILETIME ToFiletime() const;
    314 
    315   // Converts to/from the Javascript convention for times, a number of
    316   // milliseconds since the epoch:
    317   static Time FromJsTime(double ms_since_epoch);
    318   double ToJsTime() const;
    319 
    320  private:
    321   friend class time_internal::TimeBase<Time>;
    322   explicit Time(int64_t us) : TimeBase(us) {}
    323 };
    324 
    325 std::ostream& operator<<(std::ostream&, const Time&);
    326 
    327 inline Time operator+(const TimeDelta& delta, const Time& time) {
    328   return time + delta;
    329 }
    330 
    331 
    332 // -----------------------------------------------------------------------------
    333 // TimeTicks
    334 //
    335 // This class represents an abstract time that is most of the time incrementing
    336 // for use in measuring time durations. It is internally represented in
    337 // microseconds.  It can not be converted to a human-readable time, but is
    338 // guaranteed not to decrease (if the user changes the computer clock,
    339 // Time::Now() may actually decrease or jump).  But note that TimeTicks may
    340 // "stand still", for example if the computer suspended.
    341 
    342 class TimeTicks final : public time_internal::TimeBase<TimeTicks> {
    343  public:
    344   TimeTicks() : TimeBase(0) {}
    345 
    346   // Platform-dependent tick count representing "right now."
    347   // The resolution of this clock is ~1-15ms.  Resolution varies depending
    348   // on hardware/operating system configuration.
    349   // This method never returns a null TimeTicks.
    350   static TimeTicks Now();
    351 
    352   // Returns a platform-dependent high-resolution tick count. Implementation
    353   // is hardware dependent and may or may not return sub-millisecond
    354   // resolution.  THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND
    355   // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
    356   // This method never returns a null TimeTicks.
    357   static TimeTicks HighResolutionNow();
    358 
    359   // Returns true if the high-resolution clock is working on this system.
    360   static bool IsHighResolutionClockWorking();
    361 
    362  private:
    363   friend class time_internal::TimeBase<TimeTicks>;
    364 
    365   // Please use Now() to create a new object. This is for internal use
    366   // and testing. Ticks are in microseconds.
    367   explicit TimeTicks(int64_t ticks) : TimeBase(ticks) {}
    368 };
    369 
    370 inline TimeTicks operator+(const TimeDelta& delta, const TimeTicks& ticks) {
    371   return ticks + delta;
    372 }
    373 
    374 
    375 // ThreadTicks ----------------------------------------------------------------
    376 
    377 // Represents a clock, specific to a particular thread, than runs only while the
    378 // thread is running.
    379 class ThreadTicks final : public time_internal::TimeBase<ThreadTicks> {
    380  public:
    381   ThreadTicks() : TimeBase(0) {}
    382 
    383   // Returns true if ThreadTicks::Now() is supported on this system.
    384   static bool IsSupported();
    385 
    386   // Waits until the initialization is completed. Needs to be guarded with a
    387   // call to IsSupported().
    388   static void WaitUntilInitialized() {
    389 #if V8_OS_WIN
    390     WaitUntilInitializedWin();
    391 #endif
    392   }
    393 
    394   // Returns thread-specific CPU-time on systems that support this feature.
    395   // Needs to be guarded with a call to IsSupported(). Use this timer
    396   // to (approximately) measure how much time the calling thread spent doing
    397   // actual work vs. being de-scheduled. May return bogus results if the thread
    398   // migrates to another CPU between two calls. Returns an empty ThreadTicks
    399   // object until the initialization is completed. If a clock reading is
    400   // absolutely needed, call WaitUntilInitialized() before this method.
    401   static ThreadTicks Now();
    402 
    403 #if V8_OS_WIN
    404   // Similar to Now() above except this returns thread-specific CPU time for an
    405   // arbitrary thread. All comments for Now() method above apply apply to this
    406   // method as well.
    407   static ThreadTicks GetForThread(const HANDLE& thread_handle);
    408 #endif
    409 
    410  private:
    411   // Please use Now() or GetForThread() to create a new object. This is for
    412   // internal use and testing. Ticks are in microseconds.
    413   explicit ThreadTicks(int64_t ticks) : TimeBase(ticks) {}
    414 
    415 #if V8_OS_WIN
    416   // Returns the frequency of the TSC in ticks per second, or 0 if it hasn't
    417   // been measured yet. Needs to be guarded with a call to IsSupported().
    418   static double TSCTicksPerSecond();
    419   static bool IsSupportedWin();
    420   static void WaitUntilInitializedWin();
    421 #endif
    422 };
    423 
    424 }  // namespace base
    425 }  // namespace v8
    426 
    427 #endif  // V8_BASE_PLATFORM_TIME_H_
    428