Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/TimeoutBudgetManager.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
#include "TimeoutBudgetManager.h"
8
9
#include "mozilla/dom/Timeout.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
// Time between sampling timeout execution time.
15
const uint32_t kTelemetryPeriodMS = 1000;
16
17
/* static */ TimeoutBudgetManager&
18
TimeoutBudgetManager::Get()
19
0
{
20
0
  static TimeoutBudgetManager gTimeoutBudgetManager;
21
0
  return gTimeoutBudgetManager;
22
0
}
23
24
void
25
TimeoutBudgetManager::StartRecording(const TimeStamp& aNow)
26
0
{
27
0
  mStart = aNow;
28
0
}
29
30
void
31
TimeoutBudgetManager::StopRecording()
32
0
{
33
0
  mStart = TimeStamp();
34
0
}
35
36
TimeDuration
37
TimeoutBudgetManager::RecordExecution(const TimeStamp& aNow,
38
                                      const Timeout* aTimeout,
39
                                      bool aIsBackground)
40
0
{
41
0
  if (!mStart) {
42
0
    // If we've started a sync operation mStart might be null, in
43
0
    // which case we should not record this piece of execution.
44
0
    return TimeDuration();
45
0
  }
46
0
47
0
  TimeDuration duration = aNow - mStart;
48
0
49
0
  if (aIsBackground) {
50
0
    if (aTimeout->mIsTracking) {
51
0
      mTelemetryData.mBackgroundTracking += duration;
52
0
    } else {
53
0
      mTelemetryData.mBackgroundNonTracking += duration;
54
0
    }
55
0
  } else {
56
0
    if (aTimeout->mIsTracking) {
57
0
      mTelemetryData.mForegroundTracking += duration;
58
0
    } else {
59
0
      mTelemetryData.mForegroundNonTracking += duration;
60
0
    }
61
0
  }
62
0
63
0
  return duration;
64
0
}
65
66
void
67
TimeoutBudgetManager::Accumulate(Telemetry::HistogramID aId,
68
                                 const TimeDuration& aSample)
69
0
{
70
0
  uint32_t sample = std::round(aSample.ToMilliseconds());
71
0
  if (sample) {
72
0
    Telemetry::Accumulate(aId, sample);
73
0
  }
74
0
}
75
76
void
77
TimeoutBudgetManager::MaybeCollectTelemetry(const TimeStamp& aNow)
78
0
{
79
0
  if ((aNow - mLastCollection).ToMilliseconds() < kTelemetryPeriodMS) {
80
0
    return;
81
0
  }
82
0
83
0
  Accumulate(Telemetry::TIMEOUT_EXECUTION_FG_TRACKING_MS,
84
0
             mTelemetryData.mForegroundTracking);
85
0
  Accumulate(Telemetry::TIMEOUT_EXECUTION_FG_MS,
86
0
             mTelemetryData.mForegroundNonTracking);
87
0
  Accumulate(Telemetry::TIMEOUT_EXECUTION_BG_TRACKING_MS,
88
0
             mTelemetryData.mBackgroundTracking);
89
0
  Accumulate(Telemetry::TIMEOUT_EXECUTION_BG_MS,
90
0
             mTelemetryData.mBackgroundNonTracking);
91
0
92
0
  mTelemetryData = TelemetryData();
93
0
  mLastCollection = aNow;
94
0
}
95
96
} // namespace dom
97
} // namespace mozilla