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/. */
10 #include "mozilla/TimeStamp.h"
11 #include "mozilla/StaticMutex.h"
12 #include "mozilla/StaticPtr.h"
13 #include "mozilla/MozPromise.h"
22 typedef MozPromise
<nsCString
, bool, true> PerfStatsPromise
;
24 enum class Metric
: uint32_t {
25 DisplayListBuilding
= 0,
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
)))) {
43 RecordMeasurementStartInternal(aMetric
);
46 static void RecordMeasurementEnd(Metric aMetric
) {
47 if (!(sCollectionMask
& (1 << static_cast<uint64_t>(aMetric
)))) {
50 RecordMeasurementEndInternal(aMetric
);
54 class AutoMetricRecording
{
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();
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