summaryrefslogtreecommitdiffstats
path: root/layout/base/LayoutTelemetryTools.cpp
blob: b59b73f81096197899b1c3298f8ea4c099d4cb34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/layout/LayoutTelemetryTools.h"

#include "MainThreadUtils.h"
#include "mozilla/Atomics.h"
#include "mozilla/PodOperations.h"
#include "mozilla/Telemetry.h"

using namespace mozilla;
using namespace mozilla::layout_telemetry;

// Returns the key name expected by telemetry. Keep to date with
// toolkits/components/telemetry/Histograms.json.
static nsLiteralCString SubsystemTelemetryKey(LayoutSubsystem aSubsystem) {
  switch (aSubsystem) {
    default:
      MOZ_CRASH("Unexpected LayoutSubsystem value");
    case LayoutSubsystem::Restyle:
      return "Restyle"_ns;
    case LayoutSubsystem::Reflow:
      return "ReflowOther"_ns;
    case LayoutSubsystem::ReflowFlex:
      return "ReflowFlex"_ns;
    case LayoutSubsystem::ReflowGrid:
      return "ReflowGrid"_ns;
    case LayoutSubsystem::ReflowTable:
      return "ReflowTable"_ns;
    case LayoutSubsystem::ReflowText:
      return "ReflowText"_ns;
  }
}

static AutoRecord* sCurrentRecord;

static FlushKind ToKind(FlushType aFlushType) {
  switch (aFlushType) {
    default:
      MOZ_CRASH("Expected FlushType::Style or FlushType::Layout");
    case FlushType::Style:
      return FlushKind::Style;
    case FlushType::Layout:
      return FlushKind::Layout;
  }
}

namespace mozilla {
namespace layout_telemetry {

Data::Data() {
  PodZero(&mReqsPerFlush);
  PodZero(&mFlushesPerTick);
  PodZero(&mLayoutSubsystemDurationMs);
}

void Data::IncReqsPerFlush(FlushType aFlushType) {
  mReqsPerFlush[ToKind(aFlushType)]++;
}

void Data::IncFlushesPerTick(FlushType aFlushType) {
  mFlushesPerTick[ToKind(aFlushType)]++;
}

void Data::PingReqsPerFlushTelemetry(FlushType aFlushType) {
  auto flushKind = ToKind(aFlushType);
  if (flushKind == FlushKind::Layout) {
    auto styleFlushReqs = mReqsPerFlush[FlushKind::Style].value();
    auto layoutFlushReqs = mReqsPerFlush[FlushKind::Layout].value();
    Telemetry::Accumulate(Telemetry::PRESSHELL_REQS_PER_LAYOUT_FLUSH,
                          "Style"_ns, styleFlushReqs);
    Telemetry::Accumulate(Telemetry::PRESSHELL_REQS_PER_LAYOUT_FLUSH,
                          "Layout"_ns, layoutFlushReqs);
    mReqsPerFlush[FlushKind::Style] = SaturateUint8(0);
    mReqsPerFlush[FlushKind::Layout] = SaturateUint8(0);
  } else {
    auto styleFlushReqs = mReqsPerFlush[FlushKind::Style].value();
    Telemetry::Accumulate(Telemetry::PRESSHELL_REQS_PER_STYLE_FLUSH,
                          styleFlushReqs);
    mReqsPerFlush[FlushKind::Style] = SaturateUint8(0);
  }
}

void Data::PingFlushPerTickTelemetry(FlushType aFlushType) {
  auto flushKind = ToKind(aFlushType);
  auto styleFlushes = mFlushesPerTick[FlushKind::Style].value();
  if (styleFlushes > 0) {
    Telemetry::Accumulate(Telemetry::PRESSHELL_FLUSHES_PER_TICK, "Style"_ns,
                          styleFlushes);
    mFlushesPerTick[FlushKind::Style] = SaturateUint8(0);
  }

  auto layoutFlushes = mFlushesPerTick[FlushKind::Layout].value();
  if (flushKind == FlushKind::Layout && layoutFlushes > 0) {
    Telemetry::Accumulate(Telemetry::PRESSHELL_FLUSHES_PER_TICK, "Layout"_ns,
                          layoutFlushes);
    mFlushesPerTick[FlushKind::Layout] = SaturateUint8(0);
  }
}

void Data::PingTotalMsPerTickTelemetry(FlushType aFlushType) {
  auto flushKind = ToKind(aFlushType);
  auto range = (flushKind == FlushKind::Style)
                   ? MakeEnumeratedRange(LayoutSubsystem::Restyle,
                                         LayoutSubsystem::Reflow)
                   : MakeEnumeratedRange(LayoutSubsystem::Reflow,
                                         LayoutSubsystem::Count);

  for (auto subsystem : range) {
    auto key = SubsystemTelemetryKey(subsystem);
    double& duration = mLayoutSubsystemDurationMs[subsystem];
    if (duration > 0.0) {
      Telemetry::Accumulate(Telemetry::PRESSHELL_LAYOUT_TOTAL_MS_PER_TICK, key,
                            static_cast<uint32_t>(duration));
      duration = 0.0;
    }
  }
}

void Data::PingPerTickTelemetry(FlushType aFlushType) {
  PingFlushPerTickTelemetry(aFlushType);
  PingTotalMsPerTickTelemetry(aFlushType);
}

AutoRecord::AutoRecord(LayoutSubsystem aSubsystem)
    : AutoRecord(nullptr, aSubsystem) {}

AutoRecord::AutoRecord(Data* aLayoutTelemetry, LayoutSubsystem aSubsystem)
    : mParentRecord(sCurrentRecord),
      mLayoutTelemetry(aLayoutTelemetry),
      mSubsystem(aSubsystem),
      mStartTime(TimeStamp::Now()),
      mDurationMs(0.0) {
  MOZ_ASSERT(NS_IsMainThread());

  // If we're re-entering the same subsystem, don't update the current record.
  if (mParentRecord) {
    if (mParentRecord->mSubsystem == mSubsystem) {
      return;
    }

    mLayoutTelemetry = mParentRecord->mLayoutTelemetry;
    MOZ_ASSERT(mLayoutTelemetry);

    // If we're entering a new subsystem, record the amount of time spent in the
    // parent record before setting the new current record.
    mParentRecord->mDurationMs +=
        (mStartTime - mParentRecord->mStartTime).ToMilliseconds();
  }

  sCurrentRecord = this;
}

AutoRecord::~AutoRecord() {
  if (sCurrentRecord != this) {
    // If this record is not head of the list, do nothing.
    return;
  }

  TimeStamp now = TimeStamp::Now();
  mDurationMs += (now - mStartTime).ToMilliseconds();
  mLayoutTelemetry->mLayoutSubsystemDurationMs[mSubsystem] += mDurationMs;

  if (mParentRecord) {
    // Restart the parent recording from this point
    mParentRecord->mStartTime = now;
  }

  // Unlink this record from the current record list
  sCurrentRecord = mParentRecord;
}

}  // namespace layout_telemetry
}  // namespace mozilla