monitor.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const {Ci, Cc} = require("chrome");
  6. const {XPCOMUtils} = require("resource://gre/modules/XPCOMUtils.jsm");
  7. const Services = require("Services");
  8. function MonitorActor(aConnection) {
  9. this.conn = aConnection;
  10. this._updates = [];
  11. this._started = false;
  12. }
  13. MonitorActor.prototype = {
  14. actorPrefix: "monitor",
  15. // Updates.
  16. _sendUpdate: function () {
  17. if (this._started) {
  18. this.conn.sendActorEvent(this.actorID, "update", { data: this._updates });
  19. this._updates = [];
  20. }
  21. },
  22. // Methods available from the front.
  23. start: function () {
  24. if (!this._started) {
  25. this._started = true;
  26. Services.obs.addObserver(this, "devtools-monitor-update", false);
  27. Services.obs.notifyObservers(null, "devtools-monitor-start", "");
  28. this._agents.forEach(agent => this._startAgent(agent));
  29. }
  30. return {};
  31. },
  32. stop: function () {
  33. if (this._started) {
  34. this._agents.forEach(agent => agent.stop());
  35. Services.obs.notifyObservers(null, "devtools-monitor-stop", "");
  36. Services.obs.removeObserver(this, "devtools-monitor-update");
  37. this._started = false;
  38. }
  39. return {};
  40. },
  41. disconnect: function () {
  42. this.stop();
  43. },
  44. // nsIObserver.
  45. observe: function (subject, topic, data) {
  46. if (topic == "devtools-monitor-update") {
  47. try {
  48. data = JSON.parse(data);
  49. } catch (e) {
  50. console.error("Observer notification data is not a valid JSON-string:",
  51. data, e.message);
  52. return;
  53. }
  54. if (!Array.isArray(data)) {
  55. this._updates.push(data);
  56. } else {
  57. this._updates = this._updates.concat(data);
  58. }
  59. this._sendUpdate();
  60. }
  61. },
  62. QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
  63. // Update agents (see USSAgent for an example).
  64. _agents: [],
  65. _startAgent: function (agent) {
  66. try {
  67. agent.start();
  68. } catch (e) {
  69. this._removeAgent(agent);
  70. }
  71. },
  72. _addAgent: function (agent) {
  73. this._agents.push(agent);
  74. if (this._started) {
  75. this._startAgent(agent);
  76. }
  77. },
  78. _removeAgent: function (agent) {
  79. let index = this._agents.indexOf(agent);
  80. if (index > -1) {
  81. this._agents.splice(index, 1);
  82. }
  83. },
  84. };
  85. MonitorActor.prototype.requestTypes = {
  86. "start": MonitorActor.prototype.start,
  87. "stop": MonitorActor.prototype.stop,
  88. };
  89. exports.MonitorActor = MonitorActor;
  90. var USSAgent = {
  91. _mgr: null,
  92. _timeout: null,
  93. _packet: {
  94. graph: "USS",
  95. time: null,
  96. value: null
  97. },
  98. start: function () {
  99. USSAgent._mgr = Cc["@mozilla.org/memory-reporter-manager;1"].getService(Ci.nsIMemoryReporterManager);
  100. if (!USSAgent._mgr.residentUnique) {
  101. throw "Couldn't get USS.";
  102. }
  103. USSAgent.update();
  104. },
  105. update: function () {
  106. if (!USSAgent._mgr) {
  107. USSAgent.stop();
  108. return;
  109. }
  110. USSAgent._packet.time = Date.now();
  111. USSAgent._packet.value = USSAgent._mgr.residentUnique;
  112. Services.obs.notifyObservers(null, "devtools-monitor-update", JSON.stringify(USSAgent._packet));
  113. USSAgent._timeout = setTimeout(USSAgent.update, 300);
  114. },
  115. stop: function () {
  116. clearTimeout(USSAgent._timeout);
  117. USSAgent._mgr = null;
  118. }
  119. };
  120. MonitorActor.prototype._addAgent(USSAgent);