Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/mozglue/misc/TimeStamp.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 <stdio.h>
13
#include <string.h>
14
#include <stdlib.h>
15
16
namespace mozilla {
17
18
/**
19
 * Wrapper class used to initialize static data used by the TimeStamp class
20
 */
21
struct TimeStampInitialization
22
{
23
  /**
24
   * First timestamp taken when the class static initializers are run. This
25
   * timestamp is used to sanitize timestamps coming from different sources.
26
   */
27
  TimeStamp mFirstTimeStamp;
28
29
  /**
30
   * Timestamp representing the time when the process was created. This field
31
   * is populated lazily the first time this information is required and is
32
   * replaced every time the process is restarted.
33
   */
34
  TimeStamp mProcessCreation;
35
36
  TimeStampInitialization()
37
3
  {
38
3
    TimeStamp::Startup();
39
3
    mFirstTimeStamp = TimeStamp::Now();
40
3
  };
41
42
  ~TimeStampInitialization()
43
0
  {
44
0
    TimeStamp::Shutdown();
45
0
  };
46
};
47
48
static TimeStampInitialization sInitOnce;
49
50
MFBT_API TimeStamp
51
TimeStamp::ProcessCreation(bool* aIsInconsistent)
52
106
{
53
106
  if (aIsInconsistent) {
54
0
    *aIsInconsistent = false;
55
0
  }
56
106
57
106
  if (sInitOnce.mProcessCreation.IsNull()) {
58
3
    char* mozAppRestart = getenv("MOZ_APP_RESTART");
59
3
    TimeStamp ts;
60
3
61
3
    /* When calling PR_SetEnv() with an empty value the existing variable may
62
3
     * be unset or set to the empty string depending on the underlying platform
63
3
     * thus we have to check if the variable is present and not empty. */
64
3
    if (mozAppRestart && (strcmp(mozAppRestart, "") != 0)) {
65
0
      /* Firefox was restarted, use the first time-stamp we've taken as the new
66
0
       * process startup time. */
67
0
      ts = sInitOnce.mFirstTimeStamp;
68
3
    } else {
69
3
      TimeStamp now = Now();
70
3
      uint64_t uptime = ComputeProcessUptime();
71
3
72
3
      ts = now - TimeDuration::FromMicroseconds(uptime);
73
3
74
3
      if ((ts > sInitOnce.mFirstTimeStamp) || (uptime == 0)) {
75
0
        /* If the process creation timestamp was inconsistent replace it with
76
0
         * the first one instead and notify that a telemetry error was
77
0
         * detected. */
78
0
        if (aIsInconsistent) {
79
0
          *aIsInconsistent = true;
80
0
        }
81
0
        ts = sInitOnce.mFirstTimeStamp;
82
0
      }
83
3
    }
84
3
85
3
    sInitOnce.mProcessCreation = ts;
86
3
  }
87
106
88
106
  return sInitOnce.mProcessCreation;
89
106
}
90
91
void
92
TimeStamp::RecordProcessRestart()
93
0
{
94
0
  sInitOnce.mProcessCreation = TimeStamp();
95
0
}
96
97
} // namespace mozilla