Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/threads/InputEventStatistics.h
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
#if !defined(InputEventStatistics_h_)
8
#define InputEventStatistics_h_
9
10
#include "mozilla/ClearOnShutdown.h"
11
#include "mozilla/Maybe.h"
12
#include "mozilla/Preferences.h"
13
#include "mozilla/TimeStamp.h"
14
#include "nsTArray.h"
15
16
namespace mozilla {
17
18
class InputEventStatistics
19
{
20
  // The default amount of time (milliseconds) required for handling a input
21
  // event.
22
  static const uint16_t sDefaultInputDuration = 1;
23
24
  // The number of processed input events we use to predict the amount of time
25
  // required to process the following input events.
26
  static const uint16_t sInputCountForPrediction = 9;
27
28
  // The default maximum and minimum time (milliseconds) we reserve for handling
29
  // input events in each frame.
30
  static const uint16_t sMaxReservedTimeForHandlingInput = 8;
31
  static const uint16_t sMinReservedTimeForHandlingInput = 1;
32
33
  class TimeDurationCircularBuffer
34
  {
35
    int16_t mSize;
36
    int16_t mCurrentIndex;
37
    nsTArray<TimeDuration> mBuffer;
38
    TimeDuration mTotal;
39
40
  public:
41
    TimeDurationCircularBuffer(uint32_t aSize, TimeDuration& aDefaultValue)
42
      : mSize(aSize)
43
      , mCurrentIndex(0)
44
0
    {
45
0
      mSize = mSize == 0 ? sInputCountForPrediction : mSize;
46
0
      for (int16_t index = 0; index < mSize; ++index) {
47
0
        mBuffer.AppendElement(aDefaultValue);
48
0
        mTotal += aDefaultValue;
49
0
      }
50
0
    }
51
52
    void Insert(TimeDuration& aDuration)
53
0
    {
54
0
      mTotal += (aDuration - mBuffer[mCurrentIndex]);
55
0
      mBuffer[mCurrentIndex++] = aDuration;
56
0
      if (mCurrentIndex == mSize) {
57
0
        mCurrentIndex = 0;
58
0
      }
59
0
    }
60
61
    TimeDuration GetMean();
62
  };
63
64
  UniquePtr<TimeDurationCircularBuffer> mLastInputDurations;
65
  TimeDuration mMaxInputDuration;
66
  TimeDuration mMinInputDuration;
67
  bool mEnable;
68
69
  // We'd like to have our constructor and destructor be private to enforce our
70
  // singleton, but because UniquePtr needs to be able to destruct our class we
71
  // can't. This is a trick that ensures that we're the only code that can
72
  // construct ourselves: nobody else can access ConstructorCookie and therefore
73
  // nobody else can construct an InputEventStatistics.
74
  struct ConstructorCookie {};
75
76
public:
77
  explicit InputEventStatistics(ConstructorCookie&&);
78
  ~InputEventStatistics()
79
0
  {
80
0
  }
81
82
  static InputEventStatistics& Get();
83
84
  void UpdateInputDuration(TimeDuration aDuration)
85
0
  {
86
0
    if (!mEnable) {
87
0
      return;
88
0
    }
89
0
    mLastInputDurations->Insert(aDuration);
90
0
  }
91
92
  TimeStamp GetInputHandlingStartTime(uint32_t aInputCount);
93
94
  void SetEnable(bool aEnable)
95
0
  {
96
0
    mEnable = aEnable;
97
0
  }
98
};
99
100
class MOZ_RAII AutoTimeDurationHelper final
101
{
102
public:
103
  AutoTimeDurationHelper()
104
0
  {
105
0
    mStartTime = TimeStamp::Now();
106
0
  }
107
108
  ~AutoTimeDurationHelper()
109
0
  {
110
0
    InputEventStatistics::Get().UpdateInputDuration(TimeStamp::Now() - mStartTime);
111
0
  }
112
113
private:
114
  TimeStamp mStartTime;
115
};
116
117
} // namespace mozilla
118
119
#endif // InputEventStatistics_h_