| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef mozilla_TimeStamp_h |
| 8 |
#define mozilla_TimeStamp_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" |
| 11 |
#include "mozilla/Attributes.h" |
| 12 |
#include "mozilla/FloatingPoint.h" |
| 13 |
#include "mozilla/Types.h" |
| 14 |
#include <algorithm> // for std::min, std::max |
| 15 |
#include <ostream> |
| 16 |
#include <stdint.h> |
| 17 |
#include <type_traits> |
| 18 |
|
| 19 |
namespace IPC { |
| 20 |
template <typename T> |
| 21 |
struct ParamTraits; |
| 22 |
} // namespace IPC |
| 23 |
|
| 24 |
#ifdef XP_WIN |
| 25 |
// defines TimeStampValue as a complex value keeping both |
| 26 |
// GetTickCount and QueryPerformanceCounter values |
| 27 |
# include "TimeStamp_windows.h" |
| 28 |
|
| 29 |
# include "mozilla/Maybe.h" // For TimeStamp::RawQueryPerformanceCounterValue |
| 30 |
#endif |
| 31 |
|
| 32 |
namespace mozilla { |
| 33 |
|
| 34 |
#ifndef XP_WIN |
| 35 |
typedef uint64_t TimeStampValue; |
| 36 |
#endif |
| 37 |
|
| 38 |
class TimeStamp; |
| 39 |
class TimeStampTests; |
| 40 |
|
| 41 |
/** |
| 42 |
* Platform-specific implementation details of BaseTimeDuration. |
| 43 |
*/ |
| 44 |
class BaseTimeDurationPlatformUtils { |
| 45 |
public: |
| 46 |
static MFBT_API double ToSeconds(int64_t aTicks); |
| 47 |
static MFBT_API double ToSecondsSigDigits(int64_t aTicks); |
| 48 |
static MFBT_API int64_t TicksFromMilliseconds(double aMilliseconds); |
| 49 |
static MFBT_API int64_t ResolutionInTicks(); |
| 50 |
}; |
| 51 |
|
| 52 |
/** |
| 53 |
* Instances of this class represent the length of an interval of time. |
| 54 |
* Negative durations are allowed, meaning the end is before the start. |
| 55 |
* |
| 56 |
* Internally the duration is stored as a int64_t in units of |
| 57 |
* PR_TicksPerSecond() when building with NSPR interval timers, or a |
| 58 |
* system-dependent unit when building with system clocks. The |
| 59 |
* system-dependent unit must be constant, otherwise the semantics of |
| 60 |
* this class would be broken. |
| 61 |
* |
| 62 |
* The ValueCalculator template parameter determines how arithmetic |
| 63 |
* operations are performed on the integer count of ticks (mValue). |
| 64 |
*/ |
| 65 |
template <typename ValueCalculator> |
| 66 |
class BaseTimeDuration { |
| 67 |
public: |
| 68 |
// The default duration is 0. |
| 69 |
constexpr BaseTimeDuration() : mValue(0) {} |
| 70 |
// Allow construction using '0' as the initial value, for readability, |
| 71 |
// but no other numbers (so we don't have any implicit unit conversions). |
| 72 |
struct _SomethingVeryRandomHere; |
| 73 |
MOZ_IMPLICIT BaseTimeDuration(_SomethingVeryRandomHere* aZero) : mValue(0) { |
| 74 |
MOZ_ASSERT(!aZero, "Who's playing funny games here?"); |
| 75 |
} |
| 76 |
// Default copy-constructor and assignment are OK |
| 77 |
|
| 78 |
// Converting copy-constructor and assignment operator |
| 79 |
template <typename E> |
| 80 |
explicit BaseTimeDuration(const BaseTimeDuration<E>& aOther) |
| 81 |
: mValue(aOther.mValue) {} |
| 82 |
|
| 83 |
template <typename E> |
| 84 |
BaseTimeDuration& operator=(const BaseTimeDuration<E>& aOther) { |
| 85 |
mValue = aOther.mValue; |
| 86 |
return *this; |
| 87 |
} |
| 88 |
|
| 89 |
double ToSeconds() const { |
| 90 |
if (mValue == INT64_MAX) { |
| 91 |
return PositiveInfinity<double>(); |
| 92 |
} |
| 93 |
if (mValue == INT64_MIN) { |
| 94 |
return NegativeInfinity<double>(); |
| 95 |
} |
| 96 |
return BaseTimeDurationPlatformUtils::ToSeconds(mValue); |
| 97 |
} |
| 98 |
// Return a duration value that includes digits of time we think to |
| 99 |
// be significant. This method should be used when displaying a |
| 100 |
// time to humans. |
| 101 |
double ToSecondsSigDigits() const { |
| 102 |
if (mValue == INT64_MAX) { |
| 103 |
return PositiveInfinity<double>(); |
| 104 |
} |
| 105 |
if (mValue == INT64_MIN) { |
| 106 |
return NegativeInfinity<double>(); |
| 107 |
} |
| 108 |
return BaseTimeDurationPlatformUtils::ToSecondsSigDigits(mValue); |
| 109 |
} |
| 110 |
double ToMilliseconds() const { return ToSeconds() * 1000.0; } |
| 111 |
double ToMicroseconds() const { return ToMilliseconds() * 1000.0; } |
| 112 |
|
| 113 |
// Using a double here is safe enough; with 53 bits we can represent |
| 114 |
// durations up to over 280,000 years exactly. If the units of |
| 115 |
// mValue do not allow us to represent durations of that length, |
| 116 |
// long durations are clamped to the max/min representable value |
| 117 |
// instead of overflowing. |
| 118 |
static inline BaseTimeDuration FromSeconds(double aSeconds) { |
| 119 |
return FromMilliseconds(aSeconds * 1000.0); |
| 120 |
} |
| 121 |
static BaseTimeDuration FromMilliseconds(double aMilliseconds) { |
| 122 |
if (aMilliseconds == PositiveInfinity<double>()) { |
| 123 |
return Forever(); |
| 124 |
} |
| 125 |
if (aMilliseconds == NegativeInfinity<double>()) { |
| 126 |
return FromTicks(INT64_MIN); |
| 127 |
} |
| 128 |
return FromTicks( |
| 129 |
BaseTimeDurationPlatformUtils::TicksFromMilliseconds(aMilliseconds)); |
| 130 |
} |
| 131 |
static inline BaseTimeDuration FromMicroseconds(double aMicroseconds) { |
| 132 |
return FromMilliseconds(aMicroseconds / 1000.0); |
| 133 |
} |
| 134 |
|
| 135 |
static constexpr BaseTimeDuration Zero() { return BaseTimeDuration(); } |
| 136 |
static constexpr BaseTimeDuration Forever() { return FromTicks(INT64_MAX); } |
| 137 |
|
| 138 |
BaseTimeDuration operator+(const BaseTimeDuration& aOther) const { |
| 139 |
return FromTicks(ValueCalculator::Add(mValue, aOther.mValue)); |
| 140 |
} |
| 141 |
BaseTimeDuration operator-(const BaseTimeDuration& aOther) const { |
| 142 |
return FromTicks(ValueCalculator::Subtract(mValue, aOther.mValue)); |
| 143 |
} |
| 144 |
BaseTimeDuration& operator+=(const BaseTimeDuration& aOther) { |
| 145 |
mValue = ValueCalculator::Add(mValue, aOther.mValue); |
| 146 |
return *this; |
| 147 |
} |
| 148 |
BaseTimeDuration& operator-=(const BaseTimeDuration& aOther) { |
| 149 |
mValue = ValueCalculator::Subtract(mValue, aOther.mValue); |
| 150 |
return *this; |
| 151 |
} |
| 152 |
BaseTimeDuration operator-() const { |
| 153 |
// We don't just use FromTicks(ValueCalculator::Subtract(0, mValue)) |
| 154 |
// since that won't give the correct result for -TimeDuration::Forever(). |
| 155 |
int64_t ticks; |
| 156 |
if (mValue == INT64_MAX) { |
| 157 |
ticks = INT64_MIN; |
| 158 |
} else if (mValue == INT64_MIN) { |
| 159 |
ticks = INT64_MAX; |
| 160 |
} else { |
| 161 |
ticks = -mValue; |
| 162 |
} |
| 163 |
|
| 164 |
return FromTicks(ticks); |
| 165 |
} |
| 166 |
|
| 167 |
static BaseTimeDuration Max(const BaseTimeDuration& aA, |
| 168 |
const BaseTimeDuration& aB) { |
| 169 |
return FromTicks(std::max(aA.mValue, aB.mValue)); |
| 170 |
} |
| 171 |
static BaseTimeDuration Min(const BaseTimeDuration& aA, |
| 172 |
const BaseTimeDuration& aB) { |
| 173 |
return FromTicks(std::min(aA.mValue, aB.mValue)); |
| 174 |
} |
| 175 |
|
| 176 |
#if defined(DEBUG) |
| 177 |
int64_t GetValue() const { return mValue; } |
| 178 |
#endif |
| 179 |
|
| 180 |
private: |
| 181 |
// Block double multiplier (slower, imprecise if long duration) - Bug 853398. |
| 182 |
// If required, use MultDouble explicitly and with care. |
| 183 |
BaseTimeDuration operator*(const double aMultiplier) const = delete; |
| 184 |
|
| 185 |
// Block double divisor (for the same reason, and because dividing by |
| 186 |
// fractional values would otherwise invoke the int64_t variant, and rounding |
| 187 |
// the passed argument can then cause divide-by-zero) - Bug 1147491. |
| 188 |
BaseTimeDuration operator/(const double aDivisor) const = delete; |
| 189 |
|
| 190 |
public: |
| 191 |
BaseTimeDuration MultDouble(double aMultiplier) const { |
| 192 |
return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); |
| 193 |
} |
| 194 |
BaseTimeDuration operator*(const int32_t aMultiplier) const { |
| 195 |
return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); |
| 196 |
} |
| 197 |
BaseTimeDuration operator*(const uint32_t aMultiplier) const { |
| 198 |
return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); |
| 199 |
} |
| 200 |
BaseTimeDuration operator*(const int64_t aMultiplier) const { |
| 201 |
return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); |
| 202 |
} |
| 203 |
BaseTimeDuration operator*(const uint64_t aMultiplier) const { |
| 204 |
if (aMultiplier > INT64_MAX) { |
| 205 |
return Forever(); |
| 206 |
} |
| 207 |
return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); |
| 208 |
} |
| 209 |
BaseTimeDuration operator/(const int64_t aDivisor) const { |
| 210 |
MOZ_ASSERT(aDivisor != 0, "Division by zero"); |
| 211 |
return FromTicks(ValueCalculator::Divide(mValue, aDivisor)); |
| 212 |
} |
| 213 |
double operator/(const BaseTimeDuration& aOther) const { |
| 214 |
MOZ_ASSERT(aOther.mValue != 0, "Division by zero"); |
| 215 |
return ValueCalculator::DivideDouble(mValue, aOther.mValue); |
| 216 |
} |
| 217 |
BaseTimeDuration operator%(const BaseTimeDuration& aOther) const { |
| 218 |
MOZ_ASSERT(aOther.mValue != 0, "Division by zero"); |
| 219 |
return FromTicks(ValueCalculator::Modulo(mValue, aOther.mValue)); |
| 220 |
} |
| 221 |
|
| 222 |
template <typename E> |
| 223 |
bool operator<(const BaseTimeDuration<E>& aOther) const { |
| 224 |
return mValue < aOther.mValue; |
| 225 |
} |
| 226 |
template <typename E> |
| 227 |
bool operator<=(const BaseTimeDuration<E>& aOther) const { |
| 228 |
return mValue <= aOther.mValue; |
| 229 |
} |
| 230 |
template <typename E> |
| 231 |
bool operator>=(const BaseTimeDuration<E>& aOther) const { |
| 232 |
return mValue >= aOther.mValue; |
| 233 |
} |
| 234 |
template <typename E> |
| 235 |
bool operator>(const BaseTimeDuration<E>& aOther) const { |
| 236 |
return mValue > aOther.mValue; |
| 237 |
} |
| 238 |
template <typename E> |
| 239 |
bool operator==(const BaseTimeDuration<E>& aOther) const { |
| 240 |
return mValue == aOther.mValue; |
| 241 |
} |
| 242 |
template <typename E> |
| 243 |
bool operator!=(const BaseTimeDuration<E>& aOther) const { |
| 244 |
return mValue != aOther.mValue; |
| 245 |
} |
| 246 |
bool IsZero() const { return mValue == 0; } |
| 247 |
explicit operator bool() const { return mValue != 0; } |
| 248 |
|
| 249 |
friend std::ostream& operator<<(std::ostream& aStream, |
| 250 |
const BaseTimeDuration& aDuration) { |
| 251 |
return aStream << aDuration.ToMilliseconds() << " ms"; |
| 252 |
} |
| 253 |
|
| 254 |
// Return a best guess at the system's current timing resolution, |
| 255 |
// which might be variable. BaseTimeDurations below this order of |
| 256 |
// magnitude are meaningless, and those at the same order of |
| 257 |
// magnitude or just above are suspect. |
| 258 |
static BaseTimeDuration Resolution() { |
| 259 |
return FromTicks(BaseTimeDurationPlatformUtils::ResolutionInTicks()); |
| 260 |
} |
| 261 |
|
| 262 |
// We could define additional operators here: |
| 263 |
// -- convert to/from other time units |
| 264 |
// -- scale duration by a float |
| 265 |
// but let's do that on demand. |
| 266 |
// Comparing durations for equality will only lead to bugs on |
| 267 |
// platforms with high-resolution timers. |
| 268 |
|
| 269 |
private: |
| 270 |
friend class TimeStamp; |
| 271 |
friend struct IPC::ParamTraits<mozilla::BaseTimeDuration<ValueCalculator>>; |
| 272 |
template <typename> |
| 273 |
friend class BaseTimeDuration; |
| 274 |
|
| 275 |
static constexpr BaseTimeDuration FromTicks(int64_t aTicks) { |
| 276 |
BaseTimeDuration t; |
| 277 |
t.mValue = aTicks; |
| 278 |
return t; |
| 279 |
} |
| 280 |
|
| 281 |
static BaseTimeDuration FromTicks(double aTicks) { |
| 282 |
// NOTE: this MUST be a >= test, because int64_t(double(INT64_MAX)) |
| 283 |
// overflows and gives INT64_MIN. |
| 284 |
if (aTicks >= double(INT64_MAX)) { |
| 285 |
return FromTicks(INT64_MAX); |
| 286 |
} |
| 287 |
|
| 288 |
// This MUST be a <= test. |
| 289 |
if (aTicks <= double(INT64_MIN)) { |
| 290 |
return FromTicks(INT64_MIN); |
| 291 |
} |
| 292 |
|
| 293 |
return FromTicks(int64_t(aTicks)); |
| 294 |
} |
| 295 |
|
| 296 |
// Duration, result is implementation-specific difference of two TimeStamps |
| 297 |
int64_t mValue; |
| 298 |
}; |
| 299 |
|
| 300 |
/** |
| 301 |
* Perform arithmetic operations on the value of a BaseTimeDuration without |
| 302 |
* doing strict checks on the range of values. |
| 303 |
*/ |
| 304 |
class TimeDurationValueCalculator { |
| 305 |
public: |
| 306 |
static int64_t Add(int64_t aA, int64_t aB) { return aA + aB; } |
| 307 |
static int64_t Subtract(int64_t aA, int64_t aB) { return aA - aB; } |
| 308 |
|
| 309 |
template <typename T> |
| 310 |
static int64_t Multiply(int64_t aA, T aB) { |
| 311 |
static_assert(std::is_integral_v<T>, |
| 312 |
"Using integer multiplication routine with non-integer type." |
| 313 |
" Further specialization required"); |
| 314 |
return aA * static_cast<int64_t>(aB); |
| 315 |
} |
| 316 |
|
| 317 |
static int64_t Divide(int64_t aA, int64_t aB) { return aA / aB; } |
| 318 |
static double DivideDouble(int64_t aA, int64_t aB) { |
| 319 |
return static_cast<double>(aA) / aB; |
| 320 |
} |
| 321 |
static int64_t Modulo(int64_t aA, int64_t aB) { return aA % aB; } |
| 322 |
}; |
| 323 |
|
| 324 |
template <> |
| 325 |
inline int64_t TimeDurationValueCalculator::Multiply<double>(int64_t aA, |
| 326 |
double aB) { |
| 327 |
return static_cast<int64_t>(aA * aB); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Specialization of BaseTimeDuration that uses TimeDurationValueCalculator for |
| 332 |
* arithmetic on the mValue member. |
| 333 |
* |
| 334 |
* Use this class for time durations that are *not* expected to hold values of |
| 335 |
* Forever (or the negative equivalent) or when such time duration are *not* |
| 336 |
* expected to be used in arithmetic operations. |
| 337 |
*/ |
| 338 |
typedef BaseTimeDuration<TimeDurationValueCalculator> TimeDuration; |
| 339 |
|
| 340 |
/** |
| 341 |
* Instances of this class represent moments in time, or a special |
| 342 |
* "null" moment. We do not use the non-monotonic system clock or |
| 343 |
* local time, since they can be reset, causing apparent backward |
| 344 |
* travel in time, which can confuse algorithms. Instead we measure |
| 345 |
* elapsed time according to the system. This time can never go |
| 346 |
* backwards (i.e. it never wraps around, at least not in less than |
| 347 |
* five million years of system elapsed time). It might not advance |
| 348 |
* while the system is sleeping. If TimeStamp::SetNow() is not called |
| 349 |
* at all for hours or days, we might not notice the passage of some |
| 350 |
* of that time. |
| 351 |
* |
| 352 |
* We deliberately do not expose a way to convert TimeStamps to some |
| 353 |
* particular unit. All you can do is compute a difference between two |
| 354 |
* TimeStamps to get a TimeDuration. You can also add a TimeDuration |
| 355 |
* to a TimeStamp to get a new TimeStamp. You can't do something |
| 356 |
* meaningless like add two TimeStamps. |
| 357 |
* |
| 358 |
* Internally this is implemented as either a wrapper around |
| 359 |
* - high-resolution, monotonic, system clocks if they exist on this |
| 360 |
* platform |
| 361 |
* - PRIntervalTime otherwise. We detect wraparounds of |
| 362 |
* PRIntervalTime and work around them. |
| 363 |
* |
| 364 |
* This class is similar to C++11's time_point, however it is |
| 365 |
* explicitly nullable and provides an IsNull() method. time_point |
| 366 |
* is initialized to the clock's epoch and provides a |
| 367 |
* time_since_epoch() method that functions similiarly. i.e. |
| 368 |
* t.IsNull() is equivalent to t.time_since_epoch() == |
| 369 |
* decltype(t)::duration::zero(); |
| 370 |
* |
| 371 |
* Note that, since TimeStamp objects are small, prefer to pass them by value |
| 372 |
* unless there is a specific reason not to do so. |
| 373 |
*/ |
| 374 |
#if defined(XP_WIN) |
| 375 |
// If this static_assert fails then possibly the warning comment below is no |
| 376 |
// longer valid and should be removed. |
| 377 |
static_assert(sizeof(TimeStampValue) > 8); |
| 378 |
#endif |
| 379 |
/* |
| 380 |
* WARNING: On Windows, each TimeStamp is represented internally by two |
| 381 |
* different raw values (one from GTC and one from QPC) and which value gets |
| 382 |
* used for a given operation depends on whether both operands have QPC values |
| 383 |
* or not. This duality of values can lead to some surprising results when |
| 384 |
* mixing TimeStamps with and without QPC values, such as comparisons being |
| 385 |
* non-transitive (ie, a > b > c might not imply a > c). See bug 1829983 for |
| 386 |
* more details/an example. |
| 387 |
*/ |
| 388 |
class TimeStamp { |
| 389 |
public: |
| 390 |
using DurationType = TimeDuration; |
| 391 |
/** |
| 392 |
* Initialize to the "null" moment |
| 393 |
*/ |
| 394 |
constexpr TimeStamp() : mValue(0) {} |
| 395 |
// Default copy-constructor and assignment are OK |
| 396 |
|
| 397 |
/** |
| 398 |
* The system timestamps are the same as the TimeStamp |
| 399 |
* retrieved by mozilla::TimeStamp. Since we need this for |
| 400 |
* vsync timestamps, we enable the creation of mozilla::TimeStamps |
| 401 |
* on platforms that support vsync aligned refresh drivers / compositors |
| 402 |
* Verified true as of Jan 31, 2015: B2G and OS X |
| 403 |
* False on Windows 7 |
| 404 |
* Android's event time uses CLOCK_MONOTONIC via SystemClock.uptimeMilles. |
| 405 |
* So it is same value of TimeStamp posix implementation. |
| 406 |
* Wayland/GTK event time also uses CLOCK_MONOTONIC on Weston/Mutter |
| 407 |
* compositors. |
| 408 |
* UNTESTED ON OTHER PLATFORMS |
| 409 |
*/ |
| 410 |
#if defined(XP_DARWIN) || defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GTK) |
| 411 |
static TimeStamp FromSystemTime(int64_t aSystemTime) { |
| 412 |
static_assert(sizeof(aSystemTime) == sizeof(TimeStampValue), |
| 413 |
"System timestamp should be same units as TimeStampValue"); |
| 414 |
return TimeStamp(aSystemTime); |
| 415 |
} |
| 416 |
#endif |
| 417 |
|
| 418 |
/** |
| 419 |
* Return true if this is the "null" moment |
| 420 |
*/ |
| 421 |
constexpr bool IsNull() const { return mValue == 0; } |
| 422 |
|
| 423 |
/** |
| 424 |
* Return true if this is not the "null" moment, may be used in tests, e.g.: |
| 425 |
* |if (timestamp) { ... }| |
| 426 |
*/ |
| 427 |
explicit operator bool() const { return mValue != 0; } |
| 428 |
|
| 429 |
/** |
| 430 |
* Return a timestamp reflecting the current elapsed system time. This |
| 431 |
* is monotonically increasing (i.e., does not decrease) over the |
| 432 |
* lifetime of this process' XPCOM session. |
| 433 |
* |
| 434 |
* Now() is trying to ensure the best possible precision on each platform, |
| 435 |
* at least one millisecond. |
| 436 |
* |
| 437 |
* NowLoRes() has been introduced to workaround performance problems of |
| 438 |
* QueryPerformanceCounter on the Windows platform. NowLoRes() is giving |
| 439 |
* lower precision, usually 15.6 ms, but with very good performance benefit. |
| 440 |
* Use it for measurements of longer times, like >200ms timeouts. |
| 441 |
*/ |
| 442 |
static TimeStamp Now() { return Now(true); } |
| 443 |
static TimeStamp NowLoRes() { return Now(false); } |
| 444 |
|
| 445 |
/** |
| 446 |
* Return a timestamp representing the time when the current process was |
| 447 |
* created which will be comparable with other timestamps taken with this |
| 448 |
* class. |
| 449 |
* |
| 450 |
* @returns A timestamp representing the time when the process was created |
| 451 |
*/ |
| 452 |
static MFBT_API TimeStamp ProcessCreation(); |
| 453 |
|
| 454 |
/** |
| 455 |
* Return the very first timestamp that was taken. This can be used instead |
| 456 |
* of TimeStamp::ProcessCreation() by code that might not allow running the |
| 457 |
* complex logic required to compute the real process creation. This will |
| 458 |
* necessarily have been recorded sometimes after TimeStamp::ProcessCreation() |
| 459 |
* or at best should be equal to it. |
| 460 |
* |
| 461 |
* @returns The first tiemstamp that was taken by this process |
| 462 |
*/ |
| 463 |
static MFBT_API TimeStamp FirstTimeStamp(); |
| 464 |
|
| 465 |
/** |
| 466 |
* Records a process restart. After this call ProcessCreation() will return |
| 467 |
* the time when the browser was restarted instead of the actual time when |
| 468 |
* the process was created. |
| 469 |
*/ |
| 470 |
static MFBT_API void RecordProcessRestart(); |
| 471 |
|
| 472 |
#ifdef XP_LINUX |
| 473 |
uint64_t RawClockMonotonicNanosecondsSinceBoot() const { |
| 474 |
return static_cast<uint64_t>(mValue); |
| 475 |
} |
| 476 |
#endif |
| 477 |
|
| 478 |
#ifdef XP_DARWIN |
| 479 |
// Returns the number of nanoseconds since the mach_absolute_time origin. |
| 480 |
MFBT_API uint64_t RawMachAbsoluteTimeNanoseconds() const; |
| 481 |
#endif |
| 482 |
|
| 483 |
#ifdef XP_WIN |
| 484 |
Maybe<uint64_t> RawQueryPerformanceCounterValue() const { |
| 485 |
// mQPC is stored in `mt` i.e. QueryPerformanceCounter * 1000 |
| 486 |
// so divide out the 1000 |
| 487 |
return mValue.mHasQPC ? Some(mValue.mQPC / 1000ULL) : Nothing(); |
| 488 |
} |
| 489 |
#endif |
| 490 |
|
| 491 |
/** |
| 492 |
* Compute the difference between two timestamps. Both must be non-null. |
| 493 |
*/ |
| 494 |
TimeDuration operator-(const TimeStamp& aOther) const { |
| 495 |
MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 496 |
MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); |
| 497 |
static_assert(-INT64_MAX > INT64_MIN, "int64_t sanity check"); |
| 498 |
int64_t ticks = int64_t(mValue - aOther.mValue); |
| 499 |
// Check for overflow. |
| 500 |
if (mValue > aOther.mValue) { |
| 501 |
if (ticks < 0) { |
| 502 |
ticks = INT64_MAX; |
| 503 |
} |
| 504 |
} else { |
| 505 |
if (ticks > 0) { |
| 506 |
ticks = INT64_MIN; |
| 507 |
} |
| 508 |
} |
| 509 |
return TimeDuration::FromTicks(ticks); |
| 510 |
} |
| 511 |
|
| 512 |
TimeStamp operator+(const TimeDuration& aOther) const { |
| 513 |
TimeStamp result = *this; |
| 514 |
result += aOther; |
| 515 |
return result; |
| 516 |
} |
| 517 |
TimeStamp operator-(const TimeDuration& aOther) const { |
| 518 |
TimeStamp result = *this; |
| 519 |
result -= aOther; |
| 520 |
return result; |
| 521 |
} |
| 522 |
TimeStamp& operator+=(const TimeDuration& aOther) { |
| 523 |
MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 524 |
TimeStampValue value = mValue + aOther.mValue; |
| 525 |
// Check for underflow. |
| 526 |
// (We don't check for overflow because it's not obvious what the error |
| 527 |
// behavior should be in that case.) |
| 528 |
if (aOther.mValue < 0 && value > mValue) { |
| 529 |
value = 0; |
| 530 |
} |
| 531 |
mValue = value; |
| 532 |
return *this; |
| 533 |
} |
| 534 |
TimeStamp& operator-=(const TimeDuration& aOther) { |
| 535 |
MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 536 |
TimeStampValue value = mValue - aOther.mValue; |
| 537 |
// Check for underflow. |
| 538 |
// (We don't check for overflow because it's not obvious what the error |
| 539 |
// behavior should be in that case.) |
| 540 |
if (aOther.mValue > 0 && value > mValue) { |
| 541 |
value = 0; |
| 542 |
} |
| 543 |
mValue = value; |
| 544 |
return *this; |
| 545 |
} |
| 546 |
|
| 547 |
constexpr bool operator<(const TimeStamp& aOther) const { |
| 548 |
MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 549 |
MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); |
| 550 |
return mValue < aOther.mValue; |
| 551 |
} |
| 552 |
constexpr bool operator<=(const TimeStamp& aOther) const { |
| 553 |
MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 554 |
MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); |
| 555 |
return mValue <= aOther.mValue; |
| 556 |
} |
| 557 |
constexpr bool operator>=(const TimeStamp& aOther) const { |
| 558 |
MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 559 |
MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); |
| 560 |
return mValue >= aOther.mValue; |
| 561 |
} |
| 562 |
constexpr bool operator>(const TimeStamp& aOther) const { |
| 563 |
MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 564 |
MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); |
| 565 |
return mValue > aOther.mValue; |
| 566 |
} |
| 567 |
bool operator==(const TimeStamp& aOther) const { |
| 568 |
return IsNull() ? aOther.IsNull() |
| 569 |
: !aOther.IsNull() && mValue == aOther.mValue; |
| 570 |
} |
| 571 |
bool operator!=(const TimeStamp& aOther) const { return !(*this == aOther); } |
| 572 |
|
| 573 |
// Comparing TimeStamps for equality should be discouraged. Adding |
| 574 |
// two TimeStamps, or scaling TimeStamps, is nonsense and must never |
| 575 |
// be allowed. |
| 576 |
|
| 577 |
static MFBT_API void Startup(); |
| 578 |
static MFBT_API void Shutdown(); |
| 579 |
|
| 580 |
#if defined(DEBUG) |
| 581 |
TimeStampValue GetValue() const { return mValue; } |
| 582 |
#endif |
| 583 |
|
| 584 |
private: |
| 585 |
friend struct IPC::ParamTraits<mozilla::TimeStamp>; |
| 586 |
friend struct TimeStampInitialization; |
| 587 |
friend class TimeStampTests; |
| 588 |
|
| 589 |
constexpr MOZ_IMPLICIT TimeStamp(TimeStampValue aValue) : mValue(aValue) {} |
| 590 |
|
| 591 |
static MFBT_API TimeStamp Now(bool aHighResolution); |
| 592 |
|
| 593 |
/** |
| 594 |
* Computes the uptime of the current process in microseconds. The result |
| 595 |
* is platform-dependent and needs to be checked against existing timestamps |
| 596 |
* for consistency. |
| 597 |
* |
| 598 |
* @returns The number of microseconds since the calling process was started |
| 599 |
* or 0 if an error was encountered while computing the uptime |
| 600 |
*/ |
| 601 |
static MFBT_API uint64_t ComputeProcessUptime(); |
| 602 |
|
| 603 |
/** |
| 604 |
* When built with PRIntervalTime, a value of 0 means this instance |
| 605 |
* is "null". Otherwise, the low 32 bits represent a PRIntervalTime, |
| 606 |
* and the high 32 bits represent a counter of the number of |
| 607 |
* rollovers of PRIntervalTime that we've seen. This counter starts |
| 608 |
* at 1 to avoid a real time colliding with the "null" value. |
| 609 |
* |
| 610 |
* PR_INTERVAL_MAX is set at 100,000 ticks per second. So the minimum |
| 611 |
* time to wrap around is about 2^64/100000 seconds, i.e. about |
| 612 |
* 5,849,424 years. |
| 613 |
* |
| 614 |
* When using a system clock, a value is system dependent. |
| 615 |
*/ |
| 616 |
TimeStampValue mValue; |
| 617 |
}; |
| 618 |
|
| 619 |
} // namespace mozilla |
| 620 |
|
| 621 |
#endif /* mozilla_TimeStamp_h */ |