michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Implementation of the OS-independent methods of the TimeStamp class michael@0: */ michael@0: michael@0: #include "mozilla/TimeStamp.h" michael@0: #include "prenv.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: /** michael@0: * Wrapper class used to initialize static data used by the TimeStamp class michael@0: */ michael@0: struct TimeStampInitialization { michael@0: /** michael@0: * First timestamp taken when the class static initializers are run. This michael@0: * timestamp is used to sanitize timestamps coming from different sources. michael@0: */ michael@0: TimeStamp mFirstTimeStamp; michael@0: michael@0: /** michael@0: * Timestamp representing the time when the process was created. This field michael@0: * is populated lazily the first time this information is required and is michael@0: * replaced every time the process is restarted. michael@0: */ michael@0: TimeStamp mProcessCreation; michael@0: michael@0: TimeStampInitialization() { michael@0: TimeStamp::Startup(); michael@0: mFirstTimeStamp = TimeStamp::Now(); michael@0: }; michael@0: michael@0: ~TimeStampInitialization() { michael@0: TimeStamp::Shutdown(); michael@0: }; michael@0: }; michael@0: michael@0: static TimeStampInitialization sInitOnce; michael@0: michael@0: TimeStamp michael@0: TimeStamp::ProcessCreation(bool& aIsInconsistent) michael@0: { michael@0: aIsInconsistent = false; michael@0: michael@0: if (sInitOnce.mProcessCreation.IsNull()) { michael@0: char *mozAppRestart = PR_GetEnv("MOZ_APP_RESTART"); michael@0: TimeStamp ts; michael@0: michael@0: /* When calling PR_SetEnv() with an empty value the existing variable may michael@0: * be unset or set to the empty string depending on the underlying platform michael@0: * thus we have to check if the variable is present and not empty. */ michael@0: if (mozAppRestart && (strcmp(mozAppRestart, "") != 0)) { michael@0: /* Firefox was restarted, use the first time-stamp we've taken as the new michael@0: * process startup time. */ michael@0: ts = sInitOnce.mFirstTimeStamp; michael@0: } else { michael@0: TimeStamp now = Now(); michael@0: uint64_t uptime = ComputeProcessUptime(); michael@0: michael@0: ts = now - TimeDuration::FromMicroseconds(uptime); michael@0: michael@0: if ((ts > sInitOnce.mFirstTimeStamp) || (uptime == 0)) { michael@0: /* If the process creation timestamp was inconsistent replace it with michael@0: * the first one instead and notify that a telemetry error was michael@0: * detected. */ michael@0: aIsInconsistent = true; michael@0: ts = sInitOnce.mFirstTimeStamp; michael@0: } michael@0: } michael@0: michael@0: sInitOnce.mProcessCreation = ts; michael@0: } michael@0: michael@0: return sInitOnce.mProcessCreation; michael@0: } michael@0: michael@0: void michael@0: TimeStamp::RecordProcessRestart() michael@0: { michael@0: sInitOnce.mProcessCreation = TimeStamp(); michael@0: } michael@0: michael@0: } // namespace mozilla