michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_TimeStamp_h michael@0: #define mozilla_TimeStamp_h michael@0: michael@0: #include michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "nscore.h" michael@0: michael@0: namespace IPC { michael@0: template struct ParamTraits; michael@0: } michael@0: michael@0: #ifdef XP_WIN michael@0: // defines TimeStampValue as a complex value keeping both michael@0: // GetTickCount and QueryPerformanceCounter values michael@0: #include "TimeStamp_windows.h" michael@0: #endif michael@0: michael@0: namespace mozilla { michael@0: michael@0: #ifndef XP_WIN michael@0: typedef uint64_t TimeStampValue; michael@0: #endif michael@0: michael@0: class TimeStamp; michael@0: michael@0: /** michael@0: * Instances of this class represent the length of an interval of time. michael@0: * Negative durations are allowed, meaning the end is before the start. michael@0: * michael@0: * Internally the duration is stored as a int64_t in units of michael@0: * PR_TicksPerSecond() when building with NSPR interval timers, or a michael@0: * system-dependent unit when building with system clocks. The michael@0: * system-dependent unit must be constant, otherwise the semantics of michael@0: * this class would be broken. michael@0: */ michael@0: class TimeDuration michael@0: { michael@0: public: michael@0: // The default duration is 0. michael@0: MOZ_CONSTEXPR TimeDuration() : mValue(0) {} michael@0: // Allow construction using '0' as the initial value, for readability, michael@0: // but no other numbers (so we don't have any implicit unit conversions). michael@0: struct _SomethingVeryRandomHere; michael@0: TimeDuration(_SomethingVeryRandomHere* aZero) : mValue(0) { michael@0: MOZ_ASSERT(!aZero, "Who's playing funny games here?"); michael@0: } michael@0: // Default copy-constructor and assignment are OK michael@0: michael@0: double ToSeconds() const; michael@0: // Return a duration value that includes digits of time we think to michael@0: // be significant. This method should be used when displaying a michael@0: // time to humans. michael@0: double ToSecondsSigDigits() const; michael@0: double ToMilliseconds() const { michael@0: return ToSeconds() * 1000.0; michael@0: } michael@0: double ToMicroseconds() const { michael@0: return ToMilliseconds() * 1000.0; michael@0: } michael@0: michael@0: // Using a double here is safe enough; with 53 bits we can represent michael@0: // durations up to over 280,000 years exactly. If the units of michael@0: // mValue do not allow us to represent durations of that length, michael@0: // long durations are clamped to the max/min representable value michael@0: // instead of overflowing. michael@0: static inline TimeDuration FromSeconds(double aSeconds) { michael@0: return FromMilliseconds(aSeconds * 1000.0); michael@0: } michael@0: static TimeDuration FromMilliseconds(double aMilliseconds); michael@0: static inline TimeDuration FromMicroseconds(double aMicroseconds) { michael@0: return FromMilliseconds(aMicroseconds / 1000.0); michael@0: } michael@0: michael@0: static TimeDuration Forever() { michael@0: return FromTicks(INT64_MAX); michael@0: } michael@0: michael@0: TimeDuration operator+(const TimeDuration& aOther) const { michael@0: return TimeDuration::FromTicks(mValue + aOther.mValue); michael@0: } michael@0: TimeDuration operator-(const TimeDuration& aOther) const { michael@0: return TimeDuration::FromTicks(mValue - aOther.mValue); michael@0: } michael@0: TimeDuration& operator+=(const TimeDuration& aOther) { michael@0: mValue += aOther.mValue; michael@0: return *this; michael@0: } michael@0: TimeDuration& operator-=(const TimeDuration& aOther) { michael@0: mValue -= aOther.mValue; michael@0: return *this; michael@0: } michael@0: michael@0: private: michael@0: // Block double multiplier (slower, imprecise if long duration) - Bug 853398. michael@0: // If required, use MultDouble explicitly and with care. michael@0: TimeDuration operator*(const double aMultiplier) const MOZ_DELETE; michael@0: michael@0: public: michael@0: TimeDuration MultDouble(double aMultiplier) const { michael@0: return TimeDuration::FromTicks(static_cast(mValue * aMultiplier)); michael@0: } michael@0: TimeDuration operator*(const int32_t aMultiplier) const { michael@0: return TimeDuration::FromTicks(mValue * int64_t(aMultiplier)); michael@0: } michael@0: TimeDuration operator*(const uint32_t aMultiplier) const { michael@0: return TimeDuration::FromTicks(mValue * int64_t(aMultiplier)); michael@0: } michael@0: TimeDuration operator*(const int64_t aMultiplier) const { michael@0: return TimeDuration::FromTicks(mValue * int64_t(aMultiplier)); michael@0: } michael@0: TimeDuration operator/(const int64_t aDivisor) const { michael@0: return TimeDuration::FromTicks(mValue / aDivisor); michael@0: } michael@0: double operator/(const TimeDuration& aOther) const { michael@0: return static_cast(mValue) / aOther.mValue; michael@0: } michael@0: michael@0: bool operator<(const TimeDuration& aOther) const { michael@0: return mValue < aOther.mValue; michael@0: } michael@0: bool operator<=(const TimeDuration& aOther) const { michael@0: return mValue <= aOther.mValue; michael@0: } michael@0: bool operator>=(const TimeDuration& aOther) const { michael@0: return mValue >= aOther.mValue; michael@0: } michael@0: bool operator>(const TimeDuration& aOther) const { michael@0: return mValue > aOther.mValue; michael@0: } michael@0: bool operator==(const TimeDuration& aOther) const { michael@0: return mValue == aOther.mValue; michael@0: } michael@0: michael@0: // Return a best guess at the system's current timing resolution, michael@0: // which might be variable. TimeDurations below this order of michael@0: // magnitude are meaningless, and those at the same order of michael@0: // magnitude or just above are suspect. michael@0: static TimeDuration Resolution(); michael@0: michael@0: // We could define additional operators here: michael@0: // -- convert to/from other time units michael@0: // -- scale duration by a float michael@0: // but let's do that on demand. michael@0: // Comparing durations for equality will only lead to bugs on michael@0: // platforms with high-resolution timers. michael@0: michael@0: private: michael@0: friend class TimeStamp; michael@0: friend struct IPC::ParamTraits; michael@0: michael@0: static TimeDuration FromTicks(int64_t aTicks) { michael@0: TimeDuration t; michael@0: t.mValue = aTicks; michael@0: return t; michael@0: } michael@0: michael@0: static TimeDuration FromTicks(double aTicks) { michael@0: // NOTE: this MUST be a >= test, because int64_t(double(INT64_MAX)) michael@0: // overflows and gives INT64_MIN. michael@0: if (aTicks >= double(INT64_MAX)) michael@0: return TimeDuration::FromTicks(INT64_MAX); michael@0: michael@0: // This MUST be a <= test. michael@0: if (aTicks <= double(INT64_MIN)) michael@0: return TimeDuration::FromTicks(INT64_MIN); michael@0: michael@0: return TimeDuration::FromTicks(int64_t(aTicks)); michael@0: } michael@0: michael@0: // Duration, result is implementation-specific difference of two TimeStamps michael@0: int64_t mValue; michael@0: }; michael@0: michael@0: /** michael@0: * Instances of this class represent moments in time, or a special michael@0: * "null" moment. We do not use the non-monotonic system clock or michael@0: * local time, since they can be reset, causing apparent backward michael@0: * travel in time, which can confuse algorithms. Instead we measure michael@0: * elapsed time according to the system. This time can never go michael@0: * backwards (i.e. it never wraps around, at least not in less than michael@0: * five million years of system elapsed time). It might not advance michael@0: * while the system is sleeping. If TimeStamp::SetNow() is not called michael@0: * at all for hours or days, we might not notice the passage of some michael@0: * of that time. michael@0: * michael@0: * We deliberately do not expose a way to convert TimeStamps to some michael@0: * particular unit. All you can do is compute a difference between two michael@0: * TimeStamps to get a TimeDuration. You can also add a TimeDuration michael@0: * to a TimeStamp to get a new TimeStamp. You can't do something michael@0: * meaningless like add two TimeStamps. michael@0: * michael@0: * Internally this is implemented as either a wrapper around michael@0: * - high-resolution, monotonic, system clocks if they exist on this michael@0: * platform michael@0: * - PRIntervalTime otherwise. We detect wraparounds of michael@0: * PRIntervalTime and work around them. michael@0: * michael@0: * This class is similar to C++11's time_point, however it is michael@0: * explicitly nullable and provides an IsNull() method. time_point michael@0: * is initialized to the clock's epoch and provides a michael@0: * time_since_epoch() method that functions similiarly. i.e. michael@0: * t.IsNull() is equivalent to t.time_since_epoch() == decltype(t)::duration::zero(); michael@0: */ michael@0: class TimeStamp michael@0: { michael@0: public: michael@0: /** michael@0: * Initialize to the "null" moment michael@0: */ michael@0: MOZ_CONSTEXPR TimeStamp() : mValue(0) {} michael@0: // Default copy-constructor and assignment are OK michael@0: michael@0: /** michael@0: * Return true if this is the "null" moment michael@0: */ michael@0: bool IsNull() const { return mValue == 0; } michael@0: /** michael@0: * Return a timestamp reflecting the current elapsed system time. This michael@0: * is monotonically increasing (i.e., does not decrease) over the michael@0: * lifetime of this process' XPCOM session. michael@0: * michael@0: * Now() is trying to ensure the best possible precision on each platform, michael@0: * at least one millisecond. michael@0: * michael@0: * NowLoRes() has been introduced to workaround performance problems of michael@0: * QueryPerformanceCounter on the Windows platform. NowLoRes() is giving michael@0: * lower precision, usually 15.6 ms, but with very good performance benefit. michael@0: * Use it for measurements of longer times, like >200ms timeouts. michael@0: */ michael@0: static TimeStamp Now() { return Now(true); } michael@0: static TimeStamp NowLoRes() { return Now(false); } michael@0: michael@0: /** michael@0: * Return a timestamp representing the time when the current process was michael@0: * created which will be comparable with other timestamps taken with this michael@0: * class. If the actual process creation time is detected to be inconsistent michael@0: * the @a aIsInconsistent parameter will be set to true, the returned michael@0: * timestamp however will still be valid though inaccurate. michael@0: * michael@0: * @param aIsInconsistent Set to true if an inconsistency was detected in the michael@0: * process creation time michael@0: * @returns A timestamp representing the time when the process was created, michael@0: * this timestamp is always valid even when errors are reported michael@0: */ michael@0: static TimeStamp ProcessCreation(bool& aIsInconsistent); michael@0: michael@0: /** michael@0: * Records a process restart. After this call ProcessCreation() will return michael@0: * the time when the browser was restarted instead of the actual time when michael@0: * the process was created. michael@0: */ michael@0: static void RecordProcessRestart(); michael@0: michael@0: /** michael@0: * Compute the difference between two timestamps. Both must be non-null. michael@0: */ michael@0: TimeDuration operator-(const TimeStamp& aOther) const { michael@0: MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); michael@0: MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); michael@0: static_assert(-INT64_MAX > INT64_MIN, "int64_t sanity check"); michael@0: int64_t ticks = int64_t(mValue - aOther.mValue); michael@0: // Check for overflow. michael@0: if (mValue > aOther.mValue) { michael@0: if (ticks < 0) { michael@0: ticks = INT64_MAX; michael@0: } michael@0: } else { michael@0: if (ticks > 0) { michael@0: ticks = INT64_MIN; michael@0: } michael@0: } michael@0: return TimeDuration::FromTicks(ticks); michael@0: } michael@0: michael@0: TimeStamp operator+(const TimeDuration& aOther) const { michael@0: MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); michael@0: return TimeStamp(mValue + aOther.mValue); michael@0: } michael@0: TimeStamp operator-(const TimeDuration& aOther) const { michael@0: MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); michael@0: return TimeStamp(mValue - aOther.mValue); michael@0: } michael@0: TimeStamp& operator+=(const TimeDuration& aOther) { michael@0: MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); michael@0: mValue += aOther.mValue; michael@0: return *this; michael@0: } michael@0: TimeStamp& operator-=(const TimeDuration& aOther) { michael@0: MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); michael@0: mValue -= aOther.mValue; michael@0: return *this; michael@0: } michael@0: michael@0: bool operator<(const TimeStamp& aOther) const { michael@0: MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); michael@0: MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); michael@0: return mValue < aOther.mValue; michael@0: } michael@0: bool operator<=(const TimeStamp& aOther) const { michael@0: MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); michael@0: MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); michael@0: return mValue <= aOther.mValue; michael@0: } michael@0: bool operator>=(const TimeStamp& aOther) const { michael@0: MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); michael@0: MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); michael@0: return mValue >= aOther.mValue; michael@0: } michael@0: bool operator>(const TimeStamp& aOther) const { michael@0: MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); michael@0: MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); michael@0: return mValue > aOther.mValue; michael@0: } michael@0: bool operator==(const TimeStamp& aOther) const { michael@0: // Maybe it's ok to check == with null timestamps? michael@0: MOZ_ASSERT(!IsNull() && "Cannot compute with a null value"); michael@0: MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); michael@0: return mValue == aOther.mValue; michael@0: } michael@0: bool operator!=(const TimeStamp& aOther) const { michael@0: // Maybe it's ok to check != with null timestamps? michael@0: MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); michael@0: MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); michael@0: return mValue != aOther.mValue; michael@0: } michael@0: michael@0: // Comparing TimeStamps for equality should be discouraged. Adding michael@0: // two TimeStamps, or scaling TimeStamps, is nonsense and must never michael@0: // be allowed. michael@0: michael@0: static NS_HIDDEN_(nsresult) Startup(); michael@0: static NS_HIDDEN_(void) Shutdown(); michael@0: michael@0: private: michael@0: friend struct IPC::ParamTraits; michael@0: friend void StartupTimelineRecordExternal(int, uint64_t); michael@0: michael@0: TimeStamp(TimeStampValue aValue) : mValue(aValue) {} michael@0: michael@0: static TimeStamp Now(bool aHighResolution); michael@0: michael@0: /** michael@0: * Computes the uptime of the current process in microseconds. The result michael@0: * is platform-dependent and needs to be checked against existing timestamps michael@0: * for consistency. michael@0: * michael@0: * @returns The number of microseconds since the calling process was started michael@0: * or 0 if an error was encountered while computing the uptime michael@0: */ michael@0: static uint64_t ComputeProcessUptime(); michael@0: michael@0: /** michael@0: * When built with PRIntervalTime, a value of 0 means this instance michael@0: * is "null". Otherwise, the low 32 bits represent a PRIntervalTime, michael@0: * and the high 32 bits represent a counter of the number of michael@0: * rollovers of PRIntervalTime that we've seen. This counter starts michael@0: * at 1 to avoid a real time colliding with the "null" value. michael@0: * michael@0: * PR_INTERVAL_MAX is set at 100,000 ticks per second. So the minimum michael@0: * time to wrap around is about 2^64/100000 seconds, i.e. about michael@0: * 5,849,424 years. michael@0: * michael@0: * When using a system clock, a value is system dependent. michael@0: */ michael@0: TimeStampValue mValue; michael@0: }; michael@0: michael@0: } michael@0: michael@0: #endif /* mozilla_TimeStamp_h */