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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/* -*- 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/. */
#ifndef PerfStats_h
#define PerfStats_h
#include "mozilla/Atomics.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/StaticMutex.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/MozPromise.h"
#include <limits>
// PerfStats
//
// Framework for low overhead selective collection of internal performance
// metrics through ChromeUtils.
//
// Gathering: in C++, wrap execution in an RAII class
// PerfStats::AutoMetricRecording<PerfStats::Metric::MyMetric> or call
// PerfStats::RecordMeasurement{Start,End} manually. Use
// RecordMeasurementCount() for incrementing counters.
//
// Controlling: Use ChromeUtils.setPerfStatsFeatures(array), where an empty
// array disables all metrics. Pass metric names as strings, e.g.
// ["LayerTransactions", "Rasterizing"]. To enable all features, use
// ChromeUtils.enableAllPerfStatsFeatures();
//
// Reporting: Results can be accessed with ChromeUtils.CollectPerfStats().
// Browsertime will sum results across processes and report them.
// Define a new metric by adding it to this list. It will be created as a class
// enum value mozilla::PerfStats::Metric::MyMetricName.
#define FOR_EACH_PERFSTATS_METRIC(MACRO) \
MACRO(DisplayListBuilding) \
MACRO(Rasterizing) \
MACRO(WrDisplayListBuilding) \
MACRO(LayerTransactions) \
MACRO(FrameBuilding) \
MACRO(Compositing) \
MACRO(Reflowing) \
MACRO(Styling) \
MACRO(HttpChannelCompletion) \
MACRO(HttpChannelCompletion_Network) \
MACRO(HttpChannelCompletion_Cache) \
MACRO(HttpChannelAsyncOpenToTransactionPending) \
MACRO(HttpChannelResponseStartParentToContent) \
MACRO(HttpChannelResponseEndParentToContent) \
MACRO(HttpTransactionWaitTime) \
MACRO(ResponseEndSocketToParent) \
MACRO(OnStartRequestSocketToParent) \
MACRO(OnDataAvailableSocketToParent) \
MACRO(OnStopRequestSocketToParent) \
MACRO(OnStartRequestToContent) \
MACRO(OnDataAvailableToContent) \
MACRO(OnStopRequestToContent) \
MACRO(JSBC_Compression) \
MACRO(JSBC_Decompression) \
MACRO(JSBC_IO_Read) \
MACRO(JSBC_IO_Write) \
MACRO(MinorGC) \
MACRO(MajorGC) \
MACRO(NonIdleMajorGC) \
MACRO(A11Y_DoInitialUpdate) \
MACRO(A11Y_ProcessQueuedCacheUpdate) \
MACRO(A11Y_ContentRemovedNode) \
MACRO(A11Y_ContentRemovedAcc) \
MACRO(A11Y_PruneOrInsertSubtree) \
MACRO(A11Y_ShutdownChildrenInSubtree) \
MACRO(A11Y_ShowEvent) \
MACRO(A11Y_RecvCache) \
MACRO(A11Y_ProcessShowEvent) \
MACRO(A11Y_CoalesceEvents) \
MACRO(A11Y_CoalesceMutationEvents) \
MACRO(A11Y_ProcessHideEvent) \
MACRO(A11Y_SendCache) \
MACRO(A11Y_WillRefresh) \
MACRO(A11Y_AccessibilityServiceInit) \
MACRO(A11Y_PlatformShowHideEvent)
namespace mozilla {
namespace dom {
// Forward declaration.
class ContentParent;
} // namespace dom
class PerfStats {
public:
typedef MozPromise<nsCString, bool, true> PerfStatsPromise;
// MetricMask is a bitmask based on 'Metric', i.e. Metric::LayerBuilding (2)
// is synonymous to 1 << 2 in MetricMask.
using MetricMask = uint64_t;
using MetricCounter = uint32_t;
enum class Metric : MetricMask {
#define DECLARE_ENUM(metric) metric,
FOR_EACH_PERFSTATS_METRIC(DECLARE_ENUM)
#undef DECLARE_ENUM
Max
};
static void RecordMeasurementStart(Metric aMetric) {
if (!(sCollectionMask & (1 << static_cast<MetricMask>(aMetric)))) {
return;
}
RecordMeasurementStartInternal(aMetric);
}
static void RecordMeasurementEnd(Metric aMetric) {
if (!(sCollectionMask & (1 << static_cast<MetricMask>(aMetric)))) {
return;
}
RecordMeasurementEndInternal(aMetric);
}
static void RecordMeasurement(Metric aMetric, TimeDuration aDuration) {
if (!(sCollectionMask & (1 << static_cast<MetricMask>(aMetric)))) {
return;
}
RecordMeasurementInternal(aMetric, aDuration);
}
static void RecordMeasurementCounter(Metric aMetric,
MetricMask aIncrementAmount) {
if (!(sCollectionMask & (1 << static_cast<MetricMask>(aMetric)))) {
return;
}
RecordMeasurementCounterInternal(aMetric, aIncrementAmount);
}
template <Metric N>
class AutoMetricRecording {
public:
AutoMetricRecording() { PerfStats::RecordMeasurementStart(N); }
~AutoMetricRecording() { PerfStats::RecordMeasurementEnd(N); }
};
static void SetCollectionMask(MetricMask aMask);
static MetricMask GetCollectionMask();
static RefPtr<PerfStatsPromise> CollectPerfStatsJSON() {
return GetSingleton()->CollectPerfStatsJSONInternal();
}
static nsCString CollectLocalPerfStatsJSON() {
return GetSingleton()->CollectLocalPerfStatsJSONInternal();
}
static void StorePerfStats(dom::ContentParent* aParent,
const nsACString& aPerfStats) {
GetSingleton()->StorePerfStatsInternal(aParent, aPerfStats);
}
// Returns the mask with the bit set for a given metric name, or 0 if not
// found
static MetricMask GetFeatureMask(const char* aMetricName);
private:
static PerfStats* GetSingleton();
static void RecordMeasurementStartInternal(Metric aMetric);
static void RecordMeasurementEndInternal(Metric aMetric);
static void RecordMeasurementInternal(Metric aMetric, TimeDuration aDuration);
static void RecordMeasurementCounterInternal(Metric aMetric,
MetricCounter aIncrementAmount);
void ResetCollection();
void StorePerfStatsInternal(dom::ContentParent* aParent,
const nsACString& aPerfStats);
RefPtr<PerfStatsPromise> CollectPerfStatsJSONInternal();
nsCString CollectLocalPerfStatsJSONInternal();
static Atomic<MetricMask, MemoryOrdering::Relaxed> sCollectionMask;
static StaticMutex sMutex MOZ_UNANNOTATED;
static StaticAutoPtr<PerfStats> sSingleton;
TimeStamp mRecordedStarts[static_cast<MetricMask>(Metric::Max)];
double mRecordedTimes[static_cast<MetricMask>(Metric::Max)];
MetricCounter mRecordedCounts[static_cast<MetricMask>(Metric::Max)];
nsTArray<nsCString> mStoredPerfStats;
};
static_assert(static_cast<PerfStats::MetricMask>(1)
<< (static_cast<uint64_t>(PerfStats::Metric::Max) - 1) <=
std::numeric_limits<PerfStats::MetricMask>::max(),
"More metrics than can fit into sCollectionMask bitmask");
} // namespace mozilla
#endif // PerfStats_h
|