Bug 782459 - Generalize the telemetry AutoTimer RAII class to provide multiple resolution. r=taras
authorPaul Adenot <paul@paul.cx>
Sun, 26 Aug 2012 21:11:55 -0700
changeset 103583 6a8b865e6225f19de7258f1ee0a88e4837bcfdc7
parent 103582 31a27d47d242ebb956d10a8b0155f9071f43e5a4
child 103584 72e95bee76fa7d6d8fba20342f97889c8ecbb320
push id23362
push userryanvm@gmail.com
push dateTue, 28 Aug 2012 02:11:38 +0000
treeherdermozilla-central@8af2ff9c6018 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstaras
bugs782459
milestone17.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 782459 - Generalize the telemetry AutoTimer RAII class to provide multiple resolution. r=taras
toolkit/components/telemetry/Telemetry.h
--- a/toolkit/components/telemetry/Telemetry.h
+++ b/toolkit/components/telemetry/Telemetry.h
@@ -19,16 +19,21 @@ namespace base {
   class Histogram;
 }
 
 namespace mozilla {
 namespace Telemetry {
 
 #include "TelemetryHistogramEnums.h"
 
+enum TimerResolution {
+  Millisecond,
+  Microsecond
+};
+
 /**
  * Initialize the Telemetry service on the main thread at startup.
  */
 void Init();
 
 /**
  * Adds sample to a histogram defined in TelemetryHistograms.h
  *
@@ -46,27 +51,54 @@ void Accumulate(ID id, uint32_t sample);
  */
 void AccumulateTimeDelta(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now());
 
 /**
  * Return a raw Histogram for direct manipulation for users who can not use Accumulate().
  */
 base::Histogram* GetHistogramById(ID id);
 
-template<ID id>
+/**
+ * Those wrappers are needed because the VS versions we use do not support free
+ * functions with default template arguments.
+ */
+template<TimerResolution res>
+struct AccumulateDelta_impl
+{
+  static void compute(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now());
+};
+
+template<>
+struct AccumulateDelta_impl<Millisecond>
+{
+  static void compute(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now()) {
+    Accumulate(id, static_cast<uint32_t>((end - start).ToMilliseconds()));
+  }
+};
+
+template<>
+struct AccumulateDelta_impl<Microsecond>
+{
+  static void compute(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now()) {
+    Accumulate(id, static_cast<uint32_t>((end - start).ToMicroseconds()));
+  }
+};
+
+
+template<ID id, TimerResolution res = Millisecond>
 class AutoTimer {
 public:
   AutoTimer(TimeStamp aStart = TimeStamp::Now() MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
      : start(aStart)
   {
     MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   }
 
   ~AutoTimer() {
-    AccumulateTimeDelta(id, start);
+    AccumulateDelta_impl<res>::compute(id, start);
   }
 
 private:
   const TimeStamp start;
   MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
 };
 
 template<ID id>