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 105616 6a8b865e6225f19de7258f1ee0a88e4837bcfdc7
parent 105615 31a27d47d242ebb956d10a8b0155f9071f43e5a4
child 105617 72e95bee76fa7d6d8fba20342f97889c8ecbb320
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewerstaras
bugs782459
milestone17.0a1
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>