|
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/. */ |
|
6 |
|
7 /* |
|
8 * Implementation of the OS-independent methods of the TimeStamp class |
|
9 */ |
|
10 |
|
11 #include "mozilla/TimeStamp.h" |
|
12 #include "prenv.h" |
|
13 |
|
14 namespace mozilla { |
|
15 |
|
16 /** |
|
17 * Wrapper class used to initialize static data used by the TimeStamp class |
|
18 */ |
|
19 struct TimeStampInitialization { |
|
20 /** |
|
21 * First timestamp taken when the class static initializers are run. This |
|
22 * timestamp is used to sanitize timestamps coming from different sources. |
|
23 */ |
|
24 TimeStamp mFirstTimeStamp; |
|
25 |
|
26 /** |
|
27 * Timestamp representing the time when the process was created. This field |
|
28 * is populated lazily the first time this information is required and is |
|
29 * replaced every time the process is restarted. |
|
30 */ |
|
31 TimeStamp mProcessCreation; |
|
32 |
|
33 TimeStampInitialization() { |
|
34 TimeStamp::Startup(); |
|
35 mFirstTimeStamp = TimeStamp::Now(); |
|
36 }; |
|
37 |
|
38 ~TimeStampInitialization() { |
|
39 TimeStamp::Shutdown(); |
|
40 }; |
|
41 }; |
|
42 |
|
43 static TimeStampInitialization sInitOnce; |
|
44 |
|
45 TimeStamp |
|
46 TimeStamp::ProcessCreation(bool& aIsInconsistent) |
|
47 { |
|
48 aIsInconsistent = false; |
|
49 |
|
50 if (sInitOnce.mProcessCreation.IsNull()) { |
|
51 char *mozAppRestart = PR_GetEnv("MOZ_APP_RESTART"); |
|
52 TimeStamp ts; |
|
53 |
|
54 /* When calling PR_SetEnv() with an empty value the existing variable may |
|
55 * be unset or set to the empty string depending on the underlying platform |
|
56 * thus we have to check if the variable is present and not empty. */ |
|
57 if (mozAppRestart && (strcmp(mozAppRestart, "") != 0)) { |
|
58 /* Firefox was restarted, use the first time-stamp we've taken as the new |
|
59 * process startup time. */ |
|
60 ts = sInitOnce.mFirstTimeStamp; |
|
61 } else { |
|
62 TimeStamp now = Now(); |
|
63 uint64_t uptime = ComputeProcessUptime(); |
|
64 |
|
65 ts = now - TimeDuration::FromMicroseconds(uptime); |
|
66 |
|
67 if ((ts > sInitOnce.mFirstTimeStamp) || (uptime == 0)) { |
|
68 /* If the process creation timestamp was inconsistent replace it with |
|
69 * the first one instead and notify that a telemetry error was |
|
70 * detected. */ |
|
71 aIsInconsistent = true; |
|
72 ts = sInitOnce.mFirstTimeStamp; |
|
73 } |
|
74 } |
|
75 |
|
76 sInitOnce.mProcessCreation = ts; |
|
77 } |
|
78 |
|
79 return sInitOnce.mProcessCreation; |
|
80 } |
|
81 |
|
82 void |
|
83 TimeStamp::RecordProcessRestart() |
|
84 { |
|
85 sInitOnce.mProcessCreation = TimeStamp(); |
|
86 } |
|
87 |
|
88 } // namespace mozilla |