Bug 1436680 - Allow non-templated uses of AutoCounter. r?chutten
In order to stay consistent with the AutoTimer api, allowed non-templates uses or AutoCounter.
MozReview-Commit-ID: 9qnAeQTIY9T
--- a/toolkit/components/telemetry/Telemetry.h
+++ b/toolkit/components/telemetry/Telemetry.h
@@ -300,16 +300,51 @@ public:
}
private:
const TimeStamp start;
const nsCString key;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
+class MOZ_RAII RuntimeAutoCounter
+{
+public:
+ explicit RuntimeAutoCounter(
+ HistogramID aId,
+ uint32_t counterStart = 0 MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
+ : id(aId)
+ , counter(counterStart)
+ {
+ MOZ_GUARD_OBJECT_NOTIFIER_INIT;
+ }
+
+ ~RuntimeAutoCounter()
+ {
+ Accumulate(id, counter);
+ }
+
+ // Prefix increment only, to encourage good habits.
+ void operator++()
+ {
+ ++counter;
+ }
+
+ // Chaining doesn't make any sense, don't return anything.
+ void operator+=(int increment)
+ {
+ counter += increment;
+ }
+
+private:
+ HistogramID id;
+ uint32_t counter;
+ MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
+};
+
template<HistogramID id>
class MOZ_RAII AutoCounter {
public:
explicit AutoCounter(uint32_t counterStart = 0 MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: counter(counterStart)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
--- a/toolkit/components/telemetry/docs/collection/histograms.rst
+++ b/toolkit/components/telemetry/docs/collection/histograms.rst
@@ -317,21 +317,31 @@ The ``Telemetry.h`` header also declares
nsresult
nsPluginHost::StopPluginInstance(nsNPAPIPluginInstance* aInstance)
{
Telemetry::AutoTimer<Telemetry::PLUGIN_SHUTDOWN_MS> timer;
...
return NS_OK;
}
-If the HistogramID is not known at compile time, one can use the ``RuntimeAutoTimer`` class, which behaves like the template parameterized ``AutoTimer``.
+If the HistogramID is not known at compile time, one can use the ``RuntimeAutoTimer`` and ``RuntimeAutoCounter`` classes, which behave like the template parameterized ``AutoTimer`` and ``AutoCounter`` ones.
.. code-block:: cpp
void
FunctionWithTiming(Telemetry::HistogramID aTelemetryID)
{
...
Telemetry::RuntimeAutoTimer timer(aTelemetryID);
...
}
+ int32_t
+ FunctionWithCounter(Telemetry::HistogramID aTelemetryID)
+ {
+ ...
+ Telemetry::RuntimeAutoCounter myCounter(aTelemetryID);
+ ++myCounter;
+ myCounter += 42;
+ ...
+ }
+
Prefer using the template parameterized ``AutoTimer`` and ``AutoCounter`` on hot paths, if possible.