Bug 701863 - Add a way to clone histograms; r=taras
Add nsITelemetry::histogramFrom.
--- a/toolkit/components/telemetry/Telemetry.cpp
+++ b/toolkit/components/telemetry/Telemetry.cpp
@@ -116,16 +116,35 @@ const TelemetryHistogram gHistograms[] =
#define HISTOGRAM(id, min, max, bucket_count, histogram_type, b) \
{ NULL, NS_STRINGIFY(id), min, max, bucket_count, nsITelemetry::HISTOGRAM_ ## histogram_type },
#include "TelemetryHistograms.h"
#undef HISTOGRAM
};
+bool
+TelemetryHistogramType(Histogram *h, PRUint32 *result)
+{
+ switch (h->histogram_type()) {
+ case Histogram::HISTOGRAM:
+ *result = nsITelemetry::HISTOGRAM_EXPONENTIAL;
+ break;
+ case Histogram::LINEAR_HISTOGRAM:
+ *result = nsITelemetry::HISTOGRAM_LINEAR;
+ break;
+ case Histogram::BOOLEAN_HISTOGRAM:
+ *result = nsITelemetry::HISTOGRAM_BOOLEAN;
+ break;
+ default:
+ return false;
+ }
+ return true;
+}
+
nsresult
HistogramGet(const char *name, PRUint32 min, PRUint32 max, PRUint32 bucketCount,
PRUint32 histogramType, Histogram **result)
{
if (histogramType != nsITelemetry::HISTOGRAM_BOOLEAN) {
// Sanity checks for histogram parameters.
if (min >= max)
return NS_ERROR_ILLEGAL_VALUE;
@@ -425,16 +444,43 @@ TelemetryImpl::GetHistogramByName(const
nsresult rv = GetHistogramByEnumId(entry->mData, ret);
if (NS_FAILED(rv))
return rv;
return NS_OK;
}
NS_IMETHODIMP
+TelemetryImpl::HistogramFrom(const nsACString &name, const nsACString &existing_name,
+ JSContext *cx, jsval *ret)
+{
+ Histogram *existing;
+ nsresult rv = GetHistogramByName(existing_name, &existing);
+ if (NS_FAILED(rv))
+ return rv;
+
+ PRUint32 histogramType;
+ bool success = TelemetryHistogramType(existing, &histogramType);
+ if (!success)
+ return NS_ERROR_INVALID_ARG;
+
+ Histogram *clone;
+ rv = HistogramGet(PromiseFlatCString(name).get(), existing->declared_min(),
+ existing->declared_max(), existing->bucket_count(),
+ histogramType, &clone);
+ if (NS_FAILED(rv))
+ return rv;
+
+ Histogram::SampleSet ss;
+ existing->SnapshotSample(&ss);
+ clone->AddSampleSet(ss);
+ return WrapAndReturnHistogram(clone, cx, ret);
+}
+
+NS_IMETHODIMP
TelemetryImpl::GetHistogramSnapshots(JSContext *cx, jsval *ret)
{
JSObject *root_obj = JS_NewObject(cx, NULL, NULL, NULL);
if (!root_obj)
return NS_ERROR_FAILURE;
*ret = OBJECT_TO_JSVAL(root_obj);
StatisticsRecorder::Histograms h;
--- a/toolkit/components/telemetry/nsITelemetry.idl
+++ b/toolkit/components/telemetry/nsITelemetry.idl
@@ -77,16 +77,27 @@ interface nsITelemetry : nsISupports
* The returned object has the following functions:
* add(int) - Adds an int value to the appropriate bucket
* snapshot() - Returns a snapshot of the histogram with the same data fields as in histogramSnapshots()
*/
[implicit_jscontext]
jsval newHistogram(in ACString name, in PRUint32 min, in PRUint32 max, in PRUint32 bucket_count, in unsigned long histogram_type);
/**
+ * Create a histogram using the current state of an existing histogram. The
+ * existing histogram must be registered in TelemetryHistograms.h.
+ *
+ * @param name Unique histogram name
+ * @param existing_name Existing histogram name
+ * The returned object has the same functions as a histogram returned from newHistogram.
+ */
+ [implicit_jscontext]
+ jsval histogramFrom(in ACString name, in ACString existing_name);
+
+ /**
* Same as newHistogram above, but for histograms registered in TelemetryHistograms.h.
*
* @param id - unique identifier from TelemetryHistograms.h
*/
[implicit_jscontext]
jsval getHistogramById(in ACString id);
/**