Bug 716657 - add Telemetry::AutoCounter; r=taras
authorNathan Froyd <froydnj@mozilla.com>
Mon, 09 Jan 2012 18:21:28 -0500
changeset 85529 5660fa471b73d15716a15d0b308c5aaf81a24581
parent 85528 65da0d7b408d410e61dbeca98ebe559135ae0c98
child 85530 87b138f5e7829a9777777d64ba4ef075bad66685
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
bugs716657
milestone12.0a1
Bug 716657 - add Telemetry::AutoCounter; r=taras
toolkit/components/telemetry/Telemetry.h
--- a/toolkit/components/telemetry/Telemetry.h
+++ b/toolkit/components/telemetry/Telemetry.h
@@ -99,16 +99,44 @@ public:
     AccumulateTimeDelta(id, start);
   }
 
 private:
   const TimeStamp start;
   MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
 };
 
+template<ID id>
+class AutoCounter {
+public:
+  AutoCounter(PRUint32 counterStart = 0 MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
+    : counter(counterStart)
+  {
+    MOZ_GUARD_OBJECT_NOTIFIER_INIT;
+  }
+
+  ~AutoCounter() {
+    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:
+  PRUint32 counter;
+  MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
+};
+
 /**
  * Records slow SQL statements for Telemetry reporting.
  * For privacy reasons, only prepared statements are reported.
  *
  * @param statement - offending SQL statement to record
  * @param dbName - DB filename; reporting is only done for whitelisted DBs
  * @param delay - execution time in milliseconds
  */