From d48fd3d8ac2ddcc704f20fa0b41dcec4ed8105ea Mon Sep 17 00:00:00 2001 From: Gabriele Svelto Date: Mon, 28 Oct 2013 20:50:09 +0100 Subject: [PATCH] Bug 906754 - Ensure that TimeStamp's static fields are initialized only once. r=froydnj --- xpcom/ds/TimeStamp.cpp | 44 +++++++++++++++++++++++++++------- xpcom/ds/TimeStamp.h | 13 ---------- xpcom/ds/TimeStamp_darwin.cpp | 14 ----------- xpcom/ds/TimeStamp_posix.cpp | 13 ---------- xpcom/ds/TimeStamp_windows.cpp | 14 ----------- 5 files changed, 35 insertions(+), 63 deletions(-) diff --git a/xpcom/ds/TimeStamp.cpp b/xpcom/ds/TimeStamp.cpp index c20fa323a3f..32a1d9dc0e2 100644 --- a/xpcom/ds/TimeStamp.cpp +++ b/xpcom/ds/TimeStamp.cpp @@ -13,15 +13,41 @@ namespace mozilla { -TimeStamp TimeStamp::sFirstTimeStamp; -TimeStamp TimeStamp::sProcessCreation; +/** + * Wrapper class used to initialize static data used by the TimeStamp class + */ +struct TimeStampInitialization { + /** + * First timestamp taken when the class static initializers are run. This + * timestamp is used to sanitize timestamps coming from different sources. + */ + TimeStamp mFirstTimeStamp; + + /** + * Timestamp representing the time when the process was created. This field + * is populated lazily the first time this information is required and is + * replaced every time the process is restarted. + */ + TimeStamp mProcessCreation; + + TimeStampInitialization() { + TimeStamp::Startup(); + mFirstTimeStamp = TimeStamp::Now(); + }; + + ~TimeStampInitialization() { + TimeStamp::Shutdown(); + }; +}; + +static TimeStampInitialization sInitOnce; TimeStamp TimeStamp::ProcessCreation(bool& aIsInconsistent) { aIsInconsistent = false; - if (sProcessCreation.IsNull()) { + if (sInitOnce.mProcessCreation.IsNull()) { char *mozAppRestart = PR_GetEnv("MOZ_APP_RESTART"); TimeStamp ts; @@ -31,32 +57,32 @@ TimeStamp::ProcessCreation(bool& aIsInconsistent) if (mozAppRestart && (strcmp(mozAppRestart, "") != 0)) { /* Firefox was restarted, use the first time-stamp we've taken as the new * process startup time. */ - ts = sFirstTimeStamp; + ts = sInitOnce.mFirstTimeStamp; } else { TimeStamp now = Now(); uint64_t uptime = ComputeProcessUptime(); ts = now - TimeDuration::FromMicroseconds(uptime); - if ((ts > sFirstTimeStamp) || (uptime == 0)) { + if ((ts > sInitOnce.mFirstTimeStamp) || (uptime == 0)) { /* If the process creation timestamp was inconsistent replace it with * the first one instead and notify that a telemetry error was * detected. */ aIsInconsistent = true; - ts = sFirstTimeStamp; + ts = sInitOnce.mFirstTimeStamp; } } - sProcessCreation = ts; + sInitOnce.mProcessCreation = ts; } - return sProcessCreation; + return sInitOnce.mProcessCreation; } void TimeStamp::RecordProcessRestart() { - sProcessCreation = TimeStamp(); + sInitOnce.mProcessCreation = TimeStamp(); } } // namespace mozilla diff --git a/xpcom/ds/TimeStamp.h b/xpcom/ds/TimeStamp.h index 854d091ea5e..11a137d8ea5 100644 --- a/xpcom/ds/TimeStamp.h +++ b/xpcom/ds/TimeStamp.h @@ -366,19 +366,6 @@ private: * When using a system clock, a value is system dependent. */ TimeStampValue mValue; - - /** - * First timestamp taken when the class static initializers are run. This - * timestamp is used to sanitize timestamps coming from different sources. - */ - static TimeStamp sFirstTimeStamp; - - /** - * Timestamp representing the time when the process was created. This field - * is populated lazily the first time this information is required and is - * replaced every time the process is restarted. - */ - static TimeStamp sProcessCreation; }; } diff --git a/xpcom/ds/TimeStamp_darwin.cpp b/xpcom/ds/TimeStamp_darwin.cpp index ccea28d4037..7425492f925 100644 --- a/xpcom/ds/TimeStamp_darwin.cpp +++ b/xpcom/ds/TimeStamp_darwin.cpp @@ -112,18 +112,6 @@ TimeDuration::Resolution() return TimeDuration::FromTicks(int64_t(sResolution)); } -struct TimeStampInitialization -{ - TimeStampInitialization() { - TimeStamp::Startup(); - } - ~TimeStampInitialization() { - TimeStamp::Shutdown(); - } -}; - -static TimeStampInitialization initOnce; - nsresult TimeStamp::Startup() { @@ -150,8 +138,6 @@ TimeStamp::Startup() sResolutionSigDigs *= 10); gInitialized = true; - sFirstTimeStamp = TimeStamp::Now(); - sProcessCreation = TimeStamp(); return NS_OK; } diff --git a/xpcom/ds/TimeStamp_posix.cpp b/xpcom/ds/TimeStamp_posix.cpp index 6f833304c53..7636cf9bd39 100644 --- a/xpcom/ds/TimeStamp_posix.cpp +++ b/xpcom/ds/TimeStamp_posix.cpp @@ -160,17 +160,6 @@ TimeDuration::Resolution() return TimeDuration::FromTicks(int64_t(sResolution)); } -struct TimeStampInitialization -{ - TimeStampInitialization() { - TimeStamp::Startup(); - } - ~TimeStampInitialization() { - TimeStamp::Shutdown(); - } -}; - -static TimeStampInitialization initOnce; static bool gInitialized = false; nsresult @@ -193,8 +182,6 @@ TimeStamp::Startup() sResolutionSigDigs *= 10); gInitialized = true; - sFirstTimeStamp = TimeStamp::Now(); - sProcessCreation = TimeStamp(); return NS_OK; } diff --git a/xpcom/ds/TimeStamp_windows.cpp b/xpcom/ds/TimeStamp_windows.cpp index ac2c476199b..28ccbf54229 100644 --- a/xpcom/ds/TimeStamp_windows.cpp +++ b/xpcom/ds/TimeStamp_windows.cpp @@ -438,18 +438,6 @@ TimeDuration::Resolution() return TimeDuration::FromTicks(int64_t(sResolution)); } -struct TimeStampInitialization -{ - TimeStampInitialization() { - TimeStamp::Startup(); - } - ~TimeStampInitialization() { - TimeStamp::Shutdown(); - } -}; - -static TimeStampInitialization initOnce; - static bool HasStableTSC() { @@ -515,8 +503,6 @@ TimeStamp::Startup() InitThresholds(); InitResolution(); - sFirstTimeStamp = TimeStamp::Now(); - sProcessCreation = TimeStamp(); return NS_OK; }