mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-16 23:05:42 +00:00
Bug 1037465 - Add USS reporting to the Monitor actor. r=paul
This commit is contained in:
parent
290af4e37f
commit
51d859e39d
@ -2,57 +2,58 @@
|
||||
* 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
|
||||
// Updates.
|
||||
|
||||
_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);
|
||||
_sendUpdate: function() {
|
||||
if (this._started) {
|
||||
this.conn.sendActorEvent(this.actorID, "update", { data: this._updates });
|
||||
this._updates = [];
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
// Methods available from the front
|
||||
// Methods available from the front.
|
||||
|
||||
start: function() {
|
||||
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 => this._startAgent(agent));
|
||||
}
|
||||
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 {};
|
||||
},
|
||||
|
||||
// nsIObserver
|
||||
disconnect: function() {
|
||||
this.stop();
|
||||
},
|
||||
|
||||
// nsIObserver.
|
||||
|
||||
observe: function (subject, topic, data) {
|
||||
if (topic == "devtools-monitor-update") {
|
||||
@ -64,16 +65,42 @@ 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]),
|
||||
}
|
||||
|
||||
// Update agents (see USSAgent for an example).
|
||||
|
||||
_agents: [],
|
||||
|
||||
_startAgent: function(agent) {
|
||||
try {
|
||||
agent.start();
|
||||
} catch (e) {
|
||||
this._removeAgent(agent);
|
||||
}
|
||||
},
|
||||
|
||||
_addAgent: function(agent) {
|
||||
this._agents.push(agent);
|
||||
if (this._started) {
|
||||
this._startAgent(agent);
|
||||
}
|
||||
},
|
||||
|
||||
_removeAgent: function(agent) {
|
||||
let index = this._agents.indexOf(agent);
|
||||
if (index > -1) {
|
||||
this._agents.splice(index, 1);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
MonitorActor.prototype.requestTypes = {
|
||||
"start": MonitorActor.prototype.start,
|
||||
@ -91,3 +118,40 @@ 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);
|
||||
if (!USSAgent._mgr.residentUnique) {
|
||||
throw "Couldn't get USS.";
|
||||
}
|
||||
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);
|
||||
|
@ -28,11 +28,11 @@ function run_test()
|
||||
client.unregisterClient(this);
|
||||
}
|
||||
MonitorClient.prototype.detach = function () {}
|
||||
MonitorClient.prototype.start = function () {
|
||||
MonitorClient.prototype.start = function (callback) {
|
||||
this.client.request({
|
||||
to: this.actor,
|
||||
type: "start"
|
||||
});
|
||||
}, callback);
|
||||
}
|
||||
MonitorClient.prototype.stop = function (callback) {
|
||||
this.client.request({
|
||||
@ -43,34 +43,39 @@ function run_test()
|
||||
|
||||
let monitor;
|
||||
|
||||
// Start tracking event loop lags.
|
||||
// Start the monitor actor.
|
||||
client.connect(function () {
|
||||
client.listTabs(function(resp) {
|
||||
monitor = new MonitorClient(client, resp);
|
||||
monitor.start();
|
||||
monitor.on("update", gotUpdate);
|
||||
do_execute_soon(update);
|
||||
monitor.start(update);
|
||||
});
|
||||
});
|
||||
|
||||
let time = new Date().getTime();
|
||||
let time = Date.now();
|
||||
|
||||
function update() {
|
||||
let event = {
|
||||
graph: "Test",
|
||||
curve: "test",
|
||||
value: 42,
|
||||
time: time,
|
||||
value: 42
|
||||
};
|
||||
Services.obs.notifyObservers(null, "devtools-monitor-update", JSON.stringify(event));
|
||||
}
|
||||
|
||||
function gotUpdate(type, data) {
|
||||
do_check_eq(data.length, 1);
|
||||
let evt = data[0];
|
||||
do_check_eq(evt.value, 42);
|
||||
do_check_eq(evt.time, time);
|
||||
monitor.stop(function (aResponse) {
|
||||
monitor.destroy();
|
||||
finishClient(client);
|
||||
function gotUpdate(type, packet) {
|
||||
packet.data.forEach(function(event) {
|
||||
// Ignore updates that were not sent by this test.
|
||||
if (event.graph === "Test") {
|
||||
do_check_eq(event.curve, "test");
|
||||
do_check_eq(event.value, 42);
|
||||
do_check_eq(event.time, time);
|
||||
monitor.stop(function (aResponse) {
|
||||
monitor.destroy();
|
||||
finishClient(client);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user