Bug 701863 - Add a way to clone histograms; r=taras
authorNathan Froyd <froydnj@mozilla.com>
Fri, 06 Jan 2012 14:40:04 -0500
changeset 85185 4c6086b7e2fab725f2d3b3b77d575cd028029beb
parent 85184 b9601344d6d7419e03f7c39e7b394c257aca3217
child 85186 488c9feac9bd35bb0d02823193c508f1b14a1593
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstaras
bugs701863
milestone12.0a1
Bug 701863 - Add a way to clone histograms; r=taras
toolkit/components/telemetry/Telemetry.cpp
toolkit/components/telemetry/nsITelemetry.idl
--- a/toolkit/components/telemetry/Telemetry.cpp
+++ b/toolkit/components/telemetry/Telemetry.cpp
@@ -128,16 +128,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;
@@ -414,16 +433,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
@@ -96,16 +96,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);
 
   /**