bug 931856 http data usage telemetry updates r=jduell
authorPatrick McManus <mcmanus@ducksong.com>
Mon, 28 Oct 2013 21:00:27 -0400
changeset 152576 dfc04d7991c437526649159acec9db445bc25b75
parent 152575 7da0450f51576022aff8f8920b3598f33b350dec
child 152577 998b63fe349249be6742b9e694b6f3b2e0bca15b
push id25552
push usercbook@mozilla.com
push dateTue, 29 Oct 2013 12:21:45 +0000
treeherdermozilla-central@cd94525c17a4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjduell
bugs931856
milestone28.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 931856 http data usage telemetry updates r=jduell
netwerk/protocol/http/HttpDataUsage.js
--- a/netwerk/protocol/http/HttpDataUsage.js
+++ b/netwerk/protocol/http/HttpDataUsage.js
@@ -30,18 +30,17 @@ var gDataUsage;
 function HttpDataUsage() {}
 HttpDataUsage.prototype = {
     classID: Components.ID("{6d72bfca-2747-4859-887f-6f06d4ce6787}"),
     QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
     contractID: "@mozilla.org/network/HttpDataUsage;1",
 
     _isIdleObserver: false,
     _locked : false,
-    _do_telemetry : false,
-    _idle_timeout : 60 * 3,
+    _idle_timeout : 15,
     _quanta : 86400000, // per day
 
     _logtime : new Date(),
     _ethernetRead : 0,
     _ethernetWritten : 0,
     _cellRead : 0,
     _cellWritten : 0,
 
@@ -69,34 +68,33 @@ HttpDataUsage.prototype = {
         // this isn't important enough to worry about getting a
         // runtime telemetry config change for
         if (!enabled)
             return;
 
         if (this._dataUsage == null)
             return;
 
+        Services.obs.addObserver(this, "quit-application", false);
         idleService.addIdleObserver(this, this._idle_timeout);
         this._isIdleObserver = true;
     },
     
     shutdown: function shutdown() {
-        if (this._isIdleObserver)
+        if (this._isIdleObserver) {
             idleService.removeIdleObserver(this, this._idle_timeout);
+            Services.obs.removeObserver(this, "quit-application");
+        }
         this._isIdleObserver = false;
     },
 
     sUpdateStats2: function sUpdateStats2(stream, result) {
         gDataUsage.updateStats2(stream, result);
     },
 
-    sGatherTelemetry2: function sGatherTelemetry2(stream, result) {
-        gDataUsage.gatherTelemetry2(stream, result);
-    },
-
     readCounters: function readCounters(stream, result) {
         if (Components.isSuccessCode(result)) {
             let count = stream.available();
             let data = NetUtil.readInputStreamToString(stream, count);
             var list = data.split(",");
             if (list.length == 5) {
                 this._logtime = new Date(Number(list[0]));
                 this._ethernetRead = Number(list[1]);
@@ -108,17 +106,16 @@ HttpDataUsage.prototype = {
 
         this._ethernetRead += this._dataUsage.ethernetBytesRead;
         this._ethernetWritten += this._dataUsage.ethernetBytesWritten;
         this._cellRead += this._dataUsage.cellBytesRead;
         this._cellWritten += this._dataUsage.cellBytesWritten;
         this._dataUsage.resetHttpDataUsage();
     },
 
-    // writeCounters also releases the lock
     writeCounters: function writeCounters() {
         var dataout = this._logtime.getTime().toString() + "," +
             this._ethernetRead.toString() + "," +
             this._ethernetWritten.toString() + "," +
             this._cellRead.toString() + "," +
             this._cellWritten.toString() + "\n";
 
         var buffer = new this._pipe(true, false, 4096, 1, null);
@@ -127,27 +124,25 @@ HttpDataUsage.prototype = {
         var fileOut = new this._outputStream(this._dataFile, -1, -1, 0);
 
         NetUtil.asyncCopy(buffer.inputStream, fileOut,
                           function (result) { gDataUsage.finishedWriting(); });
     },
 
     updateStats2: function updateStats2(stream, result) {
         this.readCounters(stream, result);
-        this.writeCounters();
+        this.submitTelemetry();
     },
 
-    gatherTelemetry2: function gatherTelemetry2(stream, result) {
-        this.readCounters(stream, result);
-
+    submitTelemetry: function submitTelemetry() {
         var now = new Date();
         var elapsed = now.getTime() - this._logtime.getTime();
-        // make sure we have at least 1 day of data
+        // make sure we have at least 1 day of data.. if not just write new data out
         if (elapsed < this._quanta) {
-            this.finishedWriting();
+            this.writeCounters();
             return;
         }
 
         var days = elapsed / this._quanta;
         var eInPerQuanta = Math.floor(this._ethernetRead / days);
         var eOutPerQuanta = Math.floor(this._ethernetWritten / days);
         var cInPerQuanta = Math.floor(this._cellRead / days);
         var cOutPerQuanta = Math.floor(this._cellWritten / days);
@@ -166,57 +161,42 @@ HttpDataUsage.prototype = {
 
             elapsed -= this._quanta;
             this._ethernetRead -= eInPerQuanta;
             this._ethernetWritten -= eOutPerQuanta;
             this._cellRead -= cInPerQuanta;
             this._cellWritten -= cOutPerQuanta;
         }
         this._logtime = new Date(now.getTime() - elapsed);
-        
+
+        // need to write back the decremented counters
         this.writeCounters();
     },
 
     finishedWriting : function finishedWriting() {
-        this._locked = false;
-        if (this._do_telemetry) {
-            this._do_telemetry = false;
-            this.gatherTelemetry();
-        }
+        this._locked = false; // all done
     },
 
     updateStats: function updateStats() {
         if (this._locked)
             return;
         this._locked = true;
         
         NetUtil.asyncFetch(this._dataFile, this.sUpdateStats2);
     },
 
-    gatherTelemetry: function gatherTelemetry() {
-        if (this._locked)
-            return; // oh well, maybe next time
-        this._locked = true;
-
-        NetUtil.asyncFetch(this._dataFile, this.sGatherTelemetry2);
-    },
-
     observe: function (aSubject, aTopic, aData) {
         switch (aTopic) {
         case "profile-after-change":
             this.setup();
             break;
-        case "gather-telemetry":
-            this._do_telemetry = true;
-            this.updateStats();
-            break;
         case "idle":
             this.updateStats();
             break;
-        case "profile-change-net-teardown":
+        case "quit-application":
+            this.updateStats();
             this.shutdown();
             break;
         }
     },
-
 };
 
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([HttpDataUsage]);