2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2011-12-09 03:14:42 +04:00
|
|
|
|
|
|
|
// Implement TimeStamp::Now() with QueryPerformanceCounter() controlled with
|
2017-08-18 21:08:11 +03:00
|
|
|
// values of GetTickCount64().
|
2011-12-09 03:14:42 +04:00
|
|
|
|
2013-02-16 07:55:36 +04:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
|
|
|
#include "mozilla/TimeStamp.h"
|
2011-12-09 03:14:42 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2017-10-06 16:16:00 +03:00
|
|
|
#include <stdlib.h>
|
2012-09-06 19:01:06 +04:00
|
|
|
#include <intrin.h>
|
2015-06-05 23:03:11 +03:00
|
|
|
#include <windows.h>
|
2012-09-06 19:01:06 +04:00
|
|
|
|
2015-06-05 23:03:11 +03:00
|
|
|
// To enable logging define to your favorite logging API
|
|
|
|
#define LOG(x)
|
|
|
|
|
|
|
|
class AutoCriticalSection {
|
|
|
|
public:
|
2016-12-16 10:57:53 +03:00
|
|
|
explicit AutoCriticalSection(LPCRITICAL_SECTION aSection)
|
2015-06-05 23:03:11 +03:00
|
|
|
: mSection(aSection) {
|
|
|
|
::EnterCriticalSection(mSection);
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2015-06-05 23:03:11 +03:00
|
|
|
~AutoCriticalSection() { ::LeaveCriticalSection(mSection); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-06-05 23:03:11 +03:00
|
|
|
private:
|
|
|
|
LPCRITICAL_SECTION mSection;
|
|
|
|
};
|
2011-12-09 03:14:42 +04:00
|
|
|
|
|
|
|
// Estimate of the smallest duration of time we can measure.
|
|
|
|
static volatile ULONGLONG sResolution;
|
|
|
|
static volatile ULONGLONG sResolutionSigDigs;
|
|
|
|
static const double kNsPerSecd = 1000000000.0;
|
|
|
|
static const LONGLONG kNsPerMillisec = 1000000;
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Global constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
// Tolerance to failures settings.
|
2011-12-09 03:14:42 +04:00
|
|
|
//
|
2013-05-01 20:57:58 +04:00
|
|
|
// What is the interval we want to have failure free.
|
|
|
|
// in [ms]
|
|
|
|
static const uint32_t kFailureFreeInterval = 5000;
|
|
|
|
// How many failures we are willing to tolerate in the interval.
|
|
|
|
static const uint32_t kMaxFailuresPerInterval = 4;
|
|
|
|
// What is the threshold to treat fluctuations as actual failures.
|
|
|
|
// in [ms]
|
|
|
|
static const uint32_t kFailureThreshold = 50;
|
2011-12-09 03:14:42 +04:00
|
|
|
|
|
|
|
// If we are not able to get the value of GTC time increment, use this value
|
|
|
|
// which is the most usual increment.
|
|
|
|
static const DWORD kDefaultTimeIncrement = 156001;
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Global variables, not changing at runtime
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Result of QueryPerformanceFrequency
|
2017-10-06 16:16:00 +03:00
|
|
|
// We use default of 1 for the case we can't use QueryPerformanceCounter
|
|
|
|
// to make mt/ms conversions work despite that.
|
2018-09-18 21:05:26 +03:00
|
|
|
static uint64_t sFrequencyPerSec = 1;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
MFBT_API uint64_t GetQueryPerformanceFrequencyPerSec() {
|
|
|
|
return sFrequencyPerSec;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
2011-12-09 03:14:42 +04:00
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
// How much we are tolerant to GTC occasional loose of resoltion.
|
|
|
|
// This number says how many multiples of the minimal GTC resolution
|
|
|
|
// detected on the system are acceptable. This number is empirical.
|
|
|
|
static const LONGLONG kGTCTickLeapTolerance = 4;
|
|
|
|
|
|
|
|
// Base tolerance (more: "inability of detection" range) threshold is calculated
|
2017-05-04 21:29:55 +03:00
|
|
|
// dynamically, and kept in sGTCResolutionThreshold.
|
2011-12-09 03:14:42 +04:00
|
|
|
//
|
2013-05-01 20:57:58 +04:00
|
|
|
// Schematically, QPC worked "100%" correctly if ((GTC_now - GTC_epoch) -
|
2017-05-04 21:29:55 +03:00
|
|
|
// (QPC_now - QPC_epoch)) was in [-sGTCResolutionThreshold,
|
2013-05-01 20:57:58 +04:00
|
|
|
// sGTCResolutionThreshold] interval every time we'd compared two time stamps.
|
|
|
|
// If not, then we check the overflow behind this basic threshold
|
|
|
|
// is in kFailureThreshold. If not, we condider it as a QPC failure. If too
|
|
|
|
// many failures in short time are detected, QPC is considered faulty and
|
|
|
|
// disabled.
|
2011-12-09 03:14:42 +04:00
|
|
|
//
|
|
|
|
// Kept in [mt]
|
2017-05-04 21:29:55 +03:00
|
|
|
static LONGLONG sGTCResolutionThreshold;
|
2011-12-09 03:14:42 +04:00
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
// If QPC is found faulty for two stamps in this interval, we engage
|
|
|
|
// the fault detection algorithm. For duration larger then this limit
|
|
|
|
// we bypass using durations calculated from QPC when jitter is detected,
|
|
|
|
// but don't touch the sUseQPC flag.
|
2013-02-12 01:56:58 +04:00
|
|
|
//
|
2013-05-01 20:57:58 +04:00
|
|
|
// Value is in [ms].
|
|
|
|
static const uint32_t kHardFailureLimit = 2000;
|
|
|
|
// Conversion to [mt]
|
|
|
|
static LONGLONG sHardFailureLimit;
|
|
|
|
|
|
|
|
// Conversion of kFailureFreeInterval and kFailureThreshold to [mt]
|
|
|
|
static LONGLONG sFailureFreeInterval;
|
|
|
|
static LONGLONG sFailureThreshold;
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Systemm status flags
|
|
|
|
// ----------------------------------------------------------------------------
|
2013-02-12 01:56:58 +04:00
|
|
|
|
|
|
|
// Flag for stable TSC that indicates platform where QPC is stable.
|
|
|
|
static bool sHasStableTSC = false;
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Global state variables, changing at runtime
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Initially true, set to false when QPC is found unstable and never
|
|
|
|
// returns back to true since that time.
|
|
|
|
static bool volatile sUseQPC = true;
|
2012-12-22 18:46:14 +04:00
|
|
|
|
2011-12-09 03:14:42 +04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Global lock
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Thread spin count before entering the full wait state for sTimeStampLock.
|
|
|
|
// Inspired by Rob Arnold's work on PRMJ_Now().
|
|
|
|
static const DWORD kLockSpinCount = 4096;
|
|
|
|
|
|
|
|
// Common mutex (thanks the relative complexity of the logic, this is better
|
|
|
|
// then using CMPXCHG8B.)
|
|
|
|
// It is protecting the globals bellow.
|
2013-02-12 01:56:58 +04:00
|
|
|
static CRITICAL_SECTION sTimeStampLock;
|
2011-12-09 03:14:42 +04:00
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Global lock protected variables
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Timestamp in future until QPC must behave correctly.
|
|
|
|
// Set to now + kFailureFreeInterval on first QPC failure detection.
|
|
|
|
// Set to now + E * kFailureFreeInterval on following errors,
|
|
|
|
// where E is number of errors detected during last kFailureFreeInterval
|
|
|
|
// milliseconds, calculated simply as:
|
|
|
|
// E = (sFaultIntoleranceCheckpoint - now) / kFailureFreeInterval + 1.
|
|
|
|
// When E > kMaxFailuresPerInterval -> disable QPC.
|
|
|
|
//
|
|
|
|
// Kept in [mt]
|
|
|
|
static ULONGLONG sFaultIntoleranceCheckpoint = 0;
|
|
|
|
|
2011-12-09 03:14:42 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-02-12 01:56:58 +04:00
|
|
|
// Result is in [mt]
|
|
|
|
static inline ULONGLONG PerformanceCounter() {
|
|
|
|
LARGE_INTEGER pc;
|
|
|
|
::QueryPerformanceCounter(&pc);
|
2017-06-13 12:12:00 +03:00
|
|
|
|
2018-11-26 04:08:00 +03:00
|
|
|
// QueryPerformanceCounter may slightly jitter (not be 100% monotonic.)
|
|
|
|
// This is a simple go-backward protection for such a faulty hardware.
|
|
|
|
AutoCriticalSection lock(&sTimeStampLock);
|
2017-06-13 12:12:00 +03:00
|
|
|
|
2018-11-26 04:08:00 +03:00
|
|
|
static decltype(LARGE_INTEGER::QuadPart) last;
|
|
|
|
if (last > pc.QuadPart) {
|
|
|
|
return last * 1000ULL;
|
|
|
|
}
|
|
|
|
last = pc.QuadPart;
|
2013-02-12 01:56:58 +04:00
|
|
|
return pc.QuadPart * 1000ULL;
|
|
|
|
}
|
2011-12-09 03:14:42 +04:00
|
|
|
|
|
|
|
static void InitThresholds() {
|
|
|
|
DWORD timeAdjustment = 0, timeIncrement = 0;
|
|
|
|
BOOL timeAdjustmentDisabled;
|
|
|
|
GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
|
|
|
|
&timeAdjustmentDisabled);
|
|
|
|
|
2013-02-12 01:56:58 +04:00
|
|
|
LOG(("TimeStamp: timeIncrement=%d [100ns]", timeIncrement));
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!timeIncrement) {
|
2011-12-09 03:14:42 +04:00
|
|
|
timeIncrement = kDefaultTimeIncrement;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2011-12-09 03:14:42 +04:00
|
|
|
|
|
|
|
// Ceiling to a millisecond
|
|
|
|
// Example values: 156001, 210000
|
|
|
|
DWORD timeIncrementCeil = timeIncrement;
|
|
|
|
// Don't want to round up if already rounded, values will be: 156000, 209999
|
|
|
|
timeIncrementCeil -= 1;
|
|
|
|
// Convert to ms, values will be: 15, 20
|
|
|
|
timeIncrementCeil /= 10000;
|
|
|
|
// Round up, values will be: 16, 21
|
|
|
|
timeIncrementCeil += 1;
|
|
|
|
// Convert back to 100ns, values will be: 160000, 210000
|
|
|
|
timeIncrementCeil *= 10000;
|
|
|
|
|
|
|
|
// How many milli-ticks has the interval rounded up
|
|
|
|
LONGLONG ticksPerGetTickCountResolutionCeiling =
|
2012-08-22 19:56:38 +04:00
|
|
|
(int64_t(timeIncrementCeil) * sFrequencyPerSec) / 10000LL;
|
2011-12-09 03:14:42 +04:00
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
// GTC may jump by 32 (2*16) ms in two steps, therefor use the ceiling value.
|
2017-05-04 21:29:55 +03:00
|
|
|
sGTCResolutionThreshold =
|
2013-05-01 20:57:58 +04:00
|
|
|
LONGLONG(kGTCTickLeapTolerance * ticksPerGetTickCountResolutionCeiling);
|
2011-12-09 03:14:42 +04:00
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
sHardFailureLimit = ms2mt(kHardFailureLimit);
|
|
|
|
sFailureFreeInterval = ms2mt(kFailureFreeInterval);
|
|
|
|
sFailureThreshold = ms2mt(kFailureThreshold);
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void InitResolution() {
|
|
|
|
// 10 total trials is arbitrary: what we're trying to avoid by
|
|
|
|
// looping is getting unlucky and being interrupted by a context
|
|
|
|
// switch or signal, or being bitten by paging/cache effects
|
|
|
|
|
|
|
|
ULONGLONG minres = ~0ULL;
|
2017-10-06 16:16:00 +03:00
|
|
|
if (sUseQPC) {
|
|
|
|
int loops = 10;
|
|
|
|
do {
|
|
|
|
ULONGLONG start = PerformanceCounter();
|
|
|
|
ULONGLONG end = PerformanceCounter();
|
|
|
|
|
|
|
|
ULONGLONG candidate = (end - start);
|
|
|
|
if (candidate < minres) {
|
|
|
|
minres = candidate;
|
|
|
|
}
|
|
|
|
} while (--loops && minres);
|
2011-12-09 03:14:42 +04:00
|
|
|
|
2017-10-06 16:16:00 +03:00
|
|
|
if (0 == minres) {
|
|
|
|
minres = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// GetTickCount has only ~16ms known resolution
|
|
|
|
minres = ms2mt(16);
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Converting minres that is in [mt] to nanosecods, multiplicating
|
|
|
|
// the argument to preserve resolution.
|
|
|
|
ULONGLONG result = mt2ms(minres * kNsPerMillisec);
|
|
|
|
if (0 == result) {
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sResolution = result;
|
|
|
|
|
|
|
|
// find the number of significant digits in mResolution, for the
|
|
|
|
// sake of ToSecondsSigDigits()
|
|
|
|
ULONGLONG sigDigs;
|
2014-07-09 19:15:21 +04:00
|
|
|
for (sigDigs = 1; !(sigDigs == result || 10 * sigDigs > result);
|
2011-12-09 03:14:42 +04:00
|
|
|
sigDigs *= 10)
|
|
|
|
;
|
|
|
|
|
|
|
|
sResolutionSigDigs = sigDigs;
|
|
|
|
}
|
|
|
|
|
2013-02-12 01:56:58 +04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// TimeStampValue implementation
|
|
|
|
// ----------------------------------------------------------------------------
|
2015-06-05 23:03:11 +03:00
|
|
|
MFBT_API
|
2018-10-09 20:05:25 +03:00
|
|
|
TimeStampValue::TimeStampValue(ULONGLONG aGTC, ULONGLONG aQPC, bool aHasQPC,
|
|
|
|
bool aUsedCanonicalNow)
|
2013-02-12 01:56:58 +04:00
|
|
|
: mGTC(aGTC),
|
|
|
|
mQPC(aQPC),
|
2018-10-09 20:05:25 +03:00
|
|
|
mUsedCanonicalNow(aUsedCanonicalNow),
|
2013-02-12 01:56:58 +04:00
|
|
|
mHasQPC(aHasQPC) {
|
2018-10-09 20:05:25 +03:00
|
|
|
mIsNull = aGTC == 0 && aQPC == 0;
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
2013-02-12 01:56:58 +04:00
|
|
|
MFBT_API TimeStampValue& TimeStampValue::operator+=(const int64_t aOther) {
|
|
|
|
mGTC += aOther;
|
|
|
|
mQPC += aOther;
|
|
|
|
return *this;
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
2013-02-12 01:56:58 +04:00
|
|
|
MFBT_API TimeStampValue& TimeStampValue::operator-=(const int64_t aOther) {
|
|
|
|
mGTC -= aOther;
|
|
|
|
mQPC -= aOther;
|
|
|
|
return *this;
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
// If the duration is less then two seconds, perform check of QPC stability
|
|
|
|
// by comparing both GTC and QPC calculated durations of this and aOther.
|
2014-07-09 19:15:21 +04:00
|
|
|
MFBT_API uint64_t TimeStampValue::CheckQPC(const TimeStampValue& aOther) const {
|
2013-05-01 20:57:58 +04:00
|
|
|
uint64_t deltaGTC = mGTC - aOther.mGTC;
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!mHasQPC || !aOther.mHasQPC) { // Both not holding QPC
|
2013-05-01 20:57:58 +04:00
|
|
|
return deltaGTC;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2013-05-01 20:57:58 +04:00
|
|
|
|
|
|
|
uint64_t deltaQPC = mQPC - aOther.mQPC;
|
2012-09-06 19:01:06 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
if (sHasStableTSC) { // For stable TSC there is no need to check
|
2013-05-01 20:57:58 +04:00
|
|
|
return deltaQPC;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2012-09-06 19:01:06 +04:00
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
// Check QPC is sane before using it.
|
|
|
|
int64_t diff = DeprecatedAbs(int64_t(deltaQPC) - int64_t(deltaGTC));
|
2017-05-04 21:29:55 +03:00
|
|
|
if (diff <= sGTCResolutionThreshold) {
|
2013-05-01 20:57:58 +04:00
|
|
|
return deltaQPC;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2011-12-09 03:14:42 +04:00
|
|
|
|
2013-02-12 01:56:58 +04:00
|
|
|
// Treat absolutely for calibration purposes
|
2013-05-01 20:57:58 +04:00
|
|
|
int64_t duration = DeprecatedAbs(int64_t(deltaGTC));
|
2017-05-04 21:29:55 +03:00
|
|
|
int64_t overflow = diff - sGTCResolutionThreshold;
|
2011-12-09 03:14:42 +04:00
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
LOG(("TimeStamp: QPC check after %llums with overflow %1.4fms",
|
|
|
|
mt2ms(duration), mt2ms_f(overflow)));
|
2011-12-09 03:14:42 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
if (overflow <= sFailureThreshold) { // We are in the limit, let go.
|
2014-09-16 17:51:51 +04:00
|
|
|
return deltaQPC;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2011-12-09 03:14:42 +04:00
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
// QPC deviates, don't use it, since now this method may only return deltaGTC.
|
2014-09-16 17:51:51 +04:00
|
|
|
|
|
|
|
if (!sUseQPC) { // QPC already disabled, no need to run the fault tolerance
|
|
|
|
// algorithm.
|
|
|
|
return deltaGTC;
|
|
|
|
}
|
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
LOG(("TimeStamp: QPC jittered over failure threshold"));
|
2013-02-12 01:56:58 +04:00
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
if (duration < sHardFailureLimit) {
|
2013-02-12 01:56:58 +04:00
|
|
|
// Interval between the two time stamps is very short, consider
|
2013-05-01 20:57:58 +04:00
|
|
|
// QPC as unstable and record a failure.
|
2017-08-18 21:08:11 +03:00
|
|
|
uint64_t now = ms2mt(GetTickCount64());
|
2013-05-01 20:57:58 +04:00
|
|
|
|
|
|
|
AutoCriticalSection lock(&sTimeStampLock);
|
|
|
|
|
|
|
|
if (sFaultIntoleranceCheckpoint && sFaultIntoleranceCheckpoint > now) {
|
|
|
|
// There's already been an error in the last fault intollerant interval.
|
|
|
|
// Time since now to the checkpoint actually holds information on how many
|
|
|
|
// failures there were in the failure free interval we have defined.
|
2014-07-09 19:15:21 +04:00
|
|
|
uint64_t failureCount =
|
|
|
|
(sFaultIntoleranceCheckpoint - now + sFailureFreeInterval - 1) /
|
|
|
|
sFailureFreeInterval;
|
2013-05-01 20:57:58 +04:00
|
|
|
if (failureCount > kMaxFailuresPerInterval) {
|
|
|
|
sUseQPC = false;
|
|
|
|
LOG(("TimeStamp: QPC disabled"));
|
2014-07-09 19:15:21 +04:00
|
|
|
} else {
|
2013-05-01 20:57:58 +04:00
|
|
|
// Move the fault intolerance checkpoint more to the future, prolong it
|
|
|
|
// to reflect the number of detected failures.
|
|
|
|
++failureCount;
|
|
|
|
sFaultIntoleranceCheckpoint = now + failureCount * sFailureFreeInterval;
|
|
|
|
LOG(("TimeStamp: recording %dth QPC failure", failureCount));
|
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
} else {
|
2013-05-01 20:57:58 +04:00
|
|
|
// Setup fault intolerance checkpoint in the future for first detected
|
|
|
|
// error.
|
|
|
|
sFaultIntoleranceCheckpoint = now + sFailureFreeInterval;
|
|
|
|
LOG(("TimeStamp: recording 1st QPC failure"));
|
|
|
|
}
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
return deltaGTC;
|
2013-02-12 01:56:58 +04:00
|
|
|
}
|
2011-12-09 03:14:42 +04:00
|
|
|
|
2015-06-05 23:03:11 +03:00
|
|
|
MFBT_API uint64_t
|
2014-07-09 19:15:21 +04:00
|
|
|
TimeStampValue::operator-(const TimeStampValue& aOther) const {
|
2018-10-09 20:05:25 +03:00
|
|
|
if (IsNull() && aOther.IsNull()) {
|
2013-02-12 01:56:58 +04:00
|
|
|
return uint64_t(0);
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2013-02-12 01:56:58 +04:00
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
return CheckQPC(aOther);
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// TimeDuration and TimeStamp implementation
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2014-09-25 09:25:49 +04:00
|
|
|
MFBT_API double BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks) {
|
2012-08-30 05:09:01 +04:00
|
|
|
// Converting before arithmetic avoids blocked store forward
|
2014-09-25 09:25:48 +04:00
|
|
|
return double(aTicks) / (double(sFrequencyPerSec) * 1000.0);
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
2014-09-25 09:25:49 +04:00
|
|
|
MFBT_API double BaseTimeDurationPlatformUtils::ToSecondsSigDigits(
|
|
|
|
int64_t aTicks) {
|
2011-12-09 03:14:42 +04:00
|
|
|
// don't report a value < mResolution ...
|
|
|
|
LONGLONG resolution = sResolution;
|
|
|
|
LONGLONG resolutionSigDigs = sResolutionSigDigs;
|
2014-09-25 09:25:48 +04:00
|
|
|
LONGLONG valueSigDigs = resolution * (aTicks / resolution);
|
2011-12-09 03:14:42 +04:00
|
|
|
// and chop off insignificant digits
|
|
|
|
valueSigDigs = resolutionSigDigs * (valueSigDigs / resolutionSigDigs);
|
|
|
|
return double(valueSigDigs) / kNsPerSecd;
|
|
|
|
}
|
|
|
|
|
2015-06-05 23:03:11 +03:00
|
|
|
MFBT_API int64_t
|
2014-09-25 09:25:49 +04:00
|
|
|
BaseTimeDurationPlatformUtils::TicksFromMilliseconds(double aMilliseconds) {
|
2014-10-02 10:14:12 +04:00
|
|
|
double result = ms2mt(aMilliseconds);
|
|
|
|
if (result > INT64_MAX) {
|
|
|
|
return INT64_MAX;
|
|
|
|
} else if (result < INT64_MIN) {
|
|
|
|
return INT64_MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
2014-09-25 09:25:49 +04:00
|
|
|
MFBT_API int64_t BaseTimeDurationPlatformUtils::ResolutionInTicks() {
|
2014-09-25 09:25:48 +04:00
|
|
|
return static_cast<int64_t>(sResolution);
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
2013-02-12 01:56:58 +04:00
|
|
|
static bool HasStableTSC() {
|
2018-08-23 19:54:29 +03:00
|
|
|
#if defined(_M_ARM64)
|
|
|
|
// AArch64 defines that its system counter run at a constant rate
|
|
|
|
// regardless of the current clock frequency of the system. See "The
|
|
|
|
// Generic Timer", section D7, in the ARMARM for ARMv8.
|
|
|
|
return true;
|
|
|
|
#else
|
2014-07-09 19:15:21 +04:00
|
|
|
union {
|
2013-02-12 01:56:58 +04:00
|
|
|
int regs[4];
|
2014-07-09 19:15:21 +04:00
|
|
|
struct {
|
2013-02-12 01:56:58 +04:00
|
|
|
int nIds;
|
|
|
|
char cpuString[12];
|
|
|
|
};
|
|
|
|
} cpuInfo;
|
|
|
|
|
|
|
|
__cpuid(cpuInfo.regs, 0);
|
2017-06-13 12:12:00 +03:00
|
|
|
// Only allow Intel or AMD CPUs for now.
|
2013-02-12 01:56:58 +04:00
|
|
|
// The order of the registers is reg[1], reg[3], reg[2]. We just adjust the
|
|
|
|
// string so that we can compare in one go.
|
2014-07-09 19:15:21 +04:00
|
|
|
if (_strnicmp(cpuInfo.cpuString, "GenuntelineI", sizeof(cpuInfo.cpuString)) &&
|
2017-06-13 12:12:00 +03:00
|
|
|
_strnicmp(cpuInfo.cpuString, "AuthcAMDenti", sizeof(cpuInfo.cpuString))) {
|
2013-02-12 01:56:58 +04:00
|
|
|
return false;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2013-02-12 01:56:58 +04:00
|
|
|
|
|
|
|
int regs[4];
|
|
|
|
|
|
|
|
// detect if the Advanced Power Management feature is supported
|
|
|
|
__cpuid(regs, 0x80000000);
|
2017-07-01 03:32:33 +03:00
|
|
|
if ((unsigned int)regs[0] < 0x80000007) {
|
2017-06-13 12:12:00 +03:00
|
|
|
// XXX should we return true here? If there is no APM there may be
|
|
|
|
// no way how TSC can run out of sync among cores.
|
2013-02-12 01:56:58 +04:00
|
|
|
return false;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2013-02-12 01:56:58 +04:00
|
|
|
|
|
|
|
__cpuid(regs, 0x80000007);
|
|
|
|
// if bit 8 is set than TSC will run at a constant rate
|
2017-06-13 12:12:00 +03:00
|
|
|
// in all ACPI P-states, C-states and T-states
|
2013-02-12 01:56:58 +04:00
|
|
|
return regs[3] & (1 << 8);
|
2018-08-23 19:54:29 +03:00
|
|
|
#endif
|
2013-02-12 01:56:58 +04:00
|
|
|
}
|
|
|
|
|
2016-11-14 14:01:17 +03:00
|
|
|
static bool gInitialized = false;
|
|
|
|
|
2011-12-09 03:14:42 +04:00
|
|
|
MFBT_API void TimeStamp::Startup() {
|
2016-11-14 14:01:17 +03:00
|
|
|
if (gInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gInitialized = true;
|
|
|
|
|
2011-12-09 03:14:42 +04:00
|
|
|
// Decide which implementation to use for the high-performance timer.
|
|
|
|
|
|
|
|
InitializeCriticalSectionAndSpinCount(&sTimeStampLock, kLockSpinCount);
|
|
|
|
|
2017-10-06 16:16:00 +03:00
|
|
|
bool forceGTC = false;
|
|
|
|
bool forceQPC = false;
|
|
|
|
|
|
|
|
char* modevar = getenv("MOZ_TIMESTAMP_MODE");
|
|
|
|
if (modevar) {
|
|
|
|
if (!strcmp(modevar, "QPC")) {
|
|
|
|
forceQPC = true;
|
|
|
|
} else if (!strcmp(modevar, "GTC")) {
|
|
|
|
forceGTC = true;
|
|
|
|
}
|
|
|
|
}
|
2013-02-12 01:56:58 +04:00
|
|
|
|
2011-12-09 03:14:42 +04:00
|
|
|
LARGE_INTEGER freq;
|
2017-10-06 16:16:00 +03:00
|
|
|
sUseQPC = !forceGTC && ::QueryPerformanceFrequency(&freq);
|
2013-02-12 01:56:58 +04:00
|
|
|
if (!sUseQPC) {
|
2017-08-18 21:08:11 +03:00
|
|
|
// No Performance Counter. Fall back to use GetTickCount64.
|
2011-12-09 03:14:42 +04:00
|
|
|
InitResolution();
|
|
|
|
|
2017-08-18 21:08:11 +03:00
|
|
|
LOG(("TimeStamp: using GetTickCount64"));
|
2015-06-05 23:03:11 +03:00
|
|
|
return;
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
2017-10-06 16:16:00 +03:00
|
|
|
sHasStableTSC = forceQPC || HasStableTSC();
|
|
|
|
LOG(("TimeStamp: HasStableTSC=%d", sHasStableTSC));
|
|
|
|
|
2011-12-09 03:14:42 +04:00
|
|
|
sFrequencyPerSec = freq.QuadPart;
|
2013-02-12 01:56:58 +04:00
|
|
|
LOG(("TimeStamp: QPC frequency=%llu", sFrequencyPerSec));
|
2011-12-09 03:14:42 +04:00
|
|
|
|
|
|
|
InitThresholds();
|
|
|
|
InitResolution();
|
|
|
|
|
2015-06-05 23:03:11 +03:00
|
|
|
return;
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MFBT_API void TimeStamp::Shutdown() { DeleteCriticalSection(&sTimeStampLock); }
|
2018-10-09 20:05:25 +03:00
|
|
|
|
|
|
|
TimeStampValue NowInternal(bool aHighResolution) {
|
2013-02-12 01:56:58 +04:00
|
|
|
// sUseQPC is volatile
|
2013-02-12 01:56:59 +04:00
|
|
|
bool useQPC = (aHighResolution && sUseQPC);
|
2013-02-12 01:56:58 +04:00
|
|
|
|
|
|
|
// Both values are in [mt] units.
|
|
|
|
ULONGLONG QPC = useQPC ? PerformanceCounter() : uint64_t(0);
|
2017-08-18 21:08:11 +03:00
|
|
|
ULONGLONG GTC = ms2mt(GetTickCount64());
|
2018-10-09 20:05:25 +03:00
|
|
|
return TimeStampValue(GTC, QPC, useQPC, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
MFBT_API TimeStamp TimeStamp::Now(bool aHighResolution) {
|
|
|
|
return TimeStamp::NowFuzzy(NowInternal(aHighResolution));
|
|
|
|
}
|
|
|
|
|
|
|
|
MFBT_API TimeStamp TimeStamp::NowUnfuzzed(bool aHighResolution) {
|
|
|
|
return TimeStamp(NowInternal(aHighResolution));
|
2011-12-09 03:14:42 +04:00
|
|
|
}
|
|
|
|
|
2013-07-31 14:51:58 +04:00
|
|
|
// Computes and returns the process uptime in microseconds.
|
|
|
|
// Returns 0 if an error was encountered.
|
2013-03-28 14:28:09 +04:00
|
|
|
|
2013-07-31 14:51:58 +04:00
|
|
|
MFBT_API uint64_t TimeStamp::ComputeProcessUptime() {
|
2013-03-28 14:28:09 +04:00
|
|
|
SYSTEMTIME nowSys;
|
|
|
|
GetSystemTime(&nowSys);
|
|
|
|
|
|
|
|
FILETIME now;
|
|
|
|
bool success = SystemTimeToFileTime(&nowSys, &now);
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!success) {
|
2013-03-28 14:28:09 +04:00
|
|
|
return 0;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2013-03-28 14:28:09 +04:00
|
|
|
|
|
|
|
FILETIME start, foo, bar, baz;
|
|
|
|
success = GetProcessTimes(GetCurrentProcess(), &start, &foo, &bar, &baz);
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!success) {
|
2013-03-28 14:28:09 +04:00
|
|
|
return 0;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2013-03-28 14:28:09 +04:00
|
|
|
|
2014-11-03 12:28:52 +03:00
|
|
|
ULARGE_INTEGER startUsec = {{start.dwLowDateTime, start.dwHighDateTime}};
|
|
|
|
ULARGE_INTEGER nowUsec = {{now.dwLowDateTime, now.dwHighDateTime}};
|
2013-03-28 14:28:09 +04:00
|
|
|
|
|
|
|
return (nowUsec.QuadPart - startUsec.QuadPart) / 10ULL;
|
|
|
|
}
|
|
|
|
|
2011-12-09 03:14:42 +04:00
|
|
|
} // namespace mozilla
|