Bug 1637211 [wpt PR 23529] - [testdriver] fix typos in addPointer documentation,...
[gecko.git] / tools / performance / PerfStats.h
blob68ebfc3a57a0b5307fdc8a9af27b813c64c00f47
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 namespace mozilla {
20 class PerfStats {
21 public:
22 typedef MozPromise<nsCString, bool, true> PerfStatsPromise;
24 enum class Metric : uint32_t {
25 DisplayListBuilding = 0,
26 Rasterizing,
27 LayerBuilding,
28 LayerTransactions,
29 Compositing,
30 Reflowing,
31 Styling,
32 Max
35 // MetricMask is a bitmask based on 'Metric', i.e. Metric::LayerBuilding (2)
36 // is synonymous to 1 << 2 in MetricMask.
37 using MetricMask = uint64_t;
39 static void RecordMeasurementStart(Metric aMetric) {
40 if (!(sCollectionMask & (1 << static_cast<uint64_t>(aMetric)))) {
41 return;
43 RecordMeasurementStartInternal(aMetric);
46 static void RecordMeasurementEnd(Metric aMetric) {
47 if (!(sCollectionMask & (1 << static_cast<uint64_t>(aMetric)))) {
48 return;
50 RecordMeasurementEndInternal(aMetric);
53 template <Metric N>
54 class AutoMetricRecording {
55 public:
56 AutoMetricRecording() { PerfStats::RecordMeasurementStart(N); }
57 ~AutoMetricRecording() { PerfStats::RecordMeasurementEnd(N); }
60 static void SetCollectionMask(MetricMask aMask);
62 static RefPtr<PerfStatsPromise> CollectPerfStatsJSON() {
63 return GetSingleton()->CollectPerfStatsJSONInternal();
66 static nsCString CollectLocalPerfStatsJSON() {
67 return GetSingleton()->CollectLocalPerfStatsJSONInternal();
70 private:
71 static PerfStats* GetSingleton();
72 static void RecordMeasurementStartInternal(Metric aMetric);
73 static void RecordMeasurementEndInternal(Metric aMetric);
75 RefPtr<PerfStatsPromise> CollectPerfStatsJSONInternal();
76 nsCString CollectLocalPerfStatsJSONInternal();
78 static MetricMask sCollectionMask;
79 static StaticMutex sMutex;
80 static StaticAutoPtr<PerfStats> sSingleton;
81 TimeStamp mRecordedStarts[static_cast<size_t>(Metric::Max)];
82 double mRecordedTimes[static_cast<size_t>(Metric::Max)];
85 static_assert(1 << (static_cast<uint64_t>(PerfStats::Metric::Max) - 1) <=
86 std::numeric_limits<PerfStats::MetricMask>::max(),
87 "More metrics than can fit into sCollectionMask bitmask");
89 } // namespace mozilla
91 #endif // PerfStats_h