1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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/. */
7 #ifndef mozilla_TimeStamp_h
8 #define mozilla_TimeStamp_h
11 #include "mozilla/Assertions.h"
12 #include "mozilla/Attributes.h"
17 template<typename T
> struct ParamTraits
;
21 // defines TimeStampValue as a complex value keeping both
22 // GetTickCount and QueryPerformanceCounter values
23 #include "TimeStamp_windows.h"
29 typedef uint64_t TimeStampValue
;
35 * Instances of this class represent the length of an interval of time.
36 * Negative durations are allowed, meaning the end is before the start.
38 * Internally the duration is stored as a int64_t in units of
39 * PR_TicksPerSecond() when building with NSPR interval timers, or a
40 * system-dependent unit when building with system clocks. The
41 * system-dependent unit must be constant, otherwise the semantics of
42 * this class would be broken.
47 // The default duration is 0.
48 MOZ_CONSTEXPR
TimeDuration() : mValue(0) {}
49 // Allow construction using '0' as the initial value, for readability,
50 // but no other numbers (so we don't have any implicit unit conversions).
51 struct _SomethingVeryRandomHere
;
52 MOZ_IMPLICIT
TimeDuration(_SomethingVeryRandomHere
* aZero
) : mValue(0)
54 MOZ_ASSERT(!aZero
, "Who's playing funny games here?");
56 // Default copy-constructor and assignment are OK
58 double ToSeconds() const;
59 // Return a duration value that includes digits of time we think to
60 // be significant. This method should be used when displaying a
62 double ToSecondsSigDigits() const;
63 double ToMilliseconds() const { return ToSeconds() * 1000.0; }
64 double ToMicroseconds() const { return ToMilliseconds() * 1000.0; }
66 // Using a double here is safe enough; with 53 bits we can represent
67 // durations up to over 280,000 years exactly. If the units of
68 // mValue do not allow us to represent durations of that length,
69 // long durations are clamped to the max/min representable value
70 // instead of overflowing.
71 static inline TimeDuration
FromSeconds(double aSeconds
)
73 return FromMilliseconds(aSeconds
* 1000.0);
75 static TimeDuration
FromMilliseconds(double aMilliseconds
);
76 static inline TimeDuration
FromMicroseconds(double aMicroseconds
)
78 return FromMilliseconds(aMicroseconds
/ 1000.0);
81 static TimeDuration
Forever()
83 return FromTicks(INT64_MAX
);
86 TimeDuration
operator+(const TimeDuration
& aOther
) const
88 return TimeDuration::FromTicks(mValue
+ aOther
.mValue
);
90 TimeDuration
operator-(const TimeDuration
& aOther
) const
92 return TimeDuration::FromTicks(mValue
- aOther
.mValue
);
94 TimeDuration
& operator+=(const TimeDuration
& aOther
)
96 mValue
+= aOther
.mValue
;
99 TimeDuration
& operator-=(const TimeDuration
& aOther
)
101 mValue
-= aOther
.mValue
;
106 // Block double multiplier (slower, imprecise if long duration) - Bug 853398.
107 // If required, use MultDouble explicitly and with care.
108 TimeDuration
operator*(const double aMultiplier
) const MOZ_DELETE
;
111 TimeDuration
MultDouble(double aMultiplier
) const
113 return TimeDuration::FromTicks(static_cast<int64_t>(mValue
* aMultiplier
));
115 TimeDuration
operator*(const int32_t aMultiplier
) const
117 return TimeDuration::FromTicks(mValue
* int64_t(aMultiplier
));
119 TimeDuration
operator*(const uint32_t aMultiplier
) const
121 return TimeDuration::FromTicks(mValue
* int64_t(aMultiplier
));
123 TimeDuration
operator*(const int64_t aMultiplier
) const
125 return TimeDuration::FromTicks(mValue
* aMultiplier
);
127 TimeDuration
operator*(const uint64_t aMultiplier
) const
129 if (aMultiplier
> INT64_MAX
) {
130 NS_WARNING("Out-of-range multiplier when multiplying TimeDuration");
131 return TimeDuration::Forever();
133 return TimeDuration::FromTicks(mValue
* int64_t(aMultiplier
));
135 TimeDuration
operator/(const int64_t aDivisor
) const
137 return TimeDuration::FromTicks(mValue
/ aDivisor
);
139 double operator/(const TimeDuration
& aOther
) const
141 return static_cast<double>(mValue
) / aOther
.mValue
;
143 TimeDuration
operator%(const TimeDuration
& aOther
) const
145 MOZ_ASSERT(aOther
.mValue
!= 0, "Division by zero");
146 return TimeDuration::FromTicks(mValue
% aOther
.mValue
);
149 bool operator<(const TimeDuration
& aOther
) const
151 return mValue
< aOther
.mValue
;
153 bool operator<=(const TimeDuration
& aOther
) const
155 return mValue
<= aOther
.mValue
;
157 bool operator>=(const TimeDuration
& aOther
) const
159 return mValue
>= aOther
.mValue
;
161 bool operator>(const TimeDuration
& aOther
) const
163 return mValue
> aOther
.mValue
;
165 bool operator==(const TimeDuration
& aOther
) const
167 return mValue
== aOther
.mValue
;
169 bool operator!=(const TimeDuration
& aOther
) const
171 return mValue
!= aOther
.mValue
;
174 // Return a best guess at the system's current timing resolution,
175 // which might be variable. TimeDurations below this order of
176 // magnitude are meaningless, and those at the same order of
177 // magnitude or just above are suspect.
178 static TimeDuration
Resolution();
180 // We could define additional operators here:
181 // -- convert to/from other time units
182 // -- scale duration by a float
183 // but let's do that on demand.
184 // Comparing durations for equality will only lead to bugs on
185 // platforms with high-resolution timers.
188 friend class TimeStamp
;
189 friend struct IPC::ParamTraits
<mozilla::TimeDuration
>;
191 static TimeDuration
FromTicks(int64_t aTicks
)
198 static TimeDuration
FromTicks(double aTicks
)
200 // NOTE: this MUST be a >= test, because int64_t(double(INT64_MAX))
201 // overflows and gives INT64_MIN.
202 if (aTicks
>= double(INT64_MAX
)) {
203 return TimeDuration::FromTicks(INT64_MAX
);
206 // This MUST be a <= test.
207 if (aTicks
<= double(INT64_MIN
)) {
208 return TimeDuration::FromTicks(INT64_MIN
);
211 return TimeDuration::FromTicks(int64_t(aTicks
));
214 // Duration, result is implementation-specific difference of two TimeStamps
219 * Instances of this class represent moments in time, or a special
220 * "null" moment. We do not use the non-monotonic system clock or
221 * local time, since they can be reset, causing apparent backward
222 * travel in time, which can confuse algorithms. Instead we measure
223 * elapsed time according to the system. This time can never go
224 * backwards (i.e. it never wraps around, at least not in less than
225 * five million years of system elapsed time). It might not advance
226 * while the system is sleeping. If TimeStamp::SetNow() is not called
227 * at all for hours or days, we might not notice the passage of some
230 * We deliberately do not expose a way to convert TimeStamps to some
231 * particular unit. All you can do is compute a difference between two
232 * TimeStamps to get a TimeDuration. You can also add a TimeDuration
233 * to a TimeStamp to get a new TimeStamp. You can't do something
234 * meaningless like add two TimeStamps.
236 * Internally this is implemented as either a wrapper around
237 * - high-resolution, monotonic, system clocks if they exist on this
239 * - PRIntervalTime otherwise. We detect wraparounds of
240 * PRIntervalTime and work around them.
242 * This class is similar to C++11's time_point, however it is
243 * explicitly nullable and provides an IsNull() method. time_point
244 * is initialized to the clock's epoch and provides a
245 * time_since_epoch() method that functions similiarly. i.e.
246 * t.IsNull() is equivalent to t.time_since_epoch() == decltype(t)::duration::zero();
252 * Initialize to the "null" moment
254 MOZ_CONSTEXPR
TimeStamp() : mValue(0) {}
255 // Default copy-constructor and assignment are OK
258 * Return true if this is the "null" moment
260 bool IsNull() const { return mValue
== 0; }
263 * Return a timestamp reflecting the current elapsed system time. This
264 * is monotonically increasing (i.e., does not decrease) over the
265 * lifetime of this process' XPCOM session.
267 * Now() is trying to ensure the best possible precision on each platform,
268 * at least one millisecond.
270 * NowLoRes() has been introduced to workaround performance problems of
271 * QueryPerformanceCounter on the Windows platform. NowLoRes() is giving
272 * lower precision, usually 15.6 ms, but with very good performance benefit.
273 * Use it for measurements of longer times, like >200ms timeouts.
275 static TimeStamp
Now() { return Now(true); }
276 static TimeStamp
NowLoRes() { return Now(false); }
279 * Return a timestamp representing the time when the current process was
280 * created which will be comparable with other timestamps taken with this
281 * class. If the actual process creation time is detected to be inconsistent
282 * the @a aIsInconsistent parameter will be set to true, the returned
283 * timestamp however will still be valid though inaccurate.
285 * @param aIsInconsistent Set to true if an inconsistency was detected in the
286 * process creation time
287 * @returns A timestamp representing the time when the process was created,
288 * this timestamp is always valid even when errors are reported
290 static TimeStamp
ProcessCreation(bool& aIsInconsistent
);
293 * Records a process restart. After this call ProcessCreation() will return
294 * the time when the browser was restarted instead of the actual time when
295 * the process was created.
297 static void RecordProcessRestart();
300 * Compute the difference between two timestamps. Both must be non-null.
302 TimeDuration
operator-(const TimeStamp
& aOther
) const
304 MOZ_ASSERT(!IsNull(), "Cannot compute with a null value");
305 MOZ_ASSERT(!aOther
.IsNull(), "Cannot compute with aOther null value");
306 static_assert(-INT64_MAX
> INT64_MIN
, "int64_t sanity check");
307 int64_t ticks
= int64_t(mValue
- aOther
.mValue
);
308 // Check for overflow.
309 if (mValue
> aOther
.mValue
) {
318 return TimeDuration::FromTicks(ticks
);
321 TimeStamp
operator+(const TimeDuration
& aOther
) const
323 MOZ_ASSERT(!IsNull(), "Cannot compute with a null value");
324 return TimeStamp(mValue
+ aOther
.mValue
);
326 TimeStamp
operator-(const TimeDuration
& aOther
) const
328 MOZ_ASSERT(!IsNull(), "Cannot compute with a null value");
329 return TimeStamp(mValue
- aOther
.mValue
);
331 TimeStamp
& operator+=(const TimeDuration
& aOther
)
333 MOZ_ASSERT(!IsNull(), "Cannot compute with a null value");
334 mValue
+= aOther
.mValue
;
337 TimeStamp
& operator-=(const TimeDuration
& aOther
)
339 MOZ_ASSERT(!IsNull(), "Cannot compute with a null value");
340 mValue
-= aOther
.mValue
;
344 bool operator<(const TimeStamp
& aOther
) const
346 MOZ_ASSERT(!IsNull(), "Cannot compute with a null value");
347 MOZ_ASSERT(!aOther
.IsNull(), "Cannot compute with aOther null value");
348 return mValue
< aOther
.mValue
;
350 bool operator<=(const TimeStamp
& aOther
) const
352 MOZ_ASSERT(!IsNull(), "Cannot compute with a null value");
353 MOZ_ASSERT(!aOther
.IsNull(), "Cannot compute with aOther null value");
354 return mValue
<= aOther
.mValue
;
356 bool operator>=(const TimeStamp
& aOther
) const
358 MOZ_ASSERT(!IsNull(), "Cannot compute with a null value");
359 MOZ_ASSERT(!aOther
.IsNull(), "Cannot compute with aOther null value");
360 return mValue
>= aOther
.mValue
;
362 bool operator>(const TimeStamp
& aOther
) const
364 MOZ_ASSERT(!IsNull(), "Cannot compute with a null value");
365 MOZ_ASSERT(!aOther
.IsNull(), "Cannot compute with aOther null value");
366 return mValue
> aOther
.mValue
;
368 bool operator==(const TimeStamp
& aOther
) const
370 // Maybe it's ok to check == with null timestamps?
371 MOZ_ASSERT(!IsNull() && "Cannot compute with a null value");
372 MOZ_ASSERT(!aOther
.IsNull(), "Cannot compute with aOther null value");
373 return mValue
== aOther
.mValue
;
375 bool operator!=(const TimeStamp
& aOther
) const
377 // Maybe it's ok to check != with null timestamps?
378 MOZ_ASSERT(!IsNull(), "Cannot compute with a null value");
379 MOZ_ASSERT(!aOther
.IsNull(), "Cannot compute with aOther null value");
380 return mValue
!= aOther
.mValue
;
383 // Comparing TimeStamps for equality should be discouraged. Adding
384 // two TimeStamps, or scaling TimeStamps, is nonsense and must never
387 static nsresult
Startup();
388 static void Shutdown();
391 friend struct IPC::ParamTraits
<mozilla::TimeStamp
>;
392 friend void StartupTimelineRecordExternal(int, uint64_t);
394 MOZ_IMPLICIT
TimeStamp(TimeStampValue aValue
) : mValue(aValue
) {}
396 static TimeStamp
Now(bool aHighResolution
);
399 * Computes the uptime of the current process in microseconds. The result
400 * is platform-dependent and needs to be checked against existing timestamps
403 * @returns The number of microseconds since the calling process was started
404 * or 0 if an error was encountered while computing the uptime
406 static uint64_t ComputeProcessUptime();
409 * When built with PRIntervalTime, a value of 0 means this instance
410 * is "null". Otherwise, the low 32 bits represent a PRIntervalTime,
411 * and the high 32 bits represent a counter of the number of
412 * rollovers of PRIntervalTime that we've seen. This counter starts
413 * at 1 to avoid a real time colliding with the "null" value.
415 * PR_INTERVAL_MAX is set at 100,000 ticks per second. So the minimum
416 * time to wrap around is about 2^64/100000 seconds, i.e. about
419 * When using a system clock, a value is system dependent.
421 TimeStampValue mValue
;
426 #endif /* mozilla_TimeStamp_h */