Bug 1161032 - Don't listen to "cycle-collector-begin" in TelemetrySession if extended recording is disabled. r=gfritzsche a=sledru
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Thu, 14 May 2015 05:35:00 -0400
changeset 275117 a2d11db649c085d4b8f29f7bc64328d910848a57
parent 275116 ccf0bdbac4eb9f0ecbbf89064c62e0f12df446ee
child 275118 2bac618c592b14f058bcd5ebab0e5b10f71a28ef
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgfritzsche, sledru
bugs1161032
milestone40.0a2
Bug 1161032 - Don't listen to "cycle-collector-begin" in TelemetrySession if extended recording is disabled. r=gfritzsche a=sledru
toolkit/components/telemetry/TelemetrySession.jsm
--- a/toolkit/components/telemetry/TelemetrySession.jsm
+++ b/toolkit/components/telemetry/TelemetrySession.jsm
@@ -104,16 +104,18 @@ const SCHEDULER_COALESCE_THRESHOLD_MS = 
 // On idle-daily a gather-telemetry notification is fired, during it probes can
 // start asynchronous tasks to gather data.
 const IDLE_TIMEOUT_SECONDS = 5 * 60;
 
 // The frequency at which we persist session data to the disk to prevent data loss
 // in case of aborted sessions (currently 5 minutes).
 const ABORTED_SESSION_UPDATE_INTERVAL_MS = 5 * 60 * 1000;
 
+const TOPIC_CYCLE_COLLECTOR_BEGIN = "cycle-collector-begin";
+
 var gLastMemoryPoll = null;
 
 let gWasDebuggerAttached = false;
 
 function getLocale() {
   return Cc["@mozilla.org/chrome/chrome-registry;1"].
          getService(Ci.nsIXULChromeRegistry).
          getSelectedLocale('global');
@@ -1395,25 +1397,33 @@ let Impl = {
       addEnvironment: true,
     };
     return TelemetryController.submitExternalPing(getPingType(payload), payload, options);
   },
 
   attachObservers: function attachObservers() {
     if (!this._initialized)
       return;
-    Services.obs.addObserver(this, "cycle-collector-begin", false);
     Services.obs.addObserver(this, "idle-daily", false);
+    if (Telemetry.canRecordExtended) {
+      Services.obs.addObserver(this, TOPIC_CYCLE_COLLECTOR_BEGIN, false);
+    }
   },
 
   detachObservers: function detachObservers() {
     if (!this._initialized)
       return;
     Services.obs.removeObserver(this, "idle-daily");
-    Services.obs.removeObserver(this, "cycle-collector-begin");
+    try {
+      // Tests may flip Telemetry.canRecordExtended on and off. Just try to remove this
+      // observer and catch if it fails because the observer was not added.
+      Services.obs.removeObserver(this, TOPIC_CYCLE_COLLECTOR_BEGIN);
+    } catch (e) {
+      this._log.warn("detachObservers - Failed to remove " + TOPIC_CYCLE_COLLECTOR_BEGIN, e);
+    }
   },
 
   /**
    * Initializes telemetry within a timer.
    */
   setupChromeProcess: function setupChromeProcess(testing) {
     this._initStarted = true;
     if (testing && !this._log) {
@@ -1719,17 +1729,20 @@ let Impl = {
   /**
    * This observer drives telemetry.
    */
   observe: function (aSubject, aTopic, aData) {
     if (!this._log) {
       this._log = Log.repository.getLoggerWithMessagePrefix(LOGGER_NAME, LOGGER_PREFIX);
     }
 
-    this._log.trace("observe - " + aTopic + " notified.");
+    // Prevent the cycle collector begin topic from cluttering the log.
+    if (aTopic != TOPIC_CYCLE_COLLECTOR_BEGIN) {
+      this._log.trace("observe - " + aTopic + " notified.");
+    }
 
     switch (aTopic) {
     case "profile-after-change":
       // profile-after-change is only registered for chrome processes.
       return this.setupChromeProcess();
     case "app-startup":
       // app-startup is only registered for content processes.
       return this.setupContentProcess();
@@ -1737,17 +1750,17 @@ let Impl = {
       // content-child-shutdown is only registered for content processes.
       Services.obs.removeObserver(this, "content-child-shutdown");
       this.uninstall();
 
       if (Telemetry.isOfficialTelemetry) {
         this.sendContentProcessPing(REASON_SAVED_SESSION);
       }
       break;
-    case "cycle-collector-begin":
+    case TOPIC_CYCLE_COLLECTOR_BEGIN:
       let now = new Date();
       if (!gLastMemoryPoll
           || (TELEMETRY_INTERVAL <= now - gLastMemoryPoll)) {
         gLastMemoryPoll = now;
         this.gatherMemory();
       }
       break;
     case "xul-window-visible":