Bug 1869043 remove declaration of missing CreateOrDestroyAudioTracks r=padenot
[gecko.git] / tools / performance / PerfStats.h
blob46be5a0e031570b6468380f1b478f92fa3900dfe
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/. */
7 #ifndef PerfStats_h
8 #define PerfStats_h
10 #include "mozilla/TimeStamp.h"
11 #include "mozilla/StaticMutex.h"
12 #include "mozilla/StaticPtr.h"
13 #include "mozilla/MozPromise.h"
14 #include <memory>
15 #include <string>
16 #include <limits>
18 // PerfStats
20 // Framework for low overhead selective collection of internal performance
21 // metrics through ChromeUtils.
23 // Gathering: in C++, wrap execution in an RAII class
24 // PerfStats::AutoMetricRecording<PerfStats::Metric::MyMetric> or call
25 // PerfStats::RecordMeasurement{Start,End} manually. Use
26 // RecordMeasurementCount() for incrementing counters.
28 // Controlling: Use ChromeUtils.SetPerfStatsCollectionMask(mask), where mask=0
29 // disables all metrics and mask=0xFFFFFFFF enables all of them.
31 // Reporting: Results can be accessed with ChromeUtils.CollectPerfStats().
32 // Browsertime will sum results across processes and report them.
34 // Define a new metric by adding it to this list. It will be created as a class
35 // enum value mozilla::PerfStats::Metric::MyMetricName.
36 #define FOR_EACH_PERFSTATS_METRIC(MACRO) \
37 MACRO(DisplayListBuilding) \
38 MACRO(Rasterizing) \
39 MACRO(WrDisplayListBuilding) \
40 MACRO(LayerTransactions) \
41 MACRO(Compositing) \
42 MACRO(Reflowing) \
43 MACRO(Styling) \
44 MACRO(HttpChannelCompletion) \
45 MACRO(HttpChannelCompletion_Network) \
46 MACRO(HttpChannelCompletion_Cache) \
47 MACRO(HttpChannelAsyncOpenToTransactionPending) \
48 MACRO(HttpChannelResponseStartParentToContent) \
49 MACRO(HttpChannelResponseEndParentToContent) \
50 MACRO(ResponseEndSocketToParent) \
51 MACRO(OnStartRequestSocketToParent) \
52 MACRO(OnDataAvailableSocketToParent) \
53 MACRO(OnStopRequestSocketToParent) \
54 MACRO(OnStartRequestToContent) \
55 MACRO(OnDataAvailableToContent) \
56 MACRO(OnStopRequestToContent) \
57 MACRO(JSBC_Compression) \
58 MACRO(JSBC_Decompression) \
59 MACRO(JSBC_IO_Read) \
60 MACRO(JSBC_IO_Write) \
61 MACRO(MinorGC) \
62 MACRO(MajorGC) \
63 MACRO(NonIdleMajorGC) \
64 MACRO(A11Y_DoInitialUpdate) \
65 MACRO(A11Y_ProcessQueuedCacheUpdate)
67 namespace mozilla {
69 namespace dom {
70 // Forward declaration.
71 class ContentParent;
72 } // namespace dom
74 class PerfStats {
75 public:
76 typedef MozPromise<nsCString, bool, true> PerfStatsPromise;
78 enum class Metric : uint32_t {
79 #define DECLARE_ENUM(metric) metric,
80 FOR_EACH_PERFSTATS_METRIC(DECLARE_ENUM)
81 #undef DECLARE_ENUM
82 Max
85 // MetricMask is a bitmask based on 'Metric', i.e. Metric::LayerBuilding (2)
86 // is synonymous to 1 << 2 in MetricMask.
87 using MetricMask = uint64_t;
89 static void RecordMeasurementStart(Metric aMetric) {
90 if (!(sCollectionMask & (1 << static_cast<uint64_t>(aMetric)))) {
91 return;
93 RecordMeasurementStartInternal(aMetric);
96 static void RecordMeasurementEnd(Metric aMetric) {
97 if (!(sCollectionMask & (1 << static_cast<uint64_t>(aMetric)))) {
98 return;
100 RecordMeasurementEndInternal(aMetric);
103 static void RecordMeasurement(Metric aMetric, TimeDuration aDuration) {
104 if (!(sCollectionMask & (1 << static_cast<uint64_t>(aMetric)))) {
105 return;
107 RecordMeasurementInternal(aMetric, aDuration);
110 static void RecordMeasurementCounter(Metric aMetric,
111 uint64_t aIncrementAmount) {
112 if (!(sCollectionMask & (1 << static_cast<uint64_t>(aMetric)))) {
113 return;
115 RecordMeasurementCounterInternal(aMetric, aIncrementAmount);
118 template <Metric N>
119 class AutoMetricRecording {
120 public:
121 AutoMetricRecording() { PerfStats::RecordMeasurementStart(N); }
122 ~AutoMetricRecording() { PerfStats::RecordMeasurementEnd(N); }
125 static void SetCollectionMask(MetricMask aMask);
126 static MetricMask GetCollectionMask();
128 static RefPtr<PerfStatsPromise> CollectPerfStatsJSON() {
129 return GetSingleton()->CollectPerfStatsJSONInternal();
132 static nsCString CollectLocalPerfStatsJSON() {
133 return GetSingleton()->CollectLocalPerfStatsJSONInternal();
136 static void StorePerfStats(dom::ContentParent* aParent,
137 const nsACString& aPerfStats) {
138 GetSingleton()->StorePerfStatsInternal(aParent, aPerfStats);
141 private:
142 static PerfStats* GetSingleton();
143 static void RecordMeasurementStartInternal(Metric aMetric);
144 static void RecordMeasurementEndInternal(Metric aMetric);
145 static void RecordMeasurementInternal(Metric aMetric, TimeDuration aDuration);
146 static void RecordMeasurementCounterInternal(Metric aMetric,
147 uint64_t aIncrementAmount);
149 void ResetCollection();
150 void StorePerfStatsInternal(dom::ContentParent* aParent,
151 const nsACString& aPerfStats);
152 RefPtr<PerfStatsPromise> CollectPerfStatsJSONInternal();
153 nsCString CollectLocalPerfStatsJSONInternal();
155 static MetricMask sCollectionMask;
156 static StaticMutex sMutex MOZ_UNANNOTATED;
157 static StaticAutoPtr<PerfStats> sSingleton;
158 TimeStamp mRecordedStarts[static_cast<size_t>(Metric::Max)];
159 double mRecordedTimes[static_cast<size_t>(Metric::Max)];
160 uint32_t mRecordedCounts[static_cast<size_t>(Metric::Max)];
161 nsTArray<nsCString> mStoredPerfStats;
164 static_assert(1 << (static_cast<uint64_t>(PerfStats::Metric::Max) - 1) <=
165 std::numeric_limits<PerfStats::MetricMask>::max(),
166 "More metrics than can fit into sCollectionMask bitmask");
168 } // namespace mozilla
170 #endif // PerfStats_h