xpcom/ds/TimeStamp.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/ds/TimeStamp.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,88 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/*
    1.11 + * Implementation of the OS-independent methods of the TimeStamp class
    1.12 + */
    1.13 +
    1.14 +#include "mozilla/TimeStamp.h"
    1.15 +#include "prenv.h"
    1.16 +
    1.17 +namespace mozilla {
    1.18 +
    1.19 +/**
    1.20 + * Wrapper class used to initialize static data used by the TimeStamp class
    1.21 + */
    1.22 +struct TimeStampInitialization {
    1.23 +  /**
    1.24 +   * First timestamp taken when the class static initializers are run. This
    1.25 +   * timestamp is used to sanitize timestamps coming from different sources.
    1.26 +   */
    1.27 +  TimeStamp mFirstTimeStamp;
    1.28 +
    1.29 +  /**
    1.30 +   * Timestamp representing the time when the process was created. This field
    1.31 +   * is populated lazily the first time this information is required and is
    1.32 +   * replaced every time the process is restarted.
    1.33 +   */
    1.34 +  TimeStamp mProcessCreation;
    1.35 +
    1.36 +  TimeStampInitialization() {
    1.37 +    TimeStamp::Startup();
    1.38 +    mFirstTimeStamp = TimeStamp::Now();
    1.39 +  };
    1.40 +
    1.41 +  ~TimeStampInitialization() {
    1.42 +    TimeStamp::Shutdown();
    1.43 +  };
    1.44 +};
    1.45 +
    1.46 +static TimeStampInitialization sInitOnce;
    1.47 +
    1.48 +TimeStamp
    1.49 +TimeStamp::ProcessCreation(bool& aIsInconsistent)
    1.50 +{
    1.51 +  aIsInconsistent = false;
    1.52 +
    1.53 +  if (sInitOnce.mProcessCreation.IsNull()) {
    1.54 +    char *mozAppRestart = PR_GetEnv("MOZ_APP_RESTART");
    1.55 +    TimeStamp ts;
    1.56 +
    1.57 +    /* When calling PR_SetEnv() with an empty value the existing variable may
    1.58 +     * be unset or set to the empty string depending on the underlying platform
    1.59 +     * thus we have to check if the variable is present and not empty. */
    1.60 +    if (mozAppRestart && (strcmp(mozAppRestart, "") != 0)) {
    1.61 +      /* Firefox was restarted, use the first time-stamp we've taken as the new
    1.62 +       * process startup time. */
    1.63 +      ts = sInitOnce.mFirstTimeStamp;
    1.64 +    } else {
    1.65 +      TimeStamp now = Now();
    1.66 +      uint64_t uptime = ComputeProcessUptime();
    1.67 +
    1.68 +      ts = now - TimeDuration::FromMicroseconds(uptime);
    1.69 +
    1.70 +      if ((ts > sInitOnce.mFirstTimeStamp) || (uptime == 0)) {
    1.71 +        /* If the process creation timestamp was inconsistent replace it with
    1.72 +         * the first one instead and notify that a telemetry error was
    1.73 +         * detected. */
    1.74 +        aIsInconsistent = true;
    1.75 +        ts = sInitOnce.mFirstTimeStamp;
    1.76 +      }
    1.77 +    }
    1.78 +
    1.79 +    sInitOnce.mProcessCreation = ts;
    1.80 +  }
    1.81 +
    1.82 +  return sInitOnce.mProcessCreation;
    1.83 +}
    1.84 +
    1.85 +void
    1.86 +TimeStamp::RecordProcessRestart()
    1.87 +{
    1.88 +  sInitOnce.mProcessCreation = TimeStamp();
    1.89 +}
    1.90 +
    1.91 +} // namespace mozilla

mercurial