1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * Implementation of the OS-independent methods of the TimeStamp class
11 #include "mozilla/TimeStamp.h"
17 * Wrapper class used to initialize static data used by the TimeStamp class
19 struct TimeStampInitialization
22 * First timestamp taken when the class static initializers are run. This
23 * timestamp is used to sanitize timestamps coming from different sources.
25 TimeStamp mFirstTimeStamp
;
28 * Timestamp representing the time when the process was created. This field
29 * is populated lazily the first time this information is required and is
30 * replaced every time the process is restarted.
32 TimeStamp mProcessCreation
;
34 TimeStampInitialization()
37 mFirstTimeStamp
= TimeStamp::Now();
40 ~TimeStampInitialization()
42 TimeStamp::Shutdown();
46 static TimeStampInitialization sInitOnce
;
49 TimeStamp::ProcessCreation(bool& aIsInconsistent
)
51 aIsInconsistent
= false;
53 if (sInitOnce
.mProcessCreation
.IsNull()) {
54 char* mozAppRestart
= PR_GetEnv("MOZ_APP_RESTART");
57 /* When calling PR_SetEnv() with an empty value the existing variable may
58 * be unset or set to the empty string depending on the underlying platform
59 * thus we have to check if the variable is present and not empty. */
60 if (mozAppRestart
&& (strcmp(mozAppRestart
, "") != 0)) {
61 /* Firefox was restarted, use the first time-stamp we've taken as the new
62 * process startup time. */
63 ts
= sInitOnce
.mFirstTimeStamp
;
65 TimeStamp now
= Now();
66 uint64_t uptime
= ComputeProcessUptime();
68 ts
= now
- TimeDuration::FromMicroseconds(uptime
);
70 if ((ts
> sInitOnce
.mFirstTimeStamp
) || (uptime
== 0)) {
71 /* If the process creation timestamp was inconsistent replace it with
72 * the first one instead and notify that a telemetry error was
74 aIsInconsistent
= true;
75 ts
= sInitOnce
.mFirstTimeStamp
;
79 sInitOnce
.mProcessCreation
= ts
;
82 return sInitOnce
.mProcessCreation
;
86 TimeStamp::RecordProcessRestart()
88 sInitOnce
.mProcessCreation
= TimeStamp();
91 } // namespace mozilla