2015-08-11 07:38:18 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef SystemTimeConverter_h
|
|
|
|
#define SystemTimeConverter_h
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
#include "mozilla/TimeStamp.h"
|
2015-02-19 08:05:42 +03:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2015-08-11 07:38:18 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-08-11 11:11:41 +03:00
|
|
|
// Utility class that converts time values represented as an unsigned integral
|
|
|
|
// number of milliseconds from one time source (e.g. a native event time) to
|
|
|
|
// corresponding mozilla::TimeStamp objects.
|
2015-08-11 07:38:18 +03:00
|
|
|
//
|
2015-08-11 11:11:41 +03:00
|
|
|
// This class handles wrapping of integer values and skew between the time
|
|
|
|
// source and mozilla::TimeStamp values.
|
|
|
|
//
|
|
|
|
// It does this by using an historical reference time recorded in both time
|
2015-08-11 07:38:18 +03:00
|
|
|
// scales (i.e. both as a numerical time value and as a TimeStamp).
|
|
|
|
//
|
|
|
|
// For performance reasons, this class is careful to minimize calls to the
|
|
|
|
// native "current time" function (e.g. gdk_x11_server_get_time) since this can
|
|
|
|
// be slow. Furthermore, it uses TimeStamp::NowLowRes instead of TimeStamp::Now
|
|
|
|
// except when establishing the reference time.
|
|
|
|
template <typename Time>
|
|
|
|
class SystemTimeConverter {
|
|
|
|
public:
|
|
|
|
SystemTimeConverter()
|
|
|
|
: mReferenceTime(Time(0))
|
|
|
|
, mReferenceTimeStamp() // Initializes to the null timestamp
|
2015-08-11 11:11:41 +03:00
|
|
|
, mLastBackwardsSkewCheck(Time(0))
|
2015-08-11 07:38:18 +03:00
|
|
|
, kTimeRange(std::numeric_limits<Time>::max())
|
|
|
|
, kTimeHalfRange(kTimeRange / 2)
|
2015-08-11 11:11:41 +03:00
|
|
|
, kBackwardsSkewCheckInterval(Time(2000))
|
2015-02-19 08:05:42 +03:00
|
|
|
{
|
|
|
|
static_assert(!IsSigned<Time>::value, "Expected Time to be unsigned");
|
|
|
|
}
|
2015-08-11 07:38:18 +03:00
|
|
|
|
2015-02-19 08:10:00 +03:00
|
|
|
template <typename CurrentTimeGetter>
|
2015-08-11 07:38:18 +03:00
|
|
|
mozilla::TimeStamp
|
|
|
|
GetTimeStampFromSystemTime(Time aTime,
|
2015-02-19 08:10:00 +03:00
|
|
|
CurrentTimeGetter& aCurrentTimeGetter) {
|
2015-08-11 07:38:18 +03:00
|
|
|
// If the reference time is not set, use the current time value to fill
|
|
|
|
// it in.
|
|
|
|
if (mReferenceTimeStamp.IsNull()) {
|
2015-02-19 08:10:00 +03:00
|
|
|
UpdateReferenceTime(aTime, aCurrentTimeGetter);
|
2015-08-11 07:38:18 +03:00
|
|
|
}
|
|
|
|
TimeStamp roughlyNow = TimeStamp::NowLoRes();
|
|
|
|
|
2015-08-11 11:11:41 +03:00
|
|
|
// Check for skew between the source of Time values and TimeStamp values.
|
|
|
|
// We do this by comparing two durations (both in ms):
|
|
|
|
//
|
|
|
|
// i. The duration from the reference time to the passed-in time.
|
2015-08-12 07:19:14 +03:00
|
|
|
// (timeDelta in the diagram below)
|
2015-08-11 11:11:41 +03:00
|
|
|
// ii. The duration from the reference timestamp to the current time
|
|
|
|
// based on TimeStamp::NowLoRes.
|
2015-08-12 07:19:14 +03:00
|
|
|
// (timeStampDelta in the diagram below)
|
2015-08-11 11:11:41 +03:00
|
|
|
//
|
2015-08-12 07:19:14 +03:00
|
|
|
// Normally, we'd expect (ii) to be slightly larger than (i) to account
|
|
|
|
// for the time taken between generating the event and processing it.
|
|
|
|
//
|
|
|
|
// If (ii) - (i) is negative then the source of Time values is getting
|
|
|
|
// "ahead" of TimeStamp. We call this "forwards" skew below.
|
2015-08-11 11:11:41 +03:00
|
|
|
//
|
|
|
|
// For the reverse case, if (ii) - (i) is positive (and greater than some
|
|
|
|
// tolerance factor), then we may have "backwards" skew. This is often
|
|
|
|
// the case when we have a backlog of events and by the time we process
|
|
|
|
// them, the time given by the system is comparatively "old".
|
|
|
|
//
|
2015-08-12 07:19:14 +03:00
|
|
|
// We call the absolute difference between (i) and (ii), "deltaFromNow".
|
2015-08-11 11:11:41 +03:00
|
|
|
//
|
|
|
|
// Graphically:
|
|
|
|
//
|
|
|
|
// mReferenceTime aTime
|
|
|
|
// Time scale: ........+.......................*........
|
2015-08-12 07:19:14 +03:00
|
|
|
// |--------timeDelta------|
|
2015-08-11 07:38:18 +03:00
|
|
|
//
|
2015-08-11 11:11:41 +03:00
|
|
|
// mReferenceTimeStamp roughlyNow
|
|
|
|
// TimeStamp scale: ........+...........................*....
|
2015-08-12 07:19:14 +03:00
|
|
|
// |------timeStampDelta-------|
|
2015-08-11 07:38:18 +03:00
|
|
|
//
|
2015-08-11 11:11:41 +03:00
|
|
|
// |---|
|
|
|
|
// deltaFromNow
|
2015-08-11 07:38:18 +03:00
|
|
|
//
|
2015-08-12 07:19:14 +03:00
|
|
|
Time deltaFromNow;
|
|
|
|
bool newer = IsTimeNewerThanTimestamp(aTime, roughlyNow, &deltaFromNow);
|
2015-08-11 11:11:41 +03:00
|
|
|
|
|
|
|
// TimeStamp::NowLoRes should be accurate to within 15.6ms so we need to
|
|
|
|
// be at least that generous when detecting clock skew.
|
|
|
|
static const Time kTolerance = 30;
|
|
|
|
|
2015-08-12 07:19:14 +03:00
|
|
|
// Check for forwards skew
|
|
|
|
if (newer) {
|
2015-08-11 11:11:41 +03:00
|
|
|
// Make aTime correspond to roughlyNow
|
|
|
|
UpdateReferenceTime(aTime, roughlyNow);
|
|
|
|
|
|
|
|
// We didn't have backwards skew so don't bother checking for
|
|
|
|
// backwards skew again for a little while.
|
|
|
|
mLastBackwardsSkewCheck = aTime;
|
|
|
|
|
|
|
|
return roughlyNow;
|
2015-08-11 07:38:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:11:41 +03:00
|
|
|
if (deltaFromNow <= kTolerance) {
|
|
|
|
// If the time between event times and TimeStamp values is within
|
|
|
|
// the tolerance then assume we don't have clock skew so we can
|
|
|
|
// avoid checking for backwards skew for a while.
|
|
|
|
mLastBackwardsSkewCheck = aTime;
|
|
|
|
} else if (aTime - mLastBackwardsSkewCheck > kBackwardsSkewCheckInterval) {
|
|
|
|
aCurrentTimeGetter.GetTimeAsyncForPossibleBackwardsSkew(roughlyNow);
|
|
|
|
mLastBackwardsSkewCheck = aTime;
|
2015-08-11 07:38:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:11:41 +03:00
|
|
|
// Finally, calculate the timestamp
|
|
|
|
return roughlyNow - TimeDuration::FromMilliseconds(deltaFromNow);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CompensateForBackwardsSkew(Time aReferenceTime,
|
|
|
|
const TimeStamp &aLowerBound) {
|
|
|
|
// Check if we actually have backwards skew. Backwards skew looks like
|
|
|
|
// the following:
|
|
|
|
//
|
|
|
|
// mReferenceTime
|
|
|
|
// Time: ..+...a...b...c..........................
|
|
|
|
//
|
|
|
|
// mReferenceTimeStamp
|
|
|
|
// TimeStamp: ..+.....a.....b.....c....................
|
|
|
|
//
|
|
|
|
// Converted
|
|
|
|
// time: ......a'..b'..c'.........................
|
|
|
|
//
|
|
|
|
// What we need to do is bring mReferenceTime "forwards".
|
|
|
|
//
|
|
|
|
// Suppose when we get (c), we detect possible backwards skew and trigger
|
|
|
|
// an async request for the current time (which is passed in here as
|
|
|
|
// aReferenceTime).
|
|
|
|
//
|
|
|
|
// We end up with something like the following:
|
|
|
|
//
|
|
|
|
// mReferenceTime aReferenceTime
|
|
|
|
// Time: ..+...a...b...c...v......................
|
|
|
|
//
|
|
|
|
// mReferenceTimeStamp
|
|
|
|
// TimeStamp: ..+.....a.....b.....c..........x.........
|
|
|
|
// ^ ^
|
|
|
|
// aLowerBound TimeStamp::Now()
|
|
|
|
//
|
|
|
|
// If the duration (aLowerBound - mReferenceTimeStamp) is greater than
|
|
|
|
// (aReferenceTime - mReferenceTime) then we know we have backwards skew.
|
|
|
|
//
|
|
|
|
// If that's not the case, then we probably just got caught behind
|
|
|
|
// temporarily.
|
2015-08-12 07:19:14 +03:00
|
|
|
Time delta;
|
|
|
|
if (IsTimeNewerThanTimestamp(aReferenceTime, aLowerBound, &delta)) {
|
2015-08-11 11:11:41 +03:00
|
|
|
return;
|
2015-08-11 07:38:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:11:41 +03:00
|
|
|
// We have backwards skew; the equivalent TimeStamp for aReferenceTime lies
|
|
|
|
// somewhere between aLowerBound (which was the TimeStamp when we triggered
|
|
|
|
// the async request for the current time) and TimeStamp::Now().
|
|
|
|
//
|
|
|
|
// If aReferenceTime was waiting in the event queue for a long time, the
|
|
|
|
// equivalent TimeStamp might be much closer to aLowerBound than
|
|
|
|
// TimeStamp::Now() so for now we just set it to aLowerBound. That's
|
|
|
|
// guaranteed to be at least somewhat of an improvement.
|
|
|
|
UpdateReferenceTime(aReferenceTime, aLowerBound);
|
2015-08-11 07:38:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-02-19 08:10:00 +03:00
|
|
|
template <typename CurrentTimeGetter>
|
2015-08-11 07:38:18 +03:00
|
|
|
void
|
2015-08-11 11:11:41 +03:00
|
|
|
UpdateReferenceTime(Time aReferenceTime,
|
2015-02-19 08:10:00 +03:00
|
|
|
const CurrentTimeGetter& aCurrentTimeGetter) {
|
|
|
|
Time currentTime = aCurrentTimeGetter.GetCurrentTime();
|
2015-08-11 07:38:18 +03:00
|
|
|
TimeStamp currentTimeStamp = TimeStamp::Now();
|
2015-08-11 11:11:41 +03:00
|
|
|
Time timeSinceReference = currentTime - aReferenceTime;
|
|
|
|
TimeStamp referenceTimeStamp =
|
2015-08-11 07:38:18 +03:00
|
|
|
currentTimeStamp - TimeDuration::FromMilliseconds(timeSinceReference);
|
2015-08-11 11:11:41 +03:00
|
|
|
UpdateReferenceTime(aReferenceTime, referenceTimeStamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdateReferenceTime(Time aReferenceTime,
|
|
|
|
const TimeStamp& aReferenceTimeStamp) {
|
|
|
|
mReferenceTime = aReferenceTime;
|
|
|
|
mReferenceTimeStamp = aReferenceTimeStamp;
|
2015-08-11 07:38:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-12 07:19:14 +03:00
|
|
|
bool
|
|
|
|
IsTimeNewerThanTimestamp(Time aTime, TimeStamp aTimeStamp, Time* aDelta)
|
|
|
|
{
|
|
|
|
Time timeDelta = aTime - mReferenceTime;
|
|
|
|
|
|
|
|
// Cast the result to signed 64-bit integer first since that should be
|
|
|
|
// enough to hold the range of values returned by ToMilliseconds() and
|
|
|
|
// the result of converting from double to an integer-type when the value
|
|
|
|
// is outside the integer range is undefined.
|
|
|
|
// Then we do an implicit cast to Time (typically an unsigned 32-bit
|
|
|
|
// integer) which wraps times outside that range.
|
|
|
|
MOZ_ASSERT(mReferenceTimeStamp <= aTimeStamp,
|
|
|
|
"Got a negative timestamp delta");
|
|
|
|
Time timeStampDelta =
|
|
|
|
static_cast<int64_t>((aTimeStamp - mReferenceTimeStamp).ToMilliseconds());
|
|
|
|
|
|
|
|
Time timeToTimeStamp = timeStampDelta - timeDelta;
|
|
|
|
bool isNewer = false;
|
|
|
|
if (timeToTimeStamp == 0) {
|
|
|
|
*aDelta = 0;
|
|
|
|
} else if (timeToTimeStamp < kTimeHalfRange) {
|
|
|
|
*aDelta = timeToTimeStamp;
|
|
|
|
} else {
|
|
|
|
isNewer = true;
|
|
|
|
*aDelta = timeDelta - timeStampDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isNewer;
|
|
|
|
}
|
|
|
|
|
2015-08-11 07:38:18 +03:00
|
|
|
Time mReferenceTime;
|
|
|
|
TimeStamp mReferenceTimeStamp;
|
2015-08-11 11:11:41 +03:00
|
|
|
Time mLastBackwardsSkewCheck;
|
2015-08-11 07:38:18 +03:00
|
|
|
|
|
|
|
const Time kTimeRange;
|
|
|
|
const Time kTimeHalfRange;
|
2015-08-11 11:11:41 +03:00
|
|
|
const Time kBackwardsSkewCheckInterval;
|
2015-08-11 07:38:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* SystemTimeConverter_h */
|