Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* |
michael@0 | 8 | * Implementation of the OS-independent methods of the TimeStamp class |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "mozilla/TimeStamp.h" |
michael@0 | 12 | #include "prenv.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | |
michael@0 | 16 | /** |
michael@0 | 17 | * Wrapper class used to initialize static data used by the TimeStamp class |
michael@0 | 18 | */ |
michael@0 | 19 | struct TimeStampInitialization { |
michael@0 | 20 | /** |
michael@0 | 21 | * First timestamp taken when the class static initializers are run. This |
michael@0 | 22 | * timestamp is used to sanitize timestamps coming from different sources. |
michael@0 | 23 | */ |
michael@0 | 24 | TimeStamp mFirstTimeStamp; |
michael@0 | 25 | |
michael@0 | 26 | /** |
michael@0 | 27 | * Timestamp representing the time when the process was created. This field |
michael@0 | 28 | * is populated lazily the first time this information is required and is |
michael@0 | 29 | * replaced every time the process is restarted. |
michael@0 | 30 | */ |
michael@0 | 31 | TimeStamp mProcessCreation; |
michael@0 | 32 | |
michael@0 | 33 | TimeStampInitialization() { |
michael@0 | 34 | TimeStamp::Startup(); |
michael@0 | 35 | mFirstTimeStamp = TimeStamp::Now(); |
michael@0 | 36 | }; |
michael@0 | 37 | |
michael@0 | 38 | ~TimeStampInitialization() { |
michael@0 | 39 | TimeStamp::Shutdown(); |
michael@0 | 40 | }; |
michael@0 | 41 | }; |
michael@0 | 42 | |
michael@0 | 43 | static TimeStampInitialization sInitOnce; |
michael@0 | 44 | |
michael@0 | 45 | TimeStamp |
michael@0 | 46 | TimeStamp::ProcessCreation(bool& aIsInconsistent) |
michael@0 | 47 | { |
michael@0 | 48 | aIsInconsistent = false; |
michael@0 | 49 | |
michael@0 | 50 | if (sInitOnce.mProcessCreation.IsNull()) { |
michael@0 | 51 | char *mozAppRestart = PR_GetEnv("MOZ_APP_RESTART"); |
michael@0 | 52 | TimeStamp ts; |
michael@0 | 53 | |
michael@0 | 54 | /* When calling PR_SetEnv() with an empty value the existing variable may |
michael@0 | 55 | * be unset or set to the empty string depending on the underlying platform |
michael@0 | 56 | * thus we have to check if the variable is present and not empty. */ |
michael@0 | 57 | if (mozAppRestart && (strcmp(mozAppRestart, "") != 0)) { |
michael@0 | 58 | /* Firefox was restarted, use the first time-stamp we've taken as the new |
michael@0 | 59 | * process startup time. */ |
michael@0 | 60 | ts = sInitOnce.mFirstTimeStamp; |
michael@0 | 61 | } else { |
michael@0 | 62 | TimeStamp now = Now(); |
michael@0 | 63 | uint64_t uptime = ComputeProcessUptime(); |
michael@0 | 64 | |
michael@0 | 65 | ts = now - TimeDuration::FromMicroseconds(uptime); |
michael@0 | 66 | |
michael@0 | 67 | if ((ts > sInitOnce.mFirstTimeStamp) || (uptime == 0)) { |
michael@0 | 68 | /* If the process creation timestamp was inconsistent replace it with |
michael@0 | 69 | * the first one instead and notify that a telemetry error was |
michael@0 | 70 | * detected. */ |
michael@0 | 71 | aIsInconsistent = true; |
michael@0 | 72 | ts = sInitOnce.mFirstTimeStamp; |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | sInitOnce.mProcessCreation = ts; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | return sInitOnce.mProcessCreation; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | void |
michael@0 | 83 | TimeStamp::RecordProcessRestart() |
michael@0 | 84 | { |
michael@0 | 85 | sInitOnce.mProcessCreation = TimeStamp(); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | } // namespace mozilla |