Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
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 2 : TimeoutBudgetManager::Get()
19 : {
20 2 : static TimeoutBudgetManager gTimeoutBudgetManager;
21 2 : return gTimeoutBudgetManager;
22 : }
23 :
24 : void
25 1 : TimeoutBudgetManager::StartRecording(const TimeStamp& aNow)
26 : {
27 1 : mStart = aNow;
28 1 : }
29 :
30 : void
31 1 : TimeoutBudgetManager::StopRecording()
32 : {
33 1 : mStart = TimeStamp();
34 1 : }
35 :
36 : TimeDuration
37 1 : TimeoutBudgetManager::RecordExecution(const TimeStamp& aNow,
38 : const Timeout* aTimeout,
39 : bool aIsBackground)
40 : {
41 1 : if (!mStart) {
42 : // If we've started a sync operation mStart might be null, in
43 : // which case we should not record this piece of execution.
44 0 : return TimeDuration();
45 : }
46 :
47 1 : TimeDuration duration = aNow - mStart;
48 :
49 1 : if (aIsBackground) {
50 0 : if (aTimeout->mIsTracking) {
51 0 : mTelemetryData.mBackgroundTracking += duration;
52 : } else {
53 0 : mTelemetryData.mBackgroundNonTracking += duration;
54 : }
55 : } else {
56 1 : if (aTimeout->mIsTracking) {
57 0 : mTelemetryData.mForegroundTracking += duration;
58 : } else {
59 1 : mTelemetryData.mForegroundNonTracking += duration;
60 : }
61 : }
62 :
63 1 : return duration;
64 : }
65 :
66 : void
67 0 : TimeoutBudgetManager::Accumulate(Telemetry::HistogramID aId,
68 : const TimeDuration& aSample)
69 : {
70 0 : uint32_t sample = std::round(aSample.ToMilliseconds());
71 0 : if (sample) {
72 0 : Telemetry::Accumulate(aId, sample);
73 : }
74 0 : }
75 :
76 : void
77 1 : TimeoutBudgetManager::MaybeCollectTelemetry(const TimeStamp& aNow)
78 : {
79 1 : if ((aNow - mLastCollection).ToMilliseconds() < kTelemetryPeriodMS) {
80 1 : return;
81 : }
82 :
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 :
92 0 : mTelemetryData = TelemetryData();
93 0 : mLastCollection = aNow;
94 : }
95 :
96 : } // namespace dom
97 : } // namespace mozilla
|