From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- toolkit/modules/Memory.jsm | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 toolkit/modules/Memory.jsm (limited to 'toolkit/modules/Memory.jsm') diff --git a/toolkit/modules/Memory.jsm b/toolkit/modules/Memory.jsm new file mode 100644 index 000000000..bb8e331c6 --- /dev/null +++ b/toolkit/modules/Memory.jsm @@ -0,0 +1,77 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +this.EXPORTED_SYMBOLS = ["Memory"]; + +const { classes: Cc, interfaces: Ci, utils: Cu } = Components; +// How long we should wait for the Promise to resolve. +const TIMEOUT_INTERVAL = 2000; + +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/Timer.jsm"); + +this.Memory = { + /** + * This function returns a Promise that resolves with an Object that + * describes basic memory usage for each content process and the parent + * process. + * @returns Promise + * @resolves JS Object + * An Object in the following format: + * { + * "parent": { + * uss: , + * rss: , + * }, + * : { + * uss: , + * rss: , + * }, + * ... + * } + */ + summary() { + if (!this._pendingPromise) { + this._pendingPromise = new Promise((resolve) => { + this._pendingResolve = resolve; + this._summaries = {}; + Services.ppmm.broadcastAsyncMessage("Memory:GetSummary"); + Services.ppmm.addMessageListener("Memory:Summary", this); + this._pendingTimeout = setTimeout(() => { this.finish(); }, TIMEOUT_INTERVAL); + }); + } + return this._pendingPromise; + }, + + receiveMessage(msg) { + if (msg.name != "Memory:Summary" || !this._pendingResolve) { + return; + } + this._summaries[msg.data.pid] = msg.data.summary; + // Now we check if we are done for all content processes. + // Services.ppmm.childCount is a count of how many processes currently + // exist that might respond to messages sent through the ppmm, including + // the parent process. So we subtract the parent process with the "- 1", + // and that’s how many content processes we’re waiting for. + if (Object.keys(this._summaries).length >= Services.ppmm.childCount - 1) { + this.finish(); + } + }, + + finish() { + // Code to gather the USS and RSS values for the parent process. This + // functions the same way as in process-content.js. + let memMgr = Cc["@mozilla.org/memory-reporter-manager;1"] + .getService(Ci.nsIMemoryReporterManager); + let rss = memMgr.resident; + let uss = memMgr.residentUnique; + this._summaries["Parent"] = { uss, rss }; + this._pendingResolve(this._summaries); + this._pendingResolve = null; + this._summaries = null; + this._pendingPromise = null; + clearTimeout(this._pendingTimeout); + Services.ppmm.removeMessageListener("Memory:Summary", this); + } +}; -- cgit v1.2.3