Bug 906754 - Ensure that TimeStamp's static fields are initialized only once. r=froydnj

This commit is contained in:
Gabriele Svelto 2013-10-28 20:50:09 +01:00
parent c44d0d7b14
commit d48fd3d8ac
5 changed files with 35 additions and 63 deletions

View File

@ -13,15 +13,41 @@
namespace mozilla { 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
TimeStamp::ProcessCreation(bool& aIsInconsistent) TimeStamp::ProcessCreation(bool& aIsInconsistent)
{ {
aIsInconsistent = false; aIsInconsistent = false;
if (sProcessCreation.IsNull()) { if (sInitOnce.mProcessCreation.IsNull()) {
char *mozAppRestart = PR_GetEnv("MOZ_APP_RESTART"); char *mozAppRestart = PR_GetEnv("MOZ_APP_RESTART");
TimeStamp ts; TimeStamp ts;
@ -31,32 +57,32 @@ TimeStamp::ProcessCreation(bool& aIsInconsistent)
if (mozAppRestart && (strcmp(mozAppRestart, "") != 0)) { if (mozAppRestart && (strcmp(mozAppRestart, "") != 0)) {
/* Firefox was restarted, use the first time-stamp we've taken as the new /* Firefox was restarted, use the first time-stamp we've taken as the new
* process startup time. */ * process startup time. */
ts = sFirstTimeStamp; ts = sInitOnce.mFirstTimeStamp;
} else { } else {
TimeStamp now = Now(); TimeStamp now = Now();
uint64_t uptime = ComputeProcessUptime(); uint64_t uptime = ComputeProcessUptime();
ts = now - TimeDuration::FromMicroseconds(uptime); 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 /* If the process creation timestamp was inconsistent replace it with
* the first one instead and notify that a telemetry error was * the first one instead and notify that a telemetry error was
* detected. */ * detected. */
aIsInconsistent = true; aIsInconsistent = true;
ts = sFirstTimeStamp; ts = sInitOnce.mFirstTimeStamp;
} }
} }
sProcessCreation = ts; sInitOnce.mProcessCreation = ts;
} }
return sProcessCreation; return sInitOnce.mProcessCreation;
} }
void void
TimeStamp::RecordProcessRestart() TimeStamp::RecordProcessRestart()
{ {
sProcessCreation = TimeStamp(); sInitOnce.mProcessCreation = TimeStamp();
} }
} // namespace mozilla } // namespace mozilla

View File

@ -366,19 +366,6 @@ private:
* When using a system clock, a value is system dependent. * When using a system clock, a value is system dependent.
*/ */
TimeStampValue mValue; 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;
}; };
} }

View File

@ -112,18 +112,6 @@ TimeDuration::Resolution()
return TimeDuration::FromTicks(int64_t(sResolution)); return TimeDuration::FromTicks(int64_t(sResolution));
} }
struct TimeStampInitialization
{
TimeStampInitialization() {
TimeStamp::Startup();
}
~TimeStampInitialization() {
TimeStamp::Shutdown();
}
};
static TimeStampInitialization initOnce;
nsresult nsresult
TimeStamp::Startup() TimeStamp::Startup()
{ {
@ -150,8 +138,6 @@ TimeStamp::Startup()
sResolutionSigDigs *= 10); sResolutionSigDigs *= 10);
gInitialized = true; gInitialized = true;
sFirstTimeStamp = TimeStamp::Now();
sProcessCreation = TimeStamp();
return NS_OK; return NS_OK;
} }

View File

@ -160,17 +160,6 @@ TimeDuration::Resolution()
return TimeDuration::FromTicks(int64_t(sResolution)); return TimeDuration::FromTicks(int64_t(sResolution));
} }
struct TimeStampInitialization
{
TimeStampInitialization() {
TimeStamp::Startup();
}
~TimeStampInitialization() {
TimeStamp::Shutdown();
}
};
static TimeStampInitialization initOnce;
static bool gInitialized = false; static bool gInitialized = false;
nsresult nsresult
@ -193,8 +182,6 @@ TimeStamp::Startup()
sResolutionSigDigs *= 10); sResolutionSigDigs *= 10);
gInitialized = true; gInitialized = true;
sFirstTimeStamp = TimeStamp::Now();
sProcessCreation = TimeStamp();
return NS_OK; return NS_OK;
} }

View File

@ -438,18 +438,6 @@ TimeDuration::Resolution()
return TimeDuration::FromTicks(int64_t(sResolution)); return TimeDuration::FromTicks(int64_t(sResolution));
} }
struct TimeStampInitialization
{
TimeStampInitialization() {
TimeStamp::Startup();
}
~TimeStampInitialization() {
TimeStamp::Shutdown();
}
};
static TimeStampInitialization initOnce;
static bool static bool
HasStableTSC() HasStableTSC()
{ {
@ -515,8 +503,6 @@ TimeStamp::Startup()
InitThresholds(); InitThresholds();
InitResolution(); InitResolution();
sFirstTimeStamp = TimeStamp::Now();
sProcessCreation = TimeStamp();
return NS_OK; return NS_OK;
} }