From cdd36264b339f1bbcb0adef18ce22236f8033a6b Mon Sep 17 00:00:00 2001 From: Ehsan Akhgari Date: Thu, 6 Sep 2012 11:01:06 -0400 Subject: [PATCH] Bug 784859 - Part 1: Use QueryPerformanceCounter directly if the machine has a stable TSC; r=bbondy --- xpcom/ds/TimeStamp_windows.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/xpcom/ds/TimeStamp_windows.cpp b/xpcom/ds/TimeStamp_windows.cpp index ad2cefed3166..75c655c396f7 100644 --- a/xpcom/ds/TimeStamp_windows.cpp +++ b/xpcom/ds/TimeStamp_windows.cpp @@ -24,6 +24,25 @@ #include "prlog.h" #include +#include + +static bool +HasStableTSC() +{ + int regs[4]; + + // detect if the Advanced Power Management feature is supported + __cpuid(regs, 0x80000000); + if (regs[0] < 0x80000007) + return false; + + __cpuid(regs, 0x80000007); + // if bit 8 is set than TSC will run at a constant rate + // in all ACPI P-state, C-states and T-states + return regs[3] & (1 << 8); +} + + #if defined(PR_LOGGING) // Log module for mozilla::TimeStamp for Windows logging... // @@ -47,6 +66,8 @@ static const double kNsPerSecd = 1000000000.0; static const LONGLONG kNsPerSec = 1000000000; static const LONGLONG kNsPerMillisec = 1000000; +static bool sHasStableTSC = false; + // ---------------------------------------------------------------------------- // Global constants @@ -600,6 +621,8 @@ TimeStamp::Startup() InitThresholds(); InitResolution(); + sHasStableTSC = HasStableTSC(); + LOG(("TimeStamp: initial skew is %1.2fms", mt2ms_d(sSkew))); return NS_OK; @@ -614,6 +637,9 @@ TimeStamp::Shutdown() TimeStamp TimeStamp::Now() { + if (sHasStableTSC) { + return TimeStamp(uint64_t(PerformanceCounter())); + } return TimeStamp(uint64_t(CalibratedPerformanceCounter())); }