Bug 1037465 - Add USS reporting to the Monitor actor. r=paul

This commit is contained in:
Jan Keromnes 2014-07-21 17:51:43 -07:00
Родитель 4b8133c27a
Коммит 30c072f123
1 изменённых файлов: 64 добавлений и 19 удалений

Просмотреть файл

@ -2,37 +2,40 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const {Ci,Cu} = require("chrome");
const {Ci,Cu,Cc} = require("chrome");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
const Services = require("Services");
let {setTimeout} = require("sdk/timers");
let {setTimeout,clearTimeout} = require("sdk/timers");
function MonitorActor(aConnection) {
this.conn = aConnection;
this._updates = [];
this._started = false;
}
MonitorActor.prototype = {
actorPrefix: "monitor",
// Updates
// Update agents (see USSAgent for an example).
_toSend: [],
_timeout: null,
_started: false,
_scheduleUpdate: function() {
if (this._started && !this._timeout) {
this._timeout = setTimeout(() => {
if (this._toSend.length > 0) {
this.conn.sendActorEvent(this.actorID, "update", this._toSend);
this._toSend = [];
}
this._timeout = null;
}, 200);
_agents: [],
_addAgent: function(agent) {
if (this._started) {
agent.start();
}
this._agents.push(agent);
},
// Updates.
_sendUpdate: function() {
if (this._started) {
this.conn.sendActorEvent(this.actorID, "update", { data: this._updates });
this._updates = [];
}
},
// Methods available from the front
@ -40,18 +43,26 @@ MonitorActor.prototype = {
if (!this._started) {
this._started = true;
Services.obs.addObserver(this, "devtools-monitor-update", false);
Services.obs.notifyObservers(null, "devtools-monitor-start", "");
this._agents.forEach(agent => agent.start());
}
return {};
},
stop: function() {
if (this._started) {
this._agents.forEach(agent => agent.stop());
Services.obs.notifyObservers(null, "devtools-monitor-stop", "");
Services.obs.removeObserver(this, "devtools-monitor-update");
this._started = false;
}
return {};
},
disconnect: function() {
this.stop();
},
// nsIObserver
observe: function (subject, topic, data) {
@ -64,16 +75,16 @@ MonitorActor.prototype = {
return;
}
if (!Array.isArray(data)) {
this._toSend.push(data);
this._updates.push(data);
} else {
this._toSend = this._toSend.concat(data);
this._updates = this._updates.concat(data);
}
this._scheduleUpdate();
this._sendUpdate();
}
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
}
};
MonitorActor.prototype.requestTypes = {
"start": MonitorActor.prototype.start,
@ -91,3 +102,37 @@ exports.unregister = function(handle) {
handle.removeGlobalActor(MonitorActor, "monitorActor");
handle.removeTabActor(MonitorActor, "monitorActor");
};
let USSAgent = {
_mgr: null,
_timeout: null,
_packet: {
graph: "USS",
time: null,
value: null
},
start: function() {
USSAgent._mgr = Cc["@mozilla.org/memory-reporter-manager;1"].getService(Ci.nsIMemoryReporterManager);
USSAgent.update();
},
update: function() {
if (!USSAgent._mgr) {
USSAgent.stop();
return;
}
USSAgent._packet.time = Date.now();
USSAgent._packet.value = USSAgent._mgr.residentUnique;
Services.obs.notifyObservers(null, "devtools-monitor-update", JSON.stringify(USSAgent._packet));
USSAgent._timeout = setTimeout(USSAgent.update, 300);
},
stop: function() {
clearTimeout(USSAgent._timeout);
USSAgent._mgr = null;
}
};
MonitorActor.prototype._addAgent(USSAgent);